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