Also set the queue types map when rebuilding the ready queue map
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryMembers.php
1 <?php
2 /**
3 *
4 *
5 * Created on June 14, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 /**
28 * A query module to enumerate pages that belong to a category.
29 *
30 * @ingroup API
31 */
32 class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'cm' );
36 }
37
38 public function execute() {
39 $this->run();
40 }
41
42 public function getCacheMode( $params ) {
43 return 'public';
44 }
45
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
48 }
49
50 /**
51 * @param ApiPageSet $resultPageSet
52 * @return void
53 */
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
56
57 $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
58 if ( $categoryTitle->getNamespace() != NS_CATEGORY ) {
59 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
60 }
61
62 $prop = array_flip( $params['prop'] );
63 $fld_ids = isset( $prop['ids'] );
64 $fld_title = isset( $prop['title'] );
65 $fld_sortkey = isset( $prop['sortkey'] );
66 $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
67 $fld_timestamp = isset( $prop['timestamp'] );
68 $fld_type = isset( $prop['type'] );
69
70 if ( is_null( $resultPageSet ) ) {
71 $this->addFields( array( 'cl_from', 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title' ) );
72 $this->addFieldsIf( 'page_id', $fld_ids );
73 $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
74 } else {
75 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
76 $this->addFields( array( 'cl_from', 'cl_sortkey', 'cl_type' ) );
77 }
78
79 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
80
81 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
82
83 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
84 $queryTypes = $params['type'];
85 $contWhere = false;
86
87 // Scanning large datasets for rare categories sucks, and I already told
88 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
89 global $wgMiserMode;
90 $miser_ns = array();
91 if ( $wgMiserMode ) {
92 $miser_ns = $params['namespace'];
93 } else {
94 $this->addWhereFld( 'page_namespace', $params['namespace'] );
95 }
96
97 $dir = in_array( $params['dir'], array( 'asc', 'ascending', 'newer' ) ) ? 'newer' : 'older';
98
99 if ( $params['sort'] == 'timestamp' ) {
100 $this->addTimestampWhereRange( 'cl_timestamp',
101 $dir,
102 $params['start'],
103 $params['end'] );
104 // Include in ORDER BY for uniqueness
105 $this->addWhereRange( 'cl_from', $dir, null, null );
106
107 if ( !is_null( $params['continue'] ) ) {
108 $cont = explode( '|', $params['continue'] );
109 $this->dieContinueUsageIf( count( $cont ) != 2 );
110 $op = ( $dir === 'newer' ? '>' : '<' );
111 $db = $this->getDB();
112 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
113 $continueFrom = (int)$cont[1];
114 $this->dieContinueUsageIf( $continueFrom != $cont[1] );
115 $this->addWhere( "cl_timestamp $op $continueTimestamp OR " .
116 "(cl_timestamp = $continueTimestamp AND " .
117 "cl_from $op= $continueFrom)"
118 );
119 }
120
121 $this->addOption( 'USE INDEX', 'cl_timestamp' );
122 } else {
123 if ( $params['continue'] ) {
124 $cont = explode( '|', $params['continue'], 3 );
125 $this->dieContinueUsageIf( count( $cont ) != 3 );
126
127 // Remove the types to skip from $queryTypes
128 $contTypeIndex = array_search( $cont[0], $queryTypes );
129 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
130
131 // Add a WHERE clause for sortkey and from
132 // pack( "H*", $foo ) is used to convert hex back to binary
133 $escSortkey = $this->getDB()->addQuotes( pack( 'H*', $cont[1] ) );
134 $from = intval( $cont[2] );
135 $op = $dir == 'newer' ? '>' : '<';
136 // $contWhere is used further down
137 $contWhere = "cl_sortkey $op $escSortkey OR " .
138 "(cl_sortkey = $escSortkey AND " .
139 "cl_from $op= $from)";
140 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
141 $this->addWhereRange( 'cl_sortkey', $dir, null, null );
142 $this->addWhereRange( 'cl_from', $dir, null, null );
143 } else {
144 $startsortkey = $params['startsortkeyprefix'] !== null ?
145 Collation::singleton()->getSortkey( $params['startsortkeyprefix'] ) :
146 $params['startsortkey'];
147 $endsortkey = $params['endsortkeyprefix'] !== null ?
148 Collation::singleton()->getSortkey( $params['endsortkeyprefix'] ) :
149 $params['endsortkey'];
150
151 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
152 $this->addWhereRange( 'cl_sortkey',
153 $dir,
154 $startsortkey,
155 $endsortkey );
156 $this->addWhereRange( 'cl_from', $dir, null, null );
157 }
158 $this->addOption( 'USE INDEX', 'cl_sortkey' );
159 }
160
161 $this->addWhere( 'cl_from=page_id' );
162
163 $limit = $params['limit'];
164 $this->addOption( 'LIMIT', $limit + 1 );
165
166 if ( $params['sort'] == 'sortkey' ) {
167 // Run a separate SELECT query for each value of cl_type.
168 // This is needed because cl_type is an enum, and MySQL has
169 // inconsistencies between ORDER BY cl_type and
170 // WHERE cl_type >= 'foo' making proper paging impossible
171 // and unindexed.
172 $rows = array();
173 $first = true;
174 foreach ( $queryTypes as $type ) {
175 $extraConds = array( 'cl_type' => $type );
176 if ( $first && $contWhere ) {
177 // Continuation condition. Only added to the
178 // first query, otherwise we'll skip things
179 $extraConds[] = $contWhere;
180 }
181 $res = $this->select( __METHOD__, array( 'where' => $extraConds ) );
182 $rows = array_merge( $rows, iterator_to_array( $res ) );
183 if ( count( $rows ) >= $limit + 1 ) {
184 break;
185 }
186 $first = false;
187 }
188 } else {
189 // Sorting by timestamp
190 // No need to worry about per-type queries because we
191 // aren't sorting or filtering by type anyway
192 $res = $this->select( __METHOD__ );
193 $rows = iterator_to_array( $res );
194 }
195
196 $result = $this->getResult();
197 $count = 0;
198 foreach ( $rows as $row ) {
199 if ( ++$count > $limit ) {
200 // We've reached the one extra which shows that there are
201 // additional pages to be had. Stop here...
202 // @todo Security issue - if the user has no right to view next
203 // title, it will still be shown
204 if ( $params['sort'] == 'timestamp' ) {
205 $this->setContinueEnumParameter( 'continue', "$row->cl_timestamp|$row->cl_from" );
206 } else {
207 $sortkey = bin2hex( $row->cl_sortkey );
208 $this->setContinueEnumParameter( 'continue',
209 "{$row->cl_type}|$sortkey|{$row->cl_from}"
210 );
211 }
212 break;
213 }
214
215 // Since domas won't tell anyone what he told long ago, apply
216 // cmnamespace here. This means the query may return 0 actual
217 // results, but on the other hand it could save returning 5000
218 // useless results to the client. ~~~~
219 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
220 continue;
221 }
222
223 if ( is_null( $resultPageSet ) ) {
224 $vals = array();
225 if ( $fld_ids ) {
226 $vals['pageid'] = intval( $row->page_id );
227 }
228 if ( $fld_title ) {
229 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
230 ApiQueryBase::addTitleInfo( $vals, $title );
231 }
232 if ( $fld_sortkey ) {
233 $vals['sortkey'] = bin2hex( $row->cl_sortkey );
234 }
235 if ( $fld_sortkeyprefix ) {
236 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
237 }
238 if ( $fld_type ) {
239 $vals['type'] = $row->cl_type;
240 }
241 if ( $fld_timestamp ) {
242 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
243 }
244 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
245 null, $vals );
246 if ( !$fit ) {
247 if ( $params['sort'] == 'timestamp' ) {
248 $this->setContinueEnumParameter( 'continue', "$row->cl_timestamp|$row->cl_from" );
249 } else {
250 $sortkey = bin2hex( $row->cl_sortkey );
251 $this->setContinueEnumParameter( 'continue',
252 "{$row->cl_type}|$sortkey|{$row->cl_from}"
253 );
254 }
255 break;
256 }
257 } else {
258 $resultPageSet->processDbRow( $row );
259 }
260 }
261
262 if ( is_null( $resultPageSet ) ) {
263 $result->setIndexedTagName_internal(
264 array( 'query', $this->getModuleName() ), 'cm' );
265 }
266 }
267
268 public function getAllowedParams() {
269 return array(
270 'title' => array(
271 ApiBase::PARAM_TYPE => 'string',
272 ),
273 'pageid' => array(
274 ApiBase::PARAM_TYPE => 'integer'
275 ),
276 'prop' => array(
277 ApiBase::PARAM_DFLT => 'ids|title',
278 ApiBase::PARAM_ISMULTI => true,
279 ApiBase::PARAM_TYPE => array(
280 'ids',
281 'title',
282 'sortkey',
283 'sortkeyprefix',
284 'type',
285 'timestamp',
286 )
287 ),
288 'namespace' => array(
289 ApiBase::PARAM_ISMULTI => true,
290 ApiBase::PARAM_TYPE => 'namespace',
291 ),
292 'type' => array(
293 ApiBase::PARAM_ISMULTI => true,
294 ApiBase::PARAM_DFLT => 'page|subcat|file',
295 ApiBase::PARAM_TYPE => array(
296 'page',
297 'subcat',
298 'file'
299 )
300 ),
301 'continue' => null,
302 'limit' => array(
303 ApiBase::PARAM_TYPE => 'limit',
304 ApiBase::PARAM_DFLT => 10,
305 ApiBase::PARAM_MIN => 1,
306 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
307 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
308 ),
309 'sort' => array(
310 ApiBase::PARAM_DFLT => 'sortkey',
311 ApiBase::PARAM_TYPE => array(
312 'sortkey',
313 'timestamp'
314 )
315 ),
316 'dir' => array(
317 ApiBase::PARAM_DFLT => 'ascending',
318 ApiBase::PARAM_TYPE => array(
319 'asc',
320 'desc',
321 // Normalising with other modules
322 'ascending',
323 'descending',
324 'newer',
325 'older',
326 )
327 ),
328 'start' => array(
329 ApiBase::PARAM_TYPE => 'timestamp'
330 ),
331 'end' => array(
332 ApiBase::PARAM_TYPE => 'timestamp'
333 ),
334 'startsortkey' => null,
335 'endsortkey' => null,
336 'startsortkeyprefix' => null,
337 'endsortkeyprefix' => null,
338 );
339 }
340
341 public function getParamDescription() {
342 global $wgMiserMode;
343 $p = $this->getModulePrefix();
344 $desc = array(
345 'title' => "Which category to enumerate (required). Must include " .
346 "'Category:' prefix. Cannot be used together with {$p}pageid",
347 'pageid' => "Page ID of the category to enumerate. Cannot be used together with {$p}title",
348 'prop' => array(
349 'What pieces of information to include',
350 ' ids - Adds the page ID',
351 ' title - Adds the title and namespace ID of the page',
352 ' sortkey - Adds the sortkey used for sorting in the category (hexadecimal string)',
353 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the ' .
354 'category (human-readable part of the sortkey)',
355 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
356 ' timestamp - Adds the timestamp of when the page was included',
357 ),
358 'namespace' => 'Only include pages in these namespaces',
359 'type' => "What type of category members to include. Ignored when {$p}sort=timestamp is set",
360 'sort' => 'Property to sort by',
361 'dir' => 'In which direction to sort',
362 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
363 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
364 'startsortkey' => "Sortkey to start listing from. Must be given in " .
365 "binary format. Can only be used with {$p}sort=sortkey",
366 'endsortkey' => "Sortkey to end listing at. Must be given in binary " .
367 "format. Can only be used with {$p}sort=sortkey",
368 'startsortkeyprefix' => "Sortkey prefix to start listing from. Can " .
369 "only be used with {$p}sort=sortkey. Overrides {$p}startsortkey",
370 'endsortkeyprefix' => "Sortkey prefix to end listing BEFORE (not at, " .
371 "if this value occurs it will not be included!). Can only be used with " .
372 "{$p}sort=sortkey. Overrides {$p}endsortkey",
373 'continue' => 'For large categories, give the value returned from previous query',
374 'limit' => 'The maximum number of pages to return.',
375 );
376
377 if ( $wgMiserMode ) {
378 $desc['namespace'] = array(
379 $desc['namespace'],
380 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
381 'returned before continuing; in extreme cases, zero results may be returned.',
382 "Note that you can use {$p}type=subcat or {$p}type=file instead of {$p}namespace=14 or 6.",
383 );
384 }
385
386 return $desc;
387 }
388
389 public function getResultProperties() {
390 return array(
391 'ids' => array(
392 'pageid' => 'integer'
393 ),
394 'title' => array(
395 'ns' => 'namespace',
396 'title' => 'string'
397 ),
398 'sortkey' => array(
399 'sortkey' => 'string'
400 ),
401 'sortkeyprefix' => array(
402 'sortkeyprefix' => 'string'
403 ),
404 'type' => array(
405 'type' => array(
406 ApiBase::PROP_TYPE => array(
407 'page',
408 'subcat',
409 'file'
410 )
411 )
412 ),
413 'timestamp' => array(
414 'timestamp' => 'timestamp'
415 )
416 );
417 }
418
419 public function getDescription() {
420 return 'List all pages in a given category.';
421 }
422
423 public function getPossibleErrors() {
424 return array_merge( parent::getPossibleErrors(),
425 $this->getTitleOrPageIdErrorMessage(),
426 array(
427 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
428 )
429 );
430 }
431
432 public function getExamples() {
433 return array(
434 'api.php?action=query&list=categorymembers&cmtitle=Category:Physics'
435 => 'Get first 10 pages in [[Category:Physics]]',
436 'api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info'
437 => 'Get page info about first 10 pages in [[Category:Physics]]',
438 );
439 }
440
441 public function getHelpUrls() {
442 return 'https://www.mediawiki.org/wiki/API:Categorymembers';
443 }
444 }