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', 'page_namespace', 'page_title' ) );
90 $this->addFieldsIf( 'page_id', $fld_ids );
91 $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
92 $this->addFieldsIf( 'cl_sortkey', $fld_sortkey );
94 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
95 $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
98 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp ||
$params['sort'] == 'timestamp' );
99 $this->addFieldsIf( 'cl_type', $fld_type );
101 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
103 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
104 $this->addWhereFld( 'cl_type', $params['type'] );
106 // Scanning large datasets for rare categories sucks, and I already told
107 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
110 if ( $wgMiserMode ) {
111 $miser_ns = $params['namespace'];
113 $this->addWhereFld( 'page_namespace', $params['namespace'] );
116 $dir = $params['dir'] == 'asc' ?
'newer' : 'older';
118 if ( $params['sort'] == 'timestamp' ) {
119 $this->addWhereRange( 'cl_timestamp',
124 $this->addOption( 'USE INDEX', 'cl_timestamp' );
126 // The below produces ORDER BY cl_type, cl_sortkey, cl_from, possibly with DESC added to each of them
127 $this->addWhereRange( 'cl_type', $dir, null, null );
128 $this->addWhereRange( 'cl_sortkey',
130 $params['startsortkey'],
131 $params['endsortkey'] );
132 $this->addWhereRange( 'cl_from', $dir, null, null );
133 $this->addOption( 'USE INDEX', 'cl_sortkey' );
136 $this->setContinuation( $params['continue'], $params['dir'] );
138 $this->addWhere( 'cl_from=page_id' );
140 $limit = $params['limit'];
141 $this->addOption( 'LIMIT', $limit +
1 );
144 $res = $this->select( __METHOD__
);
145 foreach ( $res as $row ) {
146 if ( ++
$count > $limit ) {
147 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
148 // TODO: Security issue - if the user has no right to view next title, it will still be shown
149 if ( $params['sort'] == 'timestamp' ) {
150 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
152 $this->setContinueEnumParameter( 'continue', $row->cl_from
);
157 // Since domas won't tell anyone what he told long ago, apply
158 // cmnamespace here. This means the query may return 0 actual
159 // results, but on the other hand it could save returning 5000
160 // useless results to the client. ~~~~
161 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
165 if ( is_null( $resultPageSet ) ) {
168 $vals['pageid'] = intval( $row->page_id
);
171 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
172 ApiQueryBase
::addTitleInfo( $vals, $title );
174 if ( $fld_sortkey ) {
175 $vals['sortkey'] = $row->cl_sortkey
;
177 if ( $fld_sortkeyprefix ) {
178 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix
;
181 $vals['type'] = $row->cl_type
;
183 if ( $fld_timestamp ) {
184 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
186 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
189 if ( $params['sort'] == 'timestamp' ) {
190 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
192 $this->setContinueEnumParameter( 'continue', $row->cl_from
);
197 $resultPageSet->processDbRow( $row );
201 if ( is_null( $resultPageSet ) ) {
202 $this->getResult()->setIndexedTagName_internal(
203 array( 'query', $this->getModuleName() ), 'cm' );
208 * Add DB WHERE clause to continue previous query based on 'continue' parameter
210 private function setContinuation( $continue, $dir ) {
211 if ( is_null( $continue ) ) {
212 return; // This is not a continuation request
215 $encFrom = $this->getDB()->addQuotes( intval( $continue ) );
217 $op = ( $dir == 'desc' ?
'<=' : '>=' );
219 $this->addWhere( "cl_from $op $encFrom" );
222 public function getAllowedParams() {
225 ApiBase
::PARAM_TYPE
=> 'string',
228 ApiBase
::PARAM_TYPE
=> 'integer'
231 ApiBase
::PARAM_DFLT
=> 'ids|title',
232 ApiBase
::PARAM_ISMULTI
=> true,
233 ApiBase
::PARAM_TYPE
=> array (
242 'namespace' => array (
243 ApiBase
::PARAM_ISMULTI
=> true,
244 ApiBase
::PARAM_TYPE
=> 'namespace',
247 ApiBase
::PARAM_ISMULTI
=> true,
248 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
249 ApiBase
::PARAM_TYPE
=> array(
257 ApiBase
::PARAM_TYPE
=> 'limit',
258 ApiBase
::PARAM_DFLT
=> 10,
259 ApiBase
::PARAM_MIN
=> 1,
260 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
261 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
264 ApiBase
::PARAM_DFLT
=> 'sortkey',
265 ApiBase
::PARAM_TYPE
=> array(
271 ApiBase
::PARAM_DFLT
=> 'asc',
272 ApiBase
::PARAM_TYPE
=> array(
278 ApiBase
::PARAM_TYPE
=> 'timestamp'
281 ApiBase
::PARAM_TYPE
=> 'timestamp'
283 'startsortkey' => null,
284 'endsortkey' => null,
288 public function getParamDescription() {
290 $p = $this->getModulePrefix();
292 'title' => 'Which category to enumerate (required). Must include Category: prefix. Cannot be used together with cmpageid',
293 'pageid' => 'Page ID of the category to enumerate. Cannot be used together with cmtitle',
295 'What pieces of information to include',
296 ' ids - Adds the page ID',
297 ' title - Adds the title and namespace ID of the page',
298 ' sortkey - Adds the sortkey used for sorting in the category (may not be human-readble)',
299 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
300 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
301 ' timestamp - Adds the timestamp of when the page was included',
303 'namespace' => 'Only include pages in these namespaces',
304 'type' => 'What type of category members to include',
305 'sort' => 'Property to sort by',
306 'dir' => 'In which direction to sort',
307 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
308 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
309 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
310 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
311 'continue' => 'For large categories, give the value retured from previous query',
312 'limit' => 'The maximum number of pages to return.',
314 if ( $wgMiserMode ) {
315 $desc['namespace'] = array(
317 'NOTE: Due to $wgMiserMode, using this may result in fewer than "limit" results',
318 'returned before continuing; in extreme cases, zero results may be returned',
324 public function getDescription() {
325 return 'List all pages in a given category';
328 public function getPossibleErrors() {
329 return array_merge( parent
::getPossibleErrors(),
330 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
332 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
333 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
334 array( 'nosuchpageid', 'pageid' ),
339 protected function getExamples() {
341 'Get first 10 pages in [[Category:Physics]]:',
342 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
343 'Get page info about first 10 pages in [[Category:Physics]]:',
344 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
348 public function getVersion() {
349 return __CLASS__
. ': $Id$';