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