MediaWiki  REL1_22
ApiQueryAllPages.php
Go to the documentation of this file.
00001 <?php
00032 class ApiQueryAllPages extends ApiQueryGeneratorBase {
00033 
00034     public function __construct( $query, $moduleName ) {
00035         parent::__construct( $query, $moduleName, 'ap' );
00036     }
00037 
00038     public function execute() {
00039         $this->run();
00040     }
00041 
00042     public function getCacheMode( $params ) {
00043         return 'public';
00044     }
00045 
00050     public function executeGenerator( $resultPageSet ) {
00051         if ( $resultPageSet->isResolvingRedirects() ) {
00052             $this->dieUsage( 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator', 'params' );
00053         }
00054 
00055         $this->run( $resultPageSet );
00056     }
00057 
00062     private function run( $resultPageSet = null ) {
00063         $db = $this->getDB();
00064 
00065         $params = $this->extractRequestParams();
00066 
00067         // Page filters
00068         $this->addTables( 'page' );
00069 
00070         if ( !is_null( $params['continue'] ) ) {
00071             $cont = explode( '|', $params['continue'] );
00072             $this->dieContinueUsageIf( count( $cont ) != 1 );
00073             $op = $params['dir'] == 'descending' ? '<' : '>';
00074             $cont_from = $db->addQuotes( $cont[0] );
00075             $this->addWhere( "page_title $op= $cont_from" );
00076         }
00077 
00078         if ( $params['filterredir'] == 'redirects' ) {
00079             $this->addWhereFld( 'page_is_redirect', 1 );
00080         } elseif ( $params['filterredir'] == 'nonredirects' ) {
00081             $this->addWhereFld( 'page_is_redirect', 0 );
00082         }
00083 
00084         $this->addWhereFld( 'page_namespace', $params['namespace'] );
00085         $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
00086         $from = ( is_null( $params['from'] ) ? null : $this->titlePartToKey( $params['from'] ) );
00087         $to = ( is_null( $params['to'] ) ? null : $this->titlePartToKey( $params['to'] ) );
00088         $this->addWhereRange( 'page_title', $dir, $from, $to );
00089 
00090         if ( isset( $params['prefix'] ) ) {
00091             $this->addWhere( 'page_title' . $db->buildLike( $this->titlePartToKey( $params['prefix'] ), $db->anyString() ) );
00092         }
00093 
00094         if ( is_null( $resultPageSet ) ) {
00095             $selectFields = array(
00096                 'page_namespace',
00097                 'page_title',
00098                 'page_id'
00099             );
00100         } else {
00101             $selectFields = $resultPageSet->getPageTableFields();
00102         }
00103 
00104         $this->addFields( $selectFields );
00105         $forceNameTitleIndex = true;
00106         if ( isset( $params['minsize'] ) ) {
00107             $this->addWhere( 'page_len>=' . intval( $params['minsize'] ) );
00108             $forceNameTitleIndex = false;
00109         }
00110 
00111         if ( isset( $params['maxsize'] ) ) {
00112             $this->addWhere( 'page_len<=' . intval( $params['maxsize'] ) );
00113             $forceNameTitleIndex = false;
00114         }
00115 
00116         // Page protection filtering
00117         if ( count( $params['prtype'] ) || $params['prexpiry'] != 'all' ) {
00118             $this->addTables( 'page_restrictions' );
00119             $this->addWhere( 'page_id=pr_page' );
00120             $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
00121 
00122             if ( count( $params['prtype'] ) ) {
00123                 $this->addWhereFld( 'pr_type', $params['prtype'] );
00124 
00125                 if ( isset( $params['prlevel'] ) ) {
00126                     // Remove the empty string and '*' from the prlevel array
00127                     $prlevel = array_diff( $params['prlevel'], array( '', '*' ) );
00128 
00129                     if ( count( $prlevel ) ) {
00130                         $this->addWhereFld( 'pr_level', $prlevel );
00131                     }
00132                 }
00133                 if ( $params['prfiltercascade'] == 'cascading' ) {
00134                     $this->addWhereFld( 'pr_cascade', 1 );
00135                 } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
00136                     $this->addWhereFld( 'pr_cascade', 0 );
00137                 }
00138             }
00139             $forceNameTitleIndex = false;
00140 
00141             if ( $params['prexpiry'] == 'indefinite' ) {
00142                 $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
00143             } elseif ( $params['prexpiry'] == 'definite' ) {
00144                 $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
00145             }
00146 
00147             $this->addOption( 'DISTINCT' );
00148 
00149         } elseif ( isset( $params['prlevel'] ) ) {
00150             $this->dieUsage( 'prlevel may not be used without prtype', 'params' );
00151         }
00152 
00153         if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
00154             $this->addTables( 'langlinks' );
00155             $this->addJoinConds( array( 'langlinks' => array( 'LEFT JOIN', 'page_id=ll_from' ) ) );
00156             $this->addWhere( 'll_from IS NULL' );
00157             $forceNameTitleIndex = false;
00158         } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
00159             $this->addTables( 'langlinks' );
00160             $this->addWhere( 'page_id=ll_from' );
00161             $this->addOption( 'STRAIGHT_JOIN' );
00162             // We have to GROUP BY all selected fields to stop
00163             // PostgreSQL from whining
00164             $this->addOption( 'GROUP BY', $selectFields );
00165             $forceNameTitleIndex = false;
00166         }
00167 
00168         if ( $forceNameTitleIndex ) {
00169             $this->addOption( 'USE INDEX', 'name_title' );
00170         }
00171 
00172         $limit = $params['limit'];
00173         $this->addOption( 'LIMIT', $limit + 1 );
00174         $res = $this->select( __METHOD__ );
00175 
00176         //Get gender information
00177         if ( MWNamespace::hasGenderDistinction( $params['namespace'] ) ) {
00178             $users = array();
00179             foreach ( $res as $row ) {
00180                 $users[] = $row->page_title;
00181             }
00182             GenderCache::singleton()->doQuery( $users, __METHOD__ );
00183             $res->rewind(); //reset
00184         }
00185 
00186         $count = 0;
00187         $result = $this->getResult();
00188         foreach ( $res as $row ) {
00189             if ( ++ $count > $limit ) {
00190                 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
00191                 $this->setContinueEnumParameter( 'continue', $row->page_title );
00192                 break;
00193             }
00194 
00195             if ( is_null( $resultPageSet ) ) {
00196                 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
00197                 $vals = array(
00198                     'pageid' => intval( $row->page_id ),
00199                     'ns' => intval( $title->getNamespace() ),
00200                     'title' => $title->getPrefixedText()
00201                 );
00202                 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
00203                 if ( !$fit ) {
00204                     $this->setContinueEnumParameter( 'continue', $row->page_title );
00205                     break;
00206                 }
00207             } else {
00208                 $resultPageSet->processDbRow( $row );
00209             }
00210         }
00211 
00212         if ( is_null( $resultPageSet ) ) {
00213             $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'p' );
00214         }
00215     }
00216 
00217     public function getAllowedParams() {
00218         global $wgRestrictionLevels;
00219 
00220         return array(
00221             'from' => null,
00222             'continue' => null,
00223             'to' => null,
00224             'prefix' => null,
00225             'namespace' => array(
00226                 ApiBase::PARAM_DFLT => NS_MAIN,
00227                 ApiBase::PARAM_TYPE => 'namespace',
00228             ),
00229             'filterredir' => array(
00230                 ApiBase::PARAM_DFLT => 'all',
00231                 ApiBase::PARAM_TYPE => array(
00232                     'all',
00233                     'redirects',
00234                     'nonredirects'
00235                 )
00236             ),
00237             'minsize' => array(
00238                 ApiBase::PARAM_TYPE => 'integer',
00239             ),
00240             'maxsize' => array(
00241                 ApiBase::PARAM_TYPE => 'integer',
00242             ),
00243             'prtype' => array(
00244                 ApiBase::PARAM_TYPE => Title::getFilteredRestrictionTypes( true ),
00245                 ApiBase::PARAM_ISMULTI => true
00246             ),
00247             'prlevel' => array(
00248                 ApiBase::PARAM_TYPE => $wgRestrictionLevels,
00249                 ApiBase::PARAM_ISMULTI => true
00250             ),
00251             'prfiltercascade' => array(
00252                 ApiBase::PARAM_DFLT => 'all',
00253                 ApiBase::PARAM_TYPE => array(
00254                     'cascading',
00255                     'noncascading',
00256                     'all'
00257                 ),
00258             ),
00259             'limit' => array(
00260                 ApiBase::PARAM_DFLT => 10,
00261                 ApiBase::PARAM_TYPE => 'limit',
00262                 ApiBase::PARAM_MIN => 1,
00263                 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
00264                 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
00265             ),
00266             'dir' => array(
00267                 ApiBase::PARAM_DFLT => 'ascending',
00268                 ApiBase::PARAM_TYPE => array(
00269                     'ascending',
00270                     'descending'
00271                 )
00272             ),
00273             'filterlanglinks' => array(
00274                 ApiBase::PARAM_TYPE => array(
00275                     'withlanglinks',
00276                     'withoutlanglinks',
00277                     'all'
00278                 ),
00279                 ApiBase::PARAM_DFLT => 'all'
00280             ),
00281             'prexpiry' => array(
00282                 ApiBase::PARAM_TYPE => array(
00283                     'indefinite',
00284                     'definite',
00285                     'all'
00286                 ),
00287                 ApiBase::PARAM_DFLT => 'all'
00288             ),
00289         );
00290     }
00291 
00292     public function getParamDescription() {
00293         $p = $this->getModulePrefix();
00294         return array(
00295             'from' => 'The page title to start enumerating from',
00296             'continue' => 'When more results are available, use this to continue',
00297             'to' => 'The page title to stop enumerating at',
00298             'prefix' => 'Search for all page titles that begin with this value',
00299             'namespace' => 'The namespace to enumerate',
00300             'filterredir' => 'Which pages to list',
00301             'dir' => 'The direction in which to list',
00302             'minsize' => 'Limit to pages with at least this many bytes',
00303             'maxsize' => 'Limit to pages with at most this many bytes',
00304             'prtype' => 'Limit to protected pages only',
00305             'prlevel' => "The protection level (must be used with {$p}prtype= parameter)",
00306             'prfiltercascade' => "Filter protections based on cascadingness (ignored when {$p}prtype isn't set)",
00307             'filterlanglinks' => array(
00308                 'Filter based on whether a page has langlinks',
00309                 'Note that this may not consider langlinks added by extensions.',
00310             ),
00311             'limit' => 'How many total pages to return.',
00312             'prexpiry' => array(
00313                 'Which protection expiry to filter the page on',
00314                 ' indefinite - Get only pages with indefinite protection expiry',
00315                 ' definite - Get only pages with a definite (specific) protection expiry',
00316                 ' all - Get pages with any protections expiry'
00317             ),
00318         );
00319     }
00320 
00321     public function getResultProperties() {
00322         return array(
00323             '' => array(
00324                 'pageid' => 'integer',
00325                 'ns' => 'namespace',
00326                 'title' => 'string'
00327             )
00328         );
00329     }
00330 
00331     public function getDescription() {
00332         return 'Enumerate all pages sequentially in a given namespace';
00333     }
00334 
00335     public function getPossibleErrors() {
00336         return array_merge( parent::getPossibleErrors(), array(
00337             array( 'code' => 'params', 'info' => 'Use "gapfilterredir=nonredirects" option instead of "redirects" when using allpages as a generator' ),
00338             array( 'code' => 'params', 'info' => 'prlevel may not be used without prtype' ),
00339         ) );
00340     }
00341 
00342     public function getExamples() {
00343         return array(
00344             'api.php?action=query&list=allpages&apfrom=B' => array(
00345                 'Simple Use',
00346                 'Show a list of pages starting at the letter "B"',
00347             ),
00348             'api.php?action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info' => array(
00349                 'Using as Generator',
00350                 'Show info about 4 pages starting at the letter "T"',
00351             ),
00352             'api.php?action=query&generator=allpages&gaplimit=2&gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content' => array(
00353                 'Show content of first 2 non-redirect pages beginning at "Re"',
00354             )
00355         );
00356     }
00357 
00358     public function getHelpUrls() {
00359         return 'https://www.mediawiki.org/wiki/API:Allpages';
00360     }
00361 }