0f469dd519f3151379f8b1c9dc6540de73a6bb6b
[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 private function run( $resultPageSet = null ) {
56 $params = $this->extractRequestParams();
57
58 $this->requireOnlyOneParameter( $params, 'title', 'pageid' );
59
60 if ( isset( $params['title'] ) ) {
61 $categoryTitle = Title::newFromText( $params['title'] );
62
63 if ( is_null( $categoryTitle ) || $categoryTitle->getNamespace() != NS_CATEGORY ) {
64 $this->dieUsage( 'The category name you entered is not valid', 'invalidcategory' );
65 }
66 } elseif( isset( $params['pageid'] ) ) {
67 $categoryTitle = Title::newFromID( $params['pageid'] );
68
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' );
73 }
74 }
75
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_sortkeyprefix = isset( $prop['sortkeyprefix'] );
81 $fld_timestamp = isset( $prop['timestamp'] );
82 $fld_type = isset( $prop['type'] );
83
84 if ( is_null( $resultPageSet ) ) {
85 $this->addFields( array( 'cl_from', 'page_namespace', 'page_title' ) );
86 $this->addFieldsIf( 'page_id', $fld_ids );
87 $this->addFieldsIf( 'cl_sortkey_prefix', $fld_sortkeyprefix );
88 $this->addFieldsIf( 'cl_sortkey', $fld_sortkey );
89 } else {
90 $this->addFields( $resultPageSet->getPageTableFields() ); // will include page_ id, ns, title
91 $this->addFields( array( 'cl_from', 'cl_sortkey' ) );
92 }
93
94 $this->addFieldsIf( 'cl_timestamp', $fld_timestamp || $params['sort'] == 'timestamp' );
95 $this->addFieldsIf( 'cl_type', $fld_type );
96
97 $this->addTables( array( 'page', 'categorylinks' ) ); // must be in this order for 'USE INDEX'
98
99 $this->addWhereFld( 'cl_to', $categoryTitle->getDBkey() );
100 $this->addWhereFld( 'cl_type', $params['type'] );
101
102 // Scanning large datasets for rare categories sucks, and I already told
103 // how to have efficient subcategory access :-) ~~~~ (oh well, domas)
104 global $wgMiserMode;
105 $miser_ns = array();
106 if ( $wgMiserMode ) {
107 $miser_ns = $params['namespace'];
108 } else {
109 $this->addWhereFld( 'page_namespace', $params['namespace'] );
110 }
111
112 $dir = $params['dir'] == 'asc' ? 'newer' : 'older';
113
114 if ( $params['sort'] == 'timestamp' ) {
115 $this->addWhereRange( 'cl_timestamp',
116 $dir,
117 $params['start'],
118 $params['end'] );
119
120 $this->addOption( 'USE INDEX', 'cl_timestamp' );
121 } else {
122 // The below produces ORDER BY cl_type, cl_sortkey, cl_from, possibly with DESC added to each of them
123 $this->addWhereRange( 'cl_type', $dir, null, null );
124 $this->addWhereRange( 'cl_sortkey',
125 $dir,
126 $params['startsortkey'],
127 $params['endsortkey'] );
128 $this->addWhereRange( 'cl_from', $dir, null, null );
129 $this->addOption( 'USE INDEX', 'cl_sortkey' );
130 }
131
132 $this->setContinuation( $params['continue'], $params['dir'] );
133
134 $this->addWhere( 'cl_from=page_id' );
135
136 $limit = $params['limit'];
137 $this->addOption( 'LIMIT', $limit + 1 );
138
139 $count = 0;
140 $res = $this->select( __METHOD__ );
141 foreach ( $res as $row ) {
142 if ( ++ $count > $limit ) {
143 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
144 // TODO: Security issue - if the user has no right to view next title, it will still be shown
145 if ( $params['sort'] == 'timestamp' ) {
146 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
147 } else {
148 $this->setContinueEnumParameter( 'continue', $row->cl_from );
149 }
150 break;
151 }
152
153 // Since domas won't tell anyone what he told long ago, apply
154 // cmnamespace here. This means the query may return 0 actual
155 // results, but on the other hand it could save returning 5000
156 // useless results to the client. ~~~~
157 if ( count( $miser_ns ) && !in_array( $row->page_namespace, $miser_ns ) ) {
158 continue;
159 }
160
161 if ( is_null( $resultPageSet ) ) {
162 $vals = array();
163 if ( $fld_ids ) {
164 $vals['pageid'] = intval( $row->page_id );
165 }
166 if ( $fld_title ) {
167 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
168 ApiQueryBase::addTitleInfo( $vals, $title );
169 }
170 if ( $fld_sortkey ) {
171 $vals['sortkey'] = $row->cl_sortkey;
172 }
173 if ( $fld_sortkeyprefix ) {
174 $vals['sortkeyprefix'] = $row->cl_sortkey_prefix;
175 }
176 if ( $fld_type ) {
177 $vals['type'] = $row->cl_type;
178 }
179 if ( $fld_timestamp ) {
180 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->cl_timestamp );
181 }
182 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ),
183 null, $vals );
184 if ( !$fit ) {
185 if ( $params['sort'] == 'timestamp' ) {
186 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->cl_timestamp ) );
187 } else {
188 $this->setContinueEnumParameter( 'continue', $row->cl_from );
189 }
190 break;
191 }
192 } else {
193 $resultPageSet->processDbRow( $row );
194 }
195 }
196
197 if ( is_null( $resultPageSet ) ) {
198 $this->getResult()->setIndexedTagName_internal(
199 array( 'query', $this->getModuleName() ), 'cm' );
200 }
201 }
202
203 /**
204 * Add DB WHERE clause to continue previous query based on 'continue' parameter
205 */
206 private function setContinuation( $continue, $dir ) {
207 if ( is_null( $continue ) ) {
208 return; // This is not a continuation request
209 }
210
211 $encFrom = $this->getDB()->addQuotes( intval( $continue ) );
212
213 $op = ( $dir == 'desc' ? '<=' : '>=' );
214
215 $this->addWhere( "cl_from $op $encFrom" );
216 }
217
218 public function getAllowedParams() {
219 return array(
220 'title' => array(
221 ApiBase::PARAM_TYPE => 'string',
222 ),
223 'pageid' => array(
224 ApiBase::PARAM_TYPE => 'integer'
225 ),
226 'prop' => array(
227 ApiBase::PARAM_DFLT => 'ids|title',
228 ApiBase::PARAM_ISMULTI => true,
229 ApiBase::PARAM_TYPE => array (
230 'ids',
231 'title',
232 'sortkey',
233 'sortkeyprefix',
234 'type',
235 'timestamp',
236 )
237 ),
238 'namespace' => array (
239 ApiBase::PARAM_ISMULTI => true,
240 ApiBase::PARAM_TYPE => 'namespace',
241 ),
242 'type' => array(
243 ApiBase::PARAM_ISMULTI => true,
244 ApiBase::PARAM_DFLT => 'page|subcat|file',
245 ApiBase::PARAM_TYPE => array(
246 'page',
247 'subcat',
248 'file'
249 )
250 ),
251 'continue' => null,
252 'limit' => array(
253 ApiBase::PARAM_TYPE => 'limit',
254 ApiBase::PARAM_DFLT => 10,
255 ApiBase::PARAM_MIN => 1,
256 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
257 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
258 ),
259 'sort' => array(
260 ApiBase::PARAM_DFLT => 'sortkey',
261 ApiBase::PARAM_TYPE => array(
262 'sortkey',
263 'timestamp'
264 )
265 ),
266 'dir' => array(
267 ApiBase::PARAM_DFLT => 'asc',
268 ApiBase::PARAM_TYPE => array(
269 'asc',
270 'desc'
271 )
272 ),
273 'start' => array(
274 ApiBase::PARAM_TYPE => 'timestamp'
275 ),
276 'end' => array(
277 ApiBase::PARAM_TYPE => 'timestamp'
278 ),
279 'startsortkey' => null,
280 'endsortkey' => null,
281 );
282 }
283
284 public function getParamDescription() {
285 global $wgMiserMode;
286 $p = $this->getModulePrefix();
287 $desc = array(
288 'title' => 'Which category to enumerate (required). Must include Category: prefix. Cannot be used together with cmpageid',
289 'pageid' => 'Page ID of the category to enumerate. Cannot be used together with cmtitle',
290 'prop' => array(
291 'What pieces of information to include',
292 ' ids - Adds the page ID',
293 ' title - Adds the title and namespace ID of the page',
294 ' sortkey - Adds the sortkey used for sorting in the category (may not be human-readble)',
295 ' sortkeyprefix - Adds the sortkey prefix used for sorting in the category (human-readable part of the sortkey)',
296 ' type - Adds the type that the page has been categorised as (page, subcat or file)',
297 ' timestamp - Adds the timestamp of when the page was included',
298 ),
299 'namespace' => 'Only include pages in these namespaces',
300 'type' => 'What type of category members to include',
301 'sort' => 'Property to sort by',
302 'dir' => 'In which direction to sort',
303 'start' => "Timestamp to start listing from. Can only be used with {$p}sort=timestamp",
304 'end' => "Timestamp to end listing at. Can only be used with {$p}sort=timestamp",
305 'startsortkey' => "Sortkey to start listing from. Can only be used with {$p}sort=sortkey",
306 'endsortkey' => "Sortkey to end listing at. Can only be used with {$p}sort=sortkey",
307 'continue' => 'For large categories, give the value retured from previous query',
308 'limit' => 'The maximum number of pages to return.',
309 );
310 if ( $wgMiserMode ) {
311 $desc['namespace'] = array(
312 $desc['namespace'],
313 'NOTE: Due to $wgMiserMode, using this may result in fewer than "limit" results',
314 'returned before continuing; in extreme cases, zero results may be returned',
315 );
316 }
317 return $desc;
318 }
319
320 public function getDescription() {
321 return 'List all pages in a given category';
322 }
323
324 public function getPossibleErrors() {
325 return array_merge( parent::getPossibleErrors(), array(
326 array( 'code' => 'cmmissingparam', 'info' => 'One of the parameters title, pageid is required' ),
327 array( 'code' => 'cminvalidparammix', 'info' => 'The parameters title, pageid can not be used together' ),
328 array( 'code' => 'invalidcategory', 'info' => 'The category name you entered is not valid' ),
329 array( 'code' => 'badcontinue', 'info' => 'Invalid continue param. You should pass the original value returned by the previous query' ),
330 array( 'nosuchpageid', 'pageid' ),
331 ) );
332 }
333
334 protected function getExamples() {
335 return array(
336 'Get first 10 pages in [[Category:Physics]]:',
337 ' api.php?action=query&list=categorymembers&cmtitle=Category:Physics',
338 'Get page info about first 10 pages in [[Category:Physics]]:',
339 ' api.php?action=query&generator=categorymembers&gcmtitle=Category:Physics&prop=info',
340 );
341 }
342
343 public function getVersion() {
344 return __CLASS__ . ': $Id$';
345 }
346 }