5 * Created on Oct 16, 2006
7 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
28 * This query action adds a list of a specified user's contributions to the output.
32 class ApiQueryContributions
extends ApiQueryBase
{
34 public function __construct( $query, $moduleName ) {
35 parent
::__construct( $query, $moduleName, 'uc' );
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;
43 public function execute() {
44 // Parse some parameters
45 $this->params
= $this->extractRequestParams();
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'] );
59 // Most of this code will use the 'contributions' group DB, which can map to slaves
60 // with extra user based indexes or partioning by user. The additional metadata
61 // queries should use a regular slave since the lookup pattern is not all by user.
62 $dbSecondary = $this->getDB(); // any random slave
64 // TODO: if the query is going only against the revision table, should this be done?
65 $this->selectNamedDB( 'contributions', DB_SLAVE
, 'contributions' );
67 if ( isset( $this->params
['userprefix'] ) ) {
68 $this->prefixMode
= true;
69 $this->multiUserMode
= true;
70 $this->userprefix
= $this->params
['userprefix'];
72 $this->usernames
= array();
73 if ( !is_array( $this->params
['user'] ) ) {
74 $this->params
['user'] = array( $this->params
['user'] );
76 if ( !count( $this->params
['user'] ) ) {
77 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
79 foreach ( $this->params
['user'] as $u ) {
80 $this->prepareUsername( $u );
82 $this->prefixMode
= false;
83 $this->multiUserMode
= ( count( $this->params
['user'] ) > 1 );
86 $this->prepareQuery();
88 // Do the actual query.
89 $res = $this->select( __METHOD__
);
91 if ( $this->fld_sizediff
) {
93 foreach ( $res as $row ) {
94 if ( $row->rev_parent_id
) {
95 $revIds[] = $row->rev_parent_id
;
98 $this->parentLens
= Revision
::getParentLengths( $dbSecondary, $revIds );
99 $res->rewind(); // reset
102 // Initialise some variables
104 $limit = $this->params
['limit'];
107 foreach ( $res as $row ) {
108 if ( ++
$count > $limit ) {
109 // We've reached the one extra which shows that there are
110 // additional pages to be had. Stop here...
111 if ( $this->multiUserMode
) {
112 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
114 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
119 $vals = $this->extractRowInfo( $row );
120 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
122 if ( $this->multiUserMode
) {
123 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
125 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
) );
131 $this->getResult()->setIndexedTagName_internal(
132 array( 'query', $this->getModuleName() ),
138 * Validate the 'user' parameter and set the value to compare
139 * against `revision`.`rev_user_text`
141 * @param $user string
143 private function prepareUsername( $user ) {
144 if ( !is_null( $user ) && $user !== '' ) {
145 $name = User
::isIP( $user )
147 : User
::getCanonicalName( $user, 'valid' );
148 if ( $name === false ) {
149 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
151 $this->usernames
[] = $name;
154 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
159 * Prepares the query and returns the limit of rows requested
161 private function prepareQuery() {
162 // We're after the revision table, and the corresponding page
163 // row for anything we retrieve. We may also need the
164 // recentchanges row and/or tag summary row.
165 $user = $this->getUser();
166 $tables = array( 'page', 'revision' ); // Order may change
167 $this->addWhere( 'page_id=rev_page' );
169 // Handle continue parameter
170 if ( $this->multiUserMode
&& !is_null( $this->params
['continue'] ) ) {
171 $continue = explode( '|', $this->params
['continue'] );
172 $this->dieContinueUsageIf( count( $continue ) != 2 );
173 $db = $this->getDB();
174 $encUser = $db->addQuotes( $continue[0] );
175 $encTS = $db->addQuotes( $db->timestamp( $continue[1] ) );
176 $op = ( $this->params
['dir'] == 'older' ?
'<' : '>' );
178 "rev_user_text $op $encUser OR " .
179 "(rev_user_text = $encUser AND " .
180 "rev_timestamp $op= $encTS)"
184 // Don't include any revisions where we're not supposed to be able to
186 if ( !$user->isAllowed( 'deletedhistory' ) ) {
187 $bitmask = Revision
::DELETED_USER
;
188 } elseif ( !$user->isAllowed( 'suppressrevision' ) ) {
189 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
194 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
197 // We only want pages by the specified users.
198 if ( $this->prefixMode
) {
199 $this->addWhere( 'rev_user_text' .
200 $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
202 $this->addWhereFld( 'rev_user_text', $this->usernames
);
204 // ... and in the specified timeframe.
205 // Ensure the same sort order for rev_user_text and rev_timestamp
206 // so our query is indexed
207 if ( $this->multiUserMode
) {
208 $this->addWhereRange( 'rev_user_text', $this->params
['dir'], null, null );
210 $this->addTimestampWhereRange( 'rev_timestamp',
211 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
212 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
214 $show = $this->params
['show'];
215 if ( $this->params
['toponly'] ) { // deprecated/old param
218 if ( !is_null( $show ) ) {
219 $show = array_flip( $show );
221 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
222 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
223 ||
( isset( $show['top'] ) && isset( $show['!top'] ) )
224 ||
( isset( $show['new'] ) && isset( $show['!new'] ) )
226 $this->dieUsageMsg( 'show' );
229 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
230 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
231 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
232 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
233 $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
234 $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
235 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
236 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
238 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
239 $index = array( 'revision' => 'usertext_timestamp' );
241 // Mandatory fields: timestamp allows request continuation
242 // ns+title checks if the user has access rights for this page
243 // user_text is necessary if multiple users were specified
244 $this->addFields( array(
253 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
256 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
258 'You need the patrol right to request the patrolled flag',
263 // Use a redundant join condition on both
264 // timestamp and ID so we can use the timestamp
266 $index['recentchanges'] = 'rc_user_text';
267 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
268 // Put the tables in the right order for
270 $tables = array( 'revision', 'recentchanges', 'page' );
271 $this->addOption( 'STRAIGHT_JOIN' );
272 $this->addWhere( 'rc_user_text=rev_user_text' );
273 $this->addWhere( 'rc_timestamp=rev_timestamp' );
274 $this->addWhere( 'rc_this_oldid=rev_id' );
276 $tables[] = 'recentchanges';
277 $this->addJoinConds( array( 'recentchanges' => array(
279 'rc_user_text=rev_user_text',
280 'rc_timestamp=rev_timestamp',
281 'rc_this_oldid=rev_id' ) ) ) );
285 $this->addTables( $tables );
286 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
287 $this->addFieldsIf( 'rev_id', $this->fld_ids ||
$this->fld_flags
);
288 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
289 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
290 $this->addFieldsIf( 'rev_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
291 $this->addFieldsIf( 'rev_len', $this->fld_size ||
$this->fld_sizediff
);
292 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
293 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags ||
$this->fld_sizediff ||
$this->fld_ids
);
294 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
296 if ( $this->fld_tags
) {
297 $this->addTables( 'tag_summary' );
299 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
301 $this->addFields( 'ts_tags' );
304 if ( isset( $this->params
['tag'] ) ) {
305 $this->addTables( 'change_tag' );
307 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
309 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
312 $this->addOption( 'USE INDEX', $index );
316 * Extract fields from the database row and append them to a result array
321 private function extractRowInfo( $row ) {
325 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
326 $vals['texthidden'] = '';
330 // Any rows where we can't view the user were filtered out in the query.
331 $vals['userid'] = $row->rev_user
;
332 $vals['user'] = $row->rev_user_text
;
333 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
334 $vals['userhidden'] = '';
337 if ( $this->fld_ids
) {
338 $vals['pageid'] = intval( $row->rev_page
);
339 $vals['revid'] = intval( $row->rev_id
);
340 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
342 if ( !is_null( $row->rev_parent_id
) ) {
343 $vals['parentid'] = intval( $row->rev_parent_id
);
347 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
349 if ( $this->fld_title
) {
350 ApiQueryBase
::addTitleInfo( $vals, $title );
353 if ( $this->fld_timestamp
) {
354 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
357 if ( $this->fld_flags
) {
358 if ( $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
) ) {
361 if ( $row->rev_minor_edit
) {
364 if ( $row->page_latest
== $row->rev_id
) {
369 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->rev_comment
) ) {
370 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
371 $vals['commenthidden'] = '';
375 $userCanView = Revision
::userCanBitfield(
377 Revision
::DELETED_COMMENT
, $this->getUser()
380 if ( $userCanView ) {
381 if ( $this->fld_comment
) {
382 $vals['comment'] = $row->rev_comment
;
385 if ( $this->fld_parsedcomment
) {
386 $vals['parsedcomment'] = Linker
::formatComment( $row->rev_comment
, $title );
391 if ( $this->fld_patrolled
&& $row->rc_patrolled
) {
392 $vals['patrolled'] = '';
395 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
396 $vals['size'] = intval( $row->rev_len
);
399 if ( $this->fld_sizediff
400 && !is_null( $row->rev_len
)
401 && !is_null( $row->rev_parent_id
)
403 $parentLen = isset( $this->parentLens
[$row->rev_parent_id
] )
404 ?
$this->parentLens
[$row->rev_parent_id
]
406 $vals['sizediff'] = intval( $row->rev_len
- $parentLen );
409 if ( $this->fld_tags
) {
410 if ( $row->ts_tags
) {
411 $tags = explode( ',', $row->ts_tags
);
412 $this->getResult()->setIndexedTagName( $tags, 'tag' );
413 $vals['tags'] = $tags;
415 $vals['tags'] = array();
419 if ( $anyHidden && $row->rev_deleted
& Revision
::DELETED_RESTRICTED
) {
420 $vals['suppressed'] = '';
426 private function continueStr( $row ) {
427 return $row->rev_user_text
. '|' .
428 wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
431 public function getCacheMode( $params ) {
432 // This module provides access to deleted revisions and patrol flags if
433 // the requester is logged in
434 return 'anon-public-user-private';
437 public function getAllowedParams() {
440 ApiBase
::PARAM_DFLT
=> 10,
441 ApiBase
::PARAM_TYPE
=> 'limit',
442 ApiBase
::PARAM_MIN
=> 1,
443 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
444 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
447 ApiBase
::PARAM_TYPE
=> 'timestamp'
450 ApiBase
::PARAM_TYPE
=> 'timestamp'
454 ApiBase
::PARAM_ISMULTI
=> true
456 'userprefix' => null,
458 ApiBase
::PARAM_DFLT
=> 'older',
459 ApiBase
::PARAM_TYPE
=> array(
464 'namespace' => array(
465 ApiBase
::PARAM_ISMULTI
=> true,
466 ApiBase
::PARAM_TYPE
=> 'namespace'
469 ApiBase
::PARAM_ISMULTI
=> true,
470 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
471 ApiBase
::PARAM_TYPE
=> array(
485 ApiBase
::PARAM_ISMULTI
=> true,
486 ApiBase
::PARAM_TYPE
=> array(
499 ApiBase
::PARAM_DFLT
=> false,
500 ApiBase
::PARAM_DEPRECATED
=> true,
505 public function getParamDescription() {
507 $p = $this->getModulePrefix();
510 'limit' => 'The maximum number of contributions to return',
511 'start' => 'The start timestamp to return from',
512 'end' => 'The end timestamp to return to',
513 'continue' => 'When more results are available, use this to continue',
514 'user' => 'The users to retrieve contributions for',
515 'userprefix' => array(
516 "Retrieve contributions for all users whose names begin with this value.",
517 "Overrides {$p}user",
519 'dir' => $this->getDirectionDescription( $p ),
520 'namespace' => 'Only list contributions in these namespaces',
522 'Include additional pieces of information',
523 ' ids - Adds the page ID and revision ID',
524 ' title - Adds the title and namespace ID of the page',
525 ' timestamp - Adds the timestamp of the edit',
526 ' comment - Adds the comment of the edit',
527 ' parsedcomment - Adds the parsed comment of the edit',
528 ' size - Adds the new size of the edit',
529 ' sizediff - Adds the size delta of the edit against its parent',
530 ' flags - Adds flags of the edit',
531 ' patrolled - Tags patrolled edits',
532 ' tags - Lists tags for the edit',
535 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
536 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
537 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
539 'tag' => 'Only list revisions tagged with this tag',
540 'toponly' => 'Only list changes which are the latest revision',
544 public function getResultProperties() {
547 'userid' => 'integer',
549 'userhidden' => 'boolean'
552 'pageid' => 'integer',
553 'revid' => 'integer',
555 ApiBase
::PROP_TYPE
=> 'integer',
556 ApiBase
::PROP_NULLABLE
=> true
563 'timestamp' => array(
564 'timestamp' => 'timestamp'
568 'minor' => 'boolean',
572 'commenthidden' => 'boolean',
574 ApiBase
::PROP_TYPE
=> 'string',
575 ApiBase
::PROP_NULLABLE
=> true
578 'parsedcomment' => array(
579 'commenthidden' => 'boolean',
580 'parsedcomment' => array(
581 ApiBase
::PROP_TYPE
=> 'string',
582 ApiBase
::PROP_NULLABLE
=> true
585 'patrolled' => array(
586 'patrolled' => 'boolean'
590 ApiBase
::PROP_TYPE
=> 'integer',
591 ApiBase
::PROP_NULLABLE
=> true
596 ApiBase
::PROP_TYPE
=> 'integer',
597 ApiBase
::PROP_NULLABLE
=> true
603 public function getDescription() {
604 return 'Get all edits by a user.';
607 public function getPossibleErrors() {
608 return array_merge( parent
::getPossibleErrors(), array(
609 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
610 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
613 'code' => 'permissiondenied',
614 'info' => 'You need the patrol right to request the patrolled flag'
619 public function getExamples() {
621 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
622 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
626 public function getHelpUrls() {
627 return 'https://www.mediawiki.org/wiki/API:Usercontribs';