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