MediaWiki  master
ApiQueryCategoryMembers.php
Go to the documentation of this file.
1 <?php
28 
35 
37  private $collation;
38 
44  public function __construct(
45  ApiQuery $query,
46  $moduleName,
47  CollationFactory $collationFactory
48  ) {
49  parent::__construct( $query, $moduleName, 'cm' );
50  $this->collation = $collationFactory->getCategoryCollation();
51  }
52 
53  public function execute() {
54  $this->run();
55  }
56 
57  public function getCacheMode( $params ) {
58  return 'public';
59  }
60 
61  public function executeGenerator( $resultPageSet ) {
62  $this->run( $resultPageSet );
63  }
64 
69  private function validateHexSortkey( $hexSortkey ) {
70  // A hex sortkey has an unbound number of 2 letter pairs
71  return (bool)preg_match( '/^(?:[a-fA-F0-9]{2})*$/D', $hexSortkey );
72  }
73 
78  private function run( $resultPageSet = null ) {
79  $params = $this->extractRequestParams();
80 
81  $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
82  if ( $categoryTitle->getNamespace() !== NS_CATEGORY ) {
83  $this->dieWithError( 'apierror-invalidcategory' );
84  }
85 
86  $prop = array_fill_keys( $params['prop'], true );
87  $fld_ids = isset( $prop['ids'] );
88  $fld_title = isset( $prop['title'] );
89  $fld_sortkey = isset( $prop['sortkey'] );
90  $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
91  $fld_timestamp = isset( $prop['timestamp'] );
92  $fld_type = isset( $prop['type'] );
93 
94  if ( $resultPageSet === null ) {
95  $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title' ] );
96  $this->addFieldsIf( 'page_id', $fld_ids );
97  $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
98  } else {
99  $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
100  $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type' ] );
101  }
102 
103  $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
104 
105  $this->addTables( [ 'page', 'categorylinks' ] ); // must be in this order for 'USE INDEX'
106 
107  $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
108  $queryTypes = $params['type'];
109  $contWhere = false;
110 
111  // Scanning large datasets for rare categories sucks, and I already told
112  // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
113  $miser_ns = [];
114  if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
115  $miser_ns = $params['namespace'] ?: [];
116  } else {
117  $this->addWhereFld( 'page_namespace', $params['namespace'] );
118  }
119 
120  $dir = in_array( $params['dir'], [ 'asc', 'ascending', 'newer' ] ) ? 'newer' : 'older';
121 
122  if ( $params['sort'] == 'timestamp' ) {
123  $this->addTimestampWhereRange( 'cl_timestamp',
124  $dir,
125  $params['start'],
126  $params['end'] );
127  // Include in ORDER BY for uniqueness
128  $this->addWhereRange( 'cl_from', $dir, null, null );
129 
130  if ( $params['continue'] !== null ) {
131  $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'timestamp', 'int' ] );
132  $op = ( $dir === 'newer' ? '>=' : '<=' );
133  $db = $this->getDB();
134  $this->addWhere( $db->buildComparison( $op, [
135  'cl_timestamp' => $db->timestamp( $cont[0] ),
136  'cl_from' => $cont[1],
137  ] ) );
138  }
139 
140  $this->addOption( 'USE INDEX', [ 'categorylinks' => 'cl_timestamp' ] );
141  } else {
142  if ( $params['continue'] ) {
143  $cont = $this->parseContinueParamOrDie( $params['continue'], [ 'string', 'string', 'int' ] );
144 
145  // Remove the types to skip from $queryTypes
146  $contTypeIndex = array_search( $cont[0], $queryTypes );
147  $queryTypes = array_slice( $queryTypes, $contTypeIndex );
148 
149  // Add a WHERE clause for sortkey and from
150  $this->dieContinueUsageIf( !$this->validateHexSortkey( $cont[1] ) );
151  $op = $dir == 'newer' ? '>=' : '<=';
152  // $contWhere is used further down
153  $contWhere = $this->getDB()->buildComparison( $op, [
154  'cl_sortkey' => hex2bin( $cont[1] ),
155  'cl_from' => $cont[2],
156  ] );
157  // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
158  $this->addWhereRange( 'cl_sortkey', $dir, null, null );
159  $this->addWhereRange( 'cl_from', $dir, null, null );
160  } else {
161  if ( $params['startsortkeyprefix'] !== null ) {
162  $startsortkey = $this->collation->getSortKey( $params['startsortkeyprefix'] );
163  } elseif ( $params['starthexsortkey'] !== null ) {
164  if ( !$this->validateHexSortkey( $params['starthexsortkey'] ) ) {
165  $encParamName = $this->encodeParamName( 'starthexsortkey' );
166  $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
167  }
168  $startsortkey = hex2bin( $params['starthexsortkey'] );
169  } else {
170  $startsortkey = $params['startsortkey'];
171  }
172  if ( $params['endsortkeyprefix'] !== null ) {
173  $endsortkey = $this->collation->getSortKey( $params['endsortkeyprefix'] );
174  } elseif ( $params['endhexsortkey'] !== null ) {
175  if ( !$this->validateHexSortkey( $params['endhexsortkey'] ) ) {
176  $encParamName = $this->encodeParamName( 'endhexsortkey' );
177  $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
178  }
179  $endsortkey = hex2bin( $params['endhexsortkey'] );
180  } else {
181  $endsortkey = $params['endsortkey'];
182  }
183 
184  // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
185  $this->addWhereRange( 'cl_sortkey',
186  $dir,
187  $startsortkey,
188  $endsortkey );
189  $this->addWhereRange( 'cl_from', $dir, null, null );
190  }
191  $this->addOption( 'USE INDEX', [ 'categorylinks' => 'cl_sortkey' ] );
192  }
193 
194  $this->addWhere( 'cl_from=page_id' );
195 
196  $limit = $params['limit'];
197  $this->addOption( 'LIMIT', $limit + 1 );
198 
199  if ( $params['sort'] == 'sortkey' ) {
200  // Run a separate SELECT query for each value of cl_type.
201  // This is needed because cl_type is an enum, and MySQL has
202  // inconsistencies between ORDER BY cl_type and
203  // WHERE cl_type >= 'foo' making proper paging impossible
204  // and unindexed.
205  $rows = [];
206  $first = true;
207  foreach ( $queryTypes as $type ) {
208  $extraConds = [ 'cl_type' => $type ];
209  if ( $first && $contWhere ) {
210  // Continuation condition. Only added to the
211  // first query, otherwise we'll skip things
212  $extraConds[] = $contWhere;
213  }
214  $res = $this->select( __METHOD__, [ 'where' => $extraConds ] );
215  if ( $type === 'page' && $resultPageSet === null ) {
216  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
217  }
218  $rows = array_merge( $rows, iterator_to_array( $res ) );
219  if ( count( $rows ) >= $limit + 1 ) {
220  break;
221  }
222  $first = false;
223  }
224  } else {
225  // Sorting by timestamp
226  // No need to worry about per-type queries because we
227  // aren't sorting or filtering by type anyway
228  $res = $this->select( __METHOD__ );
229  if ( $resultPageSet === null ) {
230  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
231  }
232  $rows = iterator_to_array( $res );
233  }
234 
235  $result = $this->getResult();
236  $count = 0;
237  foreach ( $rows as $row ) {
238  if ( ++$count > $limit ) {
239  // We've reached the one extra which shows that there are
240  // additional pages to be had. Stop here...
241  // @todo Security issue - if the user has no right to view next
242  // title, it will still be shown
243  if ( $params['sort'] == 'timestamp' ) {
245  'continue',
246  $this->getDB()->timestamp( $row->cl_timestamp ) . "|$row->cl_from"
247  );
248  } else {
249  $sortkey = bin2hex( $row->cl_sortkey );
250  $this->setContinueEnumParameter( 'continue',
251  "{$row->cl_type}|$sortkey|{$row->cl_from}"
252  );
253  }
254  break;
255  }
256 
257  // Since domas won't tell anyone what he told long ago, apply
258  // cmnamespace here. This means the query may return 0 actual
259  // results, but on the other hand it could save returning 5000
260  // useless results to the client. ~~~~
261  if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
262  continue;
263  }
264 
265  if ( $resultPageSet === null ) {
266  $vals = [
267  ApiResult::META_TYPE => 'assoc',
268  ];
269  if ( $fld_ids ) {
270  $vals['pageid'] = (int)$row->page_id;
271  }
272  if ( $fld_title ) {
273  $title = Title::makeTitle( $row->page_namespace, $row->page_title );
275  }
276  if ( $fld_sortkey ) {
277  $vals['sortkey'] = bin2hex( $row->cl_sortkey );
278  }
279  if ( $fld_sortkeyprefix ) {
280  $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
281  }
282  if ( $fld_type ) {
283  $vals['type'] = $row->cl_type;
284  }
285  if ( $fld_timestamp ) {
286  $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
287  }
288  $fit = $result->addValue( [ 'query', $this->getModuleName() ],
289  null, $vals );
290  if ( !$fit ) {
291  if ( $params['sort'] == 'timestamp' ) {
293  'continue',
294  $this->getDB()->timestamp( $row->cl_timestamp ) . "|$row->cl_from"
295  );
296  } else {
297  $sortkey = bin2hex( $row->cl_sortkey );
298  $this->setContinueEnumParameter( 'continue',
299  "{$row->cl_type}|$sortkey|{$row->cl_from}"
300  );
301  }
302  break;
303  }
304  } else {
305  $resultPageSet->processDbRow( $row );
306  }
307  }
308 
309  if ( $resultPageSet === null ) {
310  $result->addIndexedTagName(
311  [ 'query', $this->getModuleName() ], 'cm' );
312  }
313  }
314 
315  public function getAllowedParams() {
316  $ret = [
317  'title' => [
318  ParamValidator::PARAM_TYPE => 'string',
319  ],
320  'pageid' => [
321  ParamValidator::PARAM_TYPE => 'integer'
322  ],
323  'prop' => [
324  ParamValidator::PARAM_DEFAULT => 'ids|title',
325  ParamValidator::PARAM_ISMULTI => true,
326  ParamValidator::PARAM_TYPE => [
327  'ids',
328  'title',
329  'sortkey',
330  'sortkeyprefix',
331  'type',
332  'timestamp',
333  ],
335  ],
336  'namespace' => [
337  ParamValidator::PARAM_ISMULTI => true,
338  ParamValidator::PARAM_TYPE => 'namespace',
339  ],
340  'type' => [
341  ParamValidator::PARAM_ISMULTI => true,
342  ParamValidator::PARAM_DEFAULT => 'page|subcat|file',
343  ParamValidator::PARAM_TYPE => [
344  'page',
345  'subcat',
346  'file'
347  ]
348  ],
349  'continue' => [
350  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
351  ],
352  'limit' => [
353  ParamValidator::PARAM_TYPE => 'limit',
354  ParamValidator::PARAM_DEFAULT => 10,
355  IntegerDef::PARAM_MIN => 1,
356  IntegerDef::PARAM_MAX => ApiBase::LIMIT_BIG1,
357  IntegerDef::PARAM_MAX2 => ApiBase::LIMIT_BIG2
358  ],
359  'sort' => [
360  ParamValidator::PARAM_DEFAULT => 'sortkey',
361  ParamValidator::PARAM_TYPE => [
362  'sortkey',
363  'timestamp'
364  ]
365  ],
366  'dir' => [
367  ParamValidator::PARAM_DEFAULT => 'ascending',
368  ParamValidator::PARAM_TYPE => [
369  'asc',
370  'desc',
371  // Normalising with other modules
372  'ascending',
373  'descending',
374  'newer',
375  'older',
376  ]
377  ],
378  'start' => [
379  ParamValidator::PARAM_TYPE => 'timestamp'
380  ],
381  'end' => [
382  ParamValidator::PARAM_TYPE => 'timestamp'
383  ],
384  'starthexsortkey' => null,
385  'endhexsortkey' => null,
386  'startsortkeyprefix' => null,
387  'endsortkeyprefix' => null,
388  'startsortkey' => [
389  ParamValidator::PARAM_DEPRECATED => true,
390  ],
391  'endsortkey' => [
392  ParamValidator::PARAM_DEPRECATED => true,
393  ],
394  ];
395 
396  if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
397  $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
398  'api-help-param-limited-in-miser-mode',
399  ];
400  }
401 
402  return $ret;
403  }
404 
405  protected function getExamplesMessages() {
406  return [
407  'action=query&list=categorymembers&cmtitle=Category:Physics'
408  => 'apihelp-query+categorymembers-example-simple',
409  'action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info'
410  => 'apihelp-query+categorymembers-example-generator',
411  ];
412  }
413 
414  public function getHelpUrls() {
415  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categorymembers';
416  }
417 }
const NS_CATEGORY
Definition: Defines.php:78
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition: ApiBase.php:1460
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1688
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:173
parseContinueParamOrDie(string $continue, array $types)
Parse the 'continue' parameter in the usual format and validate the types of each part,...
Definition: ApiBase.php:1649
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, or 'string' with PARAM_ISMULTI,...
Definition: ApiBase.php:204
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:229
getResult()
Get the result object.
Definition: ApiBase.php:637
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:773
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:166
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:231
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:506
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:1044
static addTitleInfo(&$arr, $title, $prefix='')
Add information (title and namespace) about a Title object to a result array.
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.
addFields( $value)
Add a set of fields to select to the internal array.
addOption( $name, $value=null)
Add an option such as LIMIT or USE INDEX.
addTables( $tables, $alias=null)
Add a set of tables to the internal array.
addTimestampWhereRange( $field, $dir, $start, $end, $sort=true)
Add a WHERE clause corresponding to a range, similar to addWhereRange, but converts $start and $end t...
getDB()
Get the Query database connection (read-only)
executeGenderCacheFromResultWrapper(IResultWrapper $res, $fname=__METHOD__, $fieldPrefix='page')
Preprocess the result set to fill the GenderCache with the necessary information before using self::a...
select( $method, $extraQuery=[], array &$hookData=null)
Execute a SELECT query based on the values in the internal arrays.
addFieldsIf( $value, $condition)
Same as addFields(), but add the fields only if a condition is met.
addWhereFld( $field, $value)
Equivalent to addWhere( [ $field => $value ] )
addWhere( $value)
Add a set of WHERE clauses to the internal array.
A query module to enumerate pages that belong to a category.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
__construct(ApiQuery $query, $moduleName, CollationFactory $collationFactory)
executeGenerator( $resultPageSet)
Execute this module as a generator.
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
getCacheMode( $params)
Get the cache mode for the data generated by this module.
getExamplesMessages()
Returns usage examples for this module.
getHelpUrls()
Return links to more detailed help pages about the module.
setContinueEnumParameter( $paramName, $paramValue)
Overridden to set the generator param if in generator mode.
encodeParamName( $paramName)
Overrides ApiBase to prepend 'g' to every generator parameter.
This is the main query class.
Definition: ApiQuery.php:42
const META_TYPE
Key for the 'type' metadata item.
Definition: ApiResult.php:110
Common factory to construct collation classes.
A class containing constants representing the names of configuration variables.
Represents a title within MediaWiki.
Definition: Title.php:82
Service for formatting and validating API parameters.
Type definition for integer types.
Definition: IntegerDef.php:23