MediaWiki  master
ApiQueryCategoryMembers.php
Go to the documentation of this file.
1 <?php
25 
32 
34  private $collation;
35 
41  public function __construct(
42  ApiQuery $query,
43  $moduleName,
44  CollationFactory $collationFactory
45  ) {
46  parent::__construct( $query, $moduleName, 'cm' );
47  $this->collation = $collationFactory->getCategoryCollation();
48  }
49 
50  public function execute() {
51  $this->run();
52  }
53 
54  public function getCacheMode( $params ) {
55  return 'public';
56  }
57 
58  public function executeGenerator( $resultPageSet ) {
59  $this->run( $resultPageSet );
60  }
61 
66  private function validateHexSortkey( $hexSortkey ) {
67  // A hex sortkey has an unbound number of 2 letter pairs
68  return (bool)preg_match( '/^(?:[a-fA-F0-9]{2})*$/D', $hexSortkey );
69  }
70 
75  private function run( $resultPageSet = null ) {
76  $params = $this->extractRequestParams();
77 
78  $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
79  if ( $categoryTitle->getNamespace() !== NS_CATEGORY ) {
80  $this->dieWithError( 'apierror-invalidcategory' );
81  }
82 
83  $prop = array_fill_keys( $params['prop'], true );
84  $fld_ids = isset( $prop['ids'] );
85  $fld_title = isset( $prop['title'] );
86  $fld_sortkey = isset( $prop['sortkey'] );
87  $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
88  $fld_timestamp = isset( $prop['timestamp'] );
89  $fld_type = isset( $prop['type'] );
90 
91  if ( $resultPageSet === null ) {
92  $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title' ] );
93  $this->addFieldsIf( 'page_id', $fld_ids );
94  $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
95  } else {
96  $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
97  $this->addFields( [ 'cl_from', 'cl_sortkey', 'cl_type' ] );
98  }
99 
100  $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
101 
102  $this->addTables( [ 'page', 'categorylinks' ] ); // must be in this order for 'USE INDEX'
103 
104  $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
105  $queryTypes = $params['type'];
106  $contWhere = false;
107 
108  // Scanning large datasets for rare categories sucks, and I already told
109  // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
110  $miser_ns = [];
111  if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
112  $miser_ns = $params['namespace'] ?: [];
113  } else {
114  $this->addWhereFld( 'page_namespace', $params['namespace'] );
115  }
116 
117  $dir = in_array( $params['dir'], [ 'asc', 'ascending', 'newer' ] ) ? 'newer' : 'older';
118 
119  if ( $params['sort'] == 'timestamp' ) {
120  $this->addTimestampWhereRange( 'cl_timestamp',
121  $dir,
122  $params['start'],
123  $params['end'] );
124  // Include in ORDER BY for uniqueness
125  $this->addWhereRange( 'cl_from', $dir, null, null );
126 
127  if ( $params['continue'] !== null ) {
128  $cont = explode( '|', $params['continue'] );
129  $this->dieContinueUsageIf( count( $cont ) != 2 );
130  $op = ( $dir === 'newer' ? '>' : '<' );
131  $db = $this->getDB();
132  $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
133  $continueFrom = (int)$cont[1];
134  $this->dieContinueUsageIf( $continueFrom != $cont[1] );
135  $this->addWhere( "cl_timestamp $op $continueTimestamp OR " .
136  "(cl_timestamp = $continueTimestamp AND " .
137  "cl_from $op= $continueFrom)"
138  );
139  }
140 
141  $this->addOption( 'USE INDEX', 'cl_timestamp' );
142  } else {
143  if ( $params['continue'] ) {
144  $cont = explode( '|', $params['continue'], 3 );
145  $this->dieContinueUsageIf( count( $cont ) != 3 );
146 
147  // Remove the types to skip from $queryTypes
148  $contTypeIndex = array_search( $cont[0], $queryTypes );
149  $queryTypes = array_slice( $queryTypes, $contTypeIndex );
150 
151  // Add a WHERE clause for sortkey and from
152  $this->dieContinueUsageIf( !$this->validateHexSortkey( $cont[1] ) );
153  $escSortkey = $this->getDB()->addQuotes( hex2bin( $cont[1] ) );
154  $from = (int)$cont[2];
155  $op = $dir == 'newer' ? '>' : '<';
156  // $contWhere is used further down
157  $contWhere = "cl_sortkey $op $escSortkey OR " .
158  "(cl_sortkey = $escSortkey AND " .
159  "cl_from $op= $from)";
160  // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
161  $this->addWhereRange( 'cl_sortkey', $dir, null, null );
162  $this->addWhereRange( 'cl_from', $dir, null, null );
163  } else {
164  if ( $params['startsortkeyprefix'] !== null ) {
165  $startsortkey = $this->collation->getSortKey( $params['startsortkeyprefix'] );
166  } elseif ( $params['starthexsortkey'] !== null ) {
167  if ( !$this->validateHexSortkey( $params['starthexsortkey'] ) ) {
168  $encParamName = $this->encodeParamName( 'starthexsortkey' );
169  $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
170  }
171  $startsortkey = hex2bin( $params['starthexsortkey'] );
172  } else {
173  $startsortkey = $params['startsortkey'];
174  }
175  if ( $params['endsortkeyprefix'] !== null ) {
176  $endsortkey = $this->collation->getSortKey( $params['endsortkeyprefix'] );
177  } elseif ( $params['endhexsortkey'] !== null ) {
178  if ( !$this->validateHexSortkey( $params['endhexsortkey'] ) ) {
179  $encParamName = $this->encodeParamName( 'endhexsortkey' );
180  $this->dieWithError( [ 'apierror-badparameter', $encParamName ], "badvalue_$encParamName" );
181  }
182  $endsortkey = hex2bin( $params['endhexsortkey'] );
183  } else {
184  $endsortkey = $params['endsortkey'];
185  }
186 
187  // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
188  $this->addWhereRange( 'cl_sortkey',
189  $dir,
190  $startsortkey,
191  $endsortkey );
192  $this->addWhereRange( 'cl_from', $dir, null, null );
193  }
194  $this->addOption( 'USE INDEX', 'cl_sortkey' );
195  }
196 
197  $this->addWhere( 'cl_from=page_id' );
198 
199  $limit = $params['limit'];
200  $this->addOption( 'LIMIT', $limit + 1 );
201 
202  if ( $params['sort'] == 'sortkey' ) {
203  // Run a separate SELECT query for each value of cl_type.
204  // This is needed because cl_type is an enum, and MySQL has
205  // inconsistencies between ORDER BY cl_type and
206  // WHERE cl_type >= 'foo' making proper paging impossible
207  // and unindexed.
208  $rows = [];
209  $first = true;
210  foreach ( $queryTypes as $type ) {
211  $extraConds = [ 'cl_type' => $type ];
212  if ( $first && $contWhere ) {
213  // Continuation condition. Only added to the
214  // first query, otherwise we'll skip things
215  $extraConds[] = $contWhere;
216  }
217  $res = $this->select( __METHOD__, [ 'where' => $extraConds ] );
218  if ( $type === 'page' && $resultPageSet === null ) {
219  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
220  }
221  $rows = array_merge( $rows, iterator_to_array( $res ) );
222  if ( count( $rows ) >= $limit + 1 ) {
223  break;
224  }
225  $first = false;
226  }
227  } else {
228  // Sorting by timestamp
229  // No need to worry about per-type queries because we
230  // aren't sorting or filtering by type anyway
231  $res = $this->select( __METHOD__ );
232  if ( $resultPageSet === null ) {
233  $this->executeGenderCacheFromResultWrapper( $res, __METHOD__ );
234  }
235  $rows = iterator_to_array( $res );
236  }
237 
238  $result = $this->getResult();
239  $count = 0;
240  foreach ( $rows as $row ) {
241  if ( ++$count > $limit ) {
242  // We've reached the one extra which shows that there are
243  // additional pages to be had. Stop here...
244  // @todo Security issue - if the user has no right to view next
245  // title, it will still be shown
246  if ( $params['sort'] == 'timestamp' ) {
247  $this->setContinueEnumParameter( 'continue', "$row->cl_timestamp|$row->cl_from" );
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' ) {
292  $this->setContinueEnumParameter( 'continue', "$row->cl_timestamp|$row->cl_from" );
293  } else {
294  $sortkey = bin2hex( $row->cl_sortkey );
295  $this->setContinueEnumParameter( 'continue',
296  "{$row->cl_type}|$sortkey|{$row->cl_from}"
297  );
298  }
299  break;
300  }
301  } else {
302  $resultPageSet->processDbRow( $row );
303  }
304  }
305 
306  if ( $resultPageSet === null ) {
307  $result->addIndexedTagName(
308  [ 'query', $this->getModuleName() ], 'cm' );
309  }
310  }
311 
312  public function getAllowedParams() {
313  $ret = [
314  'title' => [
315  ApiBase::PARAM_TYPE => 'string',
316  ],
317  'pageid' => [
318  ApiBase::PARAM_TYPE => 'integer'
319  ],
320  'prop' => [
321  ApiBase::PARAM_DFLT => 'ids|title',
322  ApiBase::PARAM_ISMULTI => true,
324  'ids',
325  'title',
326  'sortkey',
327  'sortkeyprefix',
328  'type',
329  'timestamp',
330  ],
332  ],
333  'namespace' => [
334  ApiBase::PARAM_ISMULTI => true,
335  ApiBase::PARAM_TYPE => 'namespace',
336  ],
337  'type' => [
338  ApiBase::PARAM_ISMULTI => true,
339  ApiBase::PARAM_DFLT => 'page|subcat|file',
341  'page',
342  'subcat',
343  'file'
344  ]
345  ],
346  'continue' => [
347  ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
348  ],
349  'limit' => [
350  ApiBase::PARAM_TYPE => 'limit',
351  ApiBase::PARAM_DFLT => 10,
352  ApiBase::PARAM_MIN => 1,
355  ],
356  'sort' => [
357  ApiBase::PARAM_DFLT => 'sortkey',
359  'sortkey',
360  'timestamp'
361  ]
362  ],
363  'dir' => [
364  ApiBase::PARAM_DFLT => 'ascending',
366  'asc',
367  'desc',
368  // Normalising with other modules
369  'ascending',
370  'descending',
371  'newer',
372  'older',
373  ]
374  ],
375  'start' => [
376  ApiBase::PARAM_TYPE => 'timestamp'
377  ],
378  'end' => [
379  ApiBase::PARAM_TYPE => 'timestamp'
380  ],
381  'starthexsortkey' => null,
382  'endhexsortkey' => null,
383  'startsortkeyprefix' => null,
384  'endsortkeyprefix' => null,
385  'startsortkey' => [
387  ],
388  'endsortkey' => [
390  ],
391  ];
392 
393  if ( $this->getConfig()->get( MainConfigNames::MiserMode ) ) {
394  $ret['namespace'][ApiBase::PARAM_HELP_MSG_APPEND] = [
395  'api-help-param-limited-in-miser-mode',
396  ];
397  }
398 
399  return $ret;
400  }
401 
402  protected function getExamplesMessages() {
403  return [
404  'action=query&list=categorymembers&cmtitle=Category:Physics'
405  => 'apihelp-query+categorymembers-example-simple',
406  'action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info'
407  => 'apihelp-query+categorymembers-example-generator',
408  ];
409  }
410 
411  public function getHelpUrls() {
412  return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Categorymembers';
413  }
414 }
const NS_CATEGORY
Definition: Defines.php:78
wfTimestamp( $outputtype=TS_UNIX, $ts=0)
Get a timestamp string in one of various formats.
const PARAM_MAX2
Definition: ApiBase.php:90
const PARAM_DEPRECATED
Definition: ApiBase.php:102
const PARAM_MAX
Definition: ApiBase.php:86
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1446
dieContinueUsageIf( $condition)
Die with the 'badcontinue' error.
Definition: ApiBase.php:1638
const PARAM_TYPE
Definition: ApiBase.php:82
const PARAM_DFLT
Definition: ApiBase.php:74
const PARAM_HELP_MSG_APPEND
((string|array|Message)[]) Specify additional i18n messages to append to the normal message for this ...
Definition: ApiBase.php:170
const PARAM_HELP_MSG_PER_VALUE
((string|array|Message)[]) When PARAM_TYPE is an array, this is an array mapping those values to $msg...
Definition: ApiBase.php:196
const PARAM_MIN
Definition: ApiBase.php:94
const LIMIT_BIG1
Fast query, standard limit.
Definition: ApiBase.php:221
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
const PARAM_HELP_MSG
(string|array|Message) Specify an alternative i18n documentation message for this parameter.
Definition: ApiBase.php:163
const LIMIT_BIG2
Fast query, apihighlimits limit.
Definition: ApiBase.php:223
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
getTitleOrPageId( $params, $load=false)
Get a WikiPage object from a title or pageid param, if possible.
Definition: ApiBase.php:1036
const PARAM_ISMULTI
Definition: ApiBase.php:78
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:40
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.
static makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:637