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