Provide command to adjust phpunit.xml for code coverage
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContribs.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
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.
9 *
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.
14 *
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
19 *
20 * @file
21 */
22
23 use MediaWiki\MediaWikiServices;
24 use MediaWiki\Revision\RevisionRecord;
25 use MediaWiki\Storage\NameTableAccessException;
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 ApiQueryUserContribs extends ApiQueryBase {
33
34 public function __construct( ApiQuery $query, $moduleName ) {
35 parent::__construct( $query, $moduleName, 'uc' );
36 }
37
38 private $params, $multiUserMode, $orderBy, $parentLens, $commentStore;
39
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;
43
44 public function execute() {
45 global $wgActorTableSchemaMigrationStage;
46
47 // Parse some parameters
48 $this->params = $this->extractRequestParams();
49
50 $this->commentStore = CommentStore::getStore();
51
52 $prop = array_flip( $this->params['prop'] );
53 $this->fld_ids = isset( $prop['ids'] );
54 $this->fld_title = isset( $prop['title'] );
55 $this->fld_comment = isset( $prop['comment'] );
56 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
57 $this->fld_size = isset( $prop['size'] );
58 $this->fld_sizediff = isset( $prop['sizediff'] );
59 $this->fld_flags = isset( $prop['flags'] );
60 $this->fld_timestamp = isset( $prop['timestamp'] );
61 $this->fld_patrolled = isset( $prop['patrolled'] );
62 $this->fld_tags = isset( $prop['tags'] );
63
64 // The main query may use the 'contributions' group DB, which can map to replica DBs
65 // with extra user based indexes or partioning by user. The additional metadata
66 // queries should use a regular replica DB since the lookup pattern is not all by user.
67 $dbSecondary = $this->getDB(); // any random replica DB
68
69 $sort = ( $this->params['dir'] == 'newer' ? '' : ' DESC' );
70 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
71
72 // Create an Iterator that produces the UserIdentity objects we need, depending
73 // on which of the 'userprefix', 'userids', or 'user' params was
74 // specified.
75 $this->requireOnlyOneParameter( $this->params, 'userprefix', 'userids', 'user' );
76 if ( isset( $this->params['userprefix'] ) ) {
77 $this->multiUserMode = true;
78 $this->orderBy = 'name';
79 $fname = __METHOD__;
80
81 // Because 'userprefix' might produce a huge number of users (e.g.
82 // a wiki with users "Test00000001" to "Test99999999"), use a
83 // generator with batched lookup and continuation.
84 $userIter = call_user_func( function () use ( $dbSecondary, $sort, $op, $fname ) {
85 global $wgActorTableSchemaMigrationStage;
86
87 $fromName = false;
88 if ( !is_null( $this->params['continue'] ) ) {
89 $continue = explode( '|', $this->params['continue'] );
90 $this->dieContinueUsageIf( count( $continue ) != 4 );
91 $this->dieContinueUsageIf( $continue[0] !== 'name' );
92 $fromName = $continue[1];
93 }
94 $like = $dbSecondary->buildLike( $this->params['userprefix'], $dbSecondary->anyString() );
95
96 $limit = 501;
97
98 do {
99 $from = $fromName ? "$op= " . $dbSecondary->addQuotes( $fromName ) : false;
100
101 // For the new schema, pull from the actor table. For the
102 // old, pull from rev_user.
103 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
104 $res = $dbSecondary->select(
105 'actor',
106 [ 'actor_id', 'user_id' => 'COALESCE(actor_user,0)', 'user_name' => 'actor_name' ],
107 array_merge( [ "actor_name$like" ], $from ? [ "actor_name $from" ] : [] ),
108 $fname,
109 [ 'ORDER BY' => [ "user_name $sort" ], 'LIMIT' => $limit ]
110 );
111 } else {
112 $res = $dbSecondary->select(
113 'revision',
114 [ 'actor_id' => 'NULL', 'user_id' => 'rev_user', 'user_name' => 'rev_user_text' ],
115 array_merge( [ "rev_user_text$like" ], $from ? [ "rev_user_text $from" ] : [] ),
116 $fname,
117 [ 'DISTINCT', 'ORDER BY' => [ "rev_user_text $sort" ], 'LIMIT' => $limit ]
118 );
119 }
120
121 $count = 0;
122 $fromName = false;
123 foreach ( $res as $row ) {
124 if ( ++$count >= $limit ) {
125 $fromName = $row->user_name;
126 break;
127 }
128 yield User::newFromRow( $row );
129 }
130 } while ( $fromName !== false );
131 } );
132 // Do the actual sorting client-side, because otherwise
133 // prepareQuery might try to sort by actor and confuse everything.
134 $batchSize = 1;
135 } elseif ( isset( $this->params['userids'] ) ) {
136 if ( $this->params['userids'] === [] ) {
137 $encParamName = $this->encodeParamName( 'userids' );
138 $this->dieWithError( [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName" );
139 }
140
141 $ids = [];
142 foreach ( $this->params['userids'] as $uid ) {
143 if ( $uid <= 0 ) {
144 $this->dieWithError( [ 'apierror-invaliduserid', $uid ], 'invaliduserid' );
145 }
146 $ids[] = $uid;
147 }
148
149 $this->orderBy = 'id';
150 $this->multiUserMode = count( $ids ) > 1;
151
152 $from = $fromId = false;
153 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
154 $continue = explode( '|', $this->params['continue'] );
155 $this->dieContinueUsageIf( count( $continue ) != 4 );
156 $this->dieContinueUsageIf( $continue[0] !== 'id' && $continue[0] !== 'actor' );
157 $fromId = (int)$continue[1];
158 $this->dieContinueUsageIf( $continue[1] !== (string)$fromId );
159 $from = "$op= $fromId";
160 }
161
162 // For the new schema, just select from the actor table. For the
163 // old, select from user.
164 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
165 $res = $dbSecondary->select(
166 'actor',
167 [ 'actor_id', 'user_id' => 'actor_user', 'user_name' => 'actor_name' ],
168 array_merge( [ 'actor_user' => $ids ], $from ? [ "actor_id $from" ] : [] ),
169 __METHOD__,
170 [ 'ORDER BY' => "user_id $sort" ]
171 );
172 } else {
173 $res = $dbSecondary->select(
174 'user',
175 [ 'actor_id' => 'NULL', 'user_id' => 'user_id', 'user_name' => 'user_name' ],
176 array_merge( [ 'user_id' => $ids ], $from ? [ "user_id $from" ] : [] ),
177 __METHOD__,
178 [ 'ORDER BY' => "user_id $sort" ]
179 );
180 }
181 $userIter = UserArray::newFromResult( $res );
182 $batchSize = count( $ids );
183 } else {
184 $names = [];
185 if ( !count( $this->params['user'] ) ) {
186 $encParamName = $this->encodeParamName( 'user' );
187 $this->dieWithError(
188 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
189 );
190 }
191 foreach ( $this->params['user'] as $u ) {
192 if ( $u === '' ) {
193 $encParamName = $this->encodeParamName( 'user' );
194 $this->dieWithError(
195 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
196 );
197 }
198
199 if ( User::isIP( $u ) || ExternalUserNames::isExternal( $u ) ) {
200 $names[$u] = null;
201 } else {
202 $name = User::getCanonicalName( $u, 'valid' );
203 if ( $name === false ) {
204 $encParamName = $this->encodeParamName( 'user' );
205 $this->dieWithError(
206 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $u ) ], "baduser_$encParamName"
207 );
208 }
209 $names[$name] = null;
210 }
211 }
212
213 $this->orderBy = 'name';
214 $this->multiUserMode = count( $names ) > 1;
215
216 $from = $fromName = false;
217 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
218 $continue = explode( '|', $this->params['continue'] );
219 $this->dieContinueUsageIf( count( $continue ) != 4 );
220 $this->dieContinueUsageIf( $continue[0] !== 'name' && $continue[0] !== 'actor' );
221 $fromName = $continue[1];
222 $from = "$op= " . $dbSecondary->addQuotes( $fromName );
223 }
224
225 // For the new schema, just select from the actor table. For the
226 // old, select from user then merge in any unknown users (IPs and imports).
227 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
228 $res = $dbSecondary->select(
229 'actor',
230 [ 'actor_id', 'user_id' => 'actor_user', 'user_name' => 'actor_name' ],
231 array_merge( [ 'actor_name' => array_keys( $names ) ], $from ? [ "actor_id $from" ] : [] ),
232 __METHOD__,
233 [ 'ORDER BY' => "actor_name $sort" ]
234 );
235 $userIter = UserArray::newFromResult( $res );
236 } else {
237 $res = $dbSecondary->select(
238 'user',
239 [ 'actor_id' => 'NULL', 'user_id', 'user_name' ],
240 array_merge( [ 'user_name' => array_keys( $names ) ], $from ? [ "user_name $from" ] : [] ),
241 __METHOD__
242 );
243 foreach ( $res as $row ) {
244 $names[$row->user_name] = $row;
245 }
246 if ( $this->params['dir'] == 'newer' ) {
247 ksort( $names, SORT_STRING );
248 } else {
249 krsort( $names, SORT_STRING );
250 }
251 $neg = $op === '>' ? -1 : 1;
252 $userIter = call_user_func( function () use ( $names, $fromName, $neg ) {
253 foreach ( $names as $name => $row ) {
254 if ( $fromName === false || $neg * strcmp( $name, $fromName ) <= 0 ) {
255 $user = $row ? User::newFromRow( $row ) : User::newFromName( $name, false );
256 yield $user;
257 }
258 }
259 } );
260 }
261 $batchSize = count( $names );
262 }
263
264 // With the new schema, the DB query will order by actor so update $this->orderBy to match.
265 if ( $batchSize > 1 && ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) ) {
266 $this->orderBy = 'actor';
267 }
268
269 // Use the 'contributions' replica, but only if we're querying by user ID (T216656).
270 if ( $this->orderBy === 'id' &&
271 !( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW )
272 ) {
273 $this->selectNamedDB( 'contributions', DB_REPLICA, 'contributions' );
274 }
275
276 $count = 0;
277 $limit = $this->params['limit'];
278 $userIter->rewind();
279 while ( $userIter->valid() ) {
280 $users = [];
281 while ( count( $users ) < $batchSize && $userIter->valid() ) {
282 $users[] = $userIter->current();
283 $userIter->next();
284 }
285
286 $hookData = [];
287 $this->prepareQuery( $users, $limit - $count );
288 $res = $this->select( __METHOD__, [], $hookData );
289
290 if ( $this->fld_sizediff ) {
291 $revIds = [];
292 foreach ( $res as $row ) {
293 if ( $row->rev_parent_id ) {
294 $revIds[] = $row->rev_parent_id;
295 }
296 }
297 $this->parentLens = MediaWikiServices::getInstance()->getRevisionStore()
298 ->listRevisionSizes( $dbSecondary, $revIds );
299 }
300
301 foreach ( $res as $row ) {
302 if ( ++$count > $limit ) {
303 // We've reached the one extra which shows that there are
304 // additional pages to be had. Stop here...
305 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
306 break 2;
307 }
308
309 $vals = $this->extractRowInfo( $row );
310 $fit = $this->processRow( $row, $vals, $hookData ) &&
311 $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
312 if ( !$fit ) {
313 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
314 break 2;
315 }
316 }
317 }
318
319 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
320 }
321
322 /**
323 * Prepares the query and returns the limit of rows requested
324 * @param User[] $users
325 * @param int $limit
326 */
327 private function prepareQuery( array $users, $limit ) {
328 global $wgActorTableSchemaMigrationStage;
329
330 $this->resetQueryParams();
331 $db = $this->getDB();
332
333 $revQuery = MediaWikiServices::getInstance()->getRevisionStore()->getQueryInfo( [ 'page' ] );
334
335 if ( $wgActorTableSchemaMigrationStage & SCHEMA_COMPAT_READ_NEW ) {
336 $revWhere = ActorMigration::newMigration()->getWhere( $db, 'rev_user', $users );
337 $orderUserField = 'rev_actor';
338 $userField = $this->orderBy === 'actor' ? 'revactor_actor' : 'actor_name';
339 $tsField = 'revactor_timestamp';
340 $idField = 'revactor_rev';
341
342 // T221511: MySQL/MariaDB (10.1.37) can sometimes irrationally decide that querying `actor`
343 // before `revision_actor_temp` and filesorting is somehow better than querying $limit+1 rows
344 // from `revision_actor_temp`. Tell it not to reorder the query (and also reorder it ourselves
345 // because as generated by RevisionStore it'll have `revision` first rather than
346 // `revision_actor_temp`). But not when uctag is used, as it seems as likely to be harmed as
347 // helped in that case, and not when there's only one User because in that case it fetches
348 // the one `actor` row as a constant and doesn't filesort.
349 if ( count( $users ) > 1 && !isset( $this->params['tag'] ) ) {
350 $revQuery['joins']['revision'] = $revQuery['joins']['temp_rev_user'];
351 unset( $revQuery['joins']['temp_rev_user'] );
352 $this->addOption( 'STRAIGHT_JOIN' );
353 // It isn't actually necesssary to reorder $revQuery['tables'] as Database does the right thing
354 // when join conditions are given for all joins, but Gergő is wary of relying on that so pull
355 // `revision_actor_temp` to the start.
356 $revQuery['tables'] =
357 [ 'temp_rev_user' => $revQuery['tables']['temp_rev_user'] ] + $revQuery['tables'];
358 }
359 } else {
360 // If we're dealing with user names (rather than IDs) in read-old mode,
361 // pass false for ActorMigration::getWhere()'s $useId parameter so
362 // $revWhere['conds'] isn't an OR.
363 $revWhere = ActorMigration::newMigration()
364 ->getWhere( $db, 'rev_user', $users, $this->orderBy === 'id' );
365 $orderUserField = $this->orderBy === 'id' ? 'rev_user' : 'rev_user_text';
366 $userField = $revQuery['fields'][$orderUserField];
367 $tsField = 'rev_timestamp';
368 $idField = 'rev_id';
369 }
370
371 $this->addTables( $revQuery['tables'] );
372 $this->addJoinConds( $revQuery['joins'] );
373 $this->addFields( $revQuery['fields'] );
374 $this->addWhere( $revWhere['conds'] );
375
376 // Handle continue parameter
377 if ( !is_null( $this->params['continue'] ) ) {
378 $continue = explode( '|', $this->params['continue'] );
379 if ( $this->multiUserMode ) {
380 $this->dieContinueUsageIf( count( $continue ) != 4 );
381 $modeFlag = array_shift( $continue );
382 $this->dieContinueUsageIf( $modeFlag !== $this->orderBy );
383 $encUser = $db->addQuotes( array_shift( $continue ) );
384 } else {
385 $this->dieContinueUsageIf( count( $continue ) != 2 );
386 }
387 $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
388 $encId = (int)$continue[1];
389 $this->dieContinueUsageIf( $encId != $continue[1] );
390 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
391 if ( $this->multiUserMode ) {
392 $this->addWhere(
393 "$userField $op $encUser OR " .
394 "($userField = $encUser AND " .
395 "($tsField $op $encTS OR " .
396 "($tsField = $encTS AND " .
397 "$idField $op= $encId)))"
398 );
399 } else {
400 $this->addWhere(
401 "$tsField $op $encTS OR " .
402 "($tsField = $encTS AND " .
403 "$idField $op= $encId)"
404 );
405 }
406 }
407
408 // Don't include any revisions where we're not supposed to be able to
409 // see the username.
410 $user = $this->getUser();
411 if ( !$this->getPermissionManager()->userHasRight( $user, 'deletedhistory' ) ) {
412 $bitmask = RevisionRecord::DELETED_USER;
413 } elseif ( !$this->getPermissionManager()
414 ->userHasAnyRight( $user, 'suppressrevision', 'viewsuppressed' )
415 ) {
416 $bitmask = RevisionRecord::DELETED_USER | RevisionRecord::DELETED_RESTRICTED;
417 } else {
418 $bitmask = 0;
419 }
420 if ( $bitmask ) {
421 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
422 }
423
424 // Add the user field to ORDER BY if there are multiple users
425 if ( count( $users ) > 1 ) {
426 $this->addWhereRange( $orderUserField, $this->params['dir'], null, null );
427 }
428
429 // Then timestamp
430 $this->addTimestampWhereRange( $tsField,
431 $this->params['dir'], $this->params['start'], $this->params['end'] );
432
433 // Then rev_id for a total ordering
434 $this->addWhereRange( $idField, $this->params['dir'], null, null );
435
436 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
437
438 $show = $this->params['show'];
439 if ( $this->params['toponly'] ) { // deprecated/old param
440 $show[] = 'top';
441 }
442 if ( !is_null( $show ) ) {
443 $show = array_flip( $show );
444
445 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
446 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
447 || ( isset( $show['autopatrolled'] ) && isset( $show['!autopatrolled'] ) )
448 || ( isset( $show['autopatrolled'] ) && isset( $show['!patrolled'] ) )
449 || ( isset( $show['top'] ) && isset( $show['!top'] ) )
450 || ( isset( $show['new'] ) && isset( $show['!new'] ) )
451 ) {
452 $this->dieWithError( 'apierror-show' );
453 }
454
455 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
456 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
457 $this->addWhereIf(
458 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED,
459 isset( $show['!patrolled'] )
460 );
461 $this->addWhereIf(
462 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED,
463 isset( $show['patrolled'] )
464 );
465 $this->addWhereIf(
466 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED,
467 isset( $show['!autopatrolled'] )
468 );
469 $this->addWhereIf(
470 'rc_patrolled = ' . RecentChange::PRC_AUTOPATROLLED,
471 isset( $show['autopatrolled'] )
472 );
473 $this->addWhereIf( $idField . ' != page_latest', isset( $show['!top'] ) );
474 $this->addWhereIf( $idField . ' = page_latest', isset( $show['top'] ) );
475 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
476 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
477 }
478 $this->addOption( 'LIMIT', $limit + 1 );
479
480 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
481 isset( $show['autopatrolled'] ) || isset( $show['!autopatrolled'] ) || $this->fld_patrolled
482 ) {
483 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
484 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
485 }
486
487 $isFilterset = isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
488 isset( $show['autopatrolled'] ) || isset( $show['!autopatrolled'] );
489 $this->addTables( 'recentchanges' );
490 $this->addJoinConds( [ 'recentchanges' => [
491 $isFilterset ? 'JOIN' : 'LEFT JOIN',
492 [
493 // This is a crazy hack. recentchanges has no index on rc_this_oldid, so instead of adding
494 // one T19237 did a join using rc_user_text and rc_timestamp instead. Now rc_user_text is
495 // probably unavailable, so just do rc_timestamp.
496 'rc_timestamp = ' . $tsField,
497 'rc_this_oldid = ' . $idField,
498 ]
499 ] ] );
500 }
501
502 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
503
504 if ( $this->fld_tags ) {
505 $this->addFields( [ 'ts_tags' => ChangeTags::makeTagSummarySubquery( 'revision' ) ] );
506 }
507
508 if ( isset( $this->params['tag'] ) ) {
509 $this->addTables( 'change_tag' );
510 $this->addJoinConds(
511 [ 'change_tag' => [ 'JOIN', [ $idField . ' = ct_rev_id' ] ] ]
512 );
513 $changeTagDefStore = MediaWikiServices::getInstance()->getChangeTagDefStore();
514 try {
515 $this->addWhereFld( 'ct_tag_id', $changeTagDefStore->getId( $this->params['tag'] ) );
516 } catch ( NameTableAccessException $exception ) {
517 // Return nothing.
518 $this->addWhere( '1=0' );
519 }
520 }
521 }
522
523 /**
524 * Extract fields from the database row and append them to a result array
525 *
526 * @param stdClass $row
527 * @return array
528 */
529 private function extractRowInfo( $row ) {
530 $vals = [];
531 $anyHidden = false;
532
533 if ( $row->rev_deleted & RevisionRecord::DELETED_TEXT ) {
534 $vals['texthidden'] = true;
535 $anyHidden = true;
536 }
537
538 // Any rows where we can't view the user were filtered out in the query.
539 $vals['userid'] = (int)$row->rev_user;
540 $vals['user'] = $row->rev_user_text;
541 if ( $row->rev_deleted & RevisionRecord::DELETED_USER ) {
542 $vals['userhidden'] = true;
543 $anyHidden = true;
544 }
545 if ( $this->fld_ids ) {
546 $vals['pageid'] = (int)$row->rev_page;
547 $vals['revid'] = (int)$row->rev_id;
548
549 if ( !is_null( $row->rev_parent_id ) ) {
550 $vals['parentid'] = (int)$row->rev_parent_id;
551 }
552 }
553
554 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
555
556 if ( $this->fld_title ) {
557 ApiQueryBase::addTitleInfo( $vals, $title );
558 }
559
560 if ( $this->fld_timestamp ) {
561 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
562 }
563
564 if ( $this->fld_flags ) {
565 $vals['new'] = $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id );
566 $vals['minor'] = (bool)$row->rev_minor_edit;
567 $vals['top'] = $row->page_latest == $row->rev_id;
568 }
569
570 if ( $this->fld_comment || $this->fld_parsedcomment ) {
571 if ( $row->rev_deleted & RevisionRecord::DELETED_COMMENT ) {
572 $vals['commenthidden'] = true;
573 $anyHidden = true;
574 }
575
576 $userCanView = RevisionRecord::userCanBitfield(
577 $row->rev_deleted,
578 RevisionRecord::DELETED_COMMENT, $this->getUser()
579 );
580
581 if ( $userCanView ) {
582 $comment = $this->commentStore->getComment( 'rev_comment', $row )->text;
583 if ( $this->fld_comment ) {
584 $vals['comment'] = $comment;
585 }
586
587 if ( $this->fld_parsedcomment ) {
588 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
589 }
590 }
591 }
592
593 if ( $this->fld_patrolled ) {
594 $vals['patrolled'] = $row->rc_patrolled != RecentChange::PRC_UNPATROLLED;
595 $vals['autopatrolled'] = $row->rc_patrolled == RecentChange::PRC_AUTOPATROLLED;
596 }
597
598 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
599 $vals['size'] = (int)$row->rev_len;
600 }
601
602 if ( $this->fld_sizediff
603 && !is_null( $row->rev_len )
604 && !is_null( $row->rev_parent_id )
605 ) {
606 $parentLen = $this->parentLens[$row->rev_parent_id] ?? 0;
607 $vals['sizediff'] = (int)$row->rev_len - $parentLen;
608 }
609
610 if ( $this->fld_tags ) {
611 if ( $row->ts_tags ) {
612 $tags = explode( ',', $row->ts_tags );
613 ApiResult::setIndexedTagName( $tags, 'tag' );
614 $vals['tags'] = $tags;
615 } else {
616 $vals['tags'] = [];
617 }
618 }
619
620 if ( $anyHidden && ( $row->rev_deleted & RevisionRecord::DELETED_RESTRICTED ) ) {
621 $vals['suppressed'] = true;
622 }
623
624 return $vals;
625 }
626
627 private function continueStr( $row ) {
628 if ( $this->multiUserMode ) {
629 switch ( $this->orderBy ) {
630 case 'id':
631 return "id|$row->rev_user|$row->rev_timestamp|$row->rev_id";
632 case 'name':
633 return "name|$row->rev_user_text|$row->rev_timestamp|$row->rev_id";
634 case 'actor':
635 return "actor|$row->rev_actor|$row->rev_timestamp|$row->rev_id";
636 }
637 } else {
638 return "$row->rev_timestamp|$row->rev_id";
639 }
640 }
641
642 public function getCacheMode( $params ) {
643 // This module provides access to deleted revisions and patrol flags if
644 // the requester is logged in
645 return 'anon-public-user-private';
646 }
647
648 public function getAllowedParams() {
649 return [
650 'limit' => [
651 ApiBase::PARAM_DFLT => 10,
652 ApiBase::PARAM_TYPE => 'limit',
653 ApiBase::PARAM_MIN => 1,
654 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
655 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
656 ],
657 'start' => [
658 ApiBase::PARAM_TYPE => 'timestamp'
659 ],
660 'end' => [
661 ApiBase::PARAM_TYPE => 'timestamp'
662 ],
663 'continue' => [
664 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
665 ],
666 'user' => [
667 ApiBase::PARAM_TYPE => 'user',
668 ApiBase::PARAM_ISMULTI => true
669 ],
670 'userids' => [
671 ApiBase::PARAM_TYPE => 'integer',
672 ApiBase::PARAM_ISMULTI => true
673 ],
674 'userprefix' => null,
675 'dir' => [
676 ApiBase::PARAM_DFLT => 'older',
677 ApiBase::PARAM_TYPE => [
678 'newer',
679 'older'
680 ],
681 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
682 ],
683 'namespace' => [
684 ApiBase::PARAM_ISMULTI => true,
685 ApiBase::PARAM_TYPE => 'namespace'
686 ],
687 'prop' => [
688 ApiBase::PARAM_ISMULTI => true,
689 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
690 ApiBase::PARAM_TYPE => [
691 'ids',
692 'title',
693 'timestamp',
694 'comment',
695 'parsedcomment',
696 'size',
697 'sizediff',
698 'flags',
699 'patrolled',
700 'tags'
701 ],
702 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
703 ],
704 'show' => [
705 ApiBase::PARAM_ISMULTI => true,
706 ApiBase::PARAM_TYPE => [
707 'minor',
708 '!minor',
709 'patrolled',
710 '!patrolled',
711 'autopatrolled',
712 '!autopatrolled',
713 'top',
714 '!top',
715 'new',
716 '!new',
717 ],
718 ApiBase::PARAM_HELP_MSG => [
719 'apihelp-query+usercontribs-param-show',
720 $this->getConfig()->get( 'RCMaxAge' )
721 ],
722 ],
723 'tag' => null,
724 'toponly' => [
725 ApiBase::PARAM_DFLT => false,
726 ApiBase::PARAM_DEPRECATED => true,
727 ],
728 ];
729 }
730
731 protected function getExamplesMessages() {
732 return [
733 'action=query&list=usercontribs&ucuser=Example'
734 => 'apihelp-query+usercontribs-example-user',
735 'action=query&list=usercontribs&ucuserprefix=192.0.2.'
736 => 'apihelp-query+usercontribs-example-ipprefix',
737 ];
738 }
739
740 public function getHelpUrls() {
741 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Usercontribs';
742 }
743 }
744
745 /**
746 * @since 1.9
747 * @deprecated since 1.32
748 */
749 class_alias( ApiQueryUserContribs::class, 'ApiQueryContributions' );