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