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( $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 $resultPageSet ApiPageSet
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)
92 $miser_ns = $params['namespace'];
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',
105 $this->addOption( 'USE INDEX', 'cl_timestamp' );
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'
115 // Remove the types to skip from $queryTypes
116 $contTypeIndex = array_search( $cont[0], $queryTypes );
117 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
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 );
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'];
139 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
140 $this->addWhereRange( 'cl_sortkey',
144 $this->addWhereRange( 'cl_from', $dir, null, null );
146 $this->addOption( 'USE INDEX', 'cl_sortkey' );
149 $this->addWhere( 'cl_from=page_id' );
151 $limit = $params['limit'];
152 $this->addOption( 'LIMIT', $limit +
1 );
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
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;
169 $res = $this->select( __METHOD__
, array( 'where' => $extraConds ) );
170 $rows = array_merge( $rows, iterator_to_array( $res ) );
171 if ( count( $rows ) >= $limit +
1 ) {
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 );
184 $result = $this->getResult();
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
) );
193 $sortkey = bin2hex( $row->cl_sortkey
);
194 $this->setContinueEnumParameter( 'continue',
195 "{$row->cl_type}|$sortkey|{$row->cl_from}"
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 ) ) {
209 if ( is_null( $resultPageSet ) ) {
212 $vals['pageid'] = intval( $row->page_id
);
215 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
216 ApiQueryBase
::addTitleInfo( $vals, $title );
218 if ( $fld_sortkey ) {
219 $vals['sortkey'] = bin2hex( $row->cl_sortkey
);
221 if ( $fld_sortkeyprefix ) {
222 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
225 $vals['type'] = $row->cl_type
;
227 if ( $fld_timestamp ) {
228 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
230 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
233 if ( $params['sort'] == 'timestamp' ) {
234 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
236 $sortkey = bin2hex( $row->cl_sortkey
);
237 $this->setContinueEnumParameter( 'continue',
238 "{$row->cl_type}|$sortkey|{$row->cl_from}"
244 $resultPageSet->processDbRow( $row );
248 if ( is_null( $resultPageSet ) ) {
249 $result->setIndexedTagName_internal(
250 array( 'query', $this->getModuleName() ), 'cm' );
254 public function getAllowedParams() {
257 ApiBase
::PARAM_TYPE
=> 'string',
260 ApiBase
::PARAM_TYPE
=> 'integer'
263 ApiBase
::PARAM_DFLT
=> 'ids|title',
264 ApiBase
::PARAM_ISMULTI
=> true,
265 ApiBase
::PARAM_TYPE
=> array (
274 'namespace' => array (
275 ApiBase
::PARAM_ISMULTI
=> true,
276 ApiBase
::PARAM_TYPE
=> 'namespace',
279 ApiBase
::PARAM_ISMULTI
=> true,
280 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
281 ApiBase
::PARAM_TYPE
=> 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
296 ApiBase
::PARAM_DFLT
=> 'sortkey',
297 ApiBase
::PARAM_TYPE
=> array(
303 ApiBase
::PARAM_DFLT
=> 'ascending',
304 ApiBase
::PARAM_TYPE
=> array(
307 // Normalising with other modules
315 ApiBase
::PARAM_TYPE
=> 'timestamp'
318 ApiBase
::PARAM_TYPE
=> 'timestamp'
320 'startsortkey' => null,
321 'endsortkey' => null,
322 'startsortkeyprefix' => null,
323 'endsortkeyprefix' => null,
327 public function getParamDescription() {
329 $p = $this->getModulePrefix();
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",
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',
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.',
356 if ( $wgMiserMode ) {
357 $desc['namespace'] = array(
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.",
367 public function getResultProperties() {
370 'pageid' => 'integer'
377 'sortkey' => 'string'
379 'sortkeyprefix' => array(
380 'sortkeyprefix' => 'string'
384 ApiBase
::PROP_TYPE
=> array(
391 'timestamp' => array(
392 'timestamp' => 'timestamp'
397 public function getDescription() {
398 return 'List all pages in a given category';
401 public function getPossibleErrors() {
402 return array_merge( parent
::getPossibleErrors(),
403 $this->getTitleOrPageIdErrorMessage(),
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' ),
411 public function getExamples() {
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]]',
418 public function getHelpUrls() {
419 return 'https://www.mediawiki.org/wiki/API:Categorymembers';
422 public function getVersion() {
423 return __CLASS__
. ': $Id$';