Merge "Removed refreshLinks2 comment"
[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
105 // additional pages to be had. Stop here...
106 if ( $this->multiUserMode ) {
107 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
108 } else {
109 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
110 }
111 break;
112 }
113
114 $vals = $this->extractRowInfo( $row );
115 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
116 if ( !$fit ) {
117 if ( $this->multiUserMode ) {
118 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
119 } else {
120 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
121 }
122 break;
123 }
124 }
125
126 $this->getResult()->setIndexedTagName_internal(
127 array( 'query', $this->getModuleName() ),
128 'item'
129 );
130 }
131
132 /**
133 * Validate the 'user' parameter and set the value to compare
134 * against `revision`.`rev_user_text`
135 *
136 * @param $user string
137 */
138 private function prepareUsername( $user ) {
139 if ( !is_null( $user ) && $user !== '' ) {
140 $name = User::isIP( $user )
141 ? $user
142 : User::getCanonicalName( $user, 'valid' );
143 if ( $name === false ) {
144 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
145 } else {
146 $this->usernames[] = $name;
147 }
148 } else {
149 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
150 }
151 }
152
153 /**
154 * Prepares the query and returns the limit of rows requested
155 */
156 private function prepareQuery() {
157 // We're after the revision table, and the corresponding page
158 // row for anything we retrieve. We may also need the
159 // recentchanges row and/or tag summary row.
160 $user = $this->getUser();
161 $tables = array( 'page', 'revision' ); // Order may change
162 $this->addWhere( 'page_id=rev_page' );
163
164 // Handle continue parameter
165 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
166 $continue = explode( '|', $this->params['continue'] );
167 $this->dieContinueUsageIf( count( $continue ) != 2 );
168 $db = $this->getDB();
169 $encUser = $db->addQuotes( $continue[0] );
170 $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) );
171 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
172 $this->addWhere(
173 "rev_user_text $op $encUser OR " .
174 "(rev_user_text = $encUser AND " .
175 "rev_timestamp $op= $encTS)"
176 );
177 }
178
179 // Don't include any revisions where we're not supposed to be able to
180 // see the username.
181 if ( !$user->isAllowed( 'deletedhistory' ) ) {
182 $bitmask = Revision::DELETED_USER;
183 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
184 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
185 } else {
186 $bitmask = 0;
187 }
188 if ( $bitmask ) {
189 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
190 }
191
192 // We only want pages by the specified users.
193 if ( $this->prefixMode ) {
194 $this->addWhere( 'rev_user_text' .
195 $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
196 } else {
197 $this->addWhereFld( 'rev_user_text', $this->usernames );
198 }
199 // ... and in the specified timeframe.
200 // Ensure the same sort order for rev_user_text and rev_timestamp
201 // so our query is indexed
202 if ( $this->multiUserMode ) {
203 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
204 }
205 $this->addTimestampWhereRange( 'rev_timestamp',
206 $this->params['dir'], $this->params['start'], $this->params['end'] );
207 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
208
209 $show = $this->params['show'];
210 if ( $this->params['toponly'] ) { // deprecated/old param
211 $show[] = 'top';
212 }
213 if ( !is_null( $show ) ) {
214 $show = array_flip( $show );
215
216 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
217 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
218 || ( isset( $show['top'] ) && isset( $show['!top'] ) )
219 || ( isset( $show['new'] ) && isset( $show['!new'] ) )
220 ) {
221 $this->dieUsageMsg( 'show' );
222 }
223
224 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
225 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
226 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
227 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
228 $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
229 $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
230 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
231 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
232 }
233 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
234 $index = array( 'revision' => 'usertext_timestamp' );
235
236 // Mandatory fields: timestamp allows request continuation
237 // ns+title checks if the user has access rights for this page
238 // user_text is necessary if multiple users were specified
239 $this->addFields( array(
240 'rev_timestamp',
241 'page_namespace',
242 'page_title',
243 'rev_user',
244 'rev_user_text',
245 'rev_deleted'
246 ) );
247
248 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
249 $this->fld_patrolled
250 ) {
251 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
252 $this->dieUsage(
253 'You need the patrol right to request the patrolled flag',
254 'permissiondenied'
255 );
256 }
257
258 // Use a redundant join condition on both
259 // timestamp and ID so we can use the timestamp
260 // index
261 $index['recentchanges'] = 'rc_user_text';
262 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
263 // Put the tables in the right order for
264 // STRAIGHT_JOIN
265 $tables = array( 'revision', 'recentchanges', 'page' );
266 $this->addOption( 'STRAIGHT_JOIN' );
267 $this->addWhere( 'rc_user_text=rev_user_text' );
268 $this->addWhere( 'rc_timestamp=rev_timestamp' );
269 $this->addWhere( 'rc_this_oldid=rev_id' );
270 } else {
271 $tables[] = 'recentchanges';
272 $this->addJoinConds( array( 'recentchanges' => array(
273 'LEFT JOIN', array(
274 'rc_user_text=rev_user_text',
275 'rc_timestamp=rev_timestamp',
276 'rc_this_oldid=rev_id' ) ) ) );
277 }
278 }
279
280 $this->addTables( $tables );
281 $this->addFieldsIf( 'rev_page', $this->fld_ids );
282 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
283 $this->addFieldsIf( 'page_latest', $this->fld_flags );
284 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
285 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
286 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
287 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
288 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff || $this->fld_ids );
289 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
290
291 if ( $this->fld_tags ) {
292 $this->addTables( 'tag_summary' );
293 $this->addJoinConds(
294 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
295 );
296 $this->addFields( 'ts_tags' );
297 }
298
299 if ( isset( $this->params['tag'] ) ) {
300 $this->addTables( 'change_tag' );
301 $this->addJoinConds(
302 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
303 );
304 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
305 }
306
307 $this->addOption( 'USE INDEX', $index );
308 }
309
310 /**
311 * Extract fields from the database row and append them to a result array
312 *
313 * @param $row
314 * @return array
315 */
316 private function extractRowInfo( $row ) {
317 $vals = array();
318 $anyHidden = false;
319
320 if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
321 $vals['texthidden'] = '';
322 $anyHidden = true;
323 }
324
325 // Any rows where we can't view the user were filtered out in the query.
326 $vals['userid'] = $row->rev_user;
327 $vals['user'] = $row->rev_user_text;
328 if ( $row->rev_deleted & Revision::DELETED_USER ) {
329 $vals['userhidden'] = '';
330 $anyHidden = true;
331 }
332 if ( $this->fld_ids ) {
333 $vals['pageid'] = intval( $row->rev_page );
334 $vals['revid'] = intval( $row->rev_id );
335 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
336
337 if ( !is_null( $row->rev_parent_id ) ) {
338 $vals['parentid'] = intval( $row->rev_parent_id );
339 }
340 }
341
342 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
343
344 if ( $this->fld_title ) {
345 ApiQueryBase::addTitleInfo( $vals, $title );
346 }
347
348 if ( $this->fld_timestamp ) {
349 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
350 }
351
352 if ( $this->fld_flags ) {
353 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
354 $vals['new'] = '';
355 }
356 if ( $row->rev_minor_edit ) {
357 $vals['minor'] = '';
358 }
359 if ( $row->page_latest == $row->rev_id ) {
360 $vals['top'] = '';
361 }
362 }
363
364 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
365 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
366 $vals['commenthidden'] = '';
367 $anyHidden = true;
368 }
369
370 $userCanView = Revision::userCanBitfield(
371 $row->rev_deleted,
372 Revision::DELETED_COMMENT, $this->getUser()
373 );
374
375 if ( $userCanView ) {
376 if ( $this->fld_comment ) {
377 $vals['comment'] = $row->rev_comment;
378 }
379
380 if ( $this->fld_parsedcomment ) {
381 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
382 }
383 }
384 }
385
386 if ( $this->fld_patrolled && $row->rc_patrolled ) {
387 $vals['patrolled'] = '';
388 }
389
390 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
391 $vals['size'] = intval( $row->rev_len );
392 }
393
394 if ( $this->fld_sizediff
395 && !is_null( $row->rev_len )
396 && !is_null( $row->rev_parent_id )
397 ) {
398 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
399 ? $this->parentLens[$row->rev_parent_id]
400 : 0;
401 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
402 }
403
404 if ( $this->fld_tags ) {
405 if ( $row->ts_tags ) {
406 $tags = explode( ',', $row->ts_tags );
407 $this->getResult()->setIndexedTagName( $tags, 'tag' );
408 $vals['tags'] = $tags;
409 } else {
410 $vals['tags'] = array();
411 }
412 }
413
414 if ( $anyHidden && $row->rev_deleted & Revision::DELETED_RESTRICTED ) {
415 $vals['suppressed'] = '';
416 }
417
418 return $vals;
419 }
420
421 private function continueStr( $row ) {
422 return $row->rev_user_text . '|' .
423 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
424 }
425
426 public function getCacheMode( $params ) {
427 // This module provides access to deleted revisions and patrol flags if
428 // the requester is logged in
429 return 'anon-public-user-private';
430 }
431
432 public function getAllowedParams() {
433 return array(
434 'limit' => array(
435 ApiBase::PARAM_DFLT => 10,
436 ApiBase::PARAM_TYPE => 'limit',
437 ApiBase::PARAM_MIN => 1,
438 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
439 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
440 ),
441 'start' => array(
442 ApiBase::PARAM_TYPE => 'timestamp'
443 ),
444 'end' => array(
445 ApiBase::PARAM_TYPE => 'timestamp'
446 ),
447 'continue' => null,
448 'user' => array(
449 ApiBase::PARAM_ISMULTI => true
450 ),
451 'userprefix' => null,
452 'dir' => array(
453 ApiBase::PARAM_DFLT => 'older',
454 ApiBase::PARAM_TYPE => array(
455 'newer',
456 'older'
457 )
458 ),
459 'namespace' => array(
460 ApiBase::PARAM_ISMULTI => true,
461 ApiBase::PARAM_TYPE => 'namespace'
462 ),
463 'prop' => array(
464 ApiBase::PARAM_ISMULTI => true,
465 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
466 ApiBase::PARAM_TYPE => array(
467 'ids',
468 'title',
469 'timestamp',
470 'comment',
471 'parsedcomment',
472 'size',
473 'sizediff',
474 'flags',
475 'patrolled',
476 'tags'
477 )
478 ),
479 'show' => array(
480 ApiBase::PARAM_ISMULTI => true,
481 ApiBase::PARAM_TYPE => array(
482 'minor',
483 '!minor',
484 'patrolled',
485 '!patrolled',
486 'top',
487 '!top',
488 'new',
489 '!new',
490 )
491 ),
492 'tag' => null,
493 'toponly' => array(
494 ApiBase::PARAM_DFLT => false,
495 ApiBase::PARAM_DEPRECATED => true,
496 ),
497 );
498 }
499
500 public function getParamDescription() {
501 global $wgRCMaxAge;
502 $p = $this->getModulePrefix();
503
504 return array(
505 'limit' => 'The maximum number of contributions to return',
506 'start' => 'The start timestamp to return from',
507 'end' => 'The end timestamp to return to',
508 'continue' => 'When more results are available, use this to continue',
509 'user' => 'The users to retrieve contributions for',
510 'userprefix' => array(
511 "Retrieve contributions for all users whose names begin with this value.",
512 "Overrides {$p}user",
513 ),
514 'dir' => $this->getDirectionDescription( $p ),
515 'namespace' => 'Only list contributions in these namespaces',
516 'prop' => array(
517 'Include additional pieces of information',
518 ' ids - Adds the page ID and revision ID',
519 ' title - Adds the title and namespace ID of the page',
520 ' timestamp - Adds the timestamp of the edit',
521 ' comment - Adds the comment of the edit',
522 ' parsedcomment - Adds the parsed comment of the edit',
523 ' size - Adds the new size of the edit',
524 ' sizediff - Adds the size delta of the edit against its parent',
525 ' flags - Adds flags of the edit',
526 ' patrolled - Tags patrolled edits',
527 ' tags - Lists tags for the edit',
528 ),
529 'show' => array(
530 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
531 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
532 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
533 ),
534 'tag' => 'Only list revisions tagged with this tag',
535 'toponly' => 'Only list changes which are the latest revision',
536 );
537 }
538
539 public function getResultProperties() {
540 return array(
541 '' => array(
542 'userid' => 'integer',
543 'user' => 'string',
544 'userhidden' => 'boolean'
545 ),
546 'ids' => array(
547 'pageid' => 'integer',
548 'revid' => 'integer',
549 'parentid' => array(
550 ApiBase::PROP_TYPE => 'integer',
551 ApiBase::PROP_NULLABLE => true
552 )
553 ),
554 'title' => array(
555 'ns' => 'namespace',
556 'title' => 'string'
557 ),
558 'timestamp' => array(
559 'timestamp' => 'timestamp'
560 ),
561 'flags' => array(
562 'new' => 'boolean',
563 'minor' => 'boolean',
564 'top' => 'boolean'
565 ),
566 'comment' => array(
567 'commenthidden' => 'boolean',
568 'comment' => array(
569 ApiBase::PROP_TYPE => 'string',
570 ApiBase::PROP_NULLABLE => true
571 )
572 ),
573 'parsedcomment' => array(
574 'commenthidden' => 'boolean',
575 'parsedcomment' => array(
576 ApiBase::PROP_TYPE => 'string',
577 ApiBase::PROP_NULLABLE => true
578 )
579 ),
580 'patrolled' => array(
581 'patrolled' => 'boolean'
582 ),
583 'size' => array(
584 'size' => array(
585 ApiBase::PROP_TYPE => 'integer',
586 ApiBase::PROP_NULLABLE => true
587 )
588 ),
589 'sizediff' => array(
590 'sizediff' => array(
591 ApiBase::PROP_TYPE => 'integer',
592 ApiBase::PROP_NULLABLE => true
593 )
594 )
595 );
596 }
597
598 public function getDescription() {
599 return 'Get all edits by a user';
600 }
601
602 public function getPossibleErrors() {
603 return array_merge( parent::getPossibleErrors(), array(
604 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
605 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
606 array( 'show' ),
607 array(
608 'code' => 'permissiondenied',
609 'info' => 'You need the patrol right to request the patrolled flag'
610 ),
611 ) );
612 }
613
614 public function getExamples() {
615 return array(
616 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
617 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
618 );
619 }
620
621 public function getHelpUrls() {
622 return 'https://www.mediawiki.org/wiki/API:Usercontribs';
623 }
624 }