MediaWiki  REL1_19
ApiQueryRandom.php
Go to the documentation of this file.
00001 <?php
00002 
00034 class ApiQueryRandom extends ApiQueryGeneratorBase {
00035 
00036         public function __construct( $query, $moduleName ) {
00037                 parent::__construct( $query, $moduleName, 'rn' );
00038         }
00039 
00040         public function execute() {
00041                 $this->run();
00042         }
00043 
00044         public function executeGenerator( $resultPageSet ) {
00045                 $this->run( $resultPageSet );
00046         }
00047 
00056         protected function prepareQuery( $randstr, $limit, $namespace, &$resultPageSet, $redirect ) {
00057                 $this->resetQueryParams();
00058                 $this->addTables( 'page' );
00059                 $this->addOption( 'LIMIT', $limit );
00060                 $this->addWhereFld( 'page_namespace', $namespace );
00061                 $this->addWhereRange( 'page_random', 'newer', $randstr, null );
00062                 $this->addWhereFld( 'page_is_redirect', $redirect );
00063                 $this->addOption( 'USE INDEX', 'page_random' );
00064                 if ( is_null( $resultPageSet ) ) {
00065                         $this->addFields( array( 'page_id', 'page_title', 'page_namespace' ) );
00066                 } else {
00067                         $this->addFields( $resultPageSet->getPageTableFields() );
00068                 }
00069         }
00070 
00075         protected function runQuery( $resultPageSet = null ) {
00076                 $res = $this->select( __METHOD__ );
00077                 $count = 0;
00078                 foreach ( $res as $row ) {
00079                         $count++;
00080                         if ( is_null( $resultPageSet ) ) {
00081                                 // Prevent duplicates
00082                                 if ( !in_array( $row->page_id, $this->pageIDs ) ) {
00083                                         $fit = $this->getResult()->addValue(
00084                                                         array( 'query', $this->getModuleName() ),
00085                                                         null, $this->extractRowInfo( $row ) );
00086                                         if ( !$fit ) {
00087                                                 // We can't really query-continue a random list.
00088                                                 // Return an insanely high value so
00089                                                 // $count < $limit is false
00090                                                 return 1E9;
00091                                         }
00092                                         $this->pageIDs[] = $row->page_id;
00093                                 }
00094                         } else {
00095                                 $resultPageSet->processDbRow( $row );
00096                         }
00097                 }
00098 
00099                 return $count;
00100         }
00101 
00106         public function run( $resultPageSet = null ) {
00107                 $params = $this->extractRequestParams();
00108                 $result = $this->getResult();
00109                 $this->pageIDs = array();
00110 
00111                 $this->prepareQuery( wfRandom(), $params['limit'], $params['namespace'], $resultPageSet, $params['redirect'] );
00112                 $count = $this->runQuery( $resultPageSet );
00113                 if ( $count < $params['limit'] ) {
00114                         /* We got too few pages, we probably picked a high value
00115                          * for page_random. We'll just take the lowest ones, see
00116                          * also the comment in Title::getRandomTitle()
00117                          */
00118                         $this->prepareQuery( 0, $params['limit'] - $count, $params['namespace'], $resultPageSet, $params['redirect'] );
00119                         $this->runQuery( $resultPageSet );
00120                 }
00121 
00122                 if ( is_null( $resultPageSet ) ) {
00123                         $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'page' );
00124                 }
00125         }
00126 
00127         private function extractRowInfo( $row ) {
00128                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00129                 $vals = array();
00130                 $vals['id'] = intval( $row->page_id );
00131                 ApiQueryBase::addTitleInfo( $vals, $title );
00132                 return $vals;
00133         }
00134 
00135         public function getCacheMode( $params ) {
00136                 return 'public';
00137         }
00138 
00139         public function getAllowedParams() {
00140                 return array(
00141                         'namespace' => array(
00142                                 ApiBase::PARAM_TYPE => 'namespace',
00143                                 ApiBase::PARAM_ISMULTI => true
00144                         ),
00145                         'limit' => array(
00146                                 ApiBase::PARAM_TYPE => 'limit',
00147                                 ApiBase::PARAM_DFLT => 1,
00148                                 ApiBase::PARAM_MIN => 1,
00149                                 ApiBase::PARAM_MAX => 10,
00150                                 ApiBase::PARAM_MAX2 => 20
00151                         ),
00152                         'redirect' => false,
00153                 );
00154         }
00155 
00156         public function getParamDescription() {
00157                 return array(
00158                         'namespace' => 'Return pages in these namespaces only',
00159                         'limit' => 'Limit how many random pages will be returned',
00160                         'redirect' => 'Load a random redirect instead of a random page'
00161                 );
00162         }
00163 
00164         public function getDescription() {
00165                 return array(
00166                         'Get a set of random pages',
00167                         '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 ',
00168                         '      random page on your list, "List of fictional monkeys" will *always* be second, "List of people on stamps of Vanuatu" third, etc',
00169                         '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'
00170                 );
00171         }
00172 
00173         public function getExamples() {
00174                 return 'api.php?action=query&list=random&rnnamespace=0&rnlimit=2';
00175         }
00176 
00177         public function getVersion() {
00178                 return __CLASS__ . ': $Id: ApiQueryRandom.php overlordq$';
00179         }
00180 }