Merge "Fixed stream wrapper in PhpHttpRequest"
[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 if ( Revision::userCanBitfield( $row->rev_deleted, Revision::DELETED_COMMENT, $this->getUser() ) ) {
364 if ( $this->fld_comment ) {
365 $vals['comment'] = $row->rev_comment;
366 }
367
368 if ( $this->fld_parsedcomment ) {
369 $vals['parsedcomment'] = Linker::formatComment( $row->rev_comment, $title );
370 }
371 }
372 }
373
374 if ( $this->fld_patrolled && $row->rc_patrolled ) {
375 $vals['patrolled'] = '';
376 }
377
378 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
379 $vals['size'] = intval( $row->rev_len );
380 }
381
382 if ( $this->fld_sizediff
383 && !is_null( $row->rev_len )
384 && !is_null( $row->rev_parent_id )
385 ) {
386 $parentLen = isset( $this->parentLens[$row->rev_parent_id] )
387 ? $this->parentLens[$row->rev_parent_id]
388 : 0;
389 $vals['sizediff'] = intval( $row->rev_len - $parentLen );
390 }
391
392 if ( $this->fld_tags ) {
393 if ( $row->ts_tags ) {
394 $tags = explode( ',', $row->ts_tags );
395 $this->getResult()->setIndexedTagName( $tags, 'tag' );
396 $vals['tags'] = $tags;
397 } else {
398 $vals['tags'] = array();
399 }
400 }
401
402 if ( $anyHidden && $row->rev_deleted & Revision::DELETED_RESTRICTED ) {
403 $vals['suppressed'] = '';
404 }
405
406 return $vals;
407 }
408
409 private function continueStr( $row ) {
410 return $row->rev_user_text . '|' .
411 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
412 }
413
414 public function getCacheMode( $params ) {
415 // This module provides access to deleted revisions and patrol flags if
416 // the requester is logged in
417 return 'anon-public-user-private';
418 }
419
420 public function getAllowedParams() {
421 return array(
422 'limit' => array(
423 ApiBase::PARAM_DFLT => 10,
424 ApiBase::PARAM_TYPE => 'limit',
425 ApiBase::PARAM_MIN => 1,
426 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
427 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
428 ),
429 'start' => array(
430 ApiBase::PARAM_TYPE => 'timestamp'
431 ),
432 'end' => array(
433 ApiBase::PARAM_TYPE => 'timestamp'
434 ),
435 'continue' => null,
436 'user' => array(
437 ApiBase::PARAM_ISMULTI => true
438 ),
439 'userprefix' => null,
440 'dir' => array(
441 ApiBase::PARAM_DFLT => 'older',
442 ApiBase::PARAM_TYPE => array(
443 'newer',
444 'older'
445 )
446 ),
447 'namespace' => array(
448 ApiBase::PARAM_ISMULTI => true,
449 ApiBase::PARAM_TYPE => 'namespace'
450 ),
451 'prop' => array(
452 ApiBase::PARAM_ISMULTI => true,
453 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
454 ApiBase::PARAM_TYPE => array(
455 'ids',
456 'title',
457 'timestamp',
458 'comment',
459 'parsedcomment',
460 'size',
461 'sizediff',
462 'flags',
463 'patrolled',
464 'tags'
465 )
466 ),
467 'show' => array(
468 ApiBase::PARAM_ISMULTI => true,
469 ApiBase::PARAM_TYPE => array(
470 'minor',
471 '!minor',
472 'patrolled',
473 '!patrolled',
474 )
475 ),
476 'tag' => null,
477 'toponly' => false,
478 );
479 }
480
481 public function getParamDescription() {
482 global $wgRCMaxAge;
483 $p = $this->getModulePrefix();
484
485 return array(
486 'limit' => 'The maximum number of contributions to return',
487 'start' => 'The start timestamp to return from',
488 'end' => 'The end timestamp to return to',
489 'continue' => 'When more results are available, use this to continue',
490 'user' => 'The users to retrieve contributions for',
491 'userprefix' => array(
492 "Retrieve contributions for all users whose names begin with this value.",
493 "Overrides {$p}user",
494 ),
495 'dir' => $this->getDirectionDescription( $p ),
496 'namespace' => 'Only list contributions in these namespaces',
497 'prop' => array(
498 'Include additional pieces of information',
499 ' ids - Adds the page ID and revision ID',
500 ' title - Adds the title and namespace ID of the page',
501 ' timestamp - Adds the timestamp of the edit',
502 ' comment - Adds the comment of the edit',
503 ' parsedcomment - Adds the parsed comment of the edit',
504 ' size - Adds the new size of the edit',
505 ' sizediff - Adds the size delta of the edit against its parent',
506 ' flags - Adds flags of the edit',
507 ' patrolled - Tags patrolled edits',
508 ' tags - Lists tags for the edit',
509 ),
510 'show' => array(
511 "Show only items that meet thse criteria, e.g. non minor edits only: {$p}show=!minor",
512 "NOTE: If {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than",
513 "\$wgRCMaxAge ($wgRCMaxAge) won't be shown",
514 ),
515 'tag' => 'Only list revisions tagged with this tag',
516 'toponly' => 'Only list changes which are the latest revision',
517 );
518 }
519
520 public function getResultProperties() {
521 return array(
522 '' => array(
523 'userid' => 'integer',
524 'user' => 'string',
525 'userhidden' => 'boolean'
526 ),
527 'ids' => array(
528 'pageid' => 'integer',
529 'revid' => 'integer',
530 'parentid' => array(
531 ApiBase::PROP_TYPE => 'integer',
532 ApiBase::PROP_NULLABLE => true
533 )
534 ),
535 'title' => array(
536 'ns' => 'namespace',
537 'title' => 'string'
538 ),
539 'timestamp' => array(
540 'timestamp' => 'timestamp'
541 ),
542 'flags' => array(
543 'new' => 'boolean',
544 'minor' => 'boolean',
545 'top' => 'boolean'
546 ),
547 'comment' => array(
548 'commenthidden' => 'boolean',
549 'comment' => array(
550 ApiBase::PROP_TYPE => 'string',
551 ApiBase::PROP_NULLABLE => true
552 )
553 ),
554 'parsedcomment' => array(
555 'commenthidden' => 'boolean',
556 'parsedcomment' => array(
557 ApiBase::PROP_TYPE => 'string',
558 ApiBase::PROP_NULLABLE => true
559 )
560 ),
561 'patrolled' => array(
562 'patrolled' => 'boolean'
563 ),
564 'size' => array(
565 'size' => array(
566 ApiBase::PROP_TYPE => 'integer',
567 ApiBase::PROP_NULLABLE => true
568 )
569 ),
570 'sizediff' => array(
571 'sizediff' => array(
572 ApiBase::PROP_TYPE => 'integer',
573 ApiBase::PROP_NULLABLE => true
574 )
575 )
576 );
577 }
578
579 public function getDescription() {
580 return 'Get all edits by a user';
581 }
582
583 public function getPossibleErrors() {
584 return array_merge( parent::getPossibleErrors(), array(
585 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
586 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
587 array( 'show' ),
588 array(
589 'code' => 'permissiondenied',
590 'info' => 'You need the patrol right to request the patrolled flag'
591 ),
592 ) );
593 }
594
595 public function getExamples() {
596 return array(
597 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
598 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
599 );
600 }
601
602 public function getHelpUrls() {
603 return 'https://www.mediawiki.org/wiki/API:Usercontribs';
604 }
605 }