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