* (bug 26873) API: Add 'toponly' filter in usercontribs module
[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 private function prepareUsername( $user ) {
126 if ( !is_null( $user ) && $user !== '' ) {
127 $name = User::isIP( $user )
128 ? $user
129 : User::getCanonicalName( $user, 'valid' );
130 if ( $name === false ) {
131 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
132 } else {
133 $this->usernames[] = $name;
134 }
135 } else {
136 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
137 }
138 }
139
140 /**
141 * Prepares the query and returns the limit of rows requested
142 */
143 private function prepareQuery() {
144 // We're after the revision table, and the corresponding page
145 // row for anything we retrieve. We may also need the
146 // recentchanges row and/or tag summary row.
147 global $wgUser;
148 $tables = array( 'page', 'revision' ); // Order may change
149 $this->addWhere( 'page_id=rev_page' );
150
151 // Handle continue parameter
152 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
153 $continue = explode( '|', $this->params['continue'] );
154 if ( count( $continue ) != 2 ) {
155 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
156 'value returned by the previous query', '_badcontinue' );
157 }
158 $encUser = $this->getDB()->strencode( $continue[0] );
159 $encTS = wfTimestamp( TS_MW, $continue[1] );
160 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
161 $this->addWhere(
162 "rev_user_text $op '$encUser' OR " .
163 "(rev_user_text = '$encUser' AND " .
164 "rev_timestamp $op= '$encTS')"
165 );
166 }
167
168 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
169 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
170 }
171 // We only want pages by the specified users.
172 if ( $this->prefixMode ) {
173 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
174 } else {
175 $this->addWhereFld( 'rev_user_text', $this->usernames );
176 }
177 // ... and in the specified timeframe.
178 // Ensure the same sort order for rev_user_text and rev_timestamp
179 // so our query is indexed
180 if ( $this->multiUserMode ) {
181 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
182 }
183 $this->addWhereRange( 'rev_timestamp',
184 $this->params['dir'], $this->params['start'], $this->params['end'] );
185 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
186
187 $show = $this->params['show'];
188 if ( !is_null( $show ) ) {
189 $show = array_flip( $show );
190 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
191 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) ) {
192 $this->dieUsageMsg( array( 'show' ) );
193 }
194
195 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
196 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
197 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
198 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
199 }
200 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
201 $index = array( 'revision' => 'usertext_timestamp' );
202
203 // Mandatory fields: timestamp allows request continuation
204 // ns+title checks if the user has access rights for this page
205 // user_text is necessary if multiple users were specified
206 $this->addFields( array(
207 'rev_timestamp',
208 'page_namespace',
209 'page_title',
210 'rev_user',
211 'rev_user_text',
212 'rev_deleted'
213 ) );
214
215 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
216 $this->fld_patrolled ) {
217 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
218 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
219 }
220
221 // Use a redundant join condition on both
222 // timestamp and ID so we can use the timestamp
223 // index
224 $index['recentchanges'] = 'rc_user_text';
225 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
226 // Put the tables in the right order for
227 // STRAIGHT_JOIN
228 $tables = array( 'revision', 'recentchanges', 'page' );
229 $this->addOption( 'STRAIGHT_JOIN' );
230 $this->addWhere( 'rc_user_text=rev_user_text' );
231 $this->addWhere( 'rc_timestamp=rev_timestamp' );
232 $this->addWhere( 'rc_this_oldid=rev_id' );
233 } else {
234 $tables[] = 'recentchanges';
235 $this->addJoinConds( array( 'recentchanges' => array(
236 'LEFT JOIN', array(
237 'rc_user_text=rev_user_text',
238 'rc_timestamp=rev_timestamp',
239 'rc_this_oldid=rev_id' ) ) ) );
240 }
241 }
242
243 $this->addTables( $tables );
244 $this->addFieldsIf( 'rev_page', $this->fld_ids );
245 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
246 $this->addFieldsIf( 'page_latest', $this->fld_flags );
247 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
248 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
249 $this->addFieldsIf( 'rev_len', $this->fld_size );
250 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
251 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags );
252 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
253
254 if ( $this->fld_tags ) {
255 $this->addTables( 'tag_summary' );
256 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
257 $this->addFields( 'ts_tags' );
258 }
259
260 if ( isset( $this->params['tag'] ) ) {
261 $this->addTables( 'change_tag' );
262 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
263 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
264 global $wgOldChangeTagsIndex;
265 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
266 }
267
268 if ( $this->params['toponly'] ) {
269 $this->addWhere( 'rev_id = page_latest' );
270 }
271
272 $this->addOption( 'USE INDEX', $index );
273 }
274
275 /**
276 * Extract fields from the database row and append them to a result 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 global $wgUser;
324 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment, $title );
325 }
326 }
327 }
328
329 if ( $this->fld_patrolled && $row->rc_patrolled ) {
330 $vals['patrolled'] = '';
331 }
332
333 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
334 $vals['size'] = intval( $row->rev_len );
335 }
336
337 if ( $this->fld_tags ) {
338 if ( $row->ts_tags ) {
339 $tags = explode( ',', $row->ts_tags );
340 $this->getResult()->setIndexedTagName( $tags, 'tag' );
341 $vals['tags'] = $tags;
342 } else {
343 $vals['tags'] = array();
344 }
345 }
346
347 return $vals;
348 }
349
350 private function continueStr( $row ) {
351 return $row->rev_user_text . '|' .
352 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
353 }
354
355 public function getCacheMode( $params ) {
356 // This module provides access to deleted revisions and patrol flags if
357 // the requester is logged in
358 return 'anon-public-user-private';
359 }
360
361 public function getAllowedParams() {
362 return array(
363 'limit' => array(
364 ApiBase::PARAM_DFLT => 10,
365 ApiBase::PARAM_TYPE => 'limit',
366 ApiBase::PARAM_MIN => 1,
367 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
368 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
369 ),
370 'start' => array(
371 ApiBase::PARAM_TYPE => 'timestamp'
372 ),
373 'end' => array(
374 ApiBase::PARAM_TYPE => 'timestamp'
375 ),
376 'continue' => null,
377 'user' => array(
378 ApiBase::PARAM_ISMULTI => true
379 ),
380 'userprefix' => null,
381 'dir' => array(
382 ApiBase::PARAM_DFLT => 'older',
383 ApiBase::PARAM_TYPE => array(
384 'newer',
385 'older'
386 )
387 ),
388 'namespace' => array(
389 ApiBase::PARAM_ISMULTI => true,
390 ApiBase::PARAM_TYPE => 'namespace'
391 ),
392 'prop' => array(
393 ApiBase::PARAM_ISMULTI => true,
394 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
395 ApiBase::PARAM_TYPE => array(
396 'ids',
397 'title',
398 'timestamp',
399 'comment',
400 'parsedcomment',
401 'size',
402 'flags',
403 'patrolled',
404 'tags'
405 )
406 ),
407 'show' => array(
408 ApiBase::PARAM_ISMULTI => true,
409 ApiBase::PARAM_TYPE => array(
410 'minor',
411 '!minor',
412 'patrolled',
413 '!patrolled',
414 )
415 ),
416 'tag' => null,
417 'toponly' => false,
418 );
419 }
420
421 public function getParamDescription() {
422 global $wgRCMaxAge;
423 $p = $this->getModulePrefix();
424 return array(
425 'limit' => 'The maximum number of contributions to return',
426 'start' => 'The start timestamp to return from',
427 'end' => 'The end timestamp to return to',
428 'continue' => 'When more results are available, use this to continue',
429 'user' => 'The users to retrieve contributions for',
430 'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
431 'dir' => $this->getDirectionDescription( $p ),
432 'namespace' => 'Only list contributions in these namespaces',
433 'prop' => array(
434 'Include additional pieces of information',
435 ' ids - Adds the page ID and revision ID',
436 ' title - Adds the title and namespace ID of the page',
437 ' timestamp - Adds the timestamp of the edit',
438 ' comment - Adds the comment of the edit',
439 ' parsedcomment - Adds the parsed comment of the edit',
440 ' size - Adds the size of the page',
441 ' flags - Adds flags of the edit',
442 ' patrolled - Tags patrolled edits',
443 ' tags - Lists tags for the edit',
444 ),
445 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
446 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown", ),
447 'tag' => 'Only list revisions tagged with this tag',
448 'toponly' => 'Only list changes which are the latest revision',
449 );
450 }
451
452 public function getDescription() {
453 return 'Get all edits by a user';
454 }
455
456 public function getPossibleErrors() {
457 return array_merge( parent::getPossibleErrors(), array(
458 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
459 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
460 array( 'show' ),
461 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
462 ) );
463 }
464
465 protected function getExamples() {
466 return array(
467 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
468 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
469 );
470 }
471
472 public function getVersion() {
473 return __CLASS__ . ': $Id$';
474 }
475 }