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 $this->addWhereFld( 'cl_type', $params['type'] );
104 // Scanning large datasets for rare categories sucks, and I already told
105 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
108 if ( $wgMiserMode ) {
109 $miser_ns = $params['namespace'];
111 $this->addWhereFld( 'page_namespace', $params['namespace'] );
114 $dir = $params['dir'] == 'asc' ?
'newer' : 'older';
116 if ( $params['sort'] == 'timestamp' ) {
117 $this->addWhereRange( 'cl_timestamp',
122 $this->addOption( 'USE INDEX', 'cl_timestamp' );
124 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'
132 $escType = $this->getDB()->addQuotes( $cont[0] );
133 $from = intval( $cont[1] );
134 $escSortkey = $this->getDB()->addQuotes( $cont[2] );
135 $op = $dir == 'newer' ?
'>' : '<';
136 $this->addWhere( "cl_type $op $escType OR " .
137 "(cl_type = $escType AND " .
138 "(cl_sortkey $op $escSortkey OR " .
139 "(cl_sortkey = $escSortkey AND " .
140 "cl_from $op= $from)))"
144 // The below produces ORDER BY cl_type, cl_sortkey, cl_from, possibly with DESC added to each of them
145 $this->addWhereRange( 'cl_type', $dir, null, null );
146 $this->addWhereRange( 'cl_sortkey',
148 $params['startsortkey'],
149 $params['endsortkey'] );
150 $this->addWhereRange( 'cl_from', $dir, null, null );
152 $this->addOption( 'USE INDEX', 'cl_sortkey' );
155 $this->addWhere( 'cl_from=page_id' );
157 $limit = $params['limit'];
158 $this->addOption( 'LIMIT', $limit +
1 );
161 $res = $this->select( __METHOD__
);
162 foreach ( $res as $row ) {
163 if ( ++
$count > $limit ) {
164 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
165 // TODO: Security issue - if the user has no right to view next title, it will still be shown
166 if ( $params['sort'] == 'timestamp' ) {
167 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
169 // Continue format is type|from|sortkey
170 // The order is a bit weird but it's convenient to put the sortkey at the end
171 // because we don't have to worry about pipes in the sortkey that way
172 // (and type and from can't contain pipes anyway)
173 $this->setContinueEnumParameter( 'continue',
174 "{$row->cl_type}|{$row->cl_from}|{$row->cl_sortkey}"
180 // Since domas won't tell anyone what he told long ago, apply
181 // cmnamespace here. This means the query may return 0 actual
182 // results, but on the other hand it could save returning 5000
183 // useless results to the client. ~~~~
184 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
188 if ( is_null( $resultPageSet ) ) {
191 $vals['pageid'] = intval( $row->page_id
);
194 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
195 ApiQueryBase
::addTitleInfo( $vals, $title );
197 if ( $fld_sortkey ) {
198 $vals['sortkey'] = $row->cl_sortkey
;
200 if ( $fld_sortkeyprefix ) {
201 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
204 $vals['type'] = $row->cl_type
;
206 if ( $fld_timestamp ) {
207 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
209 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
212 if ( $params['sort'] == 'timestamp' ) {
213 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
215 $this->setContinueEnumParameter( 'continue',
216 "{$row->cl_type}|{$row->cl_from}|{$row->cl_sortkey}"
222 $resultPageSet->processDbRow( $row );
226 if ( is_null( $resultPageSet ) ) {
227 $this->getResult()->setIndexedTagName_internal(
228 array( 'query', $this->getModuleName() ), 'cm' );
232 public function getAllowedParams() {
235 ApiBase
::PARAM_TYPE
=> 'string',
238 ApiBase
::PARAM_TYPE
=> 'integer'
241 ApiBase
::PARAM_DFLT
=> 'ids|title',
242 ApiBase
::PARAM_ISMULTI
=> true,
243 ApiBase
::PARAM_TYPE
=> array (
252 'namespace' => array (
253 ApiBase
::PARAM_ISMULTI
=> true,
254 ApiBase
::PARAM_TYPE
=> 'namespace',
257 ApiBase
::PARAM_ISMULTI
=> true,
258 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
259 ApiBase
::PARAM_TYPE
=> array(
267 ApiBase
::PARAM_TYPE
=> 'limit',
268 ApiBase
::PARAM_DFLT
=> 10,
269 ApiBase
::PARAM_MIN
=> 1,
270 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
271 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
274 ApiBase
::PARAM_DFLT
=> 'sortkey',
275 ApiBase
::PARAM_TYPE
=> array(
281 ApiBase
::PARAM_DFLT
=> 'asc',
282 ApiBase
::PARAM_TYPE
=> array(
288 ApiBase
::PARAM_TYPE
=> 'timestamp'
291 ApiBase
::PARAM_TYPE
=> 'timestamp'
293 'startsortkey' => null,
294 'endsortkey' => null,
298 public function getParamDescription() {
300 $p = $this->getModulePrefix();
302 'title' => 'Which category to enumerate (required). Must include Category: prefix. Cannot be used together with cmpageid',
303 'pageid' => 'Page ID of the category to enumerate. Cannot be used together with cmtitle',
305 'What pieces of information to include',
306 ' ids - Adds the page ID',
307 ' title - Adds the title and namespace ID of the page',
308 ' sortkey - Adds the sortkey used for sorting in the category (may not be human-readble)',
309 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
310 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
311 ' timestamp - Adds the timestamp of when the page was included',
313 'namespace' => 'Only include pages in these namespaces',
314 'type' => 'What type of category members to include',
315 'sort' => 'Property to sort by',
316 'dir' => 'In which direction to sort',
317 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
318 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
319 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
320 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
321 'continue' => 'For large categories, give the value retured from previous query',
322 'limit' => 'The maximum number of pages to return.',
325 if ( $wgMiserMode ) {
326 $desc['namespace'] = array(
328 'NOTE: Due to $wgMiserMode, using this may result in fewer than "limit" results',
329 'returned before continuing; in extreme cases, zero results may be returned.',
330 'Note that you can use cmtype=subcat or cmtype=file instead of cmnamespace=14 or 6.',
336 public function getDescription() {
337 return 'List all pages in a given category';
340 public function getPossibleErrors() {
341 return array_merge( parent
::getPossibleErrors(),
342 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
344 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
345 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
346 array( 'nosuchpageid', 'pageid' ),
351 protected function getExamples() {
353 'Get first 10 pages in [[Category:Physics]]:',
354 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
355 'Get page info about first 10 pages in [[Category:Physics]]:',
356 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
360 public function getVersion() {
361 return __CLASS__
. ': $Id$';