98a4eace0d62d24b3d4c7e9788e74e9cf5d44a00
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
33 * A query module to enumerate pages that belong to a category.
37 class ApiQueryCategoryMembers
extends ApiQueryGeneratorBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'cm' );
43 public function execute() {
47 public function getCacheMode( $params ) {
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
56 * @param $resultPageSet ApiPageSet
59 private function run( $resultPageSet = null ) {
60 $params = $this->extractRequestParams();
62 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
64 if ( isset( $params['title'] ) ) {
65 $categoryTitle = Title
::newFromText( $params['title'] );
67 if ( is_null( $categoryTitle ) ||
$categoryTitle->getNamespace() != NS_CATEGORY
) {
68 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
70 } elseif( isset( $params['pageid'] ) ) {
71 $categoryTitle = Title
::newFromID( $params['pageid'] );
73 if ( !$categoryTitle ) {
74 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
75 } elseif ( $categoryTitle->getNamespace() != NS_CATEGORY
) {
76 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
80 $prop = array_flip( $params['prop'] );
81 $fld_ids = isset( $prop['ids'] );
82 $fld_title = isset( $prop['title'] );
83 $fld_sortkey = isset( $prop['sortkey'] );
84 $fld_sortkeyprefix = isset( $prop['sortkeyprefix'] );
85 $fld_timestamp = isset( $prop['timestamp'] );
86 $fld_type = isset( $prop['type'] );
88 if ( is_null( $resultPageSet ) ) {
89 $this->addFields( array( 'cl_from', 'cl_sortkey', 'cl_type', 'page_namespace', 'page_title' ) );
90 $this->addFieldsIf( 'page_id', $fld_ids );
91 $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
93 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
94 $this->addFields( array( 'cl_from', 'cl_sortkey', 'cl_type' ) );
97 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp ||
$params['sort'] == 'timestamp' );
99 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
101 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
102 $queryTypes = $params['type'];
105 // Scanning large datasets for rare categories sucks, and I already told
106 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
109 if ( $wgMiserMode ) {
110 $miser_ns = $params['namespace'];
112 $this->addWhereFld( 'page_namespace', $params['namespace'] );
115 $dir = $params['dir'] == 'asc' ?
'newer' : 'older';
117 if ( $params['sort'] == 'timestamp' ) {
118 $this->addWhereRange( 'cl_timestamp',
123 $this->addOption( 'USE INDEX', 'cl_timestamp' );
125 if ( $params['continue'] ) {
126 $cont = explode( '|', $params['continue'], 3 );
127 if ( count( $cont ) != 3 ) {
128 $this->dieUsage( 'Invalid continue param. You should pass the original value returned '.
129 'by the previous query', '_badcontinue'
133 // Remove the types to skip from $queryTypes
134 $contTypeIndex = array_search( $cont[0], $queryTypes );
135 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
137 // Add a WHERE clause for sortkey and from
138 // pack( "H*", $foo ) is used to convert hex back to binary
139 $escSortkey = $this->getDB()->addQuotes( pack( "H*", $cont[1] ) );
140 $from = intval( $cont[2] );
141 $op = $dir == 'newer' ?
'>' : '<';
142 // $contWhere is used further down
143 $contWhere = "cl_sortkey $op $escSortkey OR " .
144 "(cl_sortkey = $escSortkey AND " .
145 "cl_from $op= $from)";
146 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
147 $this->addWhereRange( 'cl_sortkey', $dir, null, null );
148 $this->addWhereRange( 'cl_from', $dir, null, null );
150 $startsortkey = $params['startsortkeyprefix'] !== null ?
151 Collation
::singleton()->getSortkey( $params['startsortkeyprefix'] ) :
152 $params['startsortkey'];
153 $endsortkey = $params['endsortkeyprefix'] !== null ?
154 Collation
::singleton()->getSortkey( $params['endsortkeyprefix'] ) :
155 $params['endsortkey'];
157 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
158 $this->addWhereRange( 'cl_sortkey',
162 $this->addWhereRange( 'cl_from', $dir, null, null );
164 $this->addOption( 'USE INDEX', 'cl_sortkey' );
167 $this->addWhere( 'cl_from=page_id' );
169 $limit = $params['limit'];
170 $this->addOption( 'LIMIT', $limit +
1 );
172 if ( $params['sort'] == 'sortkey' ) {
173 // Run a separate SELECT query for each value of cl_type.
174 // This is needed because cl_type is an enum, and MySQL has
175 // inconsistencies between ORDER BY cl_type and
176 // WHERE cl_type >= 'foo' making proper paging impossible
180 foreach ( $queryTypes as $type ) {
181 $extraConds = array( 'cl_type' => $type );
182 if ( $first && $contWhere ) {
183 // Continuation condition. Only added to the
184 // first query, otherwise we'll skip things
185 $extraConds[] = $contWhere;
187 $res = $this->select( __METHOD__
, array( 'where' => $extraConds ) );
188 $rows = array_merge( $rows, iterator_to_array( $res ) );
189 if ( count( $rows ) >= $limit +
1 ) {
195 // Sorting by timestamp
196 // No need to worry about per-type queries because we
197 // aren't sorting or filtering by type anyway
198 $res = $this->select( __METHOD__
);
199 $rows = iterator_to_array( $res );
202 $result = $this->getResult();
204 foreach ( $rows as $row ) {
205 if ( ++
$count > $limit ) {
206 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
207 // TODO: Security issue - if the user has no right to view next title, it will still be shown
208 if ( $params['sort'] == 'timestamp' ) {
209 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
211 $sortkey = bin2hex( $row->cl_sortkey
);
212 $this->setContinueEnumParameter( 'continue',
213 "{$row->cl_type}|$sortkey|{$row->cl_from}"
219 // Since domas won't tell anyone what he told long ago, apply
220 // cmnamespace here. This means the query may return 0 actual
221 // results, but on the other hand it could save returning 5000
222 // useless results to the client. ~~~~
223 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
227 if ( is_null( $resultPageSet ) ) {
230 $vals['pageid'] = intval( $row->page_id
);
233 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
234 ApiQueryBase
::addTitleInfo( $vals, $title );
236 if ( $fld_sortkey ) {
237 $vals['sortkey'] = bin2hex( $row->cl_sortkey
);
239 if ( $fld_sortkeyprefix ) {
240 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
243 $vals['type'] = $row->cl_type
;
245 if ( $fld_timestamp ) {
246 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
248 $fit = $result->addValue( array( 'query', $this->getModuleName() ),
251 if ( $params['sort'] == 'timestamp' ) {
252 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
254 $sortkey = bin2hex( $row->cl_sortkey
);
255 $this->setContinueEnumParameter( 'continue',
256 "{$row->cl_type}|$sortkey|{$row->cl_from}"
262 $resultPageSet->processDbRow( $row );
266 if ( is_null( $resultPageSet ) ) {
267 $result->setIndexedTagName_internal(
268 array( 'query', $this->getModuleName() ), 'cm' );
272 public function getAllowedParams() {
275 ApiBase
::PARAM_TYPE
=> 'string',
278 ApiBase
::PARAM_TYPE
=> 'integer'
281 ApiBase
::PARAM_DFLT
=> 'ids|title',
282 ApiBase
::PARAM_ISMULTI
=> true,
283 ApiBase
::PARAM_TYPE
=> array (
292 'namespace' => array (
293 ApiBase
::PARAM_ISMULTI
=> true,
294 ApiBase
::PARAM_TYPE
=> 'namespace',
297 ApiBase
::PARAM_ISMULTI
=> true,
298 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
299 ApiBase
::PARAM_TYPE
=> array(
307 ApiBase
::PARAM_TYPE
=> 'limit',
308 ApiBase
::PARAM_DFLT
=> 10,
309 ApiBase
::PARAM_MIN
=> 1,
310 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
311 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
314 ApiBase
::PARAM_DFLT
=> 'sortkey',
315 ApiBase
::PARAM_TYPE
=> array(
321 ApiBase
::PARAM_DFLT
=> 'asc',
322 ApiBase
::PARAM_TYPE
=> array(
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() {
342 $p = $this->getModulePrefix();
344 'title' => "Which category to enumerate (required). Must include 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 category (human-readable part of the sortkey)',
352 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
353 ' timestamp - Adds the timestamp of when the page was included',
355 'namespace' => 'Only include pages in these namespaces',
356 'type' => "What type of category members to include. Ignored when {$p}sort=timestamp is set",
357 'sort' => 'Property to sort by',
358 'dir' => 'In which direction to sort',
359 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
360 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
361 'startsortkey' => "Sortkey to start listing from. Must be given in binary format. Can only be used with {$p}sort=sortkey",
362 'endsortkey' => "Sortkey to end listing at. Must be given in binary format. Can only be used with {$p}sort=sortkey",
363 'startsortkeyprefix' => "Sortkey prefix to start listing from. Can only be used with {$p}sort=sortkey. Overrides {$p}startsortkey",
364 '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",
365 'continue' => 'For large categories, give the value retured from previous query',
366 'limit' => 'The maximum number of pages to return.',
369 if ( $wgMiserMode ) {
370 $desc['namespace'] = array(
372 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
373 'returned before continuing; in extreme cases, zero results may be returned.',
374 "Note that you can use {$p}type=subcat or {$p}type=file instead of {$p}namespace=14 or 6.",
380 public function getDescription() {
381 return 'List all pages in a given category';
384 public function getPossibleErrors() {
385 return array_merge( parent
::getPossibleErrors(),
386 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
388 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
389 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
390 array( 'nosuchpageid', 'pageid' ),
395 public function getExamples() {
397 'Get first 10 pages in [[Category:Physics]]:',
398 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
399 'Get page info about first 10 pages in [[Category:Physics]]:',
400 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
404 public function getHelpUrls() {
405 return 'http://www.mediawiki.org/wiki/API:Categorymembers';
408 public function getVersion() {
409 return __CLASS__
. ': $Id$';