Merge "Updated result properties in paraminfo API"
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2 /**
3 *
4 *
5 * Created on Oct 16, 2006
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 /**
28 * This query action adds a list of a specified user's contributions to the output.
29 *
30 * @ingroup API
31 */
32 class ApiQueryContributions extends ApiQueryBase {
33
34 public function __construct( $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'uc' );
36 }
37
38 private $params, $prefixMode, $userprefix, $multiUserMode, $usernames, $parentLens;
39 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
40 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
41 $fld_patrolled = false, $fld_tags = false, $fld_size = false, $fld_sizediff = false;
42
43 public function execute() {
44 // Parse some parameters
45 $this->params = $this->extractRequestParams();
46
47 $prop = array_flip( $this->params['prop'] );
48 $this->fld_ids = isset( $prop['ids'] );
49 $this->fld_title = isset( $prop['title'] );
50 $this->fld_comment = isset( $prop['comment'] );
51 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
52 $this->fld_size = isset( $prop['size'] );
53 $this->fld_sizediff = isset( $prop['sizediff'] );
54 $this->fld_flags = isset( $prop['flags'] );
55 $this->fld_timestamp = isset( $prop['timestamp'] );
56 $this->fld_patrolled = isset( $prop['patrolled'] );
57 $this->fld_tags = isset( $prop['tags'] );
58
59 // TODO: if the query is going only against the revision table, should this be done?
60 $this->selectNamedDB( 'contributions', DB_SLAVE, 'contributions' );
61
62 if ( isset( $this->params['userprefix'] ) ) {
63 $this->prefixMode = true;
64 $this->multiUserMode = true;
65 $this->userprefix = $this->params['userprefix'];
66 } else {
67 $this->usernames = array();
68 if ( !is_array( $this->params['user'] ) ) {
69 $this->params['user'] = array( $this->params['user'] );
70 }
71 if ( !count( $this->params['user'] ) ) {
72 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
73 }
74 foreach ( $this->params['user'] as $u ) {
75 $this->prepareUsername( $u );
76 }
77 $this->prefixMode = false;
78 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
79 }
80
81 $this->prepareQuery();
82
83 // Do the actual query.
84 $res = $this->select( __METHOD__ );
85
86 if( $this->fld_sizediff ) {
87 $revIds = array();
88 foreach ( $res as $row ) {
89 if( $row->rev_parent_id ) {
90 $revIds[] = $row->rev_parent_id;
91 }
92 }
93 $this->parentLens = Revision::getParentLengths( $this->getDB(), $revIds );
94 $res->rewind(); // reset
95 }
96
97 // Initialise some variables
98 $count = 0;
99 $limit = $this->params['limit'];
100
101 // Fetch each row
102 foreach ( $res as $row ) {
103 if ( ++ $count > $limit ) {
104 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
105 if ( $this->multiUserMode ) {
106 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
107 } else {
108 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
109 }
110 break;
111 }
112
113 $vals = $this->extractRowInfo( $row );
114 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
115 if ( !$fit ) {
116 if ( $this->multiUserMode ) {
117 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
118 } else {
119 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
120 }
121 break;
122 }
123 }
124
125 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
126 }
127
128 /**
129 * Validate the 'user' parameter and set the value to compare
130 * against `revision`.`rev_user_text`
131 *
132 * @param $user string
133 */
134 private function prepareUsername( $user ) {
135 if ( !is_null( $user ) && $user !== '' ) {
136 $name = User::isIP( $user )
137 ? $user
138 : User::getCanonicalName( $user, 'valid' );
139 if ( $name === false ) {
140 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
141 } else {
142 $this->usernames[] = $name;
143 }
144 } else {
145 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
146 }
147 }
148
149 /**
150 * Prepares the query and returns the limit of rows requested
151 */
152 private function prepareQuery() {
153 // We're after the revision table, and the corresponding page
154 // row for anything we retrieve. We may also need the
155 // recentchanges row and/or tag summary row.
156 $user = $this->getUser();
157 $tables = array( 'page', 'revision' ); // Order may change
158 $this->addWhere( 'page_id=rev_page' );
159
160 // Handle continue parameter
161 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
162 $continue = explode( '|', $this->params['continue'] );
163 if ( count( $continue ) != 2 ) {
164 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
165 'value returned by the previous query', '_badcontinue' );
166 }
167 $db = $this->getDB();
168 $encUser = $db->addQuotes( $continue[0] );
169 $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) );
170 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
171 $this->addWhere(
172 "rev_user_text $op $encUser OR " .
173 "(rev_user_text = $encUser AND " .
174 "rev_timestamp $op= $encTS)"
175 );
176 }
177
178 if ( !$user->isAllowed( 'hideuser' ) ) {
179 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
180 }
181 // We only want pages by the specified users.
182 if ( $this->prefixMode ) {
183 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
184 } else {
185 $this->addWhereFld( 'rev_user_text', $this->usernames );
186 }
187 // ... and in the specified timeframe.
188 // Ensure the same sort order for rev_user_text and rev_timestamp
189 // so our query is indexed
190 if ( $this->multiUserMode ) {
191 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
192 }
193 $this->addTimestampWhereRange( 'rev_timestamp',
194 $this->params['dir'], $this->params['start'], $this->params['end'] );
195 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
196
197 $show = $this->params['show'];
198 if ( !is_null( $show ) ) {
199 $show = array_flip( $show );
200 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
201 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) ) {
202 $this->dieUsageMsg( 'show' );
203 }
204
205 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
206 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
207 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
208 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
209 }
210 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
211 $index = array( 'revision' => 'usertext_timestamp' );
212
213 // Mandatory fields: timestamp allows request continuation
214 // ns+title checks if the user has access rights for this page
215 // user_text is necessary if multiple users were specified
216 $this->addFields( array(
217 'rev_timestamp',
218 'page_namespace',
219 'page_title',
220 'rev_user',
221 'rev_user_text',
222 'rev_deleted'
223 ) );
224
225 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
226 $this->fld_patrolled ) {
227 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
228 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
229 }
230
231 // Use a redundant join condition on both
232 // timestamp and ID so we can use the timestamp
233 // index
234 $index['recentchanges'] = 'rc_user_text';
235 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
236 // Put the tables in the right order for
237 // STRAIGHT_JOIN
238 $tables = array( 'revision', 'recentchanges', 'page' );
239 $this->addOption( 'STRAIGHT_JOIN' );
240 $this->addWhere( 'rc_user_text=rev_user_text' );
241 $this->addWhere( 'rc_timestamp=rev_timestamp' );
242 $this->addWhere( 'rc_this_oldid=rev_id' );
243 } else {
244 $tables[] = 'recentchanges';
245 $this->addJoinConds( array( 'recentchanges' => array(
246 'LEFT JOIN', array(
247 'rc_user_text=rev_user_text',
248 'rc_timestamp=rev_timestamp',
249 'rc_this_oldid=rev_id' ) ) ) );
250 }
251 }
252
253 $this->addTables( $tables );
254 $this->addFieldsIf( 'rev_page', $this->fld_ids );
255 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
256 $this->addFieldsIf( 'page_latest', $this->fld_flags );
257 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
258 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
259 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
260 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
261 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff );
262 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
263
264 if ( $this->fld_tags ) {
265 $this->addTables( 'tag_summary' );
266 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
267 $this->addFields( 'ts_tags' );
268 }
269
270 if ( isset( $this->params['tag'] ) ) {
271 $this->addTables( 'change_tag' );
272 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
273 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
274 global $wgOldChangeTagsIndex;
275 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
276 }
277
278 if ( $this->params['toponly'] ) {
279 $this->addWhere( 'rev_id = page_latest' );
280 }
281
282 $this->addOption( 'USE INDEX', $index );
283 }
284
285 /**
286 * Extract fields from the database row and append them to a result array
287 *
288 * @param $row
289 * @return array
290 */
291 private function extractRowInfo( $row ) {
292 $vals = array();
293
294 $vals['userid'] = $row->rev_user;
295 $vals['user'] = $row->rev_user_text;
296 if ( $row->rev_deleted & Revision::DELETED_USER ) {
297 $vals['userhidden'] = '';
298 }
299 if ( $this->fld_ids ) {
300 $vals['pageid'] = intval( $row->rev_page );
301 $vals['revid'] = intval( $row->rev_id );
302 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
303 }
304
305 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
306
307 if ( $this->fld_title ) {
308 ApiQueryBase::addTitleInfo( $vals, $title );
309 }
310
311 if ( $this->fld_timestamp ) {
312 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
313 }
314
315 if ( $this->fld_flags ) {
316 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
317 $vals['new'] = '';
318 }
319 if ( $row->rev_minor_edit ) {
320 $vals['minor'] = '';
321 }
322 if ( $row->page_latest == $row->rev_id ) {
323 $vals['top'] = '';
324 }
325 }
326
327 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
328 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
329 $vals['commenthidden'] = '';
330 } else {
331 if ( $this->fld_comment ) {
332 $vals['comment'] = $row->rev_comment;
333 }
334
335 if ( $this->fld_parsedcomment ) {
336 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
337 }
338 }
339 }
340
341 if ( $this->fld_patrolled && $row->rc_patrolled ) {
342 $vals['patrolled'] = '';
343 }
344
345 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
346 $vals['size'] = intval( $row->rev_len );
347 }
348
349 if ( $this->fld_sizediff && !is_null( $row->rev_len ) && !is_null( $row->rev_parent_id ) ) {
350 $parentLen = isset( $this->parentLens[$row->rev_parent_id] ) ? $this->parentLens[$row->rev_parent_id] : 0;
351 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
352 }
353
354 if ( $this->fld_tags ) {
355 if ( $row->ts_tags ) {
356 $tags = explode( ',', $row->ts_tags );
357 $this->getResult()->setIndexedTagName( $tags, 'tag' );
358 $vals['tags'] = $tags;
359 } else {
360 $vals['tags'] = array();
361 }
362 }
363
364 return $vals;
365 }
366
367 private function continueStr( $row ) {
368 return $row->rev_user_text . '|' .
369 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
370 }
371
372 public function getCacheMode( $params ) {
373 // This module provides access to deleted revisions and patrol flags if
374 // the requester is logged in
375 return 'anon-public-user-private';
376 }
377
378 public function getAllowedParams() {
379 return array(
380 'limit' => array(
381 ApiBase::PARAM_DFLT => 10,
382 ApiBase::PARAM_TYPE => 'limit',
383 ApiBase::PARAM_MIN => 1,
384 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
385 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
386 ),
387 'start' => array(
388 ApiBase::PARAM_TYPE => 'timestamp'
389 ),
390 'end' => array(
391 ApiBase::PARAM_TYPE => 'timestamp'
392 ),
393 'continue' => null,
394 'user' => array(
395 ApiBase::PARAM_ISMULTI => true
396 ),
397 'userprefix' => null,
398 'dir' => array(
399 ApiBase::PARAM_DFLT => 'older',
400 ApiBase::PARAM_TYPE => array(
401 'newer',
402 'older'
403 )
404 ),
405 'namespace' => array(
406 ApiBase::PARAM_ISMULTI => true,
407 ApiBase::PARAM_TYPE => 'namespace'
408 ),
409 'prop' => array(
410 ApiBase::PARAM_ISMULTI => true,
411 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
412 ApiBase::PARAM_TYPE => array(
413 'ids',
414 'title',
415 'timestamp',
416 'comment',
417 'parsedcomment',
418 'size',
419 'sizediff',
420 'flags',
421 'patrolled',
422 'tags'
423 )
424 ),
425 'show' => array(
426 ApiBase::PARAM_ISMULTI => true,
427 ApiBase::PARAM_TYPE => array(
428 'minor',
429 '!minor',
430 'patrolled',
431 '!patrolled',
432 )
433 ),
434 'tag' => null,
435 'toponly' => false,
436 );
437 }
438
439 public function getParamDescription() {
440 global $wgRCMaxAge;
441 $p = $this->getModulePrefix();
442 return array(
443 'limit' => 'The maximum number of contributions to return',
444 'start' => 'The start timestamp to return from',
445 'end' => 'The end timestamp to return to',
446 'continue' => 'When more results are available, use this to continue',
447 'user' => 'The users to retrieve contributions for',
448 'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
449 'dir' => $this->getDirectionDescription( $p ),
450 'namespace' => 'Only list contributions in these namespaces',
451 'prop' => array(
452 'Include additional pieces of information',
453 ' ids - Adds the page ID and revision ID',
454 ' title - Adds the title and namespace ID of the page',
455 ' timestamp - Adds the timestamp of the edit',
456 ' comment - Adds the comment of the edit',
457 ' parsedcomment - Adds the parsed comment of the edit',
458 ' size - Adds the new size of the edit',
459 ' sizediff - Adds the size delta of the edit against its parent',
460 ' flags - Adds flags of the edit',
461 ' patrolled - Tags patrolled edits',
462 ' tags - Lists tags for the edit',
463 ),
464 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
465 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than \$wgRCMaxAge ($wgRCMaxAge) won't be shown", ),
466 'tag' => 'Only list revisions tagged with this tag',
467 'toponly' => 'Only list changes which are the latest revision',
468 );
469 }
470
471 public function getResultProperties() {
472 return array(
473 '' => array(
474 'userid' => 'integer',
475 'user' => 'string',
476 'userhidden' => 'boolean'
477 ),
478 'ids' => array(
479 'pageid' => 'integer',
480 'revid' => 'integer'
481 ),
482 'title' => array(
483 'ns' => 'namespace',
484 'title' => 'string'
485 ),
486 'timestamp' => array(
487 'timestamp' => 'timestamp'
488 ),
489 'flags' => array(
490 'new' => 'boolean',
491 'minor' => 'boolean',
492 'top' => 'boolean'
493 ),
494 'comment' => array(
495 'commenthidden' => 'boolean',
496 'comment' => array(
497 ApiBase::PROP_TYPE => 'string',
498 ApiBase::PROP_NULLABLE => true
499 )
500 ),
501 'parsedcomment' => array(
502 'commenthidden' => 'boolean',
503 'parsedcomment' => array(
504 ApiBase::PROP_TYPE => 'string',
505 ApiBase::PROP_NULLABLE => true
506 )
507 ),
508 'patrolled' => array(
509 'patrolled' => 'boolean'
510 ),
511 'size' => array(
512 'size' => array(
513 ApiBase::PROP_TYPE => 'integer',
514 ApiBase::PROP_NULLABLE => true
515 )
516 ),
517 'sizediff' => array(
518 'sizediff' => array(
519 ApiBase::PROP_TYPE => 'integer',
520 ApiBase::PROP_NULLABLE => true
521 )
522 )
523 );
524 }
525
526 public function getDescription() {
527 return 'Get all edits by a user';
528 }
529
530 public function getPossibleErrors() {
531 return array_merge( parent::getPossibleErrors(), array(
532 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
533 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
534 array( 'show' ),
535 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
536 ) );
537 }
538
539 public function getExamples() {
540 return array(
541 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
542 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
543 );
544 }
545
546 public function getHelpUrls() {
547 return 'https://www.mediawiki.org/wiki/API:Usercontribs';
548 }
549
550 public function getVersion() {
551 return __CLASS__ . ': $Id$';
552 }
553 }