Don't pass $this by reference
[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 $queryTypes = $params['type'];
103 $contWhere = false;
104
105 // Scanning large datasets for rare categories sucks, and I already told
106 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
107 global $wgMiserMode;
108 $miser_ns = array();
109 if ( $wgMiserMode ) {
110 $miser_ns = $params['namespace'];
111 } else {
112 $this->addWhereFld( 'page_namespace', $params['namespace'] );
113 }
114
115 $dir = $params['dir'] == 'asc' ? 'newer' : 'older';
116
117 if ( $params['sort'] == 'timestamp' ) {
118 $this->addWhereRange( 'cl_timestamp',
119 $dir,
120 $params['start'],
121 $params['end'] );
122
123 $this->addOption( 'USE INDEX', 'cl_timestamp' );
124 } else {
125 if ( $params['continue'] ) {
126 // type|from|sortkey
127 $cont = explode( '|', $params['continue'], 3 );
128 if ( count( $cont ) != 3 ) {
129 $this->dieUsage( 'Invalid continue param. You should pass the original value returned '.
130 'by the previous query', '_badcontinue'
131 );
132 }
133
134 // Remove the types to skip from $queryTypes
135 $contTypeIndex = array_search( $cont[0], $queryTypes );
136 $queryTypes = array_slice( $queryTypes, $contTypeIndex );
137
138 // Add a WHERE clause for sortkey and from
139 $from = intval( $cont[1] );
140 $escSortkey = $this->getDB()->addQuotes( $cont[2] );
141 $op = $dir == 'newer' ? '>' : '<';
142 // $contWhere is used further down
143 $contWhere = "cl_sortkey $op $escSortkey OR " .
144 "(cl_sortkey = $escSortkey AND " .
145 "cl_from $op= $from)";
146
147 } else {
148 // The below produces ORDER BY cl_sortkey, cl_from, possibly with DESC added to each of them
149 $this->addWhereRange( 'cl_sortkey',
150 $dir,
151 $params['startsortkey'],
152 $params['endsortkey'] );
153 $this->addWhereRange( 'cl_from', $dir, null, null );
154 }
155 $this->addOption( 'USE INDEX', 'cl_sortkey' );
156 }
157
158 $this->addWhere( 'cl_from=page_id' );
159
160 $limit = $params['limit'];
161 $this->addOption( 'LIMIT', $limit + 1 );
162
163 // Run a separate SELECT query for each value of cl_type.
164 // This is needed because cl_type is an enum, and MySQL has
165 // inconsistencies between ORDER BY cl_type and
166 // WHERE cl_type >= 'foo' making proper paging impossible
167 // and unindexed.
168 $rows = array();
169 $first = true;
170 foreach ( $queryTypes as $type ) {
171 $extraConds = array( 'cl_type' => $type );
172 if ( $first && $contWhere ) {
173 // Continuation condition. Only added to the
174 // first query, otherwise we'll skip things
175 $extraConds[] = $contWhere;
176 }
177 $res = $this->select( __METHOD__, array( 'where' => $extraConds ) );
178 $rows = array_merge( $rows, iterator_to_array( $res ) );
179 if ( count( $rows ) >= $limit + 1 ) {
180 break;
181 }
182 $first = false;
183 }
184 $count = 0;
185 foreach ( $rows as $row ) {
186 if ( ++ $count > $limit ) {
187 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
188 // TODO: Security issue - if the user has no right to view next title, it will still be shown
189 if ( $params['sort'] == 'timestamp' ) {
190 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
191 } else {
192 // Continue format is type|from|sortkey
193 // The order is a bit weird but it's convenient to put the sortkey at the end
194 // because we don't have to worry about pipes in the sortkey that way
195 // (and type and from can't contain pipes anyway)
196 $this->setContinueEnumParameter( 'continue',
197 "{$row->cl_type}|{$row->cl_from}|{$row->cl_sortkey}"
198 );
199 }
200 break;
201 }
202
203 // Since domas won't tell anyone what he told long ago, apply
204 // cmnamespace here. This means the query may return 0 actual
205 // results, but on the other hand it could save returning 5000
206 // useless results to the client. ~~~~
207 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
208 continue;
209 }
210
211 if ( is_null( $resultPageSet ) ) {
212 $vals = array();
213 if ( $fld_ids ) {
214 $vals['pageid'] = intval( $row->page_id );
215 }
216 if ( $fld_title ) {
217 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
218 ApiQueryBase::addTitleInfo( $vals, $title );
219 }
220 if ( $fld_sortkey ) {
221 $vals['sortkey'] = $row->cl_sortkey;
222 }
223 if ( $fld_sortkeyprefix ) {
224 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
225 }
226 if ( $fld_type ) {
227 $vals['type'] = $row->cl_type;
228 }
229 if ( $fld_timestamp ) {
230 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
231 }
232 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
233 null, $vals );
234 if ( !$fit ) {
235 if ( $params['sort'] == 'timestamp' ) {
236 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
237 } else {
238 $this->setContinueEnumParameter( 'continue',
239 "{$row->cl_type}|{$row->cl_from}|{$row->cl_sortkey}"
240 );
241 }
242 break;
243 }
244 } else {
245 $resultPageSet->processDbRow( $row );
246 }
247 }
248
249 if ( is_null( $resultPageSet ) ) {
250 $this->getResult()->setIndexedTagName_internal(
251 array( 'query', $this->getModuleName() ), 'cm' );
252 }
253 }
254
255 public function getAllowedParams() {
256 return array(
257 'title' => array(
258 ApiBase::PARAM_TYPE => 'string',
259 ),
260 'pageid' => array(
261 ApiBase::PARAM_TYPE => 'integer'
262 ),
263 'prop' => array(
264 ApiBase::PARAM_DFLT => 'ids|title',
265 ApiBase::PARAM_ISMULTI => true,
266 ApiBase::PARAM_TYPE => array (
267 'ids',
268 'title',
269 'sortkey',
270 'sortkeyprefix',
271 'type',
272 'timestamp',
273 )
274 ),
275 'namespace' => array (
276 ApiBase::PARAM_ISMULTI => true,
277 ApiBase::PARAM_TYPE => 'namespace',
278 ),
279 'type' => array(
280 ApiBase::PARAM_ISMULTI => true,
281 ApiBase::PARAM_DFLT => 'page|subcat|file',
282 ApiBase::PARAM_TYPE => array(
283 'page',
284 'subcat',
285 'file'
286 )
287 ),
288 'continue' => null,
289 'limit' => array(
290 ApiBase::PARAM_TYPE => 'limit',
291 ApiBase::PARAM_DFLT => 10,
292 ApiBase::PARAM_MIN => 1,
293 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
294 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
295 ),
296 'sort' => array(
297 ApiBase::PARAM_DFLT => 'sortkey',
298 ApiBase::PARAM_TYPE => array(
299 'sortkey',
300 'timestamp'
301 )
302 ),
303 'dir' => array(
304 ApiBase::PARAM_DFLT => 'asc',
305 ApiBase::PARAM_TYPE => array(
306 'asc',
307 'desc'
308 )
309 ),
310 'start' => array(
311 ApiBase::PARAM_TYPE => 'timestamp'
312 ),
313 'end' => array(
314 ApiBase::PARAM_TYPE => 'timestamp'
315 ),
316 'startsortkey' => null,
317 'endsortkey' => null,
318 );
319 }
320
321 public function getParamDescription() {
322 global $wgMiserMode;
323 $p = $this->getModulePrefix();
324 $desc = array(
325 'title' => "Which category to enumerate (required). Must include Category: prefix. Cannot be used together with {$p}pageid",
326 'pageid' => "Page ID of the category to enumerate. Cannot be used together with {$p}title",
327 'prop' => array(
328 'What pieces of information to include',
329 ' ids - Adds the page ID',
330 ' title - Adds the title and namespace ID of the page',
331 ' sortkey - Adds the sortkey used for sorting in the category (may not be human-readble)',
332 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
333 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
334 ' timestamp - Adds the timestamp of when the page was included',
335 ),
336 'namespace' => 'Only include pages in these namespaces',
337 'type' => 'What type of category members to include',
338 'sort' => 'Property to sort by',
339 'dir' => 'In which direction to sort',
340 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
341 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
342 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
343 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
344 'continue' => 'For large categories, give the value retured from previous query',
345 'limit' => 'The maximum number of pages to return.',
346 );
347
348 if ( $wgMiserMode ) {
349 $desc['namespace'] = array(
350 $desc['namespace'],
351 "NOTE: Due to \$wgMiserMode, using this may result in fewer than \"{$p}limit\" results",
352 'returned before continuing; in extreme cases, zero results may be returned.',
353 "Note that you can use {$p}type=subcat or {$p}type=file instead of {$p}namespace=14 or 6.",
354 );
355 }
356 return $desc;
357 }
358
359 public function getDescription() {
360 return 'List all pages in a given category';
361 }
362
363 public function getPossibleErrors() {
364 return array_merge( parent::getPossibleErrors(),
365 $this->getRequireOnlyOneParameterErrorMessages( array( 'title', 'pageid' ) ),
366 array(
367 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
368 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
369 array( 'nosuchpageid', 'pageid' ),
370 )
371 );
372 }
373
374 protected function getExamples() {
375 return array(
376 'Get first 10 pages in [[Category:Physics]]:',
377 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
378 'Get page info about first 10 pages in [[Category:Physics]]:',
379 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
380 );
381 }
382
383 public function getVersion() {
384 return __CLASS__ . ': $Id$';
385 }
386 }