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