Added release notes for 'ContentHandler::runLegacyHooks' removal
[mediawiki.git] / includes / api / ApiQueryUserContributions.php
blob31a92380015f47944984c9a1f867e8c162d77bd1
1 <?php
2 /**
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
24 * @file
27 /**
28 * This query action adds a list of a specified user's contributions to the output.
30 * @ingroup API
32 class ApiQueryContributions extends ApiQueryBase {
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'uc' );
38 private $params, $prefixMode, $userprefix, $multiUserMode, $idMode, $usernames, $userids,
39 $parentLens;
40 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
41 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
42 $fld_patrolled = false, $fld_tags = false, $fld_size = false, $fld_sizediff = false;
44 public function execute() {
45 // Parse some parameters
46 $this->params = $this->extractRequestParams();
48 $prop = array_flip( $this->params['prop'] );
49 $this->fld_ids = isset( $prop['ids'] );
50 $this->fld_title = isset( $prop['title'] );
51 $this->fld_comment = isset( $prop['comment'] );
52 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
53 $this->fld_size = isset( $prop['size'] );
54 $this->fld_sizediff = isset( $prop['sizediff'] );
55 $this->fld_flags = isset( $prop['flags'] );
56 $this->fld_timestamp = isset( $prop['timestamp'] );
57 $this->fld_patrolled = isset( $prop['patrolled'] );
58 $this->fld_tags = isset( $prop['tags'] );
60 // Most of this code will use the 'contributions' group DB, which can map to replica DBs
61 // with extra user based indexes or partioning by user. The additional metadata
62 // queries should use a regular replica DB since the lookup pattern is not all by user.
63 $dbSecondary = $this->getDB(); // any random replica DB
65 // TODO: if the query is going only against the revision table, should this be done?
66 $this->selectNamedDB( 'contributions', DB_REPLICA, 'contributions' );
68 $this->requireOnlyOneParameter( $this->params, 'userprefix', 'userids', 'user' );
70 $this->idMode = false;
71 if ( isset( $this->params['userprefix'] ) ) {
72 $this->prefixMode = true;
73 $this->multiUserMode = true;
74 $this->userprefix = $this->params['userprefix'];
75 } elseif ( isset( $this->params['userids'] ) ) {
76 $this->userids = [];
78 if ( !count( $this->params['userids'] ) ) {
79 $encParamName = $this->encodeParamName( 'userids' );
80 $this->dieWithError( [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName" );
83 foreach ( $this->params['userids'] as $uid ) {
84 if ( $uid <= 0 ) {
85 $this->dieWithError( [ 'apierror-invaliduserid', $uid ], 'invaliduserid' );
88 $this->userids[] = $uid;
91 $this->prefixMode = false;
92 $this->multiUserMode = ( count( $this->params['userids'] ) > 1 );
93 $this->idMode = true;
94 } else {
95 $anyIPs = false;
96 $this->userids = [];
97 $this->usernames = [];
98 if ( !count( $this->params['user'] ) ) {
99 $encParamName = $this->encodeParamName( 'user' );
100 $this->dieWithError(
101 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
104 foreach ( $this->params['user'] as $u ) {
105 if ( $u === '' ) {
106 $encParamName = $this->encodeParamName( 'user' );
107 $this->dieWithError(
108 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
112 if ( User::isIP( $u ) ) {
113 $anyIPs = true;
114 $this->usernames[] = $u;
115 } else {
116 $name = User::getCanonicalName( $u, 'valid' );
117 if ( $name === false ) {
118 $encParamName = $this->encodeParamName( 'user' );
119 $this->dieWithError(
120 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $u ) ], "baduser_$encParamName"
123 $this->usernames[] = $name;
126 $this->prefixMode = false;
127 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
129 if ( !$anyIPs ) {
130 $dbr = $this->getDB();
131 $res = $dbr->select( 'user', 'user_id', [ 'user_name' => $this->usernames ], __METHOD__ );
132 foreach ( $res as $row ) {
133 $this->userids[] = $row->user_id;
135 $this->idMode = count( $this->userids ) === count( $this->usernames );
139 $this->prepareQuery();
141 $hookData = [];
142 // Do the actual query.
143 $res = $this->select( __METHOD__, [], $hookData );
145 if ( $this->fld_sizediff ) {
146 $revIds = [];
147 foreach ( $res as $row ) {
148 if ( $row->rev_parent_id ) {
149 $revIds[] = $row->rev_parent_id;
152 $this->parentLens = Revision::getParentLengths( $dbSecondary, $revIds );
153 $res->rewind(); // reset
156 // Initialise some variables
157 $count = 0;
158 $limit = $this->params['limit'];
160 // Fetch each row
161 foreach ( $res as $row ) {
162 if ( ++$count > $limit ) {
163 // We've reached the one extra which shows that there are
164 // additional pages to be had. Stop here...
165 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
166 break;
169 $vals = $this->extractRowInfo( $row );
170 $fit = $this->processRow( $row, $vals, $hookData ) &&
171 $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
172 if ( !$fit ) {
173 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
174 break;
178 $this->getResult()->addIndexedTagName(
179 [ 'query', $this->getModuleName() ],
180 'item'
185 * Prepares the query and returns the limit of rows requested
187 private function prepareQuery() {
188 // We're after the revision table, and the corresponding page
189 // row for anything we retrieve. We may also need the
190 // recentchanges row and/or tag summary row.
191 $user = $this->getUser();
192 $tables = [ 'page', 'revision' ]; // Order may change
193 $this->addWhere( 'page_id=rev_page' );
195 // Handle continue parameter
196 if ( !is_null( $this->params['continue'] ) ) {
197 $continue = explode( '|', $this->params['continue'] );
198 $db = $this->getDB();
199 if ( $this->multiUserMode ) {
200 $this->dieContinueUsageIf( count( $continue ) != 4 );
201 $modeFlag = array_shift( $continue );
202 $this->dieContinueUsageIf( !in_array( $modeFlag, [ 'id', 'name' ] ) );
203 if ( $this->idMode && $modeFlag === 'name' ) {
204 // The users were created since this query started, but we
205 // can't go back and change modes now. So just keep on with
206 // name mode.
207 $this->idMode = false;
209 $this->dieContinueUsageIf( ( $modeFlag === 'id' ) !== $this->idMode );
210 $userField = $this->idMode ? 'rev_user' : 'rev_user_text';
211 $encUser = $db->addQuotes( array_shift( $continue ) );
212 } else {
213 $this->dieContinueUsageIf( count( $continue ) != 2 );
215 $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
216 $encId = (int)$continue[1];
217 $this->dieContinueUsageIf( $encId != $continue[1] );
218 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
219 if ( $this->multiUserMode ) {
220 $this->addWhere(
221 "$userField $op $encUser OR " .
222 "($userField = $encUser AND " .
223 "(rev_timestamp $op $encTS OR " .
224 "(rev_timestamp = $encTS AND " .
225 "rev_id $op= $encId)))"
227 } else {
228 $this->addWhere(
229 "rev_timestamp $op $encTS OR " .
230 "(rev_timestamp = $encTS AND " .
231 "rev_id $op= $encId)"
236 // Don't include any revisions where we're not supposed to be able to
237 // see the username.
238 if ( !$user->isAllowed( 'deletedhistory' ) ) {
239 $bitmask = Revision::DELETED_USER;
240 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
241 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
242 } else {
243 $bitmask = 0;
245 if ( $bitmask ) {
246 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
249 // We only want pages by the specified users.
250 if ( $this->prefixMode ) {
251 $this->addWhere( 'rev_user_text' .
252 $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
253 } elseif ( $this->idMode ) {
254 $this->addWhereFld( 'rev_user', $this->userids );
255 } else {
256 $this->addWhereFld( 'rev_user_text', $this->usernames );
258 // ... and in the specified timeframe.
259 // Ensure the same sort order for rev_user/rev_user_text and rev_timestamp
260 // so our query is indexed
261 if ( $this->multiUserMode ) {
262 $this->addWhereRange( $this->idMode ? 'rev_user' : 'rev_user_text',
263 $this->params['dir'], null, null );
265 $this->addTimestampWhereRange( 'rev_timestamp',
266 $this->params['dir'], $this->params['start'], $this->params['end'] );
267 // Include in ORDER BY for uniqueness
268 $this->addWhereRange( 'rev_id', $this->params['dir'], null, null );
270 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
272 $show = $this->params['show'];
273 if ( $this->params['toponly'] ) { // deprecated/old param
274 $show[] = 'top';
276 if ( !is_null( $show ) ) {
277 $show = array_flip( $show );
279 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
280 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
281 || ( isset( $show['top'] ) && isset( $show['!top'] ) )
282 || ( isset( $show['new'] ) && isset( $show['!new'] ) )
284 $this->dieWithError( 'apierror-show' );
287 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
288 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
289 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
290 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
291 $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
292 $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
293 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
294 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
296 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
298 // Mandatory fields: timestamp allows request continuation
299 // ns+title checks if the user has access rights for this page
300 // user_text is necessary if multiple users were specified
301 $this->addFields( [
302 'rev_id',
303 'rev_timestamp',
304 'page_namespace',
305 'page_title',
306 'rev_user',
307 'rev_user_text',
308 'rev_deleted'
309 ] );
311 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
312 $this->fld_patrolled
314 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
315 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
318 // Use a redundant join condition on both
319 // timestamp and ID so we can use the timestamp
320 // index
321 $index['recentchanges'] = 'rc_user_text';
322 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
323 // Put the tables in the right order for
324 // STRAIGHT_JOIN
325 $tables = [ 'revision', 'recentchanges', 'page' ];
326 $this->addOption( 'STRAIGHT_JOIN' );
327 $this->addWhere( 'rc_user_text=rev_user_text' );
328 $this->addWhere( 'rc_timestamp=rev_timestamp' );
329 $this->addWhere( 'rc_this_oldid=rev_id' );
330 } else {
331 $tables[] = 'recentchanges';
332 $this->addJoinConds( [ 'recentchanges' => [
333 'LEFT JOIN', [
334 'rc_user_text=rev_user_text',
335 'rc_timestamp=rev_timestamp',
336 'rc_this_oldid=rev_id' ] ] ] );
340 $this->addTables( $tables );
341 $this->addFieldsIf( 'rev_page', $this->fld_ids );
342 $this->addFieldsIf( 'page_latest', $this->fld_flags );
343 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
344 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
345 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
346 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
347 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff || $this->fld_ids );
348 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
350 if ( $this->fld_tags ) {
351 $this->addTables( 'tag_summary' );
352 $this->addJoinConds(
353 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
355 $this->addFields( 'ts_tags' );
358 if ( isset( $this->params['tag'] ) ) {
359 $this->addTables( 'change_tag' );
360 $this->addJoinConds(
361 [ 'change_tag' => [ 'INNER JOIN', [ 'rev_id=ct_rev_id' ] ] ]
363 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
366 if ( isset( $index ) ) {
367 $this->addOption( 'USE INDEX', $index );
372 * Extract fields from the database row and append them to a result array
374 * @param stdClass $row
375 * @return array
377 private function extractRowInfo( $row ) {
378 $vals = [];
379 $anyHidden = false;
381 if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
382 $vals['texthidden'] = true;
383 $anyHidden = true;
386 // Any rows where we can't view the user were filtered out in the query.
387 $vals['userid'] = (int)$row->rev_user;
388 $vals['user'] = $row->rev_user_text;
389 if ( $row->rev_deleted & Revision::DELETED_USER ) {
390 $vals['userhidden'] = true;
391 $anyHidden = true;
393 if ( $this->fld_ids ) {
394 $vals['pageid'] = intval( $row->rev_page );
395 $vals['revid'] = intval( $row->rev_id );
396 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
398 if ( !is_null( $row->rev_parent_id ) ) {
399 $vals['parentid'] = intval( $row->rev_parent_id );
403 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
405 if ( $this->fld_title ) {
406 ApiQueryBase::addTitleInfo( $vals, $title );
409 if ( $this->fld_timestamp ) {
410 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
413 if ( $this->fld_flags ) {
414 $vals['new'] = $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id );
415 $vals['minor'] = (bool)$row->rev_minor_edit;
416 $vals['top'] = $row->page_latest == $row->rev_id;
419 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
420 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
421 $vals['commenthidden'] = true;
422 $anyHidden = true;
425 $userCanView = Revision::userCanBitfield(
426 $row->rev_deleted,
427 Revision::DELETED_COMMENT, $this->getUser()
430 if ( $userCanView ) {
431 if ( $this->fld_comment ) {
432 $vals['comment'] = $row->rev_comment;
435 if ( $this->fld_parsedcomment ) {
436 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
441 if ( $this->fld_patrolled ) {
442 $vals['patrolled'] = (bool)$row->rc_patrolled;
445 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
446 $vals['size'] = intval( $row->rev_len );
449 if ( $this->fld_sizediff
450 && !is_null( $row->rev_len )
451 && !is_null( $row->rev_parent_id )
453 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
454 ? $this->parentLens[$row->rev_parent_id]
455 : 0;
456 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
459 if ( $this->fld_tags ) {
460 if ( $row->ts_tags ) {
461 $tags = explode( ',', $row->ts_tags );
462 ApiResult::setIndexedTagName( $tags, 'tag' );
463 $vals['tags'] = $tags;
464 } else {
465 $vals['tags'] = [];
469 if ( $anyHidden && $row->rev_deleted & Revision::DELETED_RESTRICTED ) {
470 $vals['suppressed'] = true;
473 return $vals;
476 private function continueStr( $row ) {
477 if ( $this->multiUserMode ) {
478 if ( $this->idMode ) {
479 return "id|$row->rev_user|$row->rev_timestamp|$row->rev_id";
480 } else {
481 return "name|$row->rev_user_text|$row->rev_timestamp|$row->rev_id";
483 } else {
484 return "$row->rev_timestamp|$row->rev_id";
488 public function getCacheMode( $params ) {
489 // This module provides access to deleted revisions and patrol flags if
490 // the requester is logged in
491 return 'anon-public-user-private';
494 public function getAllowedParams() {
495 return [
496 'limit' => [
497 ApiBase::PARAM_DFLT => 10,
498 ApiBase::PARAM_TYPE => 'limit',
499 ApiBase::PARAM_MIN => 1,
500 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
501 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
503 'start' => [
504 ApiBase::PARAM_TYPE => 'timestamp'
506 'end' => [
507 ApiBase::PARAM_TYPE => 'timestamp'
509 'continue' => [
510 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
512 'user' => [
513 ApiBase::PARAM_TYPE => 'user',
514 ApiBase::PARAM_ISMULTI => true
516 'userids' => [
517 ApiBase::PARAM_TYPE => 'integer',
518 ApiBase::PARAM_ISMULTI => true
520 'userprefix' => null,
521 'dir' => [
522 ApiBase::PARAM_DFLT => 'older',
523 ApiBase::PARAM_TYPE => [
524 'newer',
525 'older'
527 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
529 'namespace' => [
530 ApiBase::PARAM_ISMULTI => true,
531 ApiBase::PARAM_TYPE => 'namespace'
533 'prop' => [
534 ApiBase::PARAM_ISMULTI => true,
535 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
536 ApiBase::PARAM_TYPE => [
537 'ids',
538 'title',
539 'timestamp',
540 'comment',
541 'parsedcomment',
542 'size',
543 'sizediff',
544 'flags',
545 'patrolled',
546 'tags'
548 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
550 'show' => [
551 ApiBase::PARAM_ISMULTI => true,
552 ApiBase::PARAM_TYPE => [
553 'minor',
554 '!minor',
555 'patrolled',
556 '!patrolled',
557 'top',
558 '!top',
559 'new',
560 '!new',
562 ApiBase::PARAM_HELP_MSG => [
563 'apihelp-query+usercontribs-param-show',
564 $this->getConfig()->get( 'RCMaxAge' )
567 'tag' => null,
568 'toponly' => [
569 ApiBase::PARAM_DFLT => false,
570 ApiBase::PARAM_DEPRECATED => true,
575 protected function getExamplesMessages() {
576 return [
577 'action=query&list=usercontribs&ucuser=Example'
578 => 'apihelp-query+usercontribs-example-user',
579 'action=query&list=usercontribs&ucuserprefix=192.0.2.'
580 => 'apihelp-query+usercontribs-example-ipprefix',
584 public function getHelpUrls() {
585 return 'https://www.mediawiki.org/wiki/API:Usercontribs';