Merge "Reduced some master queries by adding flags to Revision functions."
[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( $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 $resultPageSet ApiPageSet
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
105 $this->addOption( 'USE INDEX', 'cl_timestamp' );
106 } else {
107 if ( $params['continue'] ) {
108 $cont = explode( '|', $params['continue'], 3 );
109 if ( count( $cont ) != 3 ) {
110 $this->dieUsage( 'Invalid continue param. You should pass the original value returned '.
111 'by the previous query', '_badcontinue'
112 );
113 }
114
115 // Remove the types to skip from $queryTypes
116 $contTypeIndex = array_search( $cont[0], $queryTypes );
117 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
118
119 // Add a WHERE clause for sortkey and from
120 // pack( "H*", $foo ) is used to convert hex back to binary
121 $escSortkey = $this->getDB()->addQuotes( pack( "H*", $cont[1] ) );
122 $from = intval( $cont[2] );
123 $op = $dir == 'newer' ? '>' : '<';
124 // $contWhere is used further down
125 $contWhere = "cl_sortkey $op $escSortkey OR " .
126 "(cl_sortkey = $escSortkey AND " .
127 "cl_from $op= $from)";
128 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
129 $this->addWhereRange( 'cl_sortkey', $dir, null, null );
130 $this->addWhereRange( 'cl_from', $dir, null, null );
131 } else {
132 $startsortkey = $params['startsortkeyprefix'] !== null ?
133 Collation::singleton()->getSortkey( $params['startsortkeyprefix'] ) :
134 $params['startsortkey'];
135 $endsortkey = $params['endsortkeyprefix'] !== null ?
136 Collation::singleton()->getSortkey( $params['endsortkeyprefix'] ) :
137 $params['endsortkey'];
138
139 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
140 $this->addWhereRange( 'cl_sortkey',
141 $dir,
142 $startsortkey,
143 $endsortkey );
144 $this->addWhereRange( 'cl_from', $dir, null, null );
145 }
146 $this->addOption( 'USE INDEX', 'cl_sortkey' );
147 }
148
149 $this->addWhere( 'cl_from=page_id' );
150
151 $limit = $params['limit'];
152 $this->addOption( 'LIMIT', $limit + 1 );
153
154 if ( $params['sort'] == 'sortkey' ) {
155 // Run a separate SELECT query for each value of cl_type.
156 // This is needed because cl_type is an enum, and MySQL has
157 // inconsistencies between ORDER BY cl_type and
158 // WHERE cl_type >= 'foo' making proper paging impossible
159 // and unindexed.
160 $rows = array();
161 $first = true;
162 foreach ( $queryTypes as $type ) {
163 $extraConds = array( 'cl_type' => $type );
164 if ( $first && $contWhere ) {
165 // Continuation condition. Only added to the
166 // first query, otherwise we'll skip things
167 $extraConds[] = $contWhere;
168 }
169 $res = $this->select( __METHOD__, array( 'where' => $extraConds ) );
170 $rows = array_merge( $rows, iterator_to_array( $res ) );
171 if ( count( $rows ) >= $limit + 1 ) {
172 break;
173 }
174 $first = false;
175 }
176 } else {
177 // Sorting by timestamp
178 // No need to worry about per-type queries because we
179 // aren't sorting or filtering by type anyway
180 $res = $this->select( __METHOD__ );
181 $rows = iterator_to_array( $res );
182 }
183
184 $result = $this->getResult();
185 $count = 0;
186 foreach ( $rows as $row ) {
187 if ( ++ $count > $limit ) {
188 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
189 // TODO: Security issue - if the user has no right to view next title, it will still be shown
190 if ( $params['sort'] == 'timestamp' ) {
191 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
192 } else {
193 $sortkey = bin2hex( $row->cl_sortkey );
194 $this->setContinueEnumParameter( 'continue',
195 "{$row->cl_type}|$sortkey|{$row->cl_from}"
196 );
197 }
198 break;
199 }
200
201 // Since domas won't tell anyone what he told long ago, apply
202 // cmnamespace here. This means the query may return 0 actual
203 // results, but on the other hand it could save returning 5000
204 // useless results to the client. ~~~~
205 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
206 continue;
207 }
208
209 if ( is_null( $resultPageSet ) ) {
210 $vals = array();
211 if ( $fld_ids ) {
212 $vals['pageid'] = intval( $row->page_id );
213 }
214 if ( $fld_title ) {
215 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
216 ApiQueryBase::addTitleInfo( $vals, $title );
217 }
218 if ( $fld_sortkey ) {
219 $vals['sortkey'] = bin2hex( $row->cl_sortkey );
220 }
221 if ( $fld_sortkeyprefix ) {
222 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
223 }
224 if ( $fld_type ) {
225 $vals['type'] = $row->cl_type;
226 }
227 if ( $fld_timestamp ) {
228 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
229 }
230 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
231 null, $vals );
232 if ( !$fit ) {
233 if ( $params['sort'] == 'timestamp' ) {
234 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
235 } else {
236 $sortkey = bin2hex( $row->cl_sortkey );
237 $this->setContinueEnumParameter( 'continue',
238 "{$row->cl_type}|$sortkey|{$row->cl_from}"
239 );
240 }
241 break;
242 }
243 } else {
244 $resultPageSet->processDbRow( $row );
245 }
246 }
247
248 if ( is_null( $resultPageSet ) ) {
249 $result->setIndexedTagName_internal(
250 array( 'query', $this->getModuleName() ), 'cm' );
251 }
252 }
253
254 public function getAllowedParams() {
255 return array(
256 'title' => array(
257 ApiBase::PARAM_TYPE => 'string',
258 ),
259 'pageid' => array(
260 ApiBase::PARAM_TYPE => 'integer'
261 ),
262 'prop' => array(
263 ApiBase::PARAM_DFLT => 'ids|title',
264 ApiBase::PARAM_ISMULTI => true,
265 ApiBase::PARAM_TYPE => array (
266 'ids',
267 'title',
268 'sortkey',
269 'sortkeyprefix',
270 'type',
271 'timestamp',
272 )
273 ),
274 'namespace' => array (
275 ApiBase::PARAM_ISMULTI => true,
276 ApiBase::PARAM_TYPE => 'namespace',
277 ),
278 'type' => array(
279 ApiBase::PARAM_ISMULTI => true,
280 ApiBase::PARAM_DFLT => 'page|subcat|file',
281 ApiBase::PARAM_TYPE => array(
282 'page',
283 'subcat',
284 'file'
285 )
286 ),
287 'continue' => null,
288 'limit' => array(
289 ApiBase::PARAM_TYPE => 'limit',
290 ApiBase::PARAM_DFLT => 10,
291 ApiBase::PARAM_MIN => 1,
292 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
293 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
294 ),
295 'sort' => array(
296 ApiBase::PARAM_DFLT => 'sortkey',
297 ApiBase::PARAM_TYPE => array(
298 'sortkey',
299 'timestamp'
300 )
301 ),
302 'dir' => array(
303 ApiBase::PARAM_DFLT => 'ascending',
304 ApiBase::PARAM_TYPE => array(
305 'asc',
306 'desc',
307 // Normalising with other modules
308 'ascending',
309 'descending',
310 'newer',
311 'older',
312 )
313 ),
314 'start' => array(
315 ApiBase::PARAM_TYPE => 'timestamp'
316 ),
317 'end' => array(
318 ApiBase::PARAM_TYPE => 'timestamp'
319 ),
320 'startsortkey' => null,
321 'endsortkey' => null,
322 'startsortkeyprefix' => null,
323 'endsortkeyprefix' => null,
324 );
325 }
326
327 public function getParamDescription() {
328 global $wgMiserMode;
329 $p = $this->getModulePrefix();
330 $desc = array(
331 'title' => "Which category to enumerate (required). Must include Category: prefix. Cannot be used together with {$p}pageid",
332 'pageid' => "Page ID of the category to enumerate. Cannot be used together with {$p}title",
333 'prop' => array(
334 'What pieces of information to include',
335 ' ids - Adds the page ID',
336 ' title - Adds the title and namespace ID of the page',
337 ' sortkey - Adds the sortkey used for sorting in the category (hexadecimal string)',
338 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
339 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
340 ' timestamp - Adds the timestamp of when the page was included',
341 ),
342 'namespace' => 'Only include pages in these namespaces',
343 'type' => "What type of category members to include. Ignored when {$p}sort=timestamp is set",
344 'sort' => 'Property to sort by',
345 'dir' => 'In which direction to sort',
346 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
347 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
348 'startsortkey' => "Sortkey to start listing from. Must be given in binary format. Can only be used with {$p}sort=sortkey",
349 'endsortkey' => "Sortkey to end listing at. Must be given in binary format. Can only be used with {$p}sort=sortkey",
350 'startsortkeyprefix' => "Sortkey prefix to start listing from. Can only be used with {$p}sort=sortkey. Overrides {$p}startsortkey",
351 'endsortkeyprefix' => "Sortkey prefix to end listing BEFORE (not at, if this value occurs it will not be included!). Can only be used with {$p}sort=sortkey. Overrides {$p}endsortkey",
352 'continue' => 'For large categories, give the value returned from previous query',
353 'limit' => 'The maximum number of pages to return.',
354 );
355
356 if ( $wgMiserMode ) {
357 $desc['namespace'] = array(
358 $desc['namespace'],
359 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
360 'returned before continuing; in extreme cases, zero results may be returned.',
361 "Note that you can use {$p}type=subcat or {$p}type=file instead of {$p}namespace=14 or 6.",
362 );
363 }
364 return $desc;
365 }
366
367 public function getResultProperties() {
368 return array(
369 'ids' => array(
370 'pageid' => 'integer'
371 ),
372 'title' => array(
373 'ns' => 'namespace',
374 'title' => 'string'
375 ),
376 'sortkey' => array(
377 'sortkey' => 'string'
378 ),
379 'sortkeyprefix' => array(
380 'sortkeyprefix' => 'string'
381 ),
382 'type' => array(
383 'type' => array(
384 ApiBase::PROP_TYPE => array(
385 'page',
386 'subcat',
387 'file'
388 )
389 )
390 ),
391 'timestamp' => array(
392 'timestamp' => 'timestamp'
393 )
394 );
395 }
396
397 public function getDescription() {
398 return 'List all pages in a given category';
399 }
400
401 public function getPossibleErrors() {
402 return array_merge( parent::getPossibleErrors(),
403 $this->getTitleOrPageIdErrorMessage(),
404 array(
405 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
406 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
407 )
408 );
409 }
410
411 public function getExamples() {
412 return array(
413 'api.php?action=query&list=categorymembers&cmtitle=Category:Physics' => 'Get first 10 pages in [[Category:Physics]]',
414 'api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info' => 'Get page info about first 10 pages in [[Category:Physics]]',
415 );
416 }
417
418 public function getHelpUrls() {
419 return 'https://www.mediawiki.org/wiki/API:Categorymembers';
420 }
421
422 public function getVersion() {
423 return __CLASS__ . ': $Id$';
424 }
425 }