Fix r62331 - If we renamed a method, we need to to it everywhere!
[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 (C) 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 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, 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, $username;
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 $db = $this->getDB();
65
66 if ( isset( $this->params['userprefix'] ) )
67 {
68 $this->prefixMode = true;
69 $this->multiUserMode = true;
70 $this->userprefix = $this->params['userprefix'];
71 }
72 else
73 {
74 $this->usernames = array();
75 if ( !is_array( $this->params['user'] ) )
76 $this->params['user'] = array( $this->params['user'] );
77 if ( !count( $this->params['user'] ) )
78 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
79 foreach ( $this->params['user'] as $u )
80 $this->prepareUsername( $u );
81 $this->prefixMode = false;
82 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
83 }
84 $this->prepareQuery();
85
86 // Do the actual query.
87 $res = $this->select( __METHOD__ );
88
89 // Initialise some variables
90 $count = 0;
91 $limit = $this->params['limit'];
92
93 // Fetch each row
94 while ( $row = $db->fetchObject( $res ) ) {
95 if ( ++ $count > $limit ) {
96 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
97 if ( $this->multiUserMode )
98 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
99 else
100 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
101 break;
102 }
103
104 $vals = $this->extractRowInfo( $row );
105 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
106 if ( !$fit )
107 {
108 if ( $this->multiUserMode )
109 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
110 else
111 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
112 break;
113 }
114 }
115
116 // Free the database record so the connection can get on with other stuff
117 $db->freeResult( $res );
118
119 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
120 }
121
122 /**
123 * Validate the 'user' parameter and set the value to compare
124 * against `revision`.`rev_user_text`
125 */
126 private function prepareUsername( $user ) {
127 if ( !is_null( $user ) && $user !== '' ) {
128 $name = User::isIP( $user )
129 ? $user
130 : User::getCanonicalName( $user, 'valid' );
131 if ( $name === false ) {
132 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
133 } else {
134 $this->usernames[] = $name;
135 }
136 } else {
137 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
138 }
139 }
140
141 /**
142 * Prepares the query and returns the limit of rows requested
143 */
144 private function prepareQuery() {
145 // We're after the revision table, and the corresponding page
146 // row for anything we retrieve. We may also need the
147 // recentchanges row and/or tag summary row.
148 global $wgUser;
149 $tables = array( 'page', 'revision' ); // Order may change
150 $this->addWhere( 'page_id=rev_page' );
151
152 // Handle continue parameter
153 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) )
154 {
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 $encUser = $this->getDB()->strencode( $continue[0] );
160 $encTS = wfTimestamp( TS_MW, $continue[1] );
161 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
162 $this->addWhere( "rev_user_text $op '$encUser' OR " .
163 "(rev_user_text = '$encUser' AND " .
164 "rev_timestamp $op= '$encTS')" );
165 }
166
167 if ( !$wgUser->isAllowed( 'hideuser' ) )
168 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
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 // ... and in the specified timeframe.
175 // Ensure the same sort order for rev_user_text and rev_timestamp
176 // so our query is indexed
177 if ( $this->multiUserMode )
178 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
179 $this->addWhereRange( 'rev_timestamp',
180 $this->params['dir'], $this->params['start'], $this->params['end'] );
181 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
182
183 $show = $this->params['show'];
184 if ( !is_null( $show ) ) {
185 $show = array_flip( $show );
186 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
187 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) )
188 $this->dieUsage( "Incorrect parameter - mutually exclusive values may not be supplied", 'show' );
189
190 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
191 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
192 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
193 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
194 }
195 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
196 $index = array( 'revision' => 'usertext_timestamp' );
197
198 // Mandatory fields: timestamp allows request continuation
199 // ns+title checks if the user has access rights for this page
200 // user_text is necessary if multiple users were specified
201 $this->addFields( array(
202 'rev_timestamp',
203 'page_namespace',
204 'page_title',
205 'rev_user_text',
206 'rev_deleted'
207 ) );
208
209 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
210 $this->fld_patrolled )
211 {
212 global $wgUser;
213 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
214 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
215 // Use a redundant join condition on both
216 // timestamp and ID so we can use the timestamp
217 // index
218 $index['recentchanges'] = 'rc_user_text';
219 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) )
220 {
221 // Put the tables in the right order for
222 // STRAIGHT_JOIN
223 $tables = array( 'revision', 'recentchanges', 'page' );
224 $this->addOption( 'STRAIGHT_JOIN' );
225 $this->addWhere( 'rc_user_text=rev_user_text' );
226 $this->addWhere( 'rc_timestamp=rev_timestamp' );
227 $this->addWhere( 'rc_this_oldid=rev_id' );
228 }
229 else
230 {
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 {
253 $this->addTables( 'tag_summary' );
254 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
255 $this->addFields( 'ts_tags' );
256 }
257
258 if ( isset( $this->params['tag'] ) ) {
259 $this->addTables( 'change_tag' );
260 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
261 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
262 global $wgOldChangeTagsIndex;
263 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
264 }
265
266 $this->addOption( 'USE INDEX', $index );
267 }
268
269 /**
270 * Extract fields from the database row and append them to a result array
271 */
272 private function extractRowInfo( $row ) {
273
274 $vals = array();
275
276 $vals['user'] = $row->rev_user_text;
277 if ( $row->rev_deleted & Revision::DELETED_USER )
278 $vals['userhidden'] = '';
279 if ( $this->fld_ids ) {
280 $vals['pageid'] = intval( $row->rev_page );
281 $vals['revid'] = intval( $row->rev_id );
282 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
283 }
284
285 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
286
287 if ( $this->fld_title )
288 ApiQueryBase::addTitleInfo( $vals, $title );
289
290 if ( $this->fld_timestamp )
291 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
292
293 if ( $this->fld_flags ) {
294 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) )
295 $vals['new'] = '';
296 if ( $row->rev_minor_edit )
297 $vals['minor'] = '';
298 if ( $row->page_latest == $row->rev_id )
299 $vals['top'] = '';
300 }
301
302 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
303 if ( $row->rev_deleted & Revision::DELETED_COMMENT )
304 $vals['commenthidden'] = '';
305 else {
306 if ( $this->fld_comment )
307 $vals['comment'] = $row->rev_comment;
308
309 if ( $this->fld_parsedcomment ) {
310 global $wgUser;
311 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment, $title );
312 }
313 }
314 }
315
316 if ( $this->fld_patrolled && $row->rc_patrolled )
317 $vals['patrolled'] = '';
318
319 if ( $this->fld_size && !is_null( $row->rev_len ) )
320 $vals['size'] = intval( $row->rev_len );
321
322 if ( $this->fld_tags ) {
323 if ( $row->ts_tags ) {
324 $tags = explode( ',', $row->ts_tags );
325 $this->getResult()->setIndexedTagName( $tags, 'tag' );
326 $vals['tags'] = $tags;
327 } else {
328 $vals['tags'] = array();
329 }
330 }
331
332 return $vals;
333 }
334
335 private function continueStr( $row )
336 {
337 return $row->rev_user_text . '|' .
338 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
339 }
340
341 public function getAllowedParams() {
342 return array (
343 'limit' => array (
344 ApiBase :: PARAM_DFLT => 10,
345 ApiBase :: PARAM_TYPE => 'limit',
346 ApiBase :: PARAM_MIN => 1,
347 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
348 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
349 ),
350 'start' => array (
351 ApiBase :: PARAM_TYPE => 'timestamp'
352 ),
353 'end' => array (
354 ApiBase :: PARAM_TYPE => 'timestamp'
355 ),
356 'continue' => null,
357 'user' => array (
358 ApiBase :: PARAM_ISMULTI => true
359 ),
360 'userprefix' => null,
361 'dir' => array (
362 ApiBase :: PARAM_DFLT => 'older',
363 ApiBase :: PARAM_TYPE => array (
364 'newer',
365 'older'
366 )
367 ),
368 'namespace' => array (
369 ApiBase :: PARAM_ISMULTI => true,
370 ApiBase :: PARAM_TYPE => 'namespace'
371 ),
372 'prop' => array (
373 ApiBase :: PARAM_ISMULTI => true,
374 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
375 ApiBase :: PARAM_TYPE => array (
376 'ids',
377 'title',
378 'timestamp',
379 'comment',
380 'parsedcomment',
381 'size',
382 'flags',
383 'patrolled',
384 'tags'
385 )
386 ),
387 'show' => array (
388 ApiBase :: PARAM_ISMULTI => true,
389 ApiBase :: PARAM_TYPE => array (
390 'minor',
391 '!minor',
392 'patrolled',
393 '!patrolled',
394 )
395 ),
396 'tag' => null,
397 );
398 }
399
400 public function getParamDescription() {
401 return array (
402 'limit' => 'The maximum number of contributions to return.',
403 'start' => 'The start timestamp to return from.',
404 'end' => 'The end timestamp to return to.',
405 'continue' => 'When more results are available, use this to continue.',
406 'user' => 'The user to retrieve contributions for.',
407 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
408 'dir' => 'The direction to search (older or newer).',
409 'namespace' => 'Only list contributions in these namespaces',
410 'prop' => 'Include additional pieces of information',
411 'show' => array( 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
412 'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown', ),
413 'tag' => 'Only list revisions tagged with this tag',
414 );
415 }
416
417 public function getDescription() {
418 return 'Get all edits by a user';
419 }
420
421 protected function getExamples() {
422 return array (
423 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
424 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
425 );
426 }
427
428 public function getVersion() {
429 return __CLASS__ . ': $Id$';
430 }
431 }