re r64403 - remove never-actually-used $flag parameter from verifyUpload()
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2
3 /**
4 * Created on Oct 16, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * This query action adds a list of a specified user's contributions to the output.
33 *
34 * @ingroup API
35 */
36 class ApiQueryContributions extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'uc' );
40 }
41
42 private $params;
43 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
44 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
45 $fld_patrolled = false, $fld_tags = false;
46
47 public function execute() {
48 // Parse some parameters
49 $this->params = $this->extractRequestParams();
50
51 $prop = array_flip( $this->params['prop'] );
52 $this->fld_ids = isset( $prop['ids'] );
53 $this->fld_title = isset( $prop['title'] );
54 $this->fld_comment = isset( $prop['comment'] );
55 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
56 $this->fld_size = isset( $prop['size'] );
57 $this->fld_flags = isset( $prop['flags'] );
58 $this->fld_timestamp = isset( $prop['timestamp'] );
59 $this->fld_patrolled = isset( $prop['patrolled'] );
60 $this->fld_tags = isset( $prop['tags'] );
61
62 // TODO: if the query is going only against the revision table, should this be done?
63 $this->selectNamedDB( 'contributions', DB_SLAVE, 'contributions' );
64
65 if ( isset( $this->params['userprefix'] ) ) {
66 $this->prefixMode = true;
67 $this->multiUserMode = true;
68 $this->userprefix = $this->params['userprefix'];
69 } else {
70 $this->usernames = array();
71 if ( !is_array( $this->params['user'] ) ) {
72 $this->params['user'] = array( $this->params['user'] );
73 }
74 if ( !count( $this->params['user'] ) ) {
75 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
76 }
77 foreach ( $this->params['user'] as $u ) {
78 $this->prepareUsername( $u );
79 }
80 $this->prefixMode = false;
81 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
82 }
83 $this->prepareQuery();
84
85 // Do the actual query.
86 $res = $this->select( __METHOD__ );
87
88 // Initialise some variables
89 $count = 0;
90 $limit = $this->params['limit'];
91
92 // Fetch each row
93 foreach ( $res as $row ) {
94 if ( ++ $count > $limit ) {
95 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
96 if ( $this->multiUserMode ) {
97 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
98 } else {
99 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
100 }
101 break;
102 }
103
104 $vals = $this->extractRowInfo( $row );
105 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
106 if ( !$fit ) {
107 if ( $this->multiUserMode ) {
108 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
109 } else {
110 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
111 }
112 break;
113 }
114 }
115
116 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
117 }
118
119 /**
120 * Validate the 'user' parameter and set the value to compare
121 * against `revision`.`rev_user_text`
122 */
123 private function prepareUsername( $user ) {
124 if ( !is_null( $user ) && $user !== '' ) {
125 $name = User::isIP( $user )
126 ? $user
127 : User::getCanonicalName( $user, 'valid' );
128 if ( $name === false ) {
129 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
130 } else {
131 $this->usernames[] = $name;
132 }
133 } else {
134 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
135 }
136 }
137
138 /**
139 * Prepares the query and returns the limit of rows requested
140 */
141 private function prepareQuery() {
142 // We're after the revision table, and the corresponding page
143 // row for anything we retrieve. We may also need the
144 // recentchanges row and/or tag summary row.
145 global $wgUser;
146 $tables = array( 'page', 'revision' ); // Order may change
147 $this->addWhere( 'page_id=rev_page' );
148
149 // Handle continue parameter
150 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
151 $continue = explode( '|', $this->params['continue'] );
152 if ( count( $continue ) != 2 ) {
153 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
154 'value returned by the previous query', '_badcontinue' );
155 }
156 $encUser = $this->getDB()->strencode( $continue[0] );
157 $encTS = wfTimestamp( TS_MW, $continue[1] );
158 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
159 $this->addWhere(
160 "rev_user_text $op '$encUser' OR " .
161 "(rev_user_text = '$encUser' AND " .
162 "rev_timestamp $op= '$encTS')"
163 );
164 }
165
166 // Make sure private data (deleted revisions) isn't cached
167 $this->getMain()->setVaryCookie();
168 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
169 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
170 }
171 // We only want pages by the specified users.
172 if ( $this->prefixMode ) {
173 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
174 } else {
175 $this->addWhereFld( 'rev_user_text', $this->usernames );
176 }
177 // ... and in the specified timeframe.
178 // Ensure the same sort order for rev_user_text and rev_timestamp
179 // so our query is indexed
180 if ( $this->multiUserMode ) {
181 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
182 }
183 $this->addWhereRange( 'rev_timestamp',
184 $this->params['dir'], $this->params['start'], $this->params['end'] );
185 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
186
187 $show = $this->params['show'];
188 if ( !is_null( $show ) ) {
189 $show = array_flip( $show );
190 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
191 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) )
192 {
193 $this->dieUsageMsg( array( 'show' ) );
194 }
195
196 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
197 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
198 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
199 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
200 }
201 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
202 $index = array( 'revision' => 'usertext_timestamp' );
203
204 // Mandatory fields: timestamp allows request continuation
205 // ns+title checks if the user has access rights for this page
206 // user_text is necessary if multiple users were specified
207 $this->addFields( array(
208 'rev_timestamp',
209 'page_namespace',
210 'page_title',
211 'rev_user_text',
212 'rev_deleted'
213 ) );
214
215 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
216 $this->fld_patrolled )
217 {
218 global $wgUser;
219 // Don't cache private data
220 $this->getMain()->setVaryCookie();
221 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
222 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
223 }
224
225 // Use a redundant join condition on both
226 // timestamp and ID so we can use the timestamp
227 // index
228 $index['recentchanges'] = 'rc_user_text';
229 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
230 // Put the tables in the right order for
231 // STRAIGHT_JOIN
232 $tables = array( 'revision', 'recentchanges', 'page' );
233 $this->addOption( 'STRAIGHT_JOIN' );
234 $this->addWhere( 'rc_user_text=rev_user_text' );
235 $this->addWhere( 'rc_timestamp=rev_timestamp' );
236 $this->addWhere( 'rc_this_oldid=rev_id' );
237 } else {
238 $tables[] = 'recentchanges';
239 $this->addJoinConds( array( 'recentchanges' => array(
240 'LEFT JOIN', array(
241 'rc_user_text=rev_user_text',
242 'rc_timestamp=rev_timestamp',
243 'rc_this_oldid=rev_id' ) ) ) );
244 }
245 }
246
247 $this->addTables( $tables );
248 $this->addFieldsIf( 'rev_page', $this->fld_ids );
249 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
250 $this->addFieldsIf( 'page_latest', $this->fld_flags );
251 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
252 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
253 $this->addFieldsIf( 'rev_len', $this->fld_size );
254 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
255 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags );
256 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
257
258 if ( $this->fld_tags ) {
259 $this->addTables( 'tag_summary' );
260 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
261 $this->addFields( 'ts_tags' );
262 }
263
264 if ( isset( $this->params['tag'] ) ) {
265 $this->addTables( 'change_tag' );
266 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
267 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
268 global $wgOldChangeTagsIndex;
269 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
270 }
271
272 $this->addOption( 'USE INDEX', $index );
273 }
274
275 /**
276 * Extract fields from the database row and append them to a result array
277 */
278 private function extractRowInfo( $row ) {
279 $vals = array();
280
281 $vals['user'] = $row->rev_user_text;
282 if ( $row->rev_deleted & Revision::DELETED_USER ) {
283 $vals['userhidden'] = '';
284 }
285 if ( $this->fld_ids ) {
286 $vals['pageid'] = intval( $row->rev_page );
287 $vals['revid'] = intval( $row->rev_id );
288 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
289 }
290
291 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
292
293 if ( $this->fld_title ) {
294 ApiQueryBase::addTitleInfo( $vals, $title );
295 }
296
297 if ( $this->fld_timestamp ) {
298 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
299 }
300
301 if ( $this->fld_flags ) {
302 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
303 $vals['new'] = '';
304 }
305 if ( $row->rev_minor_edit ) {
306 $vals['minor'] = '';
307 }
308 if ( $row->page_latest == $row->rev_id ) {
309 $vals['top'] = '';
310 }
311 }
312
313 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
314 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
315 $vals['commenthidden'] = '';
316 } else {
317 if ( $this->fld_comment ) {
318 $vals['comment'] = $row->rev_comment;
319 }
320
321 if ( $this->fld_parsedcomment ) {
322 global $wgUser;
323 $this->getMain()->setVaryCookie();
324 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment, $title );
325 }
326 }
327 }
328
329 if ( $this->fld_patrolled && $row->rc_patrolled ) {
330 $vals['patrolled'] = '';
331 }
332
333 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
334 $vals['size'] = intval( $row->rev_len );
335 }
336
337 if ( $this->fld_tags ) {
338 if ( $row->ts_tags ) {
339 $tags = explode( ',', $row->ts_tags );
340 $this->getResult()->setIndexedTagName( $tags, 'tag' );
341 $vals['tags'] = $tags;
342 } else {
343 $vals['tags'] = array();
344 }
345 }
346
347 return $vals;
348 }
349
350 private function continueStr( $row ) {
351 return $row->rev_user_text . '|' .
352 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
353 }
354
355 public function getAllowedParams() {
356 return array(
357 'limit' => array(
358 ApiBase::PARAM_DFLT => 10,
359 ApiBase::PARAM_TYPE => 'limit',
360 ApiBase::PARAM_MIN => 1,
361 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
362 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
363 ),
364 'start' => array(
365 ApiBase::PARAM_TYPE => 'timestamp'
366 ),
367 'end' => array(
368 ApiBase::PARAM_TYPE => 'timestamp'
369 ),
370 'continue' => null,
371 'user' => array(
372 ApiBase::PARAM_ISMULTI => true
373 ),
374 'userprefix' => null,
375 'dir' => array(
376 ApiBase::PARAM_DFLT => 'older',
377 ApiBase::PARAM_TYPE => array(
378 'newer',
379 'older'
380 )
381 ),
382 'namespace' => array(
383 ApiBase::PARAM_ISMULTI => true,
384 ApiBase::PARAM_TYPE => 'namespace'
385 ),
386 'prop' => array(
387 ApiBase::PARAM_ISMULTI => true,
388 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
389 ApiBase::PARAM_TYPE => array(
390 'ids',
391 'title',
392 'timestamp',
393 'comment',
394 'parsedcomment',
395 'size',
396 'flags',
397 'patrolled',
398 'tags'
399 )
400 ),
401 'show' => array(
402 ApiBase::PARAM_ISMULTI => true,
403 ApiBase::PARAM_TYPE => array(
404 'minor',
405 '!minor',
406 'patrolled',
407 '!patrolled',
408 )
409 ),
410 'tag' => null,
411 );
412 }
413
414 public function getParamDescription() {
415 global $wgRCMaxAge;
416 $p = $this->getModulePrefix();
417 return array(
418 'limit' => 'The maximum number of contributions to return',
419 'start' => 'The start timestamp to return from',
420 'end' => 'The end timestamp to return to',
421 'continue' => 'When more results are available, use this to continue',
422 'user' => 'The users to retrieve contributions for',
423 'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
424 'dir' => 'The direction to search (older or newer)',
425 'namespace' => 'Only list contributions in these namespaces',
426 'prop' => array(
427 'Include additional pieces of information',
428 ' ids - Adds the page id and revision id',
429 ' title - Adds the title and namespace id of the page',
430 ' timestamp - Adds the timestamp of the edit',
431 ' comment - Adds the comment of the edit',
432 ' parsedcomment - Adds the parsed comment of the edit',
433 ' size - Adds the size of the page',
434 ' flags - Adds flags of the edit',
435 ' patrolled - Tags patrolled edits',
436 ' tags - Lists tags for the edit',
437 ),
438 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
439 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown", ),
440 'tag' => 'Only list revisions tagged with this tag',
441 );
442 }
443
444 public function getDescription() {
445 return 'Get all edits by a user';
446 }
447
448 public function getPossibleErrors() {
449 return array_merge( parent::getPossibleErrors(), array(
450 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
451 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
452 array( 'show' ),
453 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
454 ) );
455 }
456
457 protected function getExamples() {
458 return array(
459 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
460 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
461 );
462 }
463
464 public function getVersion() {
465 return __CLASS__ . ': $Id$';
466 }
467 }