Merge "EnhancedChangesList: Add missing </td></tr>"
[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 if ( !$user->isAllowed( 'hideuser' ) ) {
180 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
181 }
182 // We only want pages by the specified users.
183 if ( $this->prefixMode ) {
184 $this->addWhere( 'rev_user_text' .
185 $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
186 } else {
187 $this->addWhereFld( 'rev_user_text', $this->usernames );
188 }
189 // ... and in the specified timeframe.
190 // Ensure the same sort order for rev_user_text and rev_timestamp
191 // so our query is indexed
192 if ( $this->multiUserMode ) {
193 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
194 }
195 $this->addTimestampWhereRange( 'rev_timestamp',
196 $this->params['dir'], $this->params['start'], $this->params['end'] );
197 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
198
199 $show = $this->params['show'];
200 if ( !is_null( $show ) ) {
201 $show = array_flip( $show );
202 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
203 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) )
204 ) {
205 $this->dieUsageMsg( 'show' );
206 }
207
208 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
209 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
210 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
211 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
212 }
213 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
214 $index = array( 'revision' => 'usertext_timestamp' );
215
216 // Mandatory fields: timestamp allows request continuation
217 // ns+title checks if the user has access rights for this page
218 // user_text is necessary if multiple users were specified
219 $this->addFields( array(
220 'rev_timestamp',
221 'page_namespace',
222 'page_title',
223 'rev_user',
224 'rev_user_text',
225 'rev_deleted'
226 ) );
227
228 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
229 $this->fld_patrolled
230 ) {
231 if ( !$user->useRCPatrol() && !$user->useNPPatrol() ) {
232 $this->dieUsage(
233 'You need the patrol right to request the patrolled flag',
234 'permissiondenied'
235 );
236 }
237
238 // Use a redundant join condition on both
239 // timestamp and ID so we can use the timestamp
240 // index
241 $index['recentchanges'] = 'rc_user_text';
242 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
243 // Put the tables in the right order for
244 // STRAIGHT_JOIN
245 $tables = array( 'revision', 'recentchanges', 'page' );
246 $this->addOption( 'STRAIGHT_JOIN' );
247 $this->addWhere( 'rc_user_text=rev_user_text' );
248 $this->addWhere( 'rc_timestamp=rev_timestamp' );
249 $this->addWhere( 'rc_this_oldid=rev_id' );
250 } else {
251 $tables[] = 'recentchanges';
252 $this->addJoinConds( array( 'recentchanges' => array(
253 'LEFT JOIN', array(
254 'rc_user_text=rev_user_text',
255 'rc_timestamp=rev_timestamp',
256 'rc_this_oldid=rev_id' ) ) ) );
257 }
258 }
259
260 $this->addTables( $tables );
261 $this->addFieldsIf( 'rev_page', $this->fld_ids );
262 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
263 $this->addFieldsIf( 'page_latest', $this->fld_flags );
264 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
265 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
266 $this->addFieldsIf( 'rev_len', $this->fld_size || $this->fld_sizediff );
267 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
268 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags || $this->fld_sizediff || $this->fld_ids );
269 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
270
271 if ( $this->fld_tags ) {
272 $this->addTables( 'tag_summary' );
273 $this->addJoinConds(
274 array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) )
275 );
276 $this->addFields( 'ts_tags' );
277 }
278
279 if ( isset( $this->params['tag'] ) ) {
280 $this->addTables( 'change_tag' );
281 $this->addJoinConds(
282 array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) )
283 );
284 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
285 $index['change_tag'] = 'change_tag_tag_id';
286 }
287
288 if ( $this->params['toponly'] ) {
289 $this->addWhere( 'rev_id = page_latest' );
290 }
291
292 $this->addOption( 'USE INDEX', $index );
293 }
294
295 /**
296 * Extract fields from the database row and append them to a result array
297 *
298 * @param $row
299 * @return array
300 */
301 private function extractRowInfo( $row ) {
302 $vals = array();
303
304 $vals['userid'] = $row->rev_user;
305 $vals['user'] = $row->rev_user_text;
306 if ( $row->rev_deleted & Revision::DELETED_USER ) {
307 $vals['userhidden'] = '';
308 }
309 if ( $this->fld_ids ) {
310 $vals['pageid'] = intval( $row->rev_page );
311 $vals['revid'] = intval( $row->rev_id );
312 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
313
314 if ( !is_null( $row->rev_parent_id ) ) {
315 $vals['parentid'] = intval( $row->rev_parent_id );
316 }
317 }
318
319 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
320
321 if ( $this->fld_title ) {
322 ApiQueryBase::addTitleInfo( $vals, $title );
323 }
324
325 if ( $this->fld_timestamp ) {
326 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
327 }
328
329 if ( $this->fld_flags ) {
330 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
331 $vals['new'] = '';
332 }
333 if ( $row->rev_minor_edit ) {
334 $vals['minor'] = '';
335 }
336 if ( $row->page_latest == $row->rev_id ) {
337 $vals['top'] = '';
338 }
339 }
340
341 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
342 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
343 $vals['commenthidden'] = '';
344 } else {
345 if ( $this->fld_comment ) {
346 $vals['comment'] = $row->rev_comment;
347 }
348
349 if ( $this->fld_parsedcomment ) {
350 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
351 }
352 }
353 }
354
355 if ( $this->fld_patrolled && $row->rc_patrolled ) {
356 $vals['patrolled'] = '';
357 }
358
359 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
360 $vals['size'] = intval( $row->rev_len );
361 }
362
363 if ( $this->fld_sizediff
364 && !is_null( $row->rev_len )
365 && !is_null( $row->rev_parent_id )
366 ) {
367 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
368 ? $this->parentLens[$row->rev_parent_id]
369 : 0;
370 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
371 }
372
373 if ( $this->fld_tags ) {
374 if ( $row->ts_tags ) {
375 $tags = explode( ',', $row->ts_tags );
376 $this->getResult()->setIndexedTagName( $tags, 'tag' );
377 $vals['tags'] = $tags;
378 } else {
379 $vals['tags'] = array();
380 }
381 }
382
383 return $vals;
384 }
385
386 private function continueStr( $row ) {
387 return $row->rev_user_text . '|' .
388 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
389 }
390
391 public function getCacheMode( $params ) {
392 // This module provides access to deleted revisions and patrol flags if
393 // the requester is logged in
394 return 'anon-public-user-private';
395 }
396
397 public function getAllowedParams() {
398 return array(
399 'limit' => array(
400 ApiBase::PARAM_DFLT => 10,
401 ApiBase::PARAM_TYPE => 'limit',
402 ApiBase::PARAM_MIN => 1,
403 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
404 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
405 ),
406 'start' => array(
407 ApiBase::PARAM_TYPE => 'timestamp'
408 ),
409 'end' => array(
410 ApiBase::PARAM_TYPE => 'timestamp'
411 ),
412 'continue' => null,
413 'user' => array(
414 ApiBase::PARAM_ISMULTI => true
415 ),
416 'userprefix' => null,
417 'dir' => array(
418 ApiBase::PARAM_DFLT => 'older',
419 ApiBase::PARAM_TYPE => array(
420 'newer',
421 'older'
422 )
423 ),
424 'namespace' => array(
425 ApiBase::PARAM_ISMULTI => true,
426 ApiBase::PARAM_TYPE => 'namespace'
427 ),
428 'prop' => array(
429 ApiBase::PARAM_ISMULTI => true,
430 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
431 ApiBase::PARAM_TYPE => array(
432 'ids',
433 'title',
434 'timestamp',
435 'comment',
436 'parsedcomment',
437 'size',
438 'sizediff',
439 'flags',
440 'patrolled',
441 'tags'
442 )
443 ),
444 'show' => array(
445 ApiBase::PARAM_ISMULTI => true,
446 ApiBase::PARAM_TYPE => array(
447 'minor',
448 '!minor',
449 'patrolled',
450 '!patrolled',
451 )
452 ),
453 'tag' => null,
454 'toponly' => false,
455 );
456 }
457
458 public function getParamDescription() {
459 global $wgRCMaxAge;
460 $p = $this->getModulePrefix();
461
462 return array(
463 'limit' => 'The maximum number of contributions to return',
464 'start' => 'The start timestamp to return from',
465 'end' => 'The end timestamp to return to',
466 'continue' => 'When more results are available, use this to continue',
467 'user' => 'The users to retrieve contributions for',
468 'userprefix' => array(
469 "Retrieve contributions for all users whose names begin with this value.",
470 "Overrides {$p}user",
471 ),
472 'dir' => $this->getDirectionDescription( $p ),
473 'namespace' => 'Only list contributions in these namespaces',
474 'prop' => array(
475 'Include additional pieces of information',
476 ' ids - Adds the page ID and revision ID',
477 ' title - Adds the title and namespace ID of the page',
478 ' timestamp - Adds the timestamp of the edit',
479 ' comment - Adds the comment of the edit',
480 ' parsedcomment - Adds the parsed comment of the edit',
481 ' size - Adds the new size of the edit',
482 ' sizediff - Adds the size delta of the edit against its parent',
483 ' flags - Adds flags of the edit',
484 ' patrolled - Tags patrolled edits',
485 ' tags - Lists tags for the edit',
486 ),
487 'show' => array(
488 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
489 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
490 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
491 ),
492 'tag' => 'Only list revisions tagged with this tag',
493 'toponly' => 'Only list changes which are the latest revision',
494 );
495 }
496
497 public function getResultProperties() {
498 return array(
499 '' => array(
500 'userid' => 'integer',
501 'user' => 'string',
502 'userhidden' => 'boolean'
503 ),
504 'ids' => array(
505 'pageid' => 'integer',
506 'revid' => 'integer',
507 'parentid' => array(
508 ApiBase::PROP_TYPE => 'integer',
509 ApiBase::PROP_NULLABLE => true
510 )
511 ),
512 'title' => array(
513 'ns' => 'namespace',
514 'title' => 'string'
515 ),
516 'timestamp' => array(
517 'timestamp' => 'timestamp'
518 ),
519 'flags' => array(
520 'new' => 'boolean',
521 'minor' => 'boolean',
522 'top' => 'boolean'
523 ),
524 'comment' => array(
525 'commenthidden' => 'boolean',
526 'comment' => array(
527 ApiBase::PROP_TYPE => 'string',
528 ApiBase::PROP_NULLABLE => true
529 )
530 ),
531 'parsedcomment' => array(
532 'commenthidden' => 'boolean',
533 'parsedcomment' => array(
534 ApiBase::PROP_TYPE => 'string',
535 ApiBase::PROP_NULLABLE => true
536 )
537 ),
538 'patrolled' => array(
539 'patrolled' => 'boolean'
540 ),
541 'size' => array(
542 'size' => array(
543 ApiBase::PROP_TYPE => 'integer',
544 ApiBase::PROP_NULLABLE => true
545 )
546 ),
547 'sizediff' => array(
548 'sizediff' => array(
549 ApiBase::PROP_TYPE => 'integer',
550 ApiBase::PROP_NULLABLE => true
551 )
552 )
553 );
554 }
555
556 public function getDescription() {
557 return 'Get all edits by a user';
558 }
559
560 public function getPossibleErrors() {
561 return array_merge( parent::getPossibleErrors(), array(
562 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
563 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
564 array( 'show' ),
565 array(
566 'code' => 'permissiondenied',
567 'info' => 'You need the patrol right to request the patrolled flag'
568 ),
569 ) );
570 }
571
572 public function getExamples() {
573 return array(
574 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
575 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
576 );
577 }
578
579 public function getHelpUrls() {
580 return 'https://www.mediawiki.org/wiki/API:Usercontribs';
581 }
582 }