Merge "Typo fix"
[mediawiki.git] / includes / api / ApiQueryCategoryMembers.php
blob704d108a0c96cfe53a0a9881c2a3b4ac9032b513
1 <?php
2 /**
5 * Created on June 14, 2007
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * @file
27 /**
28 * A query module to enumerate pages that belong to a category.
30 * @ingroup API
32 class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'cm' );
38 public function execute() {
39 $this->run();
42 public function getCacheMode( $params ) {
43 return 'public';
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
50 /**
51 * @param $resultPageSet ApiPageSet
52 * @return void
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
57 $categoryTitle = $this->getTitleOrPageId( $params )->getTitle();
58 if ( $categoryTitle->getNamespace() != NS_CATEGORY ) {
59 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
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'] );
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' ) );
79 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
81 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
83 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
84 $queryTypes = $params['type'];
85 $contWhere = false;
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'] );
97 $dir = in_array( $params['dir'], array( 'asc', 'ascending', 'newer' ) ) ? 'newer' : 'older';
99 if ( $params['sort'] == 'timestamp' ) {
100 $this->addTimestampWhereRange( 'cl_timestamp',
101 $dir,
102 $params['start'],
103 $params['end'] );
105 $this->addOption( 'USE INDEX', 'cl_timestamp' );
106 } else {
107 if ( $params['continue'] ) {
108 $cont = explode( '|', $params['continue'], 3 );
109 $this->dieContinueUsageIf( count( $cont ) != 3 );
111 // Remove the types to skip from $queryTypes
112 $contTypeIndex = array_search( $cont[0], $queryTypes );
113 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
115 // Add a WHERE clause for sortkey and from
116 // pack( "H*", $foo ) is used to convert hex back to binary
117 $escSortkey = $this->getDB()->addQuotes( pack( 'H*', $cont[1] ) );
118 $from = intval( $cont[2] );
119 $op = $dir == 'newer' ? '>' : '<';
120 // $contWhere is used further down
121 $contWhere = "cl_sortkey $op $escSortkey OR " .
122 "(cl_sortkey = $escSortkey AND " .
123 "cl_from $op= $from)";
124 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
125 $this->addWhereRange( 'cl_sortkey', $dir, null, null );
126 $this->addWhereRange( 'cl_from', $dir, null, null );
127 } else {
128 $startsortkey = $params['startsortkeyprefix'] !== null ?
129 Collation::singleton()->getSortkey( $params['startsortkeyprefix'] ) :
130 $params['startsortkey'];
131 $endsortkey = $params['endsortkeyprefix'] !== null ?
132 Collation::singleton()->getSortkey( $params['endsortkeyprefix'] ) :
133 $params['endsortkey'];
135 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
136 $this->addWhereRange( 'cl_sortkey',
137 $dir,
138 $startsortkey,
139 $endsortkey );
140 $this->addWhereRange( 'cl_from', $dir, null, null );
142 $this->addOption( 'USE INDEX', 'cl_sortkey' );
145 $this->addWhere( 'cl_from=page_id' );
147 $limit = $params['limit'];
148 $this->addOption( 'LIMIT', $limit + 1 );
150 if ( $params['sort'] == 'sortkey' ) {
151 // Run a separate SELECT query for each value of cl_type.
152 // This is needed because cl_type is an enum, and MySQL has
153 // inconsistencies between ORDER BY cl_type and
154 // WHERE cl_type >= 'foo' making proper paging impossible
155 // and unindexed.
156 $rows = array();
157 $first = true;
158 foreach ( $queryTypes as $type ) {
159 $extraConds = array( 'cl_type' => $type );
160 if ( $first && $contWhere ) {
161 // Continuation condition. Only added to the
162 // first query, otherwise we'll skip things
163 $extraConds[] = $contWhere;
165 $res = $this->select( __METHOD__, array( 'where' => $extraConds ) );
166 $rows = array_merge( $rows, iterator_to_array( $res ) );
167 if ( count( $rows ) >= $limit + 1 ) {
168 break;
170 $first = false;
172 } else {
173 // Sorting by timestamp
174 // No need to worry about per-type queries because we
175 // aren't sorting or filtering by type anyway
176 $res = $this->select( __METHOD__ );
177 $rows = iterator_to_array( $res );
180 $result = $this->getResult();
181 $count = 0;
182 foreach ( $rows as $row ) {
183 if ( ++ $count > $limit ) {
184 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
185 // TODO: Security issue - if the user has no right to view next title, it will still be shown
186 if ( $params['sort'] == 'timestamp' ) {
187 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
188 } else {
189 $sortkey = bin2hex( $row->cl_sortkey );
190 $this->setContinueEnumParameter( 'continue',
191 "{$row->cl_type}|$sortkey|{$row->cl_from}"
194 break;
197 // Since domas won't tell anyone what he told long ago, apply
198 // cmnamespace here. This means the query may return 0 actual
199 // results, but on the other hand it could save returning 5000
200 // useless results to the client. ~~~~
201 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
202 continue;
205 if ( is_null( $resultPageSet ) ) {
206 $vals = array();
207 if ( $fld_ids ) {
208 $vals['pageid'] = intval( $row->page_id );
210 if ( $fld_title ) {
211 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
212 ApiQueryBase::addTitleInfo( $vals, $title );
214 if ( $fld_sortkey ) {
215 $vals['sortkey'] = bin2hex( $row->cl_sortkey );
217 if ( $fld_sortkeyprefix ) {
218 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
220 if ( $fld_type ) {
221 $vals['type'] = $row->cl_type;
223 if ( $fld_timestamp ) {
224 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
226 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
227 null, $vals );
228 if ( !$fit ) {
229 if ( $params['sort'] == 'timestamp' ) {
230 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
231 } else {
232 $sortkey = bin2hex( $row->cl_sortkey );
233 $this->setContinueEnumParameter( 'continue',
234 "{$row->cl_type}|$sortkey|{$row->cl_from}"
237 break;
239 } else {
240 $resultPageSet->processDbRow( $row );
244 if ( is_null( $resultPageSet ) ) {
245 $result->setIndexedTagName_internal(
246 array( 'query', $this->getModuleName() ), 'cm' );
250 public function getAllowedParams() {
251 return array(
252 'title' => array(
253 ApiBase::PARAM_TYPE => 'string',
255 'pageid' => array(
256 ApiBase::PARAM_TYPE => 'integer'
258 'prop' => array(
259 ApiBase::PARAM_DFLT => 'ids|title',
260 ApiBase::PARAM_ISMULTI => true,
261 ApiBase::PARAM_TYPE => array(
262 'ids',
263 'title',
264 'sortkey',
265 'sortkeyprefix',
266 'type',
267 'timestamp',
270 'namespace' => array(
271 ApiBase::PARAM_ISMULTI => true,
272 ApiBase::PARAM_TYPE => 'namespace',
274 'type' => array(
275 ApiBase::PARAM_ISMULTI => true,
276 ApiBase::PARAM_DFLT => 'page|subcat|file',
277 ApiBase::PARAM_TYPE => array(
278 'page',
279 'subcat',
280 'file'
283 'continue' => null,
284 'limit' => array(
285 ApiBase::PARAM_TYPE => 'limit',
286 ApiBase::PARAM_DFLT => 10,
287 ApiBase::PARAM_MIN => 1,
288 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
289 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
291 'sort' => array(
292 ApiBase::PARAM_DFLT => 'sortkey',
293 ApiBase::PARAM_TYPE => array(
294 'sortkey',
295 'timestamp'
298 'dir' => array(
299 ApiBase::PARAM_DFLT => 'ascending',
300 ApiBase::PARAM_TYPE => array(
301 'asc',
302 'desc',
303 // Normalising with other modules
304 'ascending',
305 'descending',
306 'newer',
307 'older',
310 'start' => array(
311 ApiBase::PARAM_TYPE => 'timestamp'
313 'end' => array(
314 ApiBase::PARAM_TYPE => 'timestamp'
316 'startsortkey' => null,
317 'endsortkey' => null,
318 'startsortkeyprefix' => null,
319 'endsortkeyprefix' => null,
323 public function getParamDescription() {
324 global $wgMiserMode;
325 $p = $this->getModulePrefix();
326 $desc = array(
327 'title' => "Which category to enumerate (required). Must include Category: prefix. Cannot be used together with {$p}pageid",
328 'pageid' => "Page ID of the category to enumerate. Cannot be used together with {$p}title",
329 'prop' => array(
330 'What pieces of information to include',
331 ' ids - Adds the page ID',
332 ' title - Adds the title and namespace ID of the page',
333 ' sortkey - Adds the sortkey used for sorting in the category (hexadecimal string)',
334 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
335 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
336 ' timestamp - Adds the timestamp of when the page was included',
338 'namespace' => 'Only include pages in these namespaces',
339 'type' => "What type of category members to include. Ignored when {$p}sort=timestamp is set",
340 'sort' => 'Property to sort by',
341 'dir' => 'In which direction to sort',
342 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
343 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
344 'startsortkey' => "Sortkey to start listing from. Must be given in binary format. Can only be used with {$p}sort=sortkey",
345 'endsortkey' => "Sortkey to end listing at. Must be given in binary format. Can only be used with {$p}sort=sortkey",
346 'startsortkeyprefix' => "Sortkey prefix to start listing from. Can only be used with {$p}sort=sortkey. Overrides {$p}startsortkey",
347 '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",
348 'continue' => 'For large categories, give the value returned from previous query',
349 'limit' => 'The maximum number of pages to return.',
352 if ( $wgMiserMode ) {
353 $desc['namespace'] = array(
354 $desc['namespace'],
355 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
356 'returned before continuing; in extreme cases, zero results may be returned.',
357 "Note that you can use {$p}type=subcat or {$p}type=file instead of {$p}namespace=14 or 6.",
360 return $desc;
363 public function getResultProperties() {
364 return array(
365 'ids' => array(
366 'pageid' => 'integer'
368 'title' => array(
369 'ns' => 'namespace',
370 'title' => 'string'
372 'sortkey' => array(
373 'sortkey' => 'string'
375 'sortkeyprefix' => array(
376 'sortkeyprefix' => 'string'
378 'type' => array(
379 'type' => array(
380 ApiBase::PROP_TYPE => array(
381 'page',
382 'subcat',
383 'file'
387 'timestamp' => array(
388 'timestamp' => 'timestamp'
393 public function getDescription() {
394 return 'List all pages in a given category';
397 public function getPossibleErrors() {
398 return array_merge( parent::getPossibleErrors(),
399 $this->getTitleOrPageIdErrorMessage(),
400 array(
401 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
406 public function getExamples() {
407 return array(
408 'api.php?action=query&list=categorymembers&cmtitle=Category:Physics' => 'Get first 10 pages in [[Category:Physics]]',
409 'api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info' => 'Get page info about first 10 pages in [[Category:Physics]]',
413 public function getHelpUrls() {
414 return 'https://www.mediawiki.org/wiki/API:Categorymembers';