Merge "Warn if stateful ParserOutput transforms are used"
[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, $prefixMode, $userprefix, $multiUserMode, $idMode, $usernames, $userids,
35 $parentLens, $commentStore;
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 // Parse some parameters
42 $this->params = $this->extractRequestParams();
43
44 $this->commentStore = new CommentStore( 'rev_comment' );
45
46 $prop = array_flip( $this->params['prop'] );
47 $this->fld_ids = isset( $prop['ids'] );
48 $this->fld_title = isset( $prop['title'] );
49 $this->fld_comment = isset( $prop['comment'] );
50 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
51 $this->fld_size = isset( $prop['size'] );
52 $this->fld_sizediff = isset( $prop['sizediff'] );
53 $this->fld_flags = isset( $prop['flags'] );
54 $this->fld_timestamp = isset( $prop['timestamp'] );
55 $this->fld_patrolled = isset( $prop['patrolled'] );
56 $this->fld_tags = isset( $prop['tags'] );
57
58 // Most of this code will use the 'contributions' group DB, which can map to replica DBs
59 // with extra user based indexes or partioning by user. The additional metadata
60 // queries should use a regular replica DB since the lookup pattern is not all by user.
61 $dbSecondary = $this->getDB(); // any random replica DB
62
63 // TODO: if the query is going only against the revision table, should this be done?
64 $this->selectNamedDB( 'contributions', DB_REPLICA, 'contributions' );
65
66 $this->requireOnlyOneParameter( $this->params, 'userprefix', 'userids', 'user' );
67
68 $this->idMode = false;
69 if ( isset( $this->params['userprefix'] ) ) {
70 $this->prefixMode = true;
71 $this->multiUserMode = true;
72 $this->userprefix = $this->params['userprefix'];
73 } elseif ( isset( $this->params['userids'] ) ) {
74 $this->userids = [];
75
76 if ( !count( $this->params['userids'] ) ) {
77 $encParamName = $this->encodeParamName( 'userids' );
78 $this->dieWithError( [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName" );
79 }
80
81 foreach ( $this->params['userids'] as $uid ) {
82 if ( $uid <= 0 ) {
83 $this->dieWithError( [ 'apierror-invaliduserid', $uid ], 'invaliduserid' );
84 }
85
86 $this->userids[] = $uid;
87 }
88
89 $this->prefixMode = false;
90 $this->multiUserMode = ( count( $this->params['userids'] ) > 1 );
91 $this->idMode = true;
92 } else {
93 $anyIPs = false;
94 $this->userids = [];
95 $this->usernames = [];
96 if ( !count( $this->params['user'] ) ) {
97 $encParamName = $this->encodeParamName( 'user' );
98 $this->dieWithError(
99 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
100 );
101 }
102 foreach ( $this->params['user'] as $u ) {
103 if ( $u === '' ) {
104 $encParamName = $this->encodeParamName( 'user' );
105 $this->dieWithError(
106 [ 'apierror-paramempty', $encParamName ], "paramempty_$encParamName"
107 );
108 }
109
110 if ( User::isIP( $u ) ) {
111 $anyIPs = true;
112 $this->usernames[] = $u;
113 } else {
114 $name = User::getCanonicalName( $u, 'valid' );
115 if ( $name === false ) {
116 $encParamName = $this->encodeParamName( 'user' );
117 $this->dieWithError(
118 [ 'apierror-baduser', $encParamName, wfEscapeWikiText( $u ) ], "baduser_$encParamName"
119 );
120 }
121 $this->usernames[] = $name;
122 }
123 }
124 $this->prefixMode = false;
125 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
126
127 if ( !$anyIPs ) {
128 $dbr = $this->getDB();
129 $res = $dbr->select( 'user', 'user_id', [ 'user_name' => $this->usernames ], __METHOD__ );
130 foreach ( $res as $row ) {
131 $this->userids[] = $row->user_id;
132 }
133 $this->idMode = count( $this->userids ) === count( $this->usernames );
134 }
135 }
136
137 $this->prepareQuery();
138
139 $hookData = [];
140 // Do the actual query.
141 $res = $this->select( __METHOD__, [], $hookData );
142
143 if ( $this->fld_sizediff ) {
144 $revIds = [];
145 foreach ( $res as $row ) {
146 if ( $row->rev_parent_id ) {
147 $revIds[] = $row->rev_parent_id;
148 }
149 }
150 $this->parentLens = Revision::getParentLengths( $dbSecondary, $revIds );
151 $res->rewind(); // reset
152 }
153
154 // Initialise some variables
155 $count = 0;
156 $limit = $this->params['limit'];
157
158 // Fetch each row
159 foreach ( $res as $row ) {
160 if ( ++$count > $limit ) {
161 // We've reached the one extra which shows that there are
162 // additional pages to be had. Stop here...
163 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
164 break;
165 }
166
167 $vals = $this->extractRowInfo( $row );
168 $fit = $this->processRow( $row, $vals, $hookData ) &&
169 $this->getResult()->addValue( [ 'query', $this->getModuleName() ], null, $vals );
170 if ( !$fit ) {
171 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
172 break;
173 }
174 }
175
176 $this->getResult()->addIndexedTagName(
177 [ 'query', $this->getModuleName() ],
178 'item'
179 );
180 }
181
182 /**
183 * Prepares the query and returns the limit of rows requested
184 */
185 private function prepareQuery() {
186 // We're after the revision table, and the corresponding page
187 // row for anything we retrieve. We may also need the
188 // recentchanges row and/or tag summary row.
189 $user = $this->getUser();
190 $tables = [ 'page', 'revision' ]; // Order may change
191 $this->addWhere( 'page_id=rev_page' );
192
193 // Handle continue parameter
194 if ( !is_null( $this->params['continue'] ) ) {
195 $continue = explode( '|', $this->params['continue'] );
196 $db = $this->getDB();
197 if ( $this->multiUserMode ) {
198 $this->dieContinueUsageIf( count( $continue ) != 4 );
199 $modeFlag = array_shift( $continue );
200 $this->dieContinueUsageIf( !in_array( $modeFlag, [ 'id', 'name' ] ) );
201 if ( $this->idMode && $modeFlag === 'name' ) {
202 // The users were created since this query started, but we
203 // can't go back and change modes now. So just keep on with
204 // name mode.
205 $this->idMode = false;
206 }
207 $this->dieContinueUsageIf( ( $modeFlag === 'id' ) !== $this->idMode );
208 $userField = $this->idMode ? 'rev_user' : 'rev_user_text';
209 $encUser = $db->addQuotes( array_shift( $continue ) );
210 } else {
211 $this->dieContinueUsageIf( count( $continue ) != 2 );
212 }
213 $encTS = $db->addQuotes( $db->timestamp( $continue[0] ) );
214 $encId = (int)$continue[1];
215 $this->dieContinueUsageIf( $encId != $continue[1] );
216 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
217 if ( $this->multiUserMode ) {
218 $this->addWhere(
219 "$userField $op $encUser OR " .
220 "($userField = $encUser AND " .
221 "(rev_timestamp $op $encTS OR " .
222 "(rev_timestamp = $encTS AND " .
223 "rev_id $op= $encId)))"
224 );
225 } else {
226 $this->addWhere(
227 "rev_timestamp $op $encTS OR " .
228 "(rev_timestamp = $encTS AND " .
229 "rev_id $op= $encId)"
230 );
231 }
232 }
233
234 // Don't include any revisions where we're not supposed to be able to
235 // see the username.
236 if ( !$user->isAllowed( 'deletedhistory' ) ) {
237 $bitmask = Revision::DELETED_USER;
238 } elseif ( !$user->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
239 $bitmask = Revision::DELETED_USER | Revision::DELETED_RESTRICTED;
240 } else {
241 $bitmask = 0;
242 }
243 if ( $bitmask ) {
244 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', $bitmask ) . " != $bitmask" );
245 }
246
247 // We only want pages by the specified users.
248 if ( $this->prefixMode ) {
249 $this->addWhere( 'rev_user_text' .
250 $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
251 } elseif ( $this->idMode ) {
252 $this->addWhereFld( 'rev_user', $this->userids );
253 } else {
254 $this->addWhereFld( 'rev_user_text', $this->usernames );
255 }
256 // ... and in the specified timeframe.
257 // Ensure the same sort order for rev_user/rev_user_text and rev_timestamp
258 // so our query is indexed
259 if ( $this->multiUserMode ) {
260 $this->addWhereRange( $this->idMode ? 'rev_user' : 'rev_user_text',
261 $this->params['dir'], null, null );
262 }
263 $this->addTimestampWhereRange( 'rev_timestamp',
264 $this->params['dir'], $this->params['start'], $this->params['end'] );
265 // Include in ORDER BY for uniqueness
266 $this->addWhereRange( 'rev_id', $this->params['dir'], null, null );
267
268 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
269
270 $show = $this->params['show'];
271 if ( $this->params['toponly'] ) { // deprecated/old param
272 $show[] = 'top';
273 }
274 if ( !is_null( $show ) ) {
275 $show = array_flip( $show );
276
277 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
278 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
279 || ( isset( $show['top'] ) && isset( $show['!top'] ) )
280 || ( isset( $show['new'] ) && isset( $show['!new'] ) )
281 ) {
282 $this->dieWithError( 'apierror-show' );
283 }
284
285 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
286 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
287 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
288 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
289 $this->addWhereIf( 'rev_id != page_latest', isset( $show['!top'] ) );
290 $this->addWhereIf( 'rev_id = page_latest', isset( $show['top'] ) );
291 $this->addWhereIf( 'rev_parent_id != 0', isset( $show['!new'] ) );
292 $this->addWhereIf( 'rev_parent_id = 0', isset( $show['new'] ) );
293 }
294 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
295
296 // Mandatory fields: timestamp allows request continuation
297 // ns+title checks if the user has access rights for this page
298 // user_text is necessary if multiple users were specified
299 $this->addFields( [
300 'rev_id',
301 'rev_timestamp',
302 'page_namespace',
303 'page_title',
304 'rev_user',
305 'rev_user_text',
306 'rev_deleted'
307 ] );
308
309 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
310 $this->fld_patrolled
311 ) {
312 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
313 $this->dieWithError( 'apierror-permissiondenied-patrolflag', 'permissiondenied' );
314 }
315
316 // Use a redundant join condition on both
317 // timestamp and ID so we can use the timestamp
318 // index
319 $index['recentchanges'] = 'rc_user_text';
320 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
321 // Put the tables in the right order for
322 // STRAIGHT_JOIN
323 $tables = [ 'revision', 'recentchanges', 'page' ];
324 $this->addOption( 'STRAIGHT_JOIN' );
325 $this->addWhere( 'rc_user_text=rev_user_text' );
326 $this->addWhere( 'rc_timestamp=rev_timestamp' );
327 $this->addWhere( 'rc_this_oldid=rev_id' );
328 } else {
329 $tables[] = 'recentchanges';
330 $this->addJoinConds( [ 'recentchanges' => [
331 'LEFT JOIN', [
332 'rc_user_text=rev_user_text',
333 'rc_timestamp=rev_timestamp',
334 'rc_this_oldid=rev_id' ] ] ] );
335 }
336 }
337
338 $this->addTables( $tables );
339 $this->addFieldsIf( 'rev_page', $this->fld_ids );
340 $this->addFieldsIf( 'page_latest', $this->fld_flags );
341 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
342 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
343 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
344 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff || $this->fld_ids );
345 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
346
347 if ( $this->fld_comment || $this->fld_parsedcomment ) {
348 $commentQuery = $this->commentStore->getJoin();
349 $this->addTables( $commentQuery['tables'] );
350 $this->addFields( $commentQuery['fields'] );
351 $this->addJoinConds( $commentQuery['joins'] );
352 }
353
354 if ( $this->fld_tags ) {
355 $this->addTables( 'tag_summary' );
356 $this->addJoinConds(
357 [ 'tag_summary' => [ 'LEFT JOIN', [ 'rev_id=ts_rev_id' ] ] ]
358 );
359 $this->addFields( 'ts_tags' );
360 }
361
362 if ( isset( $this->params['tag'] ) ) {
363 $this->addTables( 'change_tag' );
364 $this->addJoinConds(
365 [ 'change_tag' => [ 'INNER JOIN', [ 'rev_id=ct_rev_id' ] ] ]
366 );
367 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
368 }
369
370 if ( isset( $index ) ) {
371 $this->addOption( 'USE INDEX', $index );
372 }
373 }
374
375 /**
376 * Extract fields from the database row and append them to a result array
377 *
378 * @param stdClass $row
379 * @return array
380 */
381 private function extractRowInfo( $row ) {
382 $vals = [];
383 $anyHidden = false;
384
385 if ( $row->rev_deleted & Revision::DELETED_TEXT ) {
386 $vals['texthidden'] = true;
387 $anyHidden = true;
388 }
389
390 // Any rows where we can't view the user were filtered out in the query.
391 $vals['userid'] = (int)$row->rev_user;
392 $vals['user'] = $row->rev_user_text;
393 if ( $row->rev_deleted & Revision::DELETED_USER ) {
394 $vals['userhidden'] = true;
395 $anyHidden = true;
396 }
397 if ( $this->fld_ids ) {
398 $vals['pageid'] = intval( $row->rev_page );
399 $vals['revid'] = intval( $row->rev_id );
400 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
401
402 if ( !is_null( $row->rev_parent_id ) ) {
403 $vals['parentid'] = intval( $row->rev_parent_id );
404 }
405 }
406
407 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
408
409 if ( $this->fld_title ) {
410 ApiQueryBase::addTitleInfo( $vals, $title );
411 }
412
413 if ( $this->fld_timestamp ) {
414 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
415 }
416
417 if ( $this->fld_flags ) {
418 $vals['new'] = $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id );
419 $vals['minor'] = (bool)$row->rev_minor_edit;
420 $vals['top'] = $row->page_latest == $row->rev_id;
421 }
422
423 if ( $this->fld_comment || $this->fld_parsedcomment ) {
424 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
425 $vals['commenthidden'] = true;
426 $anyHidden = true;
427 }
428
429 $userCanView = Revision::userCanBitfield(
430 $row->rev_deleted,
431 Revision::DELETED_COMMENT, $this->getUser()
432 );
433
434 if ( $userCanView ) {
435 $comment = $this->commentStore->getComment( $row )->text;
436 if ( $this->fld_comment ) {
437 $vals['comment'] = $comment;
438 }
439
440 if ( $this->fld_parsedcomment ) {
441 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
442 }
443 }
444 }
445
446 if ( $this->fld_patrolled ) {
447 $vals['patrolled'] = (bool)$row->rc_patrolled;
448 }
449
450 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
451 $vals['size'] = intval( $row->rev_len );
452 }
453
454 if ( $this->fld_sizediff
455 && !is_null( $row->rev_len )
456 && !is_null( $row->rev_parent_id )
457 ) {
458 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
459 ? $this->parentLens[$row->rev_parent_id]
460 : 0;
461 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
462 }
463
464 if ( $this->fld_tags ) {
465 if ( $row->ts_tags ) {
466 $tags = explode( ',', $row->ts_tags );
467 ApiResult::setIndexedTagName( $tags, 'tag' );
468 $vals['tags'] = $tags;
469 } else {
470 $vals['tags'] = [];
471 }
472 }
473
474 if ( $anyHidden && $row->rev_deleted & Revision::DELETED_RESTRICTED ) {
475 $vals['suppressed'] = true;
476 }
477
478 return $vals;
479 }
480
481 private function continueStr( $row ) {
482 if ( $this->multiUserMode ) {
483 if ( $this->idMode ) {
484 return "id|$row->rev_user|$row->rev_timestamp|$row->rev_id";
485 } else {
486 return "name|$row->rev_user_text|$row->rev_timestamp|$row->rev_id";
487 }
488 } else {
489 return "$row->rev_timestamp|$row->rev_id";
490 }
491 }
492
493 public function getCacheMode( $params ) {
494 // This module provides access to deleted revisions and patrol flags if
495 // the requester is logged in
496 return 'anon-public-user-private';
497 }
498
499 public function getAllowedParams() {
500 return [
501 'limit' => [
502 ApiBase::PARAM_DFLT => 10,
503 ApiBase::PARAM_TYPE => 'limit',
504 ApiBase::PARAM_MIN => 1,
505 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
506 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
507 ],
508 'start' => [
509 ApiBase::PARAM_TYPE => 'timestamp'
510 ],
511 'end' => [
512 ApiBase::PARAM_TYPE => 'timestamp'
513 ],
514 'continue' => [
515 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
516 ],
517 'user' => [
518 ApiBase::PARAM_TYPE => 'user',
519 ApiBase::PARAM_ISMULTI => true
520 ],
521 'userids' => [
522 ApiBase::PARAM_TYPE => 'integer',
523 ApiBase::PARAM_ISMULTI => true
524 ],
525 'userprefix' => null,
526 'dir' => [
527 ApiBase::PARAM_DFLT => 'older',
528 ApiBase::PARAM_TYPE => [
529 'newer',
530 'older'
531 ],
532 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
533 ],
534 'namespace' => [
535 ApiBase::PARAM_ISMULTI => true,
536 ApiBase::PARAM_TYPE => 'namespace'
537 ],
538 'prop' => [
539 ApiBase::PARAM_ISMULTI => true,
540 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
541 ApiBase::PARAM_TYPE => [
542 'ids',
543 'title',
544 'timestamp',
545 'comment',
546 'parsedcomment',
547 'size',
548 'sizediff',
549 'flags',
550 'patrolled',
551 'tags'
552 ],
553 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
554 ],
555 'show' => [
556 ApiBase::PARAM_ISMULTI => true,
557 ApiBase::PARAM_TYPE => [
558 'minor',
559 '!minor',
560 'patrolled',
561 '!patrolled',
562 'top',
563 '!top',
564 'new',
565 '!new',
566 ],
567 ApiBase::PARAM_HELP_MSG => [
568 'apihelp-query+usercontribs-param-show',
569 $this->getConfig()->get( 'RCMaxAge' )
570 ],
571 ],
572 'tag' => null,
573 'toponly' => [
574 ApiBase::PARAM_DFLT => false,
575 ApiBase::PARAM_DEPRECATED => true,
576 ],
577 ];
578 }
579
580 protected function getExamplesMessages() {
581 return [
582 'action=query&list=usercontribs&ucuser=Example'
583 => 'apihelp-query+usercontribs-example-user',
584 'action=query&list=usercontribs&ucuserprefix=192.0.2.'
585 => 'apihelp-query+usercontribs-example-ipprefix',
586 ];
587 }
588
589 public function getHelpUrls() {
590 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Usercontribs';
591 }
592 }