Merge "Fix 'Tags' padding to keep it farther from the edge and document the source...
[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 /**
24 * This query action adds a list of a specified user's contributions to the output.
25 *
26 * @ingroup API
27 */
28 class ApiQueryUserContribs extends ApiQueryBase {
29
30 public function __construct( ApiQuery $query, $moduleName ) {
31 parent::__construct( $query, $moduleName, 'uc' );
32 }
33
34 private $params, $multiUserMode, $orderBy, $parentLens, $commentStore;
35
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;
39
40 public function execute() {
41 global $wgActorTableSchemaMigrationStage;
42
43 // Parse some parameters
44 $this->params = $this->extractRequestParams();
45
46 $this->commentStore = CommentStore::getStore();
47
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'] );
59
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
64
65 // TODO: if the query is going only against the revision table, should this be done?
66 $this->selectNamedDB( 'contributions', DB_REPLICA, 'contributions' );
67
68 $sort = ( $this->params['dir'] == 'newer' ? '' : ' DESC' );
69 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
70
71 // Create an Iterator that produces the UserIdentity objects we need, depending
72 // on which of the 'userprefix', 'userids', or 'user' params was
73 // specified.
74 $this->requireOnlyOneParameter( $this->params, 'userprefix', 'userids', 'user' );
75 if ( isset( $this->params['userprefix'] ) ) {
76 $this->multiUserMode = true;
77 $this->orderBy = 'name';
78 $fname = __METHOD__;
79
80 // Because 'userprefix' might produce a huge number of users (e.g.
81 // a wiki with users "Test00000001" to "Test99999999"), use a
82 // generator with batched lookup and continuation.
83 $userIter = call_user_func( function () use ( $dbSecondary, $sort, $op, $fname ) {
84 global $wgActorTableSchemaMigrationStage;
85
86 $fromName = false;
87 if ( !is_null( $this->params['continue'] ) ) {
88 $continue = explode( '|', $this->params['continue'] );
89 $this->dieContinueUsageIf( count( $continue ) != 4 );
90 $this->dieContinueUsageIf( $continue[0] !== 'name' );
91 $fromName = $continue[1];
92 }
93 $like = $dbSecondary->buildLike( $this->params['userprefix'], $dbSecondary->anyString() );
94
95 $limit = 501;
96
97 do {
98 $from = $fromName ? "$op= " . $dbSecondary->addQuotes( $fromName ) : false;
99
100 // For the new schema, pull from the actor table. For the
101 // old, pull from rev_user. For migration a FULL [OUTER]
102 // JOIN would be what we want, except MySQL doesn't support
103 // that so we have to UNION instead.
104 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
105 $res = $dbSecondary->select(
106 'actor',
107 [ 'actor_id', 'user_id' => 'COALESCE(actor_user,0)', 'user_name' => 'actor_name' ],
108 array_merge( [ "actor_name$like" ], $from ? [ "actor_name $from" ] : [] ),
109 $fname,
110 [ 'ORDER BY' => [ "user_name $sort" ], 'LIMIT' => $limit ]
111 );
112 } elseif ( $wgActorTableSchemaMigrationStage === MIGRATION_OLD ) {
113 $res = $dbSecondary->select(
114 'revision',
115 [ 'actor_id' => 'NULL', 'user_id' => 'rev_user', 'user_name' => 'rev_user_text' ],
116 array_merge( [ "rev_user_text$like" ], $from ? [ "rev_user_text $from" ] : [] ),
117 $fname,
118 [ 'DISTINCT', 'ORDER BY' => [ "rev_user_text $sort" ], 'LIMIT' => $limit ]
119 );
120 } else {
121 // There are three queries we have to combine to be sure of getting all results:
122 // - actor table (any rows that have been migrated will have empty rev_user_text)
123 // - revision+actor by user id
124 // - revision+actor by name for anons
125 $options = $dbSecondary->unionSupportsOrderAndLimit()
126 ? [ 'ORDER BY' => [ "user_name $sort" ], 'LIMIT' => $limit ] : [];
127 $subsql = [];
128 $subsql[] = $dbSecondary->selectSQLText(
129 'actor',
130 [ 'actor_id', 'user_id' => 'COALESCE(actor_user,0)', 'user_name' => 'actor_name' ],
131 array_merge( [ "actor_name$like" ], $from ? [ "actor_name $from" ] : [] ),
132 $fname,
133 $options
134 );
135 $subsql[] = $dbSecondary->selectSQLText(
136 [ 'revision', 'actor' ],
137 [ 'actor_id', 'user_id' => 'rev_user', 'user_name' => 'rev_user_text' ],
138 array_merge(
139 [ "rev_user_text$like", 'rev_user != 0' ],
140 $from ? [ "rev_user_text $from" ] : []
141 ),
142 $fname,
143 array_merge( [ 'DISTINCT' ], $options ),
144 [ 'actor' => [ 'LEFT JOIN', 'rev_user = actor_user' ] ]
145 );
146 $subsql[] = $dbSecondary->selectSQLText(
147 [ 'revision', 'actor' ],
148 [ 'actor_id', 'user_id' => 'rev_user', 'user_name' => 'rev_user_text' ],
149 array_merge(
150 [ "rev_user_text$like", 'rev_user = 0' ],
151 $from ? [ "rev_user_text $from" ] : []
152 ),
153 $fname,
154 array_merge( [ 'DISTINCT' ], $options ),
155 [ 'actor' => [ 'LEFT JOIN', 'rev_user_text = actor_name' ] ]
156 );
157 $sql = $dbSecondary->unionQueries( $subsql, false ) . " ORDER BY user_name $sort";
158 $sql = $dbSecondary->limitResult( $sql, $limit );
159 $res = $dbSecondary->query( $sql, $fname );
160 }
161
162 $count = 0;
163 $fromName = false;
164 foreach ( $res as $row ) {
165 if ( ++$count >= $limit ) {
166 $fromName = $row->user_name;
167 break;
168 }
169 yield User::newFromRow( $row );
170 }
171 } while ( $fromName !== false );
172 } );
173 // Do the actual sorting client-side, because otherwise
174 // prepareQuery might try to sort by actor and confuse everything.
175 $batchSize = 1;
176 } elseif ( isset( $this->params['userids'] ) ) {
177 if ( !count( $this->params['userids'] ) ) {
178 $encParamName = $this->encodeParamName( 'userids' );
179 $this->dieWithError( [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName" );
180 }
181
182 $ids = [];
183 foreach ( $this->params['userids'] as $uid ) {
184 if ( $uid <= 0 ) {
185 $this->dieWithError( [ 'apierror-invaliduserid', $uid ], 'invaliduserid' );
186 }
187 $ids[] = $uid;
188 }
189
190 $this->orderBy = 'id';
191 $this->multiUserMode = count( $ids ) > 1;
192
193 $from = $fromId = false;
194 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
195 $continue = explode( '|', $this->params['continue'] );
196 $this->dieContinueUsageIf( count( $continue ) != 4 );
197 $this->dieContinueUsageIf( $continue[0] !== 'id' && $continue[0] !== 'actor' );
198 $fromId = (int)$continue[1];
199 $this->dieContinueUsageIf( $continue[1] !== (string)$fromId );
200 $from = "$op= $fromId";
201 }
202
203 // For the new schema, just select from the actor table. For the
204 // old and transitional schemas, select from user and left join
205 // actor if it exists.
206 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
207 $res = $dbSecondary->select(
208 'actor',
209 [ 'actor_id', 'user_id' => 'actor_user', 'user_name' => 'actor_name' ],
210 array_merge( [ 'actor_user' => $ids ], $from ? [ "actor_id $from" ] : [] ),
211 __METHOD__,
212 [ 'ORDER BY' => "user_id $sort" ]
213 );
214 } elseif ( $wgActorTableSchemaMigrationStage === MIGRATION_OLD ) {
215 $res = $dbSecondary->select(
216 'user',
217 [ 'actor_id' => 'NULL', 'user_id' => 'user_id', 'user_name' => 'user_name' ],
218 array_merge( [ 'user_id' => $ids ], $from ? [ "user_id $from" ] : [] ),
219 __METHOD__,
220 [ 'ORDER BY' => "user_id $sort" ]
221 );
222 } else {
223 $res = $dbSecondary->select(
224 [ 'user', 'actor' ],
225 [ 'actor_id', 'user_id', 'user_name' ],
226 array_merge( [ 'user_id' => $ids ], $from ? [ "user_id $from" ] : [] ),
227 __METHOD__,
228 [ 'ORDER BY' => "user_id $sort" ],
229 [ 'actor' => [ 'LEFT JOIN', 'actor_user = user_id' ] ]
230 );
231 }
232 $userIter = UserArray::newFromResult( $res );
233 $batchSize = count( $ids );
234 } else {
235 $names = [];
236 if ( !count( $this->params['user'] ) ) {
237 $encParamName = $this->encodeParamName( 'user' );
238 $this->dieWithError(
239 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
240 );
241 }
242 foreach ( $this->params['user'] as $u ) {
243 if ( $u === '' ) {
244 $encParamName = $this->encodeParamName( 'user' );
245 $this->dieWithError(
246 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
247 );
248 }
249
250 if ( User::isIP( $u ) || ExternalUserNames::isExternal( $u ) ) {
251 $names[$u] = null;
252 } else {
253 $name = User::getCanonicalName( $u, 'valid' );
254 if ( $name === false ) {
255 $encParamName = $this->encodeParamName( 'user' );
256 $this->dieWithError(
257 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $u ) ], "baduser_$encParamName"
258 );
259 }
260 $names[$name] = null;
261 }
262 }
263
264 $this->orderBy = 'name';
265 $this->multiUserMode = count( $names ) > 1;
266
267 $from = $fromName = false;
268 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
269 $continue = explode( '|', $this->params['continue'] );
270 $this->dieContinueUsageIf( count( $continue ) != 4 );
271 $this->dieContinueUsageIf( $continue[0] !== 'name' && $continue[0] !== 'actor' );
272 $fromName = $continue[1];
273 $from = "$op= " . $dbSecondary->addQuotes( $fromName );
274 }
275
276 // For the new schema, just select from the actor table. For the
277 // old and transitional schemas, select from user and left join
278 // actor if it exists then merge in any unknown users (IPs and imports).
279 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
280 $res = $dbSecondary->select(
281 'actor',
282 [ 'actor_id', 'user_id' => 'actor_user', 'user_name' => 'actor_name' ],
283 array_merge( [ 'actor_name' => array_keys( $names ) ], $from ? [ "actor_id $from" ] : [] ),
284 __METHOD__,
285 [ 'ORDER BY' => "actor_name $sort" ]
286 );
287 $userIter = UserArray::newFromResult( $res );
288 } else {
289 if ( $wgActorTableSchemaMigrationStage === MIGRATION_OLD ) {
290 $res = $dbSecondary->select(
291 'user',
292 [ 'actor_id' => 'NULL', 'user_id', 'user_name' ],
293 array_merge( [ 'user_name' => array_keys( $names ) ], $from ? [ "user_name $from" ] : [] ),
294 __METHOD__
295 );
296 } else {
297 $res = $dbSecondary->select(
298 [ 'user', 'actor' ],
299 [ 'actor_id', 'user_id', 'user_name' ],
300 array_merge( [ 'user_name' => array_keys( $names ) ], $from ? [ "user_name $from" ] : [] ),
301 __METHOD__,
302 [],
303 [ 'actor' => [ 'LEFT JOIN', 'actor_user = user_id' ] ]
304 );
305 }
306 foreach ( $res as $row ) {
307 $names[$row->user_name] = $row;
308 }
309 if ( $this->params['dir'] == 'newer' ) {
310 ksort( $names, SORT_STRING );
311 } else {
312 krsort( $names, SORT_STRING );
313 }
314 $neg = $op === '>' ? -1 : 1;
315 $userIter = call_user_func( function () use ( $names, $fromName, $neg ) {
316 foreach ( $names as $name => $row ) {
317 if ( $fromName === false || $neg * strcmp( $name, $fromName ) <= 0 ) {
318 $user = $row ? User::newFromRow( $row ) : User::newFromName( $name, false );
319 yield $user;
320 }
321 }
322 } );
323 }
324 $batchSize = count( $names );
325 }
326
327 // During migration, force ordering on the client side because we're
328 // having to combine multiple queries that would otherwise have
329 // different sort orders.
330 if ( $wgActorTableSchemaMigrationStage === MIGRATION_WRITE_BOTH ||
331 $wgActorTableSchemaMigrationStage === MIGRATION_WRITE_NEW
332 ) {
333 $batchSize = 1;
334 }
335
336 // With the new schema, the DB query will order by actor so update $this->orderBy to match.
337 if ( $batchSize > 1 && $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
338 $this->orderBy = 'actor';
339 }
340
341 $count = 0;
342 $limit = $this->params['limit'];
343 $userIter->rewind();
344 while ( $userIter->valid() ) {
345 $users = [];
346 while ( count( $users ) < $batchSize && $userIter->valid() ) {
347 $users[] = $userIter->current();
348 $userIter->next();
349 }
350
351 // Ugh. We have to run the query three times, once for each
352 // possible 'orcond' from ActorMigration, and then merge them all
353 // together in the proper order. And preserving the correct
354 // $hookData for each one.
355 // @todo When ActorMigration is removed, this can go back to a
356 // single prepare and select.
357 $merged = [];
358 foreach ( [ 'actor', 'userid', 'username' ] as $which ) {
359 if ( $this->prepareQuery( $users, $limit - $count, $which ) ) {
360 $hookData = [];
361 $res = $this->select( __METHOD__, [], $hookData );
362 foreach ( $res as $row ) {
363 $merged[] = [ $row, &$hookData ];
364 }
365 }
366 }
367 $neg = $this->params['dir'] == 'newer' ? 1 : -1;
368 usort( $merged, function ( $a, $b ) use ( $neg, $batchSize ) {
369 if ( $batchSize === 1 ) { // One user, can't be different
370 $ret = 0;
371 } elseif ( $this->orderBy === 'id' ) {
372 $ret = $a[0]->rev_user <=> $b[0]->rev_user;
373 } elseif ( $this->orderBy === 'name' ) {
374 $ret = strcmp( $a[0]->rev_user_text, $b[0]->rev_user_text );
375 } else {
376 $ret = $a[0]->rev_actor <=> $b[0]->rev_actor;
377 }
378
379 if ( !$ret ) {
380 $ret = strcmp(
381 wfTimestamp( TS_MW, $a[0]->rev_timestamp ),
382 wfTimestamp( TS_MW, $b[0]->rev_timestamp )
383 );
384 }
385
386 if ( !$ret ) {
387 $ret = $a[0]->rev_id <=> $b[0]->rev_id;
388 }
389
390 return $neg * $ret;
391 } );
392 $merged = array_slice( $merged, 0, $limit - $count + 1 );
393 // (end "Ugh")
394
395 if ( $this->fld_sizediff ) {
396 $revIds = [];
397 foreach ( $merged as $data ) {
398 if ( $data[0]->rev_parent_id ) {
399 $revIds[] = $data[0]->rev_parent_id;
400 }
401 }
402 $this->parentLens = Revision::getParentLengths( $dbSecondary, $revIds );
403 }
404
405 foreach ( $merged as $data ) {
406 $row = $data[0];
407 $hookData = &$data[1];
408 if ( ++$count > $limit ) {
409 // We've reached the one extra which shows that there are
410 // additional pages to be had. Stop here...
411 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
412 break 2;
413 }
414
415 $vals = $this->extractRowInfo( $row );
416 $fit = $this->processRow( $row, $vals, $hookData ) &&
417 $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
418 if ( !$fit ) {
419 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
420 break 2;
421 }
422 }
423 }
424
425 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
426 }
427
428 /**
429 * Prepares the query and returns the limit of rows requested
430 * @param User[] $users
431 * @param int $limit
432 * @param string $which 'actor', 'userid', or 'username'
433 * @return bool
434 */
435 private function prepareQuery( array $users, $limit, $which ) {
436 global $wgActorTableSchemaMigrationStage;
437
438 $this->resetQueryParams();
439 $db = $this->getDB();
440
441 $revQuery = Revision::getQueryInfo( [ 'page' ] );
442 $this->addTables( $revQuery['tables'] );
443 $this->addJoinConds( $revQuery['joins'] );
444 $this->addFields( $revQuery['fields'] );
445
446 $revWhere = ActorMigration::newMigration()->getWhere( $db, 'rev_user', $users );
447 if ( !isset( $revWhere['orconds'][$which] ) ) {
448 return false;
449 }
450 $this->addWhere( $revWhere['orconds'][$which] );
451
452 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
453 $orderUserField = 'rev_actor';
454 $userField = $this->orderBy === 'actor' ? 'revactor_actor' : 'actor_name';
455 } else {
456 $orderUserField = $this->orderBy === 'id' ? 'rev_user' : 'rev_user_text';
457 $userField = $revQuery['fields'][$orderUserField];
458 }
459 if ( $which === 'actor' ) {
460 $tsField = 'revactor_timestamp';
461 $idField = 'revactor_rev';
462 } else {
463 $tsField = 'rev_timestamp';
464 $idField = 'rev_id';
465 }
466
467 // Handle continue parameter
468 if ( !is_null( $this->params['continue'] ) ) {
469 $continue = explode( '|', $this->params['continue'] );
470 if ( $this->multiUserMode ) {
471 $this->dieContinueUsageIf( count( $continue ) != 4 );
472 $modeFlag = array_shift( $continue );
473 $this->dieContinueUsageIf( $modeFlag !== $this->orderBy );
474 $encUser = $db->addQuotes( array_shift( $continue ) );
475 } else {
476 $this->dieContinueUsageIf( count( $continue ) != 2 );
477 }
478 $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
479 $encId = (int)$continue[1];
480 $this->dieContinueUsageIf( $encId != $continue[1] );
481 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
482 if ( $this->multiUserMode ) {
483 $this->addWhere(
484 "$userField $op $encUser OR " .
485 "($userField = $encUser AND " .
486 "($tsField $op $encTS OR " .
487 "($tsField = $encTS AND " .
488 "$idField $op= $encId)))"
489 );
490 } else {
491 $this->addWhere(
492 "$tsField $op $encTS OR " .
493 "($tsField = $encTS AND " .
494 "$idField $op= $encId)"
495 );
496 }
497 }
498
499 // Don't include any revisions where we're not supposed to be able to
500 // see the username.
501 $user = $this->getUser();
502 if ( !$user->isAllowed( 'deletedhistory' ) ) {
503 $bitmask = Revision::DELETED_USER;
504 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
505 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
506 } else {
507 $bitmask = 0;
508 }
509 if ( $bitmask ) {
510 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
511 }
512
513 // Add the user field to ORDER BY if there are multiple users
514 if ( count( $users ) > 1 ) {
515 $this->addWhereRange( $orderUserField, $this->params['dir'], null, null );
516 }
517
518 // Then timestamp
519 $this->addTimestampWhereRange( $tsField,
520 $this->params['dir'], $this->params['start'], $this->params['end'] );
521
522 // Then rev_id for a total ordering
523 $this->addWhereRange( $idField, $this->params['dir'], null, null );
524
525 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
526
527 $show = $this->params['show'];
528 if ( $this->params['toponly'] ) { // deprecated/old param
529 $show[] = 'top';
530 }
531 if ( !is_null( $show ) ) {
532 $show = array_flip( $show );
533
534 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
535 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
536 || ( isset( $show['autopatrolled'] ) && isset( $show['!autopatrolled'] ) )
537 || ( isset( $show['autopatrolled'] ) && isset( $show['!patrolled'] ) )
538 || ( isset( $show['top'] ) && isset( $show['!top'] ) )
539 || ( isset( $show['new'] ) && isset( $show['!new'] ) )
540 ) {
541 $this->dieWithError( 'apierror-show' );
542 }
543
544 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
545 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
546 $this->addWhereIf(
547 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED,
548 isset( $show['!patrolled'] )
549 );
550 $this->addWhereIf(
551 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED,
552 isset( $show['patrolled'] )
553 );
554 $this->addWhereIf(
555 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED,
556 isset( $show['!autopatrolled'] )
557 );
558 $this->addWhereIf(
559 'rc_patrolled = ' . RecentChange::PRC_AUTOPATROLLED,
560 isset( $show['autopatrolled'] )
561 );
562 $this->addWhereIf( $idField . ' != page_latest', isset( $show['!top'] ) );
563 $this->addWhereIf( $idField . ' = page_latest', isset( $show['top'] ) );
564 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
565 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
566 }
567 $this->addOption( 'LIMIT', $limit + 1 );
568
569 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
570 isset( $show['autopatrolled'] ) || isset( $show['!autopatrolled'] ) || $this->fld_patrolled
571 ) {
572 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
573 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
574 }
575
576 $isFilterset = isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
577 isset( $show['autopatrolled'] ) || isset( $show['!autopatrolled'] );
578 $this->addTables( 'recentchanges' );
579 $this->addJoinConds( [ 'recentchanges' => [
580 $isFilterset ? 'JOIN' : 'LEFT JOIN',
581 [
582 // This is a crazy hack. recentchanges has no index on rc_this_oldid, so instead of adding
583 // one T19237 did a join using rc_user_text and rc_timestamp instead. Now rc_user_text is
584 // probably unavailable, so just do rc_timestamp.
585 'rc_timestamp = ' . $tsField,
586 'rc_this_oldid = ' . $idField,
587 ]
588 ] ] );
589 }
590
591 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
592
593 if ( $this->fld_tags ) {
594 $this->addTables( 'tag_summary' );
595 $this->addJoinConds(
596 [ 'tag_summary' => [ 'LEFT JOIN', [ $idField . ' = ts_rev_id' ] ] ]
597 );
598 $this->addFields( 'ts_tags' );
599 }
600
601 if ( isset( $this->params['tag'] ) ) {
602 $this->addTables( 'change_tag' );
603 $this->addJoinConds(
604 [ 'change_tag' => [ 'INNER JOIN', [ $idField . ' = ct_rev_id' ] ] ]
605 );
606 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
607 }
608
609 return true;
610 }
611
612 /**
613 * Extract fields from the database row and append them to a result array
614 *
615 * @param stdClass $row
616 * @return array
617 */
618 private function extractRowInfo( $row ) {
619 $vals = [];
620 $anyHidden = false;
621
622 if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
623 $vals['texthidden'] = true;
624 $anyHidden = true;
625 }
626
627 // Any rows where we can't view the user were filtered out in the query.
628 $vals['userid'] = (int)$row->rev_user;
629 $vals['user'] = $row->rev_user_text;
630 if ( $row->rev_deleted & Revision::DELETED_USER ) {
631 $vals['userhidden'] = true;
632 $anyHidden = true;
633 }
634 if ( $this->fld_ids ) {
635 $vals['pageid'] = intval( $row->rev_page );
636 $vals['revid'] = intval( $row->rev_id );
637 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
638
639 if ( !is_null( $row->rev_parent_id ) ) {
640 $vals['parentid'] = intval( $row->rev_parent_id );
641 }
642 }
643
644 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
645
646 if ( $this->fld_title ) {
647 ApiQueryBase::addTitleInfo( $vals, $title );
648 }
649
650 if ( $this->fld_timestamp ) {
651 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
652 }
653
654 if ( $this->fld_flags ) {
655 $vals['new'] = $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id );
656 $vals['minor'] = (bool)$row->rev_minor_edit;
657 $vals['top'] = $row->page_latest == $row->rev_id;
658 }
659
660 if ( $this->fld_comment || $this->fld_parsedcomment ) {
661 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
662 $vals['commenthidden'] = true;
663 $anyHidden = true;
664 }
665
666 $userCanView = Revision::userCanBitfield(
667 $row->rev_deleted,
668 Revision::DELETED_COMMENT, $this->getUser()
669 );
670
671 if ( $userCanView ) {
672 $comment = $this->commentStore->getComment( 'rev_comment', $row )->text;
673 if ( $this->fld_comment ) {
674 $vals['comment'] = $comment;
675 }
676
677 if ( $this->fld_parsedcomment ) {
678 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
679 }
680 }
681 }
682
683 if ( $this->fld_patrolled ) {
684 $vals['patrolled'] = $row->rc_patrolled != RecentChange::PRC_UNPATROLLED;
685 $vals['autopatrolled'] = $row->rc_patrolled == RecentChange::PRC_AUTOPATROLLED;
686 }
687
688 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
689 $vals['size'] = intval( $row->rev_len );
690 }
691
692 if ( $this->fld_sizediff
693 && !is_null( $row->rev_len )
694 && !is_null( $row->rev_parent_id )
695 ) {
696 $parentLen = $this->parentLens[$row->rev_parent_id] ?? 0;
697 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
698 }
699
700 if ( $this->fld_tags ) {
701 if ( $row->ts_tags ) {
702 $tags = explode( ',', $row->ts_tags );
703 ApiResult::setIndexedTagName( $tags, 'tag' );
704 $vals['tags'] = $tags;
705 } else {
706 $vals['tags'] = [];
707 }
708 }
709
710 if ( $anyHidden && $row->rev_deleted & Revision::DELETED_RESTRICTED ) {
711 $vals['suppressed'] = true;
712 }
713
714 return $vals;
715 }
716
717 private function continueStr( $row ) {
718 if ( $this->multiUserMode ) {
719 switch ( $this->orderBy ) {
720 case 'id':
721 return "id|$row->rev_user|$row->rev_timestamp|$row->rev_id";
722 case 'name':
723 return "name|$row->rev_user_text|$row->rev_timestamp|$row->rev_id";
724 case 'actor':
725 return "actor|$row->rev_actor|$row->rev_timestamp|$row->rev_id";
726 }
727 } else {
728 return "$row->rev_timestamp|$row->rev_id";
729 }
730 }
731
732 public function getCacheMode( $params ) {
733 // This module provides access to deleted revisions and patrol flags if
734 // the requester is logged in
735 return 'anon-public-user-private';
736 }
737
738 public function getAllowedParams() {
739 return [
740 'limit' => [
741 ApiBase::PARAM_DFLT => 10,
742 ApiBase::PARAM_TYPE => 'limit',
743 ApiBase::PARAM_MIN => 1,
744 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
745 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
746 ],
747 'start' => [
748 ApiBase::PARAM_TYPE => 'timestamp'
749 ],
750 'end' => [
751 ApiBase::PARAM_TYPE => 'timestamp'
752 ],
753 'continue' => [
754 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
755 ],
756 'user' => [
757 ApiBase::PARAM_TYPE => 'user',
758 ApiBase::PARAM_ISMULTI => true
759 ],
760 'userids' => [
761 ApiBase::PARAM_TYPE => 'integer',
762 ApiBase::PARAM_ISMULTI => true
763 ],
764 'userprefix' => null,
765 'dir' => [
766 ApiBase::PARAM_DFLT => 'older',
767 ApiBase::PARAM_TYPE => [
768 'newer',
769 'older'
770 ],
771 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
772 ],
773 'namespace' => [
774 ApiBase::PARAM_ISMULTI => true,
775 ApiBase::PARAM_TYPE => 'namespace'
776 ],
777 'prop' => [
778 ApiBase::PARAM_ISMULTI => true,
779 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
780 ApiBase::PARAM_TYPE => [
781 'ids',
782 'title',
783 'timestamp',
784 'comment',
785 'parsedcomment',
786 'size',
787 'sizediff',
788 'flags',
789 'patrolled',
790 'tags'
791 ],
792 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
793 ],
794 'show' => [
795 ApiBase::PARAM_ISMULTI => true,
796 ApiBase::PARAM_TYPE => [
797 'minor',
798 '!minor',
799 'patrolled',
800 '!patrolled',
801 'autopatrolled',
802 '!autopatrolled',
803 'top',
804 '!top',
805 'new',
806 '!new',
807 ],
808 ApiBase::PARAM_HELP_MSG => [
809 'apihelp-query+usercontribs-param-show',
810 $this->getConfig()->get( 'RCMaxAge' )
811 ],
812 ],
813 'tag' => null,
814 'toponly' => [
815 ApiBase::PARAM_DFLT => false,
816 ApiBase::PARAM_DEPRECATED => true,
817 ],
818 ];
819 }
820
821 protected function getExamplesMessages() {
822 return [
823 'action=query&list=usercontribs&ucuser=Example'
824 => 'apihelp-query+usercontribs-example-user',
825 'action=query&list=usercontribs&ucuserprefix=192.0.2.'
826 => 'apihelp-query+usercontribs-example-ipprefix',
827 ];
828 }
829
830 public function getHelpUrls() {
831 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Usercontribs';
832 }
833 }
834
835 /**
836 * @since 1.9
837 * @deprecated since 1.32
838 */
839 class_alias( ApiQueryUserContribs::class, 'ApiQueryContributions' );