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 $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 );
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',
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
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 ) {
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();
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
) );
189 $sortkey = bin2hex( $row->cl_sortkey
);
190 $this->setContinueEnumParameter( 'continue',
191 "{$row->cl_type}|$sortkey|{$row->cl_from}"
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 ) ) {
205 if ( is_null( $resultPageSet ) ) {
208 $vals['pageid'] = intval( $row->page_id
);
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
;
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() ),
229 if ( $params['sort'] == 'timestamp' ) {
230 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
232 $sortkey = bin2hex( $row->cl_sortkey
);
233 $this->setContinueEnumParameter( 'continue',
234 "{$row->cl_type}|$sortkey|{$row->cl_from}"
240 $resultPageSet->processDbRow( $row );
244 if ( is_null( $resultPageSet ) ) {
245 $result->setIndexedTagName_internal(
246 array( 'query', $this->getModuleName() ), 'cm' );
250 public function getAllowedParams() {
253 ApiBase
::PARAM_TYPE
=> 'string',
256 ApiBase
::PARAM_TYPE
=> 'integer'
259 ApiBase
::PARAM_DFLT
=> 'ids|title',
260 ApiBase
::PARAM_ISMULTI
=> true,
261 ApiBase
::PARAM_TYPE
=> array(
270 'namespace' => array(
271 ApiBase
::PARAM_ISMULTI
=> true,
272 ApiBase
::PARAM_TYPE
=> 'namespace',
275 ApiBase
::PARAM_ISMULTI
=> true,
276 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
277 ApiBase
::PARAM_TYPE
=> 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
292 ApiBase
::PARAM_DFLT
=> 'sortkey',
293 ApiBase
::PARAM_TYPE
=> array(
299 ApiBase
::PARAM_DFLT
=> 'ascending',
300 ApiBase
::PARAM_TYPE
=> array(
303 // Normalising with other modules
311 ApiBase
::PARAM_TYPE
=> 'timestamp'
314 ApiBase
::PARAM_TYPE
=> 'timestamp'
316 'startsortkey' => null,
317 'endsortkey' => null,
318 'startsortkeyprefix' => null,
319 'endsortkeyprefix' => null,
323 public function getParamDescription() {
325 $p = $this->getModulePrefix();
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",
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(
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.",
363 public function getResultProperties() {
366 'pageid' => 'integer'
373 'sortkey' => 'string'
375 'sortkeyprefix' => array(
376 'sortkeyprefix' => 'string'
380 ApiBase
::PROP_TYPE
=> array(
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(),
401 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
406 public function getExamples() {
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';