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