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