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