MediaWiki  master
ApiQueryAllPages.php
Go to the documentation of this file.
1 <?php
23 
30 
35  public function __construct( ApiQuery $query, $moduleName ) {
36  parent::__construct( $query, $moduleName, 'ap' );
37  }
38 
39  public function execute() {
40  $this->run();
41  }
42 
43  public function getCacheMode( $params ) {
44  return 'public';
45  }
46 
51  public function executeGenerator( $resultPageSet ) {
52  if ( $resultPageSet->isResolvingRedirects() ) {
53  $this->dieWithError( 'apierror-allpages-generator-redirects', 'params' );
54  }
55 
56  $this->run( $resultPageSet );
57  }
58 
63  private function run( $resultPageSet = null ) {
64  $db = $this->getDB();
65 
66  $params = $this->extractRequestParams();
67 
68  // Page filters
69  $this->addTables( 'page' );
70 
71  if ( $params['continue'] !== null ) {
72  $cont = explode( '|', $params['continue'] );
73  $this->dieContinueUsageIf( count( $cont ) != 1 );
74  $op = $params['dir'] == 'descending' ? '<' : '>';
75  $cont_from = $db->addQuotes( $cont[0] );
76  $this->addWhere( "page_title $op= $cont_from" );
77  }
78 
79  $miserMode = $this->getConfig()->get( 'MiserMode' );
80  if ( !$miserMode ) {
81  if ( $params['filterredir'] == 'redirects' ) {
82  $this->addWhereFld( 'page_is_redirect', 1 );
83  } elseif ( $params['filterredir'] == 'nonredirects' ) {
84  $this->addWhereFld( 'page_is_redirect', 0 );
85  }
86  }
87 
88  $this->addWhereFld( 'page_namespace', $params['namespace'] );
89  $dir = ( $params['dir'] == 'descending' ? 'older' : 'newer' );
90  $from = ( $params['from'] === null
91  ? null
92  : $this->titlePartToKey( $params['from'], $params['namespace'] ) );
93  $to = ( $params['to'] === null
94  ? null
95  : $this->titlePartToKey( $params['to'], $params['namespace'] ) );
96  $this->addWhereRange( 'page_title', $dir, $from, $to );
97 
98  if ( isset( $params['prefix'] ) ) {
99  $this->addWhere( 'page_title' . $db->buildLike(
100  $this->titlePartToKey( $params['prefix'], $params['namespace'] ),
101  $db->anyString() ) );
102  }
103 
104  if ( $resultPageSet === null ) {
105  $selectFields = [
106  'page_namespace',
107  'page_title',
108  'page_id'
109  ];
110  } else {
111  $selectFields = $resultPageSet->getPageTableFields();
112  }
113 
114  $miserModeFilterRedirValue = null;
115  $miserModeFilterRedir = $miserMode && $params['filterredir'] !== 'all';
116  if ( $miserModeFilterRedir ) {
117  $selectFields[] = 'page_is_redirect';
118 
119  if ( $params['filterredir'] == 'redirects' ) {
120  $miserModeFilterRedirValue = 1;
121  } elseif ( $params['filterredir'] == 'nonredirects' ) {
122  $miserModeFilterRedirValue = 0;
123  }
124  }
125 
126  $this->addFields( $selectFields );
127  $forceNameTitleIndex = true;
128  if ( isset( $params['minsize'] ) ) {
129  $this->addWhere( 'page_len>=' . (int)$params['minsize'] );
130  $forceNameTitleIndex = false;
131  }
132 
133  if ( isset( $params['maxsize'] ) ) {
134  $this->addWhere( 'page_len<=' . (int)$params['maxsize'] );
135  $forceNameTitleIndex = false;
136  }
137 
138  // Page protection filtering
139  if ( $params['prtype'] || $params['prexpiry'] != 'all' ) {
140  $this->addTables( 'page_restrictions' );
141  $this->addWhere( 'page_id=pr_page' );
142  $this->addWhere( "pr_expiry > {$db->addQuotes( $db->timestamp() )} OR pr_expiry IS NULL" );
143 
144  if ( $params['prtype'] ) {
145  $this->addWhereFld( 'pr_type', $params['prtype'] );
146 
147  if ( isset( $params['prlevel'] ) ) {
148  // Remove the empty string and '*' from the prlevel array
149  $prlevel = array_diff( $params['prlevel'], [ '', '*' ] );
150 
151  if ( count( $prlevel ) ) {
152  $this->addWhereFld( 'pr_level', $prlevel );
153  }
154  }
155  if ( $params['prfiltercascade'] == 'cascading' ) {
156  $this->addWhereFld( 'pr_cascade', 1 );
157  } elseif ( $params['prfiltercascade'] == 'noncascading' ) {
158  $this->addWhereFld( 'pr_cascade', 0 );
159  }
160  }
161  $forceNameTitleIndex = false;
162 
163  if ( $params['prexpiry'] == 'indefinite' ) {
164  $this->addWhere( "pr_expiry = {$db->addQuotes( $db->getInfinity() )} OR pr_expiry IS NULL" );
165  } elseif ( $params['prexpiry'] == 'definite' ) {
166  $this->addWhere( "pr_expiry != {$db->addQuotes( $db->getInfinity() )}" );
167  }
168 
169  $this->addOption( 'DISTINCT' );
170  } elseif ( isset( $params['prlevel'] ) ) {
171  $this->dieWithError(
172  [ 'apierror-invalidparammix-mustusewith', 'prlevel', 'prtype' ], 'invalidparammix'
173  );
174  }
175 
176  if ( $params['filterlanglinks'] == 'withoutlanglinks' ) {
177  $this->addTables( 'langlinks' );
178  $this->addJoinConds( [ 'langlinks' => [ 'LEFT JOIN', 'page_id=ll_from' ] ] );
179  $this->addWhere( 'll_from IS NULL' );
180  $forceNameTitleIndex = false;
181  } elseif ( $params['filterlanglinks'] == 'withlanglinks' ) {
182  $this->addTables( 'langlinks' );
183  $this->addWhere( 'page_id=ll_from' );
184  $this->addOption( 'STRAIGHT_JOIN' );
185 
186  // MySQL filesorts if we use a GROUP BY that works with the rules
187  // in the 1992 SQL standard (it doesn't like having the
188  // constant-in-WHERE page_namespace column in there). Using the
189  // 1999 rules works fine, but that breaks other DBs. Sigh.
190  // @todo Once we drop support for 1992-rule DBs, we can simplify this.
191  $dbType = $db->getType();
192  if ( $dbType === 'mysql' || $dbType === 'sqlite' ) {
193  // Ignore the rules, or 1999 rules if you count unique keys
194  // over non-NULL columns as satisfying the requirement for
195  // "functional dependency" and don't require including
196  // constant-in-WHERE columns in the GROUP BY.
197  $this->addOption( 'GROUP BY', [ 'page_title' ] );
198  } elseif ( $dbType === 'postgres' && $db->getServerVersion() >= 9.1 ) {
199  // 1999 rules only counting primary keys
200  $this->addOption( 'GROUP BY', [ 'page_title', 'page_id' ] );
201  } else {
202  // 1992 rules
203  $this->addOption( 'GROUP BY', $selectFields );
204  }
205 
206  $forceNameTitleIndex = false;
207  }
208 
209  if ( $forceNameTitleIndex ) {
210  $this->addOption( 'USE INDEX', 'name_title' );
211  }
212 
213  $limit = $params['limit'];
214  $this->addOption( 'LIMIT', $limit + 1 );
215  $res = $this->select( __METHOD__ );
216 
217  // Get gender information
218  $services = MediaWikiServices::getInstance();
219  if ( $services->getNamespaceInfo()->hasGenderDistinction( $params['namespace'] ) ) {
220  $users = [];
221  foreach ( $res as $row ) {
222  $users[] = $row->page_title;
223  }
224  $services->getGenderCache()->doQuery( $users, __METHOD__ );
225  $res->rewind(); // reset
226  }
227 
228  $count = 0;
229  $result = $this->getResult();
230  foreach ( $res as $row ) {
231  if ( ++$count > $limit ) {
232  // We've reached the one extra which shows that there are
233  // additional pages to be had. Stop here...
234  $this->setContinueEnumParameter( 'continue', $row->page_title );
235  break;
236  }
237 
238  if ( $miserModeFilterRedir && (int)$row->page_is_redirect !== $miserModeFilterRedirValue ) {
239  // Filter implemented in PHP due to being in Miser Mode
240  continue;
241  }
242 
243  if ( $resultPageSet === null ) {
244  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
245  $vals = [
246  'pageid' => (int)$row->page_id,
247  'ns' => $title->getNamespace(),
248  'title' => $title->getPrefixedText()
249  ];
250  $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
251  if ( !$fit ) {
252  $this->setContinueEnumParameter( 'continue', $row->page_title );
253  break;
254  }
255  } else {
256  $resultPageSet->processDbRow( $row );
257  }
258  }
259 
260  if ( $resultPageSet === null ) {
261  $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'p' );
262  }
263  }
264 
265  public function getAllowedParams() {
266  $ret = [
267  'from' => null,
268  'continue' => [
269  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
270  ],
271  'to' => null,
272  'prefix' => null,
273  'namespace' => [
275  ApiBase::PARAM_TYPE => 'namespace',
276  ],
277  'filterredir' => [
278  ApiBase::PARAM_DFLT => 'all',
280  'all',
281  'redirects',
282  'nonredirects'
283  ]
284  ],
285  'minsize' => [
286  ApiBase::PARAM_TYPE => 'integer',
287  ],
288  'maxsize' => [
289  ApiBase::PARAM_TYPE => 'integer',
290  ],
291  'prtype' => [
294  ],
295  'prlevel' => [
296  ApiBase::PARAM_TYPE => $this->getConfig()->get( 'RestrictionLevels' ),
298  ],
299  'prfiltercascade' => [
300  ApiBase::PARAM_DFLT => 'all',
301  ApiBase::PARAM_TYPE => [
302  'cascading',
303  'noncascading',
304  'all'
305  ],
306  ],
307  'limit' => [
308  ApiBase::PARAM_DFLT => 10,
309  ApiBase::PARAM_TYPE => 'limit',
310  ApiBase::PARAM_MIN => 1,
313  ],
314  'dir' => [
315  ApiBase::PARAM_DFLT => 'ascending',
317  'ascending',
318  'descending'
319  ]
320  ],
321  'filterlanglinks' => [
323  'withlanglinks',
324  'withoutlanglinks',
325  'all'
326  ],
327  ApiBase::PARAM_DFLT => 'all'
328  ],
329  'prexpiry' => [
331  'indefinite',
332  'definite',
333  'all'
334  ],
335  ApiBase::PARAM_DFLT => 'all'
336  ],
337  ];
338 
339  if ( $this->getConfig()->get( 'MiserMode' ) ) {
340  $ret['filterredir'][ApiBase::PARAM_HELP_MSG_APPEND] = [ 'api-help-param-limited-in-miser-mode' ];
341  }
342 
343  return $ret;
344  }
345 
346  protected function getExamplesMessages() {
347  return [
348  'action=query&list=allpages&apfrom=B'
349  => 'apihelp-query+allpages-example-b',
350  'action=query&generator=allpages&gaplimit=4&gapfrom=T&prop=info'
351  => 'apihelp-query+allpages-example-generator',
352  'action=query&generator=allpages&gaplimit=2&' .
353  'gapfilterredir=nonredirects&gapfrom=Re&prop=revisions&rvprop=content'
354  => 'apihelp-query+allpages-example-generator-revisions',
355  ];
356  }
357 
358  public function getHelpUrls() {
359  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Allpages';
360  }
361 }
ContextSource\getConfig
getConfig()
Definition: ContextSource.php:71
ApiQueryBase\addFields
addFields( $value)
Add a set of fields to select to the internal array.
Definition: ApiQueryBase.php:212
ApiQuery
This is the main query class.
Definition: ApiQuery.php:37
Title\getFilteredRestrictionTypes
static getFilteredRestrictionTypes( $exists=true)
Get a filtered list of all restriction types supported by this wiki.
Definition: Title.php:2447
ApiQueryAllPages\getHelpUrls
getHelpUrls()
Return links to more detailed help pages about the module.
Definition: ApiQueryAllPages.php:358
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:173
ApiQueryAllPages
Query module to enumerate all available pages.
Definition: ApiQueryAllPages.php:29
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1384
ApiBase\PARAM_HELP_MSG
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:105
true
return true
Definition: router.php:90
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:72
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:561
ApiQueryBase\addOption
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
Definition: ApiQueryBase.php:378
$res
$res
Definition: testCompression.php:57
ApiBase\PARAM_HELP_MSG_APPEND
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:112
ApiQueryAllPages\__construct
__construct(ApiQuery $query, $moduleName)
Definition: ApiQueryAllPages.php:35
NS_MAIN
const NS_MAIN
Definition: Defines.php:64
ApiQueryGeneratorBase\setContinueEnumParameter
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
Definition: ApiQueryGeneratorBase.php:83
ApiBase\PARAM_MIN
const PARAM_MIN
Definition: ApiBase.php:75
ApiBase\LIMIT_BIG1
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:163
ApiQueryBase\getDB
getDB()
Get the Query database connection (read-only)
Definition: ApiQueryBase.php:117
ApiBase\PARAM_MAX
const PARAM_MAX
Definition: ApiBase.php:73
ApiQueryBase\addTables
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
Definition: ApiQueryBase.php:182
ApiQueryBase\select
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
Definition: ApiQueryBase.php:399
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:720
ApiQueryAllPages\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiQueryAllPages.php:346
$title
$title
Definition: testCompression.php:38
Title\makeTitle
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:626
ApiQueryBase\addWhereRange
addWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, and an ORDER BY clause to sort in the right direction.
Definition: ApiQueryBase.php:337
ApiBase\dieContinueUsageIf
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1567
ApiQueryBase\addJoinConds
addJoinConds( $join_conds)
Add a set of JOIN conditions to the internal array.
Definition: ApiQueryBase.php:201
ApiQueryAllPages\run
run( $resultPageSet=null)
Definition: ApiQueryAllPages.php:63
ApiQueryAllPages\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiQueryAllPages.php:265
ApiQueryBase\addWhereFld
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
Definition: ApiQueryBase.php:282
ApiQueryAllPages\executeGenerator
executeGenerator( $resultPageSet)
Definition: ApiQueryAllPages.php:51
ApiQueryAllPages\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiQueryAllPages.php:39
ApiQueryGeneratorBase
Definition: ApiQueryGeneratorBase.php:28
ApiBase\LIMIT_BIG2
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:165
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:70
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:440
ApiBase\PARAM_ISMULTI
const PARAM_ISMULTI
Definition: ApiBase.php:71
ApiBase\PARAM_MAX2
const PARAM_MAX2
Definition: ApiBase.php:74
ApiQueryBase\addWhere
addWhere( $value)
Add a set of WHERE clauses to the internal array.
Definition: ApiQueryBase.php:245
ApiQueryAllPages\getCacheMode
getCacheMode( $params)
Get the cache mode for the data generated by this module.
Definition: ApiQueryAllPages.php:43
ApiQueryBase\titlePartToKey
titlePartToKey( $titlePart, $namespace=NS_MAIN)
Convert an input title or title prefix into a dbkey.
Definition: ApiQueryBase.php:529