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