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 );
55 private function run( $resultPageSet = null ) {
56 $params = $this->extractRequestParams();
58 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
60 if ( isset( $params['title'] ) ) {
61 $categoryTitle = Title
::newFromText( $params['title'] );
63 if ( is_null( $categoryTitle ) ||
$categoryTitle->getNamespace() != NS_CATEGORY
) {
64 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
66 } elseif( isset( $params['pageid'] ) ) {
67 $categoryTitle = Title
::newFromID( $params['pageid'] );
69 if ( !$categoryTitle ) {
70 $this->dieUsageMsg( array( 'nosuchpageid', $params['pageid'] ) );
71 } elseif ( $categoryTitle->getNamespace() != NS_CATEGORY
) {
72 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
76 $prop = array_flip( $params['prop'] );
77 $fld_ids = isset( $prop['ids'] );
78 $fld_title = isset( $prop['title'] );
79 $fld_sortkey = isset( $prop['sortkey'] );
80 $fld_timestamp = isset( $prop['timestamp'] );
81 $fld_type = isset( $prop['type'] );
83 if ( is_null( $resultPageSet ) ) {
84 $this->addFields( array( 'cl_from', 'cl_sortkey', 'page_namespace', 'page_title' ) );
85 $this->addFieldsIf( 'page_id', $fld_ids );
87 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
88 $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
91 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp ||
$params['sort'] == 'timestamp' );
92 $this->addFieldsIf( 'cl_type', $fld_type );
94 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
96 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
97 $this->addWhereFld( 'cl_type', $params['type'] );
99 // Scanning large datasets for rare categories sucks, and I already told
100 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
103 if ( $wgMiserMode ) {
104 $miser_ns = $params['namespace'];
106 $this->addWhereFld( 'page_namespace', $params['namespace'] );
109 $dir = $params['dir'] == 'asc' ?
'newer' : 'older';
111 if ( $params['sort'] == 'timestamp' ) {
112 $this->addWhereRange( 'cl_timestamp',
117 $this->addOption( 'USE INDEX', 'cl_timestamp' );
119 $this->addWhereRange( 'cl_sortkey',
121 $params['startsortkey'],
122 $params['endsortkey'] );
124 $this->addWhereRange( 'cl_from', $dir, null, null );
125 $this->addOption( 'USE INDEX', 'cl_sortkey' );
128 $this->setContinuation( $params['continue'], $params['dir'] );
130 $this->addWhere( 'cl_from=page_id' );
132 $limit = $params['limit'];
133 $this->addOption( 'LIMIT', $limit +
1 );
137 $res = $this->select( __METHOD__
);
138 foreach ( $res as $row ) {
139 if ( ++
$count > $limit ) {
140 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
141 // TODO: Security issue - if the user has no right to view next title, it will still be shown
142 if ( $params['sort'] == 'timestamp' ) {
143 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
145 $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
150 // Since domas won't tell anyone what he told long ago, apply
151 // cmnamespace here. This means the query may return 0 actual
152 // results, but on the other hand it could save returning 5000
153 // useless results to the client. ~~~~
154 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
158 if ( is_null( $resultPageSet ) ) {
161 $vals['pageid'] = intval( $row->page_id
);
164 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
165 ApiQueryBase
::addTitleInfo( $vals, $title );
167 if ( $fld_sortkey ) {
168 $vals['sortkey'] = $row->cl_sortkey
;
171 $vals['type'] = $row->cl_type
;
173 if ( $fld_timestamp ) {
174 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
176 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
179 if ( $params['sort'] == 'timestamp' ) {
180 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
182 $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
187 $resultPageSet->processDbRow( $row );
189 $lastSortKey = $row->cl_sortkey
; // detect duplicate sortkeys
192 if ( is_null( $resultPageSet ) ) {
193 $this->getResult()->setIndexedTagName_internal(
194 array( 'query', $this->getModuleName() ), 'cm' );
198 private function getContinueStr( $row, $lastSortKey ) {
199 $ret = $row->cl_sortkey
. '|';
200 if ( $row->cl_sortkey
== $lastSortKey ) { // duplicate sort key, add cl_from
201 $ret .= $row->cl_from
;
207 * Add DB WHERE clause to continue previous query based on 'continue' parameter
209 private function setContinuation( $continue, $dir ) {
210 if ( is_null( $continue ) ) {
211 return; // This is not a continuation request
214 $pos = strrpos( $continue, '|' );
215 $sortkey = substr( $continue, 0, $pos );
216 $fromstr = substr( $continue, $pos +
1 );
217 $from = intval( $fromstr );
219 if ( $from == 0 && strlen( $fromstr ) > 0 ) {
220 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
223 $encSortKey = $this->getDB()->addQuotes( $sortkey );
224 $encFrom = $this->getDB()->addQuotes( $from );
226 $op = ( $dir == 'desc' ?
'<' : '>' );
229 // Duplicate sort key continue
230 $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
232 $this->addWhere( "cl_sortkey$op=$encSortKey" );
236 public function getAllowedParams() {
239 ApiBase
::PARAM_TYPE
=> 'string',
242 ApiBase
::PARAM_TYPE
=> 'integer'
245 ApiBase
::PARAM_DFLT
=> 'ids|title',
246 ApiBase
::PARAM_ISMULTI
=> true,
247 ApiBase
::PARAM_TYPE
=> array (
255 'namespace' => array (
256 ApiBase
::PARAM_ISMULTI
=> true,
257 ApiBase
::PARAM_TYPE
=> 'namespace',
260 ApiBase
::PARAM_ISMULTI
=> true,
261 ApiBase
::PARAM_DFLT
=> 'page|subcat|file',
262 ApiBase
::PARAM_TYPE
=> array(
270 ApiBase
::PARAM_TYPE
=> 'limit',
271 ApiBase
::PARAM_DFLT
=> 10,
272 ApiBase
::PARAM_MIN
=> 1,
273 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
274 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
277 ApiBase
::PARAM_DFLT
=> 'sortkey',
278 ApiBase
::PARAM_TYPE
=> array(
284 ApiBase
::PARAM_DFLT
=> 'asc',
285 ApiBase
::PARAM_TYPE
=> array(
291 ApiBase
::PARAM_TYPE
=> 'timestamp'
294 ApiBase
::PARAM_TYPE
=> 'timestamp'
296 'startsortkey' => null,
297 'endsortkey' => null,
301 public function getParamDescription() {
303 $p = $this->getModulePrefix();
305 'title' => 'Which category to enumerate (required). Must include Category: prefix. Cannot be used together with cmpageid',
306 'pageid' => 'Page ID of the category to enumerate. Cannot be used together with cmtitle',
308 'What pieces of information to include',
309 ' ids - Adds the page ID',
310 ' title - Adds the title and namespace ID of the page',
311 ' sortkey - Adds the sortkey used for the category',
312 ' type - Adds the type that the page has been categorised as',
313 ' timestamp - Adds the timestamp of when the page was included',
315 'namespace' => 'Only include pages in these namespaces',
316 'type' => 'What type of category members to include',
317 'sort' => 'Property to sort by',
318 'dir' => 'In which direction to sort',
319 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
320 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
321 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
322 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
323 'continue' => 'For large categories, give the value retured from previous query',
324 'limit' => 'The maximum number of pages to return.',
326 if ( $wgMiserMode ) {
327 $desc['namespace'] = array(
329 'NOTE: Due to $wgMiserMode, using this may result in fewer than "limit" results',
330 'returned before continuing; in extreme cases, zero results may be returned',
336 public function getDescription() {
337 return 'List all pages in a given category';
340 public function getPossibleErrors() {
341 return array_merge( parent
::getPossibleErrors(), array(
342 array( 'code' => 'cmmissingparam', 'info' => 'One of the parameters title, pageid is required' ),
343 array( 'code' => 'cminvalidparammix', 'info' => 'The parameters title, pageid can not be used together' ),
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' ),
350 protected function getExamples() {
352 'Get first 10 pages in [[Category:Physics]]:',
353 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
354 'Get page info about first 10 pages in [[Category:Physics]]:',
355 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
359 public function getVersion() {
360 return __CLASS__
. ': $Id$';