More parameter documentation
[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', '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 );
93 } else {
94 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
95 $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
96 }
97
98 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
99 $this->addFieldsIf( 'cl_type', $fld_type );
100
101 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
102
103 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
104 $this->addWhereFld( 'cl_type', $params['type'] );
105
106 // Scanning large datasets for rare categories sucks, and I already told
107 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
108 global $wgMiserMode;
109 $miser_ns = array();
110 if ( $wgMiserMode ) {
111 $miser_ns = $params['namespace'];
112 } else {
113 $this->addWhereFld( 'page_namespace', $params['namespace'] );
114 }
115
116 $dir = $params['dir'] == 'asc' ? 'newer' : 'older';
117
118 if ( $params['sort'] == 'timestamp' ) {
119 $this->addWhereRange( 'cl_timestamp',
120 $dir,
121 $params['start'],
122 $params['end'] );
123
124 $this->addOption( 'USE INDEX', 'cl_timestamp' );
125 } else {
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',
129 $dir,
130 $params['startsortkey'],
131 $params['endsortkey'] );
132 $this->addWhereRange( 'cl_from', $dir, null, null );
133 $this->addOption( 'USE INDEX', 'cl_sortkey' );
134 }
135
136 $this->setContinuation( $params['continue'], $params['dir'] );
137
138 $this->addWhere( 'cl_from=page_id' );
139
140 $limit = $params['limit'];
141 $this->addOption( 'LIMIT', $limit + 1 );
142
143 $count = 0;
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 ) );
151 } else {
152 $this->setContinueEnumParameter( 'continue', $row->cl_from );
153 }
154 break;
155 }
156
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 ) ) {
162 continue;
163 }
164
165 if ( is_null( $resultPageSet ) ) {
166 $vals = array();
167 if ( $fld_ids ) {
168 $vals['pageid'] = intval( $row->page_id );
169 }
170 if ( $fld_title ) {
171 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
172 ApiQueryBase::addTitleInfo( $vals, $title );
173 }
174 if ( $fld_sortkey ) {
175 $vals['sortkey'] = $row->cl_sortkey;
176 }
177 if ( $fld_sortkeyprefix ) {
178 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
179 }
180 if ( $fld_type ) {
181 $vals['type'] = $row->cl_type;
182 }
183 if ( $fld_timestamp ) {
184 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
185 }
186 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
187 null, $vals );
188 if ( !$fit ) {
189 if ( $params['sort'] == 'timestamp' ) {
190 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
191 } else {
192 $this->setContinueEnumParameter( 'continue', $row->cl_from );
193 }
194 break;
195 }
196 } else {
197 $resultPageSet->processDbRow( $row );
198 }
199 }
200
201 if ( is_null( $resultPageSet ) ) {
202 $this->getResult()->setIndexedTagName_internal(
203 array( 'query', $this->getModuleName() ), 'cm' );
204 }
205 }
206
207 /**
208 * Add DB WHERE clause to continue previous query based on 'continue' parameter
209 */
210 private function setContinuation( $continue, $dir ) {
211 if ( is_null( $continue ) ) {
212 return; // This is not a continuation request
213 }
214
215 $encFrom = $this->getDB()->addQuotes( intval( $continue ) );
216
217 $op = ( $dir == 'desc' ? '<=' : '>=' );
218
219 $this->addWhere( "cl_from $op $encFrom" );
220 }
221
222 public function getAllowedParams() {
223 return array(
224 'title' => array(
225 ApiBase::PARAM_TYPE => 'string',
226 ),
227 'pageid' => array(
228 ApiBase::PARAM_TYPE => 'integer'
229 ),
230 'prop' => array(
231 ApiBase::PARAM_DFLT => 'ids|title',
232 ApiBase::PARAM_ISMULTI => true,
233 ApiBase::PARAM_TYPE => array (
234 'ids',
235 'title',
236 'sortkey',
237 'sortkeyprefix',
238 'type',
239 'timestamp',
240 )
241 ),
242 'namespace' => array (
243 ApiBase::PARAM_ISMULTI => true,
244 ApiBase::PARAM_TYPE => 'namespace',
245 ),
246 'type' => array(
247 ApiBase::PARAM_ISMULTI => true,
248 ApiBase::PARAM_DFLT => 'page|subcat|file',
249 ApiBase::PARAM_TYPE => array(
250 'page',
251 'subcat',
252 'file'
253 )
254 ),
255 'continue' => null,
256 'limit' => 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
262 ),
263 'sort' => array(
264 ApiBase::PARAM_DFLT => 'sortkey',
265 ApiBase::PARAM_TYPE => array(
266 'sortkey',
267 'timestamp'
268 )
269 ),
270 'dir' => array(
271 ApiBase::PARAM_DFLT => 'asc',
272 ApiBase::PARAM_TYPE => array(
273 'asc',
274 'desc'
275 )
276 ),
277 'start' => array(
278 ApiBase::PARAM_TYPE => 'timestamp'
279 ),
280 'end' => array(
281 ApiBase::PARAM_TYPE => 'timestamp'
282 ),
283 'startsortkey' => null,
284 'endsortkey' => null,
285 );
286 }
287
288 public function getParamDescription() {
289 global $wgMiserMode;
290 $p = $this->getModulePrefix();
291 $desc = array(
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',
294 'prop' => array(
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',
302 ),
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.',
313 );
314 if ( $wgMiserMode ) {
315 $desc['namespace'] = array(
316 $desc['namespace'],
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',
319 );
320 }
321 return $desc;
322 }
323
324 public function getDescription() {
325 return 'List all pages in a given category';
326 }
327
328 public function getPossibleErrors() {
329 return array_merge( parent::getPossibleErrors(), array(
330 array( 'code' => 'cmmissingparam', 'info' => 'One of the parameters title, pageid is required' ),
331 array( 'code' => 'cminvalidparammix', 'info' => 'The parameters title, pageid can not be used together' ),
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' ),
335 ) );
336 }
337
338 protected function getExamples() {
339 return array(
340 'Get first 10 pages in [[Category:Physics]]:',
341 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
342 'Get page info about first 10 pages in [[Category:Physics]]:',
343 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
344 );
345 }
346
347 public function getVersion() {
348 return __CLASS__ . ': $Id$';
349 }
350 }