(bug 27965) Paging in list=categorymembers was completely broken. It was paging by...
[lhc/web/wiklou.git] / includes / api / ApiQueryCategoryMembers.php
1 <?php
2 /**
3 *
4 *
5 * Created on June 14, 2007
6 *
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
8 *
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.
13 *
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.
18 *
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
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiQueryBase.php" );
30 }
31
32 /**
33 * A query module to enumerate pages that belong to a category.
34 *
35 * @ingroup API
36 */
37 class ApiQueryCategoryMembers extends ApiQueryGeneratorBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'cm' );
41 }
42
43 public function execute() {
44 $this->run();
45 }
46
47 public function getCacheMode( $params ) {
48 return 'public';
49 }
50
51 public function executeGenerator( $resultPageSet ) {
52 $this->run( $resultPageSet );
53 }
54
55 /**
56 * @param $resultPageSet ApiPageSet
57 * @return void
58 */
59 private function run( $resultPageSet = null ) {
60 $params = $this->extractRequestParams();
61
62 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
63
64 if ( isset( $params['title'] ) ) {
65 $categoryTitle = Title::newFromText( $params['title'] );
66
67 if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY ) {
68 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
69 }
70 } elseif( isset( $params['pageid'] ) ) {
71 $categoryTitle = Title::newFromID( $params['pageid'] );
72
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' );
77 }
78 }
79
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'] );
87
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 );
92 } else {
93 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
94 $this->addFields( array( 'cl_from', 'cl_sortkey', 'cl_type' ) );
95 }
96
97 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
98
99 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
100
101 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
102 $this->addWhereFld( 'cl_type', $params['type'] );
103
104 // Scanning large datasets for rare categories sucks, and I already told
105 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
106 global $wgMiserMode;
107 $miser_ns = array();
108 if ( $wgMiserMode ) {
109 $miser_ns = $params['namespace'];
110 } else {
111 $this->addWhereFld( 'page_namespace', $params['namespace'] );
112 }
113
114 $dir = $params['dir'] == 'asc' ? 'newer' : 'older';
115
116 if ( $params['sort'] == 'timestamp' ) {
117 $this->addWhereRange( 'cl_timestamp',
118 $dir,
119 $params['start'],
120 $params['end'] );
121
122 $this->addOption( 'USE INDEX', 'cl_timestamp' );
123 } else {
124 if ( $params['continue'] ) {
125 // type|from|sortkey
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'
130 );
131 }
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)))"
141 );
142
143 } else {
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',
147 $dir,
148 $params['startsortkey'],
149 $params['endsortkey'] );
150 $this->addWhereRange( 'cl_from', $dir, null, null );
151 }
152 $this->addOption( 'USE INDEX', 'cl_sortkey' );
153 }
154
155 $this->addWhere( 'cl_from=page_id' );
156
157 $limit = $params['limit'];
158 $this->addOption( 'LIMIT', $limit + 1 );
159
160 $count = 0;
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 ) );
168 } else {
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}"
175 );
176 }
177 break;
178 }
179
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 ) ) {
185 continue;
186 }
187
188 if ( is_null( $resultPageSet ) ) {
189 $vals = array();
190 if ( $fld_ids ) {
191 $vals['pageid'] = intval( $row->page_id );
192 }
193 if ( $fld_title ) {
194 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
195 ApiQueryBase::addTitleInfo( $vals, $title );
196 }
197 if ( $fld_sortkey ) {
198 $vals['sortkey'] = $row->cl_sortkey;
199 }
200 if ( $fld_sortkeyprefix ) {
201 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
202 }
203 if ( $fld_type ) {
204 $vals['type'] = $row->cl_type;
205 }
206 if ( $fld_timestamp ) {
207 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
208 }
209 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
210 null, $vals );
211 if ( !$fit ) {
212 if ( $params['sort'] == 'timestamp' ) {
213 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
214 } else {
215 $this->setContinueEnumParameter( 'continue',
216 "{$row->cl_type}|{$row->cl_from}|{$row->cl_sortkey}"
217 );
218 }
219 break;
220 }
221 } else {
222 $resultPageSet->processDbRow( $row );
223 }
224 }
225
226 if ( is_null( $resultPageSet ) ) {
227 $this->getResult()->setIndexedTagName_internal(
228 array( 'query', $this->getModuleName() ), 'cm' );
229 }
230 }
231
232 public function getAllowedParams() {
233 return array(
234 'title' => array(
235 ApiBase::PARAM_TYPE => 'string',
236 ),
237 'pageid' => array(
238 ApiBase::PARAM_TYPE => 'integer'
239 ),
240 'prop' => array(
241 ApiBase::PARAM_DFLT => 'ids|title',
242 ApiBase::PARAM_ISMULTI => true,
243 ApiBase::PARAM_TYPE => array (
244 'ids',
245 'title',
246 'sortkey',
247 'sortkeyprefix',
248 'type',
249 'timestamp',
250 )
251 ),
252 'namespace' => array (
253 ApiBase::PARAM_ISMULTI => true,
254 ApiBase::PARAM_TYPE => 'namespace',
255 ),
256 'type' => array(
257 ApiBase::PARAM_ISMULTI => true,
258 ApiBase::PARAM_DFLT => 'page|subcat|file',
259 ApiBase::PARAM_TYPE => array(
260 'page',
261 'subcat',
262 'file'
263 )
264 ),
265 'continue' => null,
266 'limit' => 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
272 ),
273 'sort' => array(
274 ApiBase::PARAM_DFLT => 'sortkey',
275 ApiBase::PARAM_TYPE => array(
276 'sortkey',
277 'timestamp'
278 )
279 ),
280 'dir' => array(
281 ApiBase::PARAM_DFLT => 'asc',
282 ApiBase::PARAM_TYPE => array(
283 'asc',
284 'desc'
285 )
286 ),
287 'start' => array(
288 ApiBase::PARAM_TYPE => 'timestamp'
289 ),
290 'end' => array(
291 ApiBase::PARAM_TYPE => 'timestamp'
292 ),
293 'startsortkey' => null,
294 'endsortkey' => null,
295 );
296 }
297
298 public function getParamDescription() {
299 global $wgMiserMode;
300 $p = $this->getModulePrefix();
301 $desc = array(
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',
304 'prop' => array(
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',
312 ),
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.',
323 );
324
325 if ( $wgMiserMode ) {
326 $desc['namespace'] = array(
327 $desc['namespace'],
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.',
331 );
332 }
333 return $desc;
334 }
335
336 public function getDescription() {
337 return 'List all pages in a given category';
338 }
339
340 public function getPossibleErrors() {
341 return array_merge( parent::getPossibleErrors(),
342 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
343 array(
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' ),
347 )
348 );
349 }
350
351 protected function getExamples() {
352 return array(
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',
357 );
358 }
359
360 public function getVersion() {
361 return __CLASS__ . ': $Id$';
362 }
363 }