Merge "Made Title cache use MapCacheLRU"
[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 }
286
287 if ( $this->params['toponly'] ) {
288 $this->addWhere( 'rev_id = page_latest' );
289 }
290
291 $this->addOption( 'USE INDEX', $index );
292 }
293
294 /**
295 * Extract fields from the database row and append them to a result array
296 *
297 * @param $row
298 * @return array
299 */
300 private function extractRowInfo( $row ) {
301 $vals = array();
302
303 $vals['userid'] = $row->rev_user;
304 $vals['user'] = $row->rev_user_text;
305 if ( $row->rev_deleted & Revision::DELETED_USER ) {
306 $vals['userhidden'] = '';
307 }
308 if ( $this->fld_ids ) {
309 $vals['pageid'] = intval( $row->rev_page );
310 $vals['revid'] = intval( $row->rev_id );
311 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
312
313 if ( !is_null( $row->rev_parent_id ) ) {
314 $vals['parentid'] = intval( $row->rev_parent_id );
315 }
316 }
317
318 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
319
320 if ( $this->fld_title ) {
321 ApiQueryBase::addTitleInfo( $vals, $title );
322 }
323
324 if ( $this->fld_timestamp ) {
325 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
326 }
327
328 if ( $this->fld_flags ) {
329 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
330 $vals['new'] = '';
331 }
332 if ( $row->rev_minor_edit ) {
333 $vals['minor'] = '';
334 }
335 if ( $row->page_latest == $row->rev_id ) {
336 $vals['top'] = '';
337 }
338 }
339
340 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
341 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
342 $vals['commenthidden'] = '';
343 } else {
344 if ( $this->fld_comment ) {
345 $vals['comment'] = $row->rev_comment;
346 }
347
348 if ( $this->fld_parsedcomment ) {
349 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
350 }
351 }
352 }
353
354 if ( $this->fld_patrolled && $row->rc_patrolled ) {
355 $vals['patrolled'] = '';
356 }
357
358 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
359 $vals['size'] = intval( $row->rev_len );
360 }
361
362 if ( $this->fld_sizediff
363 && !is_null( $row->rev_len )
364 && !is_null( $row->rev_parent_id )
365 ) {
366 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
367 ? $this->parentLens[$row->rev_parent_id]
368 : 0;
369 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
370 }
371
372 if ( $this->fld_tags ) {
373 if ( $row->ts_tags ) {
374 $tags = explode( ',', $row->ts_tags );
375 $this->getResult()->setIndexedTagName( $tags, 'tag' );
376 $vals['tags'] = $tags;
377 } else {
378 $vals['tags'] = array();
379 }
380 }
381
382 return $vals;
383 }
384
385 private function continueStr( $row ) {
386 return $row->rev_user_text . '|' .
387 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
388 }
389
390 public function getCacheMode( $params ) {
391 // This module provides access to deleted revisions and patrol flags if
392 // the requester is logged in
393 return 'anon-public-user-private';
394 }
395
396 public function getAllowedParams() {
397 return array(
398 'limit' => array(
399 ApiBase::PARAM_DFLT => 10,
400 ApiBase::PARAM_TYPE => 'limit',
401 ApiBase::PARAM_MIN => 1,
402 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
403 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
404 ),
405 'start' => array(
406 ApiBase::PARAM_TYPE => 'timestamp'
407 ),
408 'end' => array(
409 ApiBase::PARAM_TYPE => 'timestamp'
410 ),
411 'continue' => null,
412 'user' => array(
413 ApiBase::PARAM_ISMULTI => true
414 ),
415 'userprefix' => null,
416 'dir' => array(
417 ApiBase::PARAM_DFLT => 'older',
418 ApiBase::PARAM_TYPE => array(
419 'newer',
420 'older'
421 )
422 ),
423 'namespace' => array(
424 ApiBase::PARAM_ISMULTI => true,
425 ApiBase::PARAM_TYPE => 'namespace'
426 ),
427 'prop' => array(
428 ApiBase::PARAM_ISMULTI => true,
429 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
430 ApiBase::PARAM_TYPE => array(
431 'ids',
432 'title',
433 'timestamp',
434 'comment',
435 'parsedcomment',
436 'size',
437 'sizediff',
438 'flags',
439 'patrolled',
440 'tags'
441 )
442 ),
443 'show' => array(
444 ApiBase::PARAM_ISMULTI => true,
445 ApiBase::PARAM_TYPE => array(
446 'minor',
447 '!minor',
448 'patrolled',
449 '!patrolled',
450 )
451 ),
452 'tag' => null,
453 'toponly' => false,
454 );
455 }
456
457 public function getParamDescription() {
458 global $wgRCMaxAge;
459 $p = $this->getModulePrefix();
460
461 return array(
462 'limit' => 'The maximum number of contributions to return',
463 'start' => 'The start timestamp to return from',
464 'end' => 'The end timestamp to return to',
465 'continue' => 'When more results are available, use this to continue',
466 'user' => 'The users to retrieve contributions for',
467 'userprefix' => array(
468 "Retrieve contributions for all users whose names begin with this value.",
469 "Overrides {$p}user",
470 ),
471 'dir' => $this->getDirectionDescription( $p ),
472 'namespace' => 'Only list contributions in these namespaces',
473 'prop' => array(
474 'Include additional pieces of information',
475 ' ids - Adds the page ID and revision ID',
476 ' title - Adds the title and namespace ID of the page',
477 ' timestamp - Adds the timestamp of the edit',
478 ' comment - Adds the comment of the edit',
479 ' parsedcomment - Adds the parsed comment of the edit',
480 ' size - Adds the new size of the edit',
481 ' sizediff - Adds the size delta of the edit against its parent',
482 ' flags - Adds flags of the edit',
483 ' patrolled - Tags patrolled edits',
484 ' tags - Lists tags for the edit',
485 ),
486 'show' => array(
487 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
488 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
489 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
490 ),
491 'tag' => 'Only list revisions tagged with this tag',
492 'toponly' => 'Only list changes which are the latest revision',
493 );
494 }
495
496 public function getResultProperties() {
497 return array(
498 '' => array(
499 'userid' => 'integer',
500 'user' => 'string',
501 'userhidden' => 'boolean'
502 ),
503 'ids' => array(
504 'pageid' => 'integer',
505 'revid' => 'integer',
506 'parentid' => array(
507 ApiBase::PROP_TYPE => 'integer',
508 ApiBase::PROP_NULLABLE => true
509 )
510 ),
511 'title' => array(
512 'ns' => 'namespace',
513 'title' => 'string'
514 ),
515 'timestamp' => array(
516 'timestamp' => 'timestamp'
517 ),
518 'flags' => array(
519 'new' => 'boolean',
520 'minor' => 'boolean',
521 'top' => 'boolean'
522 ),
523 'comment' => array(
524 'commenthidden' => 'boolean',
525 'comment' => array(
526 ApiBase::PROP_TYPE => 'string',
527 ApiBase::PROP_NULLABLE => true
528 )
529 ),
530 'parsedcomment' => array(
531 'commenthidden' => 'boolean',
532 'parsedcomment' => array(
533 ApiBase::PROP_TYPE => 'string',
534 ApiBase::PROP_NULLABLE => true
535 )
536 ),
537 'patrolled' => array(
538 'patrolled' => 'boolean'
539 ),
540 'size' => array(
541 'size' => array(
542 ApiBase::PROP_TYPE => 'integer',
543 ApiBase::PROP_NULLABLE => true
544 )
545 ),
546 'sizediff' => array(
547 'sizediff' => array(
548 ApiBase::PROP_TYPE => 'integer',
549 ApiBase::PROP_NULLABLE => true
550 )
551 )
552 );
553 }
554
555 public function getDescription() {
556 return 'Get all edits by a user';
557 }
558
559 public function getPossibleErrors() {
560 return array_merge( parent::getPossibleErrors(), array(
561 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
562 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
563 array( 'show' ),
564 array(
565 'code' => 'permissiondenied',
566 'info' => 'You need the patrol right to request the patrolled flag'
567 ),
568 ) );
569 }
570
571 public function getExamples() {
572 return array(
573 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
574 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
575 );
576 }
577
578 public function getHelpUrls() {
579 return 'https://www.mediawiki.org/wiki/API:Usercontribs';
580 }
581 }