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
28 * A query module to enumerate pages that belong to a category.
32 class ApiQueryCategoryMembers
extends ApiQueryGeneratorBase
{
34 public function __construct( ApiQuery
$query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'cm' );
38 public function execute() {
42 public function getCacheMode( $params ) {
46 public function executeGenerator( $resultPageSet ) {
47 $this->run( $resultPageSet );
51 * @param ApiPageSet $resultPageSet
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 );
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'];
87 // Scanning large datasets for rare categories sucks, and I already told
88 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
90 if ( $this->getConfig()->get( 'MiserMode' ) ) {
91 $miser_ns = $params['namespace'];
93 $this->addWhereFld( 'page_namespace', $params['namespace'] );
96 $dir = in_array( $params['dir'], array( 'asc', 'ascending', 'newer' ) ) ?
'newer' : 'older';
98 if ( $params['sort'] == 'timestamp' ) {
99 $this->addTimestampWhereRange( 'cl_timestamp',
103 // Include in ORDER BY for uniqueness
104 $this->addWhereRange( 'cl_from', $dir, null, null );
106 if ( !is_null( $params['continue'] ) ) {
107 $cont = explode( '|', $params['continue'] );
108 $this->dieContinueUsageIf( count( $cont ) != 2 );
109 $op = ( $dir === 'newer' ?
'>' : '<' );
110 $db = $this->getDB();
111 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
112 $continueFrom = (int)$cont[1];
113 $this->dieContinueUsageIf( $continueFrom != $cont[1] );
114 $this->addWhere( "cl_timestamp $op $continueTimestamp OR " .
115 "(cl_timestamp = $continueTimestamp AND " .
116 "cl_from $op= $continueFrom)"
120 $this->addOption( 'USE INDEX', 'cl_timestamp' );
122 if ( $params['continue'] ) {
123 $cont = explode( '|', $params['continue'], 3 );
124 $this->dieContinueUsageIf( count( $cont ) != 3 );
126 // Remove the types to skip from $queryTypes
127 $contTypeIndex = array_search( $cont[0], $queryTypes );
128 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
130 // Add a WHERE clause for sortkey and from
131 // pack( "H*", $foo ) is used to convert hex back to binary
132 $escSortkey = $this->getDB()->addQuotes( pack( 'H*', $cont[1] ) );
133 $from = intval( $cont[2] );
134 $op = $dir == 'newer' ?
'>' : '<';
135 // $contWhere is used further down
136 $contWhere = "cl_sortkey $op $escSortkey OR " .
137 "(cl_sortkey = $escSortkey AND " .
138 "cl_from $op= $from)";
139 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
140 $this->addWhereRange( 'cl_sortkey', $dir, null, null );
141 $this->addWhereRange( 'cl_from', $dir, null, null );
143 $startsortkey = $params['startsortkeyprefix'] !== null ?
144 Collation
::singleton()->getSortkey( $params['startsortkeyprefix'] ) :
145 $params['startsortkey'];
146 $endsortkey = $params['endsortkeyprefix'] !== null ?
147 Collation
::singleton()->getSortkey( $params['endsortkeyprefix'] ) :
148 $params['endsortkey'];
150 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
151 $this->addWhereRange( 'cl_sortkey',
155 $this->addWhereRange( 'cl_from', $dir, null, null );
157 $this->addOption( 'USE INDEX', 'cl_sortkey' );
160 $this->addWhere( 'cl_from=page_id' );
162 $limit = $params['limit'];
163 $this->addOption( 'LIMIT', $limit +
1 );
165 if ( $params['sort'] == 'sortkey' ) {
166 // Run a separate SELECT query for each value of cl_type.
167 // This is needed because cl_type is an enum, and MySQL has
168 // inconsistencies between ORDER BY cl_type and
169 // WHERE cl_type >= 'foo' making proper paging impossible
173 foreach ( $queryTypes as $type ) {
174 $extraConds = array( 'cl_type' => $type );
175 if ( $first && $contWhere ) {
176 // Continuation condition. Only added to the
177 // first query, otherwise we'll skip things
178 $extraConds[] = $contWhere;
180 $res = $this->select( __METHOD__
, array( 'where' => $extraConds ) );
181 $rows = array_merge( $rows, iterator_to_array( $res ) );
182 if ( count( $rows ) >= $limit +
1 ) {
188 // Sorting by timestamp
189 // No need to worry about per-type queries because we
190 // aren't sorting or filtering by type anyway
191 $res = $this->select( __METHOD__
);
192 $rows = iterator_to_array( $res );
195 $result = $this->getResult();
197 foreach ( $rows as $row ) {
198 if ( ++
$count > $limit ) {
199 // We've reached the one extra which shows that there are
200 // additional pages to be had. Stop here...
201 // @todo Security issue - if the user has no right to view next
202 // title, it will still be shown
203 if ( $params['sort'] == 'timestamp' ) {
204 $this->setContinueEnumParameter( 'continue', "$row->cl_timestamp|$row->cl_from" );
206 $sortkey = bin2hex( $row->cl_sortkey
);
207 $this->setContinueEnumParameter( 'continue',
208 "{$row->cl_type}|$sortkey|{$row->cl_from}"
214 // Since domas won't tell anyone what he told long ago, apply
215 // cmnamespace here. This means the query may return 0 actual
216 // results, but on the other hand it could save returning 5000
217 // useless results to the client. ~~~~
218 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
222 if ( is_null( $resultPageSet ) ) {
225 $vals['pageid'] = intval( $row->page_id
);
228 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
229 ApiQueryBase
::addTitleInfo( $vals, $title );
231 if ( $fld_sortkey ) {
232 $vals['sortkey'] = bin2hex( $row->cl_sortkey
);
234 if ( $fld_sortkeyprefix ) {
235 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
238 $vals['type'] = $row->cl_type
;
240 if ( $fld_timestamp ) {
241 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
243 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
246 if ( $params['sort'] == 'timestamp' ) {
247 $this->setContinueEnumParameter( 'continue', "$row->cl_timestamp|$row->cl_from" );
249 $sortkey = bin2hex( $row->cl_sortkey
);
250 $this->setContinueEnumParameter( 'continue',
251 "{$row->cl_type}|$sortkey|{$row->cl_from}"
257 $resultPageSet->processDbRow( $row );
261 if ( is_null( $resultPageSet ) ) {
262 $result->setIndexedTagName_internal(
263 array( 'query', $this->getModuleName() ), 'cm' );
267 public function getAllowedParams() {
270 ApiBase
::PARAM_TYPE
=> 'string',
273 ApiBase
::PARAM_TYPE
=> 'integer'
276 ApiBase
::PARAM_DFLT
=> 'ids|title',
277 ApiBase
::PARAM_ISMULTI
=> true,
278 ApiBase
::PARAM_TYPE
=> array(
287 'namespace' => array(
288 ApiBase
::PARAM_ISMULTI
=> true,
289 ApiBase
::PARAM_TYPE
=> 'namespace',
292 ApiBase
::PARAM_ISMULTI
=> true,
293 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
294 ApiBase
::PARAM_TYPE
=> array(
302 ApiBase
::PARAM_TYPE
=> 'limit',
303 ApiBase
::PARAM_DFLT
=> 10,
304 ApiBase
::PARAM_MIN
=> 1,
305 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
306 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
309 ApiBase
::PARAM_DFLT
=> 'sortkey',
310 ApiBase
::PARAM_TYPE
=> array(
316 ApiBase
::PARAM_DFLT
=> 'ascending',
317 ApiBase
::PARAM_TYPE
=> array(
320 // Normalising with other modules
328 ApiBase
::PARAM_TYPE
=> 'timestamp'
331 ApiBase
::PARAM_TYPE
=> 'timestamp'
333 'startsortkey' => null,
334 'endsortkey' => null,
335 'startsortkeyprefix' => null,
336 'endsortkeyprefix' => null,
340 public function getParamDescription() {
341 $p = $this->getModulePrefix();
343 'title' => "Which category to enumerate (required). Must include " .
344 "'Category:' prefix. Cannot be used together with {$p}pageid",
345 'pageid' => "Page ID of the category to enumerate. Cannot be used together with {$p}title",
347 'What pieces of information to include',
348 ' ids - Adds the page ID',
349 ' title - Adds the title and namespace ID of the page',
350 ' sortkey - Adds the sortkey used for sorting in the category (hexadecimal string)',
351 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the ' .
352 'category (human-readable part of the sortkey)',
353 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
354 ' timestamp - Adds the timestamp of when the page was included',
356 'namespace' => 'Only include pages in these namespaces',
357 'type' => "What type of category members to include. Ignored when {$p}sort=timestamp is set",
358 'sort' => 'Property to sort by',
359 'dir' => 'In which direction to sort',
360 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
361 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
362 'startsortkey' => "Sortkey to start listing from. Must be given in " .
363 "binary format. Can only be used with {$p}sort=sortkey",
364 'endsortkey' => "Sortkey to end listing at. Must be given in binary " .
365 "format. Can only be used with {$p}sort=sortkey",
366 'startsortkeyprefix' => "Sortkey prefix to start listing from. Can " .
367 "only be used with {$p}sort=sortkey. Overrides {$p}startsortkey",
368 'endsortkeyprefix' => "Sortkey prefix to end listing BEFORE (not at, " .
369 "if this value occurs it will not be included!). Can only be used with " .
370 "{$p}sort=sortkey. Overrides {$p}endsortkey",
371 'continue' => 'For large categories, give the value returned from previous query',
372 'limit' => 'The maximum number of pages to return.',
375 if ( $this->getConfig()->get( 'MiserMode' ) ) {
376 $desc['namespace'] = array(
378 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
379 'returned before continuing; in extreme cases, zero results may be returned.',
380 "Note that you can use {$p}type=subcat or {$p}type=file instead of {$p}namespace=14 or 6.",
387 public function getResultProperties() {
390 'pageid' => 'integer'
397 'sortkey' => 'string'
399 'sortkeyprefix' => array(
400 'sortkeyprefix' => 'string'
404 ApiBase
::PROP_TYPE
=> array(
411 'timestamp' => array(
412 'timestamp' => 'timestamp'
417 public function getDescription() {
418 return 'List all pages in a given category.';
421 public function getPossibleErrors() {
422 return array_merge( parent
::getPossibleErrors(),
423 $this->getTitleOrPageIdErrorMessage(),
425 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
430 public function getExamples() {
432 'api.php?action=query&list=categorymembers&cmtitle=Category:Physics'
433 => 'Get first 10 pages in [[Category:Physics]]',
434 'api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info'
435 => 'Get page info about first 10 pages in [[Category:Physics]]',
439 public function getHelpUrls() {
440 return 'https://www.mediawiki.org/wiki/API:Categorymembers';