00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026 if (!defined('MEDIAWIKI')) {
00027
00028 require_once ('ApiQueryBase.php');
00029 }
00030
00037 class ApiQueryRandom extends ApiQueryGeneratorBase {
00038
00039 public function __construct($query, $moduleName) {
00040 parent :: __construct($query, $moduleName, 'rn');
00041 }
00042
00043 public function execute() {
00044 $this->run();
00045 }
00046
00047 public function executeGenerator($resultPageSet) {
00048 $this->run($resultPageSet);
00049 }
00050
00051 protected function prepareQuery($randstr, $limit, $namespace, &$resultPageSet, $redirect) {
00052 $this->resetQueryParams();
00053 $this->addTables('page');
00054 $this->addOption('LIMIT', $limit);
00055 $this->addWhereFld('page_namespace', $namespace);
00056 $this->addWhereRange('page_random', 'newer', $randstr, null);
00057 $this->addWhereFld('page_is_redirect', $redirect);
00058 $this->addOption('USE INDEX', 'page_random');
00059 if(is_null($resultPageSet))
00060 $this->addFields(array('page_id', 'page_title', 'page_namespace'));
00061 else
00062 $this->addFields($resultPageSet->getPageTableFields());
00063 }
00064
00065 protected function runQuery(&$resultPageSet) {
00066 $db = $this->getDB();
00067 $res = $this->select(__METHOD__);
00068 $count = 0;
00069 while($row = $db->fetchObject($res)) {
00070 $count++;
00071 if(is_null($resultPageSet))
00072 {
00073
00074 if(!in_array($row->page_id, $this->pageIDs))
00075 {
00076 $fit = $this->getResult()->addValue(
00077 array('query', $this->getModuleName()),
00078 null, $this->extractRowInfo($row));
00079 if(!$fit)
00080 # We can't really query-continue a random list.
00081 # Return an insanely high value so
00082 # $count < $limit is false
00083 return 1E9;
00084 $this->pageIDs[] = $row->page_id;
00085 }
00086 }
00087 else
00088 $resultPageSet->processDbRow($row);
00089 }
00090 $db->freeResult($res);
00091 return $count;
00092 }
00093
00094 public function run($resultPageSet = null) {
00095 $params = $this->extractRequestParams();
00096 $result = $this->getResult();
00097 $this->pageIDs = array();
00098
00099 $this->prepareQuery(wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect']);
00100 $count = $this->runQuery($resultPageSet);
00101 if($count < $params['limit'])
00102 {
00103
00104
00105
00106
00107 $this->prepareQuery(0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect']);
00108 $this->runQuery($resultPageSet);
00109 }
00110
00111 if(is_null($resultPageSet)) {
00112 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
00113 }
00114 }
00115
00116 private function extractRowInfo($row) {
00117 $title = Title::makeTitle($row->page_namespace, $row->page_title);
00118 $vals = array();
00119 $vals['id'] = intval($row->page_id);
00120 ApiQueryBase::addTitleInfo($vals, $title);
00121 return $vals;
00122 }
00123
00124 public function getAllowedParams() {
00125 return array (
00126 'namespace' => array(
00127 ApiBase :: PARAM_TYPE => 'namespace',
00128 ApiBase :: PARAM_ISMULTI => true
00129 ),
00130 'limit' => array (
00131 ApiBase :: PARAM_TYPE => 'limit',
00132 ApiBase :: PARAM_DFLT => 1,
00133 ApiBase :: PARAM_MIN => 1,
00134 ApiBase :: PARAM_MAX => 10,
00135 ApiBase :: PARAM_MAX2 => 20
00136 ),
00137 'redirect' => false,
00138 );
00139 }
00140
00141 public function getParamDescription() {
00142 return array (
00143 'namespace' => 'Return pages in these namespaces only',
00144 'limit' => 'Limit how many random pages will be returned',
00145 'redirect' => 'Load a random redirect instead of a random page'
00146 );
00147 }
00148
00149 public function getDescription() {
00150 return array( 'Get a set of random pages',
00151 'NOTE: Pages are listed in a fixed sequence, only the starting point is random. This means that if, for example, "Main Page" is the first ',
00152 ' random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc.',
00153 'NOTE: If the number of pages in the namespace is lower than rnlimit, you will get fewer pages. You will not get the same page twice.'
00154 );
00155 }
00156
00157 protected function getExamples() {
00158 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
00159 }
00160
00161 public function getVersion() {
00162 return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$';
00163 }
00164 }