Followup r70460/r70461
[lhc/web/wiklou.git] / includes / api / ApiQueryUserContributions.php
1 <?php
2
3 /**
4 * Created on Oct 16, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * http://www.gnu.org/copyleft/gpl.html
24 */
25
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once( 'ApiQueryBase.php' );
29 }
30
31 /**
32 * This query action adds a list of a specified user's contributions to the output.
33 *
34 * @ingroup API
35 */
36 class ApiQueryContributions extends ApiQueryBase {
37
38 public function __construct( $query, $moduleName ) {
39 parent::__construct( $query, $moduleName, 'uc' );
40 }
41
42 private $params;
43 private $fld_ids = false, $fld_title = false, $fld_timestamp = false,
44 $fld_comment = false, $fld_parsedcomment = false, $fld_flags = false,
45 $fld_patrolled = false, $fld_tags = false;
46
47 public function execute() {
48 // Parse some parameters
49 $this->params = $this->extractRequestParams();
50
51 $prop = array_flip( $this->params['prop'] );
52 $this->fld_ids = isset( $prop['ids'] );
53 $this->fld_title = isset( $prop['title'] );
54 $this->fld_comment = isset( $prop['comment'] );
55 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
56 $this->fld_size = isset( $prop['size'] );
57 $this->fld_flags = isset( $prop['flags'] );
58 $this->fld_timestamp = isset( $prop['timestamp'] );
59 $this->fld_patrolled = isset( $prop['patrolled'] );
60 $this->fld_tags = isset( $prop['tags'] );
61
62 // TODO: if the query is going only against the revision table, should this be done?
63 $this->selectNamedDB( 'contributions', DB_SLAVE, 'contributions' );
64
65 if ( isset( $this->params['userprefix'] ) ) {
66 $this->prefixMode = true;
67 $this->multiUserMode = true;
68 $this->userprefix = $this->params['userprefix'];
69 } else {
70 $this->usernames = array();
71 if ( !is_array( $this->params['user'] ) ) {
72 $this->params['user'] = array( $this->params['user'] );
73 }
74 if ( !count( $this->params['user'] ) ) {
75 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
76 }
77 foreach ( $this->params['user'] as $u ) {
78 $this->prepareUsername( $u );
79 }
80 $this->prefixMode = false;
81 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
82 }
83 $this->prepareQuery();
84
85 // Do the actual query.
86 $res = $this->select( __METHOD__ );
87
88 // Initialise some variables
89 $count = 0;
90 $limit = $this->params['limit'];
91
92 // Fetch each row
93 foreach ( $res as $row ) {
94 if ( ++ $count > $limit ) {
95 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
96 if ( $this->multiUserMode ) {
97 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
98 } else {
99 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
100 }
101 break;
102 }
103
104 $vals = $this->extractRowInfo( $row );
105 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
106 if ( !$fit ) {
107 if ( $this->multiUserMode ) {
108 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
109 } else {
110 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
111 }
112 break;
113 }
114 }
115
116 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
117 }
118
119 /**
120 * Validate the 'user' parameter and set the value to compare
121 * against `revision`.`rev_user_text`
122 */
123 private function prepareUsername( $user ) {
124 if ( !is_null( $user ) && $user !== '' ) {
125 $name = User::isIP( $user )
126 ? $user
127 : User::getCanonicalName( $user, 'valid' );
128 if ( $name === false ) {
129 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
130 } else {
131 $this->usernames[] = $name;
132 }
133 } else {
134 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
135 }
136 }
137
138 /**
139 * Prepares the query and returns the limit of rows requested
140 */
141 private function prepareQuery() {
142 // We're after the revision table, and the corresponding page
143 // row for anything we retrieve. We may also need the
144 // recentchanges row and/or tag summary row.
145 global $wgUser;
146 $tables = array( 'page', 'revision' ); // Order may change
147 $this->addWhere( 'page_id=rev_page' );
148
149 // Handle continue parameter
150 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
151 $continue = explode( '|', $this->params['continue'] );
152 if ( count( $continue ) != 2 ) {
153 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
154 'value returned by the previous query', '_badcontinue' );
155 }
156 $encUser = $this->getDB()->strencode( $continue[0] );
157 $encTS = wfTimestamp( TS_MW, $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 ( !$wgUser->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->addWhereRange( '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( array( '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_text',
209 'rev_deleted'
210 ) );
211
212 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
213 $this->fld_patrolled ) {
214 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
215 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
216 }
217
218 // Use a redundant join condition on both
219 // timestamp and ID so we can use the timestamp
220 // index
221 $index['recentchanges'] = 'rc_user_text';
222 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
223 // Put the tables in the right order for
224 // STRAIGHT_JOIN
225 $tables = array( 'revision', 'recentchanges', 'page' );
226 $this->addOption( 'STRAIGHT_JOIN' );
227 $this->addWhere( 'rc_user_text=rev_user_text' );
228 $this->addWhere( 'rc_timestamp=rev_timestamp' );
229 $this->addWhere( 'rc_this_oldid=rev_id' );
230 } else {
231 $tables[] = 'recentchanges';
232 $this->addJoinConds( array( 'recentchanges' => array(
233 'LEFT JOIN', array(
234 'rc_user_text=rev_user_text',
235 'rc_timestamp=rev_timestamp',
236 'rc_this_oldid=rev_id' ) ) ) );
237 }
238 }
239
240 $this->addTables( $tables );
241 $this->addFieldsIf( 'rev_page', $this->fld_ids );
242 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
243 $this->addFieldsIf( 'page_latest', $this->fld_flags );
244 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
245 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
246 $this->addFieldsIf( 'rev_len', $this->fld_size );
247 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
248 $this->addFieldsIf( '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 $this->addOption( 'USE INDEX', $index );
266 }
267
268 /**
269 * Extract fields from the database row and append them to a result array
270 */
271 private function extractRowInfo( $row ) {
272 $vals = array();
273
274 $vals['user'] = $row->rev_user_text;
275 if ( $row->rev_deleted & Revision::DELETED_USER ) {
276 $vals['userhidden'] = '';
277 }
278 if ( $this->fld_ids ) {
279 $vals['pageid'] = intval( $row->rev_page );
280 $vals['revid'] = intval( $row->rev_id );
281 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
282 }
283
284 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
285
286 if ( $this->fld_title ) {
287 ApiQueryBase::addTitleInfo( $vals, $title );
288 }
289
290 if ( $this->fld_timestamp ) {
291 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
292 }
293
294 if ( $this->fld_flags ) {
295 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
296 $vals['new'] = '';
297 }
298 if ( $row->rev_minor_edit ) {
299 $vals['minor'] = '';
300 }
301 if ( $row->page_latest == $row->rev_id ) {
302 $vals['top'] = '';
303 }
304 }
305
306 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
307 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
308 $vals['commenthidden'] = '';
309 } else {
310 if ( $this->fld_comment ) {
311 $vals['comment'] = $row->rev_comment;
312 }
313
314 if ( $this->fld_parsedcomment ) {
315 global $wgUser;
316 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment, $title );
317 }
318 }
319 }
320
321 if ( $this->fld_patrolled && $row->rc_patrolled ) {
322 $vals['patrolled'] = '';
323 }
324
325 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
326 $vals['size'] = intval( $row->rev_len );
327 }
328
329 if ( $this->fld_tags ) {
330 if ( $row->ts_tags ) {
331 $tags = explode( ',', $row->ts_tags );
332 $this->getResult()->setIndexedTagName( $tags, 'tag' );
333 $vals['tags'] = $tags;
334 } else {
335 $vals['tags'] = array();
336 }
337 }
338
339 return $vals;
340 }
341
342 private function continueStr( $row ) {
343 return $row->rev_user_text . '|' .
344 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
345 }
346
347 public function getCacheMode( $params ) {
348 // This module provides access to deleted revisions and patrol flags if
349 // the requester is logged in
350 return 'anon-public-user-private';
351 }
352
353 public function getAllowedParams() {
354 return array(
355 'limit' => array(
356 ApiBase::PARAM_DFLT => 10,
357 ApiBase::PARAM_TYPE => 'limit',
358 ApiBase::PARAM_MIN => 1,
359 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
360 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
361 ),
362 'start' => array(
363 ApiBase::PARAM_TYPE => 'timestamp'
364 ),
365 'end' => array(
366 ApiBase::PARAM_TYPE => 'timestamp'
367 ),
368 'continue' => null,
369 'user' => array(
370 ApiBase::PARAM_ISMULTI => true
371 ),
372 'userprefix' => null,
373 'dir' => array(
374 ApiBase::PARAM_DFLT => 'older',
375 ApiBase::PARAM_TYPE => array(
376 'newer',
377 'older'
378 )
379 ),
380 'namespace' => array(
381 ApiBase::PARAM_ISMULTI => true,
382 ApiBase::PARAM_TYPE => 'namespace'
383 ),
384 'prop' => array(
385 ApiBase::PARAM_ISMULTI => true,
386 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
387 ApiBase::PARAM_TYPE => array(
388 'ids',
389 'title',
390 'timestamp',
391 'comment',
392 'parsedcomment',
393 'size',
394 'flags',
395 'patrolled',
396 'tags'
397 )
398 ),
399 'show' => array(
400 ApiBase::PARAM_ISMULTI => true,
401 ApiBase::PARAM_TYPE => array(
402 'minor',
403 '!minor',
404 'patrolled',
405 '!patrolled',
406 )
407 ),
408 'tag' => null,
409 );
410 }
411
412 public function getParamDescription() {
413 global $wgRCMaxAge;
414 $p = $this->getModulePrefix();
415 return array(
416 'limit' => 'The maximum number of contributions to return',
417 'start' => 'The start timestamp to return from',
418 'end' => 'The end timestamp to return to',
419 'continue' => 'When more results are available, use this to continue',
420 'user' => 'The users to retrieve contributions for',
421 'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
422 'dir' => 'The direction to search (older or newer)',
423 'namespace' => 'Only list contributions in these namespaces',
424 'prop' => array(
425 'Include additional pieces of information',
426 ' ids - Adds the page id and revision id',
427 ' title - Adds the title and namespace id of the page',
428 ' timestamp - Adds the timestamp of the edit',
429 ' comment - Adds the comment of the edit',
430 ' parsedcomment - Adds the parsed comment of the edit',
431 ' size - Adds the size of the page',
432 ' flags - Adds flags of the edit',
433 ' patrolled - Tags patrolled edits',
434 ' tags - Lists tags for the edit',
435 ),
436 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
437 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown", ),
438 'tag' => 'Only list revisions tagged with this tag',
439 );
440 }
441
442 public function getDescription() {
443 return 'Get all edits by a user';
444 }
445
446 public function getPossibleErrors() {
447 return array_merge( parent::getPossibleErrors(), array(
448 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
449 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
450 array( 'show' ),
451 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
452 ) );
453 }
454
455 protected function getExamples() {
456 return array(
457 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
458 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
459 );
460 }
461
462 public function getVersion() {
463 return __CLASS__ . ': $Id$';
464 }
465 }