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