Rename ApiQueryContributions to ApiQueryUserContribs to match api module name
[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 call_user_func_array(
310 $this->params['dir'] == 'newer' ? 'ksort' : 'krsort', [ &$names, SORT_STRING ]
311 );
312 $neg = $op === '>' ? -1 : 1;
313 $userIter = call_user_func( function () use ( $names, $fromName, $neg ) {
314 foreach ( $names as $name => $row ) {
315 if ( $fromName === false || $neg * strcmp( $name, $fromName ) <= 0 ) {
316 $user = $row ? User::newFromRow( $row ) : User::newFromName( $name, false );
317 yield $user;
318 }
319 }
320 } );
321 }
322 $batchSize = count( $names );
323 }
324
325 // During migration, force ordering on the client side because we're
326 // having to combine multiple queries that would otherwise have
327 // different sort orders.
328 if ( $wgActorTableSchemaMigrationStage === MIGRATION_WRITE_BOTH ||
329 $wgActorTableSchemaMigrationStage === MIGRATION_WRITE_NEW
330 ) {
331 $batchSize = 1;
332 }
333
334 // With the new schema, the DB query will order by actor so update $this->orderBy to match.
335 if ( $batchSize > 1 && $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
336 $this->orderBy = 'actor';
337 }
338
339 $count = 0;
340 $limit = $this->params['limit'];
341 $userIter->rewind();
342 while ( $userIter->valid() ) {
343 $users = [];
344 while ( count( $users ) < $batchSize && $userIter->valid() ) {
345 $users[] = $userIter->current();
346 $userIter->next();
347 }
348
349 // Ugh. We have to run the query three times, once for each
350 // possible 'orcond' from ActorMigration, and then merge them all
351 // together in the proper order. And preserving the correct
352 // $hookData for each one.
353 // @todo When ActorMigration is removed, this can go back to a
354 // single prepare and select.
355 $merged = [];
356 foreach ( [ 'actor', 'userid', 'username' ] as $which ) {
357 if ( $this->prepareQuery( $users, $limit - $count, $which ) ) {
358 $hookData = [];
359 $res = $this->select( __METHOD__, [], $hookData );
360 foreach ( $res as $row ) {
361 $merged[] = [ $row, &$hookData ];
362 }
363 }
364 }
365 $neg = $this->params['dir'] == 'newer' ? 1 : -1;
366 usort( $merged, function ( $a, $b ) use ( $neg, $batchSize ) {
367 if ( $batchSize === 1 ) { // One user, can't be different
368 $ret = 0;
369 } elseif ( $this->orderBy === 'id' ) {
370 $ret = $a[0]->rev_user - $b[0]->rev_user;
371 } elseif ( $this->orderBy === 'name' ) {
372 $ret = strcmp( $a[0]->rev_user_text, $b[0]->rev_user_text );
373 } else {
374 $ret = $a[0]->rev_actor - $b[0]->rev_actor;
375 }
376
377 if ( !$ret ) {
378 $ret = strcmp(
379 wfTimestamp( TS_MW, $a[0]->rev_timestamp ),
380 wfTimestamp( TS_MW, $b[0]->rev_timestamp )
381 );
382 }
383
384 if ( !$ret ) {
385 $ret = $a[0]->rev_id - $b[0]->rev_id;
386 }
387
388 return $neg * $ret;
389 } );
390 $merged = array_slice( $merged, 0, $limit - $count + 1 );
391 // (end "Ugh")
392
393 if ( $this->fld_sizediff ) {
394 $revIds = [];
395 foreach ( $merged as $data ) {
396 if ( $data[0]->rev_parent_id ) {
397 $revIds[] = $data[0]->rev_parent_id;
398 }
399 }
400 $this->parentLens = Revision::getParentLengths( $dbSecondary, $revIds );
401 }
402
403 foreach ( $merged as $data ) {
404 $row = $data[0];
405 $hookData = &$data[1];
406 if ( ++$count > $limit ) {
407 // We've reached the one extra which shows that there are
408 // additional pages to be had. Stop here...
409 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
410 break 2;
411 }
412
413 $vals = $this->extractRowInfo( $row );
414 $fit = $this->processRow( $row, $vals, $hookData ) &&
415 $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
416 if ( !$fit ) {
417 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
418 break 2;
419 }
420 }
421 }
422
423 $this->getResult()->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
424 }
425
426 /**
427 * Prepares the query and returns the limit of rows requested
428 * @param User[] $users
429 * @param int $limit
430 * @param string $which 'actor', 'userid', or 'username'
431 * @return bool
432 */
433 private function prepareQuery( array $users, $limit, $which ) {
434 global $wgActorTableSchemaMigrationStage;
435
436 $this->resetQueryParams();
437 $db = $this->getDB();
438
439 $revQuery = Revision::getQueryInfo( [ 'page' ] );
440 $this->addTables( $revQuery['tables'] );
441 $this->addJoinConds( $revQuery['joins'] );
442 $this->addFields( $revQuery['fields'] );
443
444 $revWhere = ActorMigration::newMigration()->getWhere( $db, 'rev_user', $users );
445 if ( !isset( $revWhere['orconds'][$which] ) ) {
446 return false;
447 }
448 $this->addWhere( $revWhere['orconds'][$which] );
449
450 if ( $wgActorTableSchemaMigrationStage === MIGRATION_NEW ) {
451 $orderUserField = 'rev_actor';
452 $userField = $this->orderBy === 'actor' ? 'revactor_actor' : 'actor_name';
453 } else {
454 $orderUserField = $this->orderBy === 'id' ? 'rev_user' : 'rev_user_text';
455 $userField = $revQuery['fields'][$orderUserField];
456 }
457 if ( $which === 'actor' ) {
458 $tsField = 'revactor_timestamp';
459 $idField = 'revactor_rev';
460 } else {
461 $tsField = 'rev_timestamp';
462 $idField = 'rev_id';
463 }
464
465 // Handle continue parameter
466 if ( !is_null( $this->params['continue'] ) ) {
467 $continue = explode( '|', $this->params['continue'] );
468 if ( $this->multiUserMode ) {
469 $this->dieContinueUsageIf( count( $continue ) != 4 );
470 $modeFlag = array_shift( $continue );
471 $this->dieContinueUsageIf( $modeFlag !== $this->orderBy );
472 $encUser = $db->addQuotes( array_shift( $continue ) );
473 } else {
474 $this->dieContinueUsageIf( count( $continue ) != 2 );
475 }
476 $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
477 $encId = (int)$continue[1];
478 $this->dieContinueUsageIf( $encId != $continue[1] );
479 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
480 if ( $this->multiUserMode ) {
481 $this->addWhere(
482 "$userField $op $encUser OR " .
483 "($userField = $encUser AND " .
484 "($tsField $op $encTS OR " .
485 "($tsField = $encTS AND " .
486 "$idField $op= $encId)))"
487 );
488 } else {
489 $this->addWhere(
490 "$tsField $op $encTS OR " .
491 "($tsField = $encTS AND " .
492 "$idField $op= $encId)"
493 );
494 }
495 }
496
497 // Don't include any revisions where we're not supposed to be able to
498 // see the username.
499 $user = $this->getUser();
500 if ( !$user->isAllowed( 'deletedhistory' ) ) {
501 $bitmask = Revision::DELETED_USER;
502 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
503 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
504 } else {
505 $bitmask = 0;
506 }
507 if ( $bitmask ) {
508 $this->addWhere( $db->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
509 }
510
511 // Add the user field to ORDER BY if there are multiple users
512 if ( count( $users ) > 1 ) {
513 $this->addWhereRange( $orderUserField, $this->params['dir'], null, null );
514 }
515
516 // Then timestamp
517 $this->addTimestampWhereRange( $tsField,
518 $this->params['dir'], $this->params['start'], $this->params['end'] );
519
520 // Then rev_id for a total ordering
521 $this->addWhereRange( $idField, $this->params['dir'], null, null );
522
523 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
524
525 $show = $this->params['show'];
526 if ( $this->params['toponly'] ) { // deprecated/old param
527 $show[] = 'top';
528 }
529 if ( !is_null( $show ) ) {
530 $show = array_flip( $show );
531
532 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
533 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
534 || ( isset( $show['autopatrolled'] ) && isset( $show['!autopatrolled'] ) )
535 || ( isset( $show['autopatrolled'] ) && isset( $show['!patrolled'] ) )
536 || ( isset( $show['top'] ) && isset( $show['!top'] ) )
537 || ( isset( $show['new'] ) && isset( $show['!new'] ) )
538 ) {
539 $this->dieWithError( 'apierror-show' );
540 }
541
542 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
543 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
544 $this->addWhereIf(
545 'rc_patrolled = ' . RecentChange::PRC_UNPATROLLED,
546 isset( $show['!patrolled'] )
547 );
548 $this->addWhereIf(
549 'rc_patrolled != ' . RecentChange::PRC_UNPATROLLED,
550 isset( $show['patrolled'] )
551 );
552 $this->addWhereIf(
553 'rc_patrolled != ' . RecentChange::PRC_AUTOPATROLLED,
554 isset( $show['!autopatrolled'] )
555 );
556 $this->addWhereIf(
557 'rc_patrolled = ' . RecentChange::PRC_AUTOPATROLLED,
558 isset( $show['autopatrolled'] )
559 );
560 $this->addWhereIf( $idField . ' != page_latest', isset( $show['!top'] ) );
561 $this->addWhereIf( $idField . ' = page_latest', isset( $show['top'] ) );
562 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
563 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
564 }
565 $this->addOption( 'LIMIT', $limit + 1 );
566
567 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
568 isset( $show['autopatrolled'] ) || isset( $show['!autopatrolled'] ) || $this->fld_patrolled
569 ) {
570 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
571 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
572 }
573
574 $isFilterset = isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
575 isset( $show['autopatrolled'] ) || isset( $show['!autopatrolled'] );
576 $this->addTables( 'recentchanges' );
577 $this->addJoinConds( [ 'recentchanges' => [
578 $isFilterset ? 'JOIN' : 'LEFT JOIN',
579 [
580 // This is a crazy hack. recentchanges has no index on rc_this_oldid, so instead of adding
581 // one T19237 did a join using rc_user_text and rc_timestamp instead. Now rc_user_text is
582 // probably unavailable, so just do rc_timestamp.
583 'rc_timestamp = ' . $tsField,
584 'rc_this_oldid = ' . $idField,
585 ]
586 ] ] );
587 }
588
589 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
590
591 if ( $this->fld_tags ) {
592 $this->addTables( 'tag_summary' );
593 $this->addJoinConds(
594 [ 'tag_summary' => [ 'LEFT JOIN', [ $idField . ' = ts_rev_id' ] ] ]
595 );
596 $this->addFields( 'ts_tags' );
597 }
598
599 if ( isset( $this->params['tag'] ) ) {
600 $this->addTables( 'change_tag' );
601 $this->addJoinConds(
602 [ 'change_tag' => [ 'INNER JOIN', [ $idField . ' = ct_rev_id' ] ] ]
603 );
604 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
605 }
606
607 return true;
608 }
609
610 /**
611 * Extract fields from the database row and append them to a result array
612 *
613 * @param stdClass $row
614 * @return array
615 */
616 private function extractRowInfo( $row ) {
617 $vals = [];
618 $anyHidden = false;
619
620 if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
621 $vals['texthidden'] = true;
622 $anyHidden = true;
623 }
624
625 // Any rows where we can't view the user were filtered out in the query.
626 $vals['userid'] = (int)$row->rev_user;
627 $vals['user'] = $row->rev_user_text;
628 if ( $row->rev_deleted & Revision::DELETED_USER ) {
629 $vals['userhidden'] = true;
630 $anyHidden = true;
631 }
632 if ( $this->fld_ids ) {
633 $vals['pageid'] = intval( $row->rev_page );
634 $vals['revid'] = intval( $row->rev_id );
635 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
636
637 if ( !is_null( $row->rev_parent_id ) ) {
638 $vals['parentid'] = intval( $row->rev_parent_id );
639 }
640 }
641
642 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
643
644 if ( $this->fld_title ) {
645 ApiQueryBase::addTitleInfo( $vals, $title );
646 }
647
648 if ( $this->fld_timestamp ) {
649 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
650 }
651
652 if ( $this->fld_flags ) {
653 $vals['new'] = $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id );
654 $vals['minor'] = (bool)$row->rev_minor_edit;
655 $vals['top'] = $row->page_latest == $row->rev_id;
656 }
657
658 if ( $this->fld_comment || $this->fld_parsedcomment ) {
659 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
660 $vals['commenthidden'] = true;
661 $anyHidden = true;
662 }
663
664 $userCanView = Revision::userCanBitfield(
665 $row->rev_deleted,
666 Revision::DELETED_COMMENT, $this->getUser()
667 );
668
669 if ( $userCanView ) {
670 $comment = $this->commentStore->getComment( 'rev_comment', $row )->text;
671 if ( $this->fld_comment ) {
672 $vals['comment'] = $comment;
673 }
674
675 if ( $this->fld_parsedcomment ) {
676 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
677 }
678 }
679 }
680
681 if ( $this->fld_patrolled ) {
682 $vals['patrolled'] = $row->rc_patrolled != RecentChange::PRC_UNPATROLLED;
683 $vals['autopatrolled'] = $row->rc_patrolled == RecentChange::PRC_AUTOPATROLLED;
684 }
685
686 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
687 $vals['size'] = intval( $row->rev_len );
688 }
689
690 if ( $this->fld_sizediff
691 && !is_null( $row->rev_len )
692 && !is_null( $row->rev_parent_id )
693 ) {
694 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
695 ? $this->parentLens[$row->rev_parent_id]
696 : 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 class_alias( ApiQueryUserContribs::class, 'ApiQueryContributions' );