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