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