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