4 * Created on June 14, 2007
6 * API for MediaWiki 1.8+
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( "ApiQueryBase.php" );
32 * A query module to enumerate pages that belong to a category.
36 class ApiQueryCategoryMembers
extends ApiQueryGeneratorBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
::__construct( $query, $moduleName, 'cm' );
42 public function execute() {
46 public function getCacheMode( $params ) {
50 public function executeGenerator( $resultPageSet ) {
51 $this->run( $resultPageSet );
54 private function run( $resultPageSet = null ) {
55 $params = $this->extractRequestParams();
57 $categoryTitle = Title
::newFromText( $params['title'] );
59 if ( is_null( $categoryTitle ) ||
$categoryTitle->getNamespace() != NS_CATEGORY
) {
60 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
63 $prop = array_flip( $params['prop'] );
64 $fld_ids = isset( $prop['ids'] );
65 $fld_title = isset( $prop['title'] );
66 $fld_sortkey = isset( $prop['sortkey'] );
67 $fld_timestamp = isset( $prop['timestamp'] );
69 if ( is_null( $resultPageSet ) ) {
70 $this->addFields( array( 'cl_from', 'cl_sortkey', 'page_namespace', 'page_title' ) );
71 $this->addFieldsIf( 'page_id', $fld_ids );
73 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
74 $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
77 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp ||
$params['sort'] == 'timestamp' );
78 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
79 // Not needed after bug 10280 is applied to servers
80 if ( $params['sort'] == 'timestamp' ) {
81 $this->addOption( 'USE INDEX', 'cl_timestamp' );
83 $this->addOption( 'USE INDEX', 'cl_sortkey' );
86 $this->addWhere( 'cl_from=page_id' );
87 $this->setContinuation( $params['continue'], $params['dir'] );
88 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
89 // Scanning large datasets for rare categories sucks, and I already told
90 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
94 $miser_ns = $params['namespace'];
96 $this->addWhereFld( 'page_namespace', $params['namespace'] );
98 if ( $params['sort'] == 'timestamp' ) {
99 $this->addWhereRange( 'cl_timestamp', ( $params['dir'] == 'asc' ?
'newer' : 'older' ), $params['start'], $params['end'] );
101 $this->addWhereRange( 'cl_sortkey', ( $params['dir'] == 'asc' ?
'newer' : 'older' ), $params['startsortkey'], $params['endsortkey'] );
102 $this->addWhereRange( 'cl_from', ( $params['dir'] == 'asc' ?
'newer' : 'older' ), null, null );
105 $limit = $params['limit'];
106 $this->addOption( 'LIMIT', $limit +
1 );
110 $res = $this->select( __METHOD__
);
111 foreach ( $res as $row ) {
112 if ( ++
$count > $limit ) {
113 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
114 // TODO: Security issue - if the user has no right to view next title, it will still be shown
115 if ( $params['sort'] == 'timestamp' ) {
116 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
118 $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
123 // Since domas won't tell anyone what he told long ago, apply
124 // cmnamespace here. This means the query may return 0 actual
125 // results, but on the other hand it could save returning 5000
126 // useless results to the client. ~~~~
127 if ( count( $miser_ns ) && !in_array( $row->page_namespace
, $miser_ns ) ) {
131 if ( is_null( $resultPageSet ) ) {
134 $vals['pageid'] = intval( $row->page_id
);
137 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
138 ApiQueryBase
::addTitleInfo( $vals, $title );
140 if ( $fld_sortkey ) {
141 $vals['sortkey'] = $row->cl_sortkey
;
143 if ( $fld_timestamp ) {
144 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
);
146 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
149 if ( $params['sort'] == 'timestamp' ) {
150 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->cl_timestamp
) );
152 $this->setContinueEnumParameter( 'continue', $this->getContinueStr( $row, $lastSortKey ) );
157 $resultPageSet->processDbRow( $row );
159 $lastSortKey = $row->cl_sortkey
; // detect duplicate sortkeys
162 if ( is_null( $resultPageSet ) ) {
163 $this->getResult()->setIndexedTagName_internal(
164 array( 'query', $this->getModuleName() ), 'cm' );
168 private function getContinueStr( $row, $lastSortKey ) {
169 $ret = $row->cl_sortkey
. '|';
170 if ( $row->cl_sortkey
== $lastSortKey ) { // duplicate sort key, add cl_from
171 $ret .= $row->cl_from
;
177 * Add DB WHERE clause to continue previous query based on 'continue' parameter
179 private function setContinuation( $continue, $dir ) {
180 if ( is_null( $continue ) ) {
181 return; // This is not a continuation request
184 $pos = strrpos( $continue, '|' );
185 $sortkey = substr( $continue, 0, $pos );
186 $fromstr = substr( $continue, $pos +
1 );
187 $from = intval( $fromstr );
189 if ( $from == 0 && strlen( $fromstr ) > 0 ) {
190 $this->dieUsage( 'Invalid continue param. You should pass the original value returned by the previous query', 'badcontinue' );
193 $encSortKey = $this->getDB()->addQuotes( $sortkey );
194 $encFrom = $this->getDB()->addQuotes( $from );
196 $op = ( $dir == 'desc' ?
'<' : '>' );
199 // Duplicate sort key continue
200 $this->addWhere( "cl_sortkey$op$encSortKey OR (cl_sortkey=$encSortKey AND cl_from$op=$encFrom)" );
202 $this->addWhere( "cl_sortkey$op=$encSortKey" );
206 public function getAllowedParams() {
209 ApiBase
::PARAM_TYPE
=> 'string',
210 ApiBase
::PARAM_REQUIRED
=> true
214 ApiBase
::PARAM_DFLT
=> 'ids|title',
215 ApiBase
::PARAM_ISMULTI
=> true,
216 ApiBase
::PARAM_TYPE
=> array (
223 'namespace' => array (
224 ApiBase
::PARAM_ISMULTI
=> true,
225 ApiBase
::PARAM_TYPE
=> 'namespace',
229 ApiBase
::PARAM_TYPE
=> 'limit',
230 ApiBase
::PARAM_DFLT
=> 10,
231 ApiBase
::PARAM_MIN
=> 1,
232 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
233 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
236 ApiBase
::PARAM_DFLT
=> 'sortkey',
237 ApiBase
::PARAM_TYPE
=> array(
243 ApiBase
::PARAM_DFLT
=> 'asc',
244 ApiBase
::PARAM_TYPE
=> array(
250 ApiBase
::PARAM_TYPE
=> 'timestamp'
253 ApiBase
::PARAM_TYPE
=> 'timestamp'
255 'startsortkey' => null,
256 'endsortkey' => null,
260 public function getParamDescription() {
262 $p = $this->getModulePrefix();
264 'title' => 'Which category to enumerate (required). Must include Category: prefix',
266 'What pieces of information to include',
267 ' ids - Adds the page id',
268 ' title - Adds the title and namespace id of the page',
269 ' sortkey - Adds the sortkey used for the category',
270 ' timestamp - Adds the timestamp of when the page was included',
272 'namespace' => 'Only include pages in these namespaces',
273 'sort' => 'Property to sort by',
274 'dir' => 'In which direction to sort',
275 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
276 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
277 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
278 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
279 'continue' => 'For large categories, give the value retured from previous query',
280 'limit' => 'The maximum number of pages to return.',
282 if ( $wgMiserMode ) {
283 $desc['namespace'] = array(
285 'NOTE: Due to $wgMiserMode, using this may result in fewer than "limit" results',
286 'returned before continuing; in extreme cases, zero results may be returned',
292 public function getDescription() {
293 return 'List all pages in a given category';
296 public function getPossibleErrors() {
297 return array_merge( parent
::getPossibleErrors(), array(
298 array( 'code' => 'notitle', 'info' => 'The cmtitle parameter is required' ),
299 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
300 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
304 protected function getExamples() {
306 'Get first 10 pages in [[Category:Physics]]:',
307 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
308 'Get page info about first 10 pages in [[Category:Physics]]:',
309 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
313 public function getVersion() {
314 return __CLASS__
. ': $Id$';