3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
24 * This query action adds a list of a specified user's contributions to the output.
28 class ApiQueryContributions
extends ApiQueryBase
{
30 public function __construct( ApiQuery
$query, $moduleName ) {
31 parent
::__construct( $query, $moduleName, 'uc' );
34 private $params, $prefixMode, $userprefix, $multiUserMode, $idMode, $usernames, $userids,
35 $parentLens, $commentStore;
36 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
37 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
38 $fld_patrolled = false, $fld_tags = false, $fld_size = false, $fld_sizediff = false;
40 public function execute() {
41 // Parse some parameters
42 $this->params
= $this->extractRequestParams();
44 $this->commentStore
= CommentStore
::getStore();
46 $prop = array_flip( $this->params
['prop'] );
47 $this->fld_ids
= isset( $prop['ids'] );
48 $this->fld_title
= isset( $prop['title'] );
49 $this->fld_comment
= isset( $prop['comment'] );
50 $this->fld_parsedcomment
= isset( $prop['parsedcomment'] );
51 $this->fld_size
= isset( $prop['size'] );
52 $this->fld_sizediff
= isset( $prop['sizediff'] );
53 $this->fld_flags
= isset( $prop['flags'] );
54 $this->fld_timestamp
= isset( $prop['timestamp'] );
55 $this->fld_patrolled
= isset( $prop['patrolled'] );
56 $this->fld_tags
= isset( $prop['tags'] );
58 // Most of this code will use the 'contributions' group DB, which can map to replica DBs
59 // with extra user based indexes or partioning by user. The additional metadata
60 // queries should use a regular replica DB since the lookup pattern is not all by user.
61 $dbSecondary = $this->getDB(); // any random replica DB
63 // TODO: if the query is going only against the revision table, should this be done?
64 $this->selectNamedDB( 'contributions', DB_REPLICA
, 'contributions' );
66 $this->requireOnlyOneParameter( $this->params
, 'userprefix', 'userids', 'user' );
68 $this->idMode
= false;
69 if ( isset( $this->params
['userprefix'] ) ) {
70 $this->prefixMode
= true;
71 $this->multiUserMode
= true;
72 $this->userprefix
= $this->params
['userprefix'];
73 } elseif ( isset( $this->params
['userids'] ) ) {
76 if ( !count( $this->params
['userids'] ) ) {
77 $encParamName = $this->encodeParamName( 'userids' );
78 $this->dieWithError( [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName" );
81 foreach ( $this->params
['userids'] as $uid ) {
83 $this->dieWithError( [ 'apierror-invaliduserid', $uid ], 'invaliduserid' );
86 $this->userids
[] = $uid;
89 $this->prefixMode
= false;
90 $this->multiUserMode
= ( count( $this->params
['userids'] ) > 1 );
95 $this->usernames
= [];
96 if ( !count( $this->params
['user'] ) ) {
97 $encParamName = $this->encodeParamName( 'user' );
99 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
102 foreach ( $this->params
['user'] as $u ) {
104 $encParamName = $this->encodeParamName( 'user' );
106 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
110 if ( User
::isIP( $u ) ) {
112 $this->usernames
[] = $u;
114 $name = User
::getCanonicalName( $u, 'valid' );
115 if ( $name === false ) {
116 $encParamName = $this->encodeParamName( 'user' );
118 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $u ) ], "baduser_$encParamName"
121 $this->usernames
[] = $name;
124 $this->prefixMode
= false;
125 $this->multiUserMode
= ( count( $this->params
['user'] ) > 1 );
128 $dbr = $this->getDB();
129 $res = $dbr->select( 'user', 'user_id', [ 'user_name' => $this->usernames
], __METHOD__
);
130 foreach ( $res as $row ) {
131 $this->userids
[] = $row->user_id
;
133 $this->idMode
= count( $this->userids
) === count( $this->usernames
);
137 $this->prepareQuery();
140 // Do the actual query.
141 $res = $this->select( __METHOD__
, [], $hookData );
143 if ( $this->fld_sizediff
) {
145 foreach ( $res as $row ) {
146 if ( $row->rev_parent_id
) {
147 $revIds[] = $row->rev_parent_id
;
150 $this->parentLens
= Revision
::getParentLengths( $dbSecondary, $revIds );
151 $res->rewind(); // reset
154 // Initialise some variables
156 $limit = $this->params
['limit'];
159 foreach ( $res as $row ) {
160 if ( ++
$count > $limit ) {
161 // We've reached the one extra which shows that there are
162 // additional pages to be had. Stop here...
163 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
167 $vals = $this->extractRowInfo( $row );
168 $fit = $this->processRow( $row, $vals, $hookData ) &&
169 $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
171 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
176 $this->getResult()->addIndexedTagName(
177 [ 'query', $this->getModuleName() ],
183 * Prepares the query and returns the limit of rows requested
185 private function prepareQuery() {
186 // We're after the revision table, and the corresponding page
187 // row for anything we retrieve. We may also need the
188 // recentchanges row and/or tag summary row.
189 $user = $this->getUser();
190 $tables = [ 'page', 'revision' ]; // Order may change
191 $this->addWhere( 'page_id=rev_page' );
193 // Handle continue parameter
194 if ( !is_null( $this->params
['continue'] ) ) {
195 $continue = explode( '|', $this->params
['continue'] );
196 $db = $this->getDB();
197 if ( $this->multiUserMode
) {
198 $this->dieContinueUsageIf( count( $continue ) != 4 );
199 $modeFlag = array_shift( $continue );
200 $this->dieContinueUsageIf( !in_array( $modeFlag, [ 'id', 'name' ] ) );
201 if ( $this->idMode
&& $modeFlag === 'name' ) {
202 // The users were created since this query started, but we
203 // can't go back and change modes now. So just keep on with
205 $this->idMode
= false;
207 $this->dieContinueUsageIf( ( $modeFlag === 'id' ) !== $this->idMode
);
208 $userField = $this->idMode ?
'rev_user' : 'rev_user_text';
209 $encUser = $db->addQuotes( array_shift( $continue ) );
211 $this->dieContinueUsageIf( count( $continue ) != 2 );
213 $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
214 $encId = (int)$continue[1];
215 $this->dieContinueUsageIf( $encId != $continue[1] );
216 $op = ( $this->params
['dir'] == 'older' ?
'<' : '>' );
217 if ( $this->multiUserMode
) {
219 "$userField $op $encUser OR " .
220 "($userField = $encUser AND " .
221 "(rev_timestamp $op $encTS OR " .
222 "(rev_timestamp = $encTS AND " .
223 "rev_id $op= $encId)))"
227 "rev_timestamp $op $encTS OR " .
228 "(rev_timestamp = $encTS AND " .
229 "rev_id $op= $encId)"
234 // Don't include any revisions where we're not supposed to be able to
236 if ( !$user->isAllowed( 'deletedhistory' ) ) {
237 $bitmask = Revision
::DELETED_USER
;
238 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
239 $bitmask = Revision
::DELETED_USER | Revision
::DELETED_RESTRICTED
;
244 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
247 // We only want pages by the specified users.
248 if ( $this->prefixMode
) {
249 $this->addWhere( 'rev_user_text' .
250 $this->getDB()->buildLike( $this->userprefix
, $this->getDB()->anyString() ) );
251 } elseif ( $this->idMode
) {
252 $this->addWhereFld( 'rev_user', $this->userids
);
254 $this->addWhereFld( 'rev_user_text', $this->usernames
);
256 // ... and in the specified timeframe.
257 // Ensure the same sort order for rev_user/rev_user_text and rev_timestamp
258 // so our query is indexed
259 if ( $this->multiUserMode
) {
260 $this->addWhereRange( $this->idMode ?
'rev_user' : 'rev_user_text',
261 $this->params
['dir'], null, null );
263 $this->addTimestampWhereRange( 'rev_timestamp',
264 $this->params
['dir'], $this->params
['start'], $this->params
['end'] );
265 // Include in ORDER BY for uniqueness
266 $this->addWhereRange( 'rev_id', $this->params
['dir'], null, null );
268 $this->addWhereFld( 'page_namespace', $this->params
['namespace'] );
270 $show = $this->params
['show'];
271 if ( $this->params
['toponly'] ) { // deprecated/old param
274 if ( !is_null( $show ) ) {
275 $show = array_flip( $show );
277 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
278 ||
( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
279 ||
( isset( $show['top'] ) && isset( $show['!top'] ) )
280 ||
( isset( $show['new'] ) && isset( $show['!new'] ) )
282 $this->dieWithError( 'apierror-show' );
285 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
286 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
287 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
288 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
289 $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
290 $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
291 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
292 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
294 $this->addOption( 'LIMIT', $this->params
['limit'] +
1 );
296 // Mandatory fields: timestamp allows request continuation
297 // ns+title checks if the user has access rights for this page
298 // user_text is necessary if multiple users were specified
309 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ||
312 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
313 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
316 // Use a redundant join condition on both
317 // timestamp and ID so we can use the timestamp
319 $index['recentchanges'] = 'rc_user_text';
320 if ( isset( $show['patrolled'] ) ||
isset( $show['!patrolled'] ) ) {
321 // Put the tables in the right order for
323 $tables = [ 'revision', 'recentchanges', 'page' ];
324 $this->addOption( 'STRAIGHT_JOIN' );
325 $this->addWhere( 'rc_user_text=rev_user_text' );
326 $this->addWhere( 'rc_timestamp=rev_timestamp' );
327 $this->addWhere( 'rc_this_oldid=rev_id' );
329 $tables[] = 'recentchanges';
330 $this->addJoinConds( [ 'recentchanges' => [
332 'rc_user_text=rev_user_text',
333 'rc_timestamp=rev_timestamp',
334 'rc_this_oldid=rev_id' ] ] ] );
338 $this->addTables( $tables );
339 $this->addFieldsIf( 'rev_page', $this->fld_ids
);
340 $this->addFieldsIf( 'page_latest', $this->fld_flags
);
341 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
342 $this->addFieldsIf( 'rev_len', $this->fld_size ||
$this->fld_sizediff
);
343 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags
);
344 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags ||
$this->fld_sizediff ||
$this->fld_ids
);
345 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled
);
347 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
348 $commentQuery = $this->commentStore
->getJoin( 'rev_comment' );
349 $this->addTables( $commentQuery['tables'] );
350 $this->addFields( $commentQuery['fields'] );
351 $this->addJoinConds( $commentQuery['joins'] );
354 if ( $this->fld_tags
) {
355 $this->addTables( 'tag_summary' );
357 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
359 $this->addFields( 'ts_tags' );
362 if ( isset( $this->params
['tag'] ) ) {
363 $this->addTables( 'change_tag' );
365 [ 'change_tag' => [ 'INNER JOIN', [ 'rev_id=ct_rev_id' ] ] ]
367 $this->addWhereFld( 'ct_tag', $this->params
['tag'] );
370 if ( isset( $index ) ) {
371 $this->addOption( 'USE INDEX', $index );
376 * Extract fields from the database row and append them to a result array
378 * @param stdClass $row
381 private function extractRowInfo( $row ) {
385 if ( $row->rev_deleted
& Revision
::DELETED_TEXT
) {
386 $vals['texthidden'] = true;
390 // Any rows where we can't view the user were filtered out in the query.
391 $vals['userid'] = (int)$row->rev_user
;
392 $vals['user'] = $row->rev_user_text
;
393 if ( $row->rev_deleted
& Revision
::DELETED_USER
) {
394 $vals['userhidden'] = true;
397 if ( $this->fld_ids
) {
398 $vals['pageid'] = intval( $row->rev_page
);
399 $vals['revid'] = intval( $row->rev_id
);
400 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
402 if ( !is_null( $row->rev_parent_id
) ) {
403 $vals['parentid'] = intval( $row->rev_parent_id
);
407 $title = Title
::makeTitle( $row->page_namespace
, $row->page_title
);
409 if ( $this->fld_title
) {
410 ApiQueryBase
::addTitleInfo( $vals, $title );
413 if ( $this->fld_timestamp
) {
414 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->rev_timestamp
);
417 if ( $this->fld_flags
) {
418 $vals['new'] = $row->rev_parent_id
== 0 && !is_null( $row->rev_parent_id
);
419 $vals['minor'] = (bool)$row->rev_minor_edit
;
420 $vals['top'] = $row->page_latest
== $row->rev_id
;
423 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
424 if ( $row->rev_deleted
& Revision
::DELETED_COMMENT
) {
425 $vals['commenthidden'] = true;
429 $userCanView = Revision
::userCanBitfield(
431 Revision
::DELETED_COMMENT
, $this->getUser()
434 if ( $userCanView ) {
435 $comment = $this->commentStore
->getComment( 'rev_comment', $row )->text
;
436 if ( $this->fld_comment
) {
437 $vals['comment'] = $comment;
440 if ( $this->fld_parsedcomment
) {
441 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
446 if ( $this->fld_patrolled
) {
447 $vals['patrolled'] = (bool)$row->rc_patrolled
;
450 if ( $this->fld_size
&& !is_null( $row->rev_len
) ) {
451 $vals['size'] = intval( $row->rev_len
);
454 if ( $this->fld_sizediff
455 && !is_null( $row->rev_len
)
456 && !is_null( $row->rev_parent_id
)
458 $parentLen = isset( $this->parentLens
[$row->rev_parent_id
] )
459 ?
$this->parentLens
[$row->rev_parent_id
]
461 $vals['sizediff'] = intval( $row->rev_len
- $parentLen );
464 if ( $this->fld_tags
) {
465 if ( $row->ts_tags
) {
466 $tags = explode( ',', $row->ts_tags
);
467 ApiResult
::setIndexedTagName( $tags, 'tag' );
468 $vals['tags'] = $tags;
474 if ( $anyHidden && $row->rev_deleted
& Revision
::DELETED_RESTRICTED
) {
475 $vals['suppressed'] = true;
481 private function continueStr( $row ) {
482 if ( $this->multiUserMode
) {
483 if ( $this->idMode
) {
484 return "id|$row->rev_user|$row->rev_timestamp|$row->rev_id";
486 return "name|$row->rev_user_text|$row->rev_timestamp|$row->rev_id";
489 return "$row->rev_timestamp|$row->rev_id";
493 public function getCacheMode( $params ) {
494 // This module provides access to deleted revisions and patrol flags if
495 // the requester is logged in
496 return 'anon-public-user-private';
499 public function getAllowedParams() {
502 ApiBase
::PARAM_DFLT
=> 10,
503 ApiBase
::PARAM_TYPE
=> 'limit',
504 ApiBase
::PARAM_MIN
=> 1,
505 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
506 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
509 ApiBase
::PARAM_TYPE
=> 'timestamp'
512 ApiBase
::PARAM_TYPE
=> 'timestamp'
515 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
518 ApiBase
::PARAM_TYPE
=> 'user',
519 ApiBase
::PARAM_ISMULTI
=> true
522 ApiBase
::PARAM_TYPE
=> 'integer',
523 ApiBase
::PARAM_ISMULTI
=> true
525 'userprefix' => null,
527 ApiBase
::PARAM_DFLT
=> 'older',
528 ApiBase
::PARAM_TYPE
=> [
532 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
535 ApiBase
::PARAM_ISMULTI
=> true,
536 ApiBase
::PARAM_TYPE
=> 'namespace'
539 ApiBase
::PARAM_ISMULTI
=> true,
540 ApiBase
::PARAM_DFLT
=> 'ids|title|timestamp|comment|size|flags',
541 ApiBase
::PARAM_TYPE
=> [
553 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
556 ApiBase
::PARAM_ISMULTI
=> true,
557 ApiBase
::PARAM_TYPE
=> [
567 ApiBase
::PARAM_HELP_MSG
=> [
568 'apihelp-query+usercontribs-param-show',
569 $this->getConfig()->get( 'RCMaxAge' )
574 ApiBase
::PARAM_DFLT
=> false,
575 ApiBase
::PARAM_DEPRECATED
=> true,
580 protected function getExamplesMessages() {
582 'action=query&list=usercontribs&ucuser=Example'
583 => 'apihelp-query+usercontribs-example-user',
584 'action=query&list=usercontribs&ucuserprefix=192.0.2.'
585 => 'apihelp-query+usercontribs-example-ipprefix',
589 public function getHelpUrls() {
590 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Usercontribs';