f200c8b3f371667bca1766ad495ba42800724d95
[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, $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 $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 $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 foreach ( $res as $row ) {
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 }
102 break;
103 }
104
105 $vals = $this->extractRowInfo( $row );
106 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
107 if ( !$fit ) {
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 }
113 break;
114 }
115 }
116
117 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
118 }
119
120 /**
121 * Validate the 'user' parameter and set the value to compare
122 * against `revision`.`rev_user_text`
123 */
124 private function prepareUsername( $user ) {
125 if ( !is_null( $user ) && $user !== '' ) {
126 $name = User::isIP( $user )
127 ? $user
128 : User::getCanonicalName( $user, 'valid' );
129 if ( $name === false ) {
130 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
131 } else {
132 $this->usernames[] = $name;
133 }
134 } else {
135 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
136 }
137 }
138
139 /**
140 * Prepares the query and returns the limit of rows requested
141 */
142 private function prepareQuery() {
143 // We're after the revision table, and the corresponding page
144 // row for anything we retrieve. We may also need the
145 // recentchanges row and/or tag summary row.
146 global $wgUser;
147 $tables = array( 'page', 'revision' ); // Order may change
148 $this->addWhere( 'page_id=rev_page' );
149
150 // Handle continue parameter
151 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
152 $continue = explode( '|', $this->params['continue'] );
153 if ( count( $continue ) != 2 ) {
154 $this->dieUsage( 'Invalid continue param. You should pass the original ' .
155 'value returned by the previous query', '_badcontinue' );
156 }
157 $encUser = $this->getDB()->strencode( $continue[0] );
158 $encTS = wfTimestamp( TS_MW, $continue[1] );
159 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
160 $this->addWhere(
161 "rev_user_text $op '$encUser' OR " .
162 "(rev_user_text = '$encUser' AND " .
163 "rev_timestamp $op= '$encTS')"
164 );
165 }
166
167 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
168 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
169 }
170 // We only want pages by the specified users.
171 if ( $this->prefixMode ) {
172 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
173 } else {
174 $this->addWhereFld( 'rev_user_text', $this->usernames );
175 }
176 // ... and in the specified timeframe.
177 // Ensure the same sort order for rev_user_text and rev_timestamp
178 // so our query is indexed
179 if ( $this->multiUserMode ) {
180 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
181 }
182 $this->addWhereRange( 'rev_timestamp',
183 $this->params['dir'], $this->params['start'], $this->params['end'] );
184 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
185
186 $show = $this->params['show'];
187 if ( !is_null( $show ) ) {
188 $show = array_flip( $show );
189 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
190 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) )
191 {
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_text',
211 'rev_deleted'
212 ) );
213
214 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
215 $this->fld_patrolled )
216 {
217 global $wgUser;
218 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
219 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
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 $this->addOption( 'USE INDEX', $index );
269 }
270
271 /**
272 * Extract fields from the database row and append them to a result array
273 */
274 private function extractRowInfo( $row ) {
275 $vals = array();
276
277 $vals['user'] = $row->rev_user_text;
278 if ( $row->rev_deleted & Revision::DELETED_USER ) {
279 $vals['userhidden'] = '';
280 }
281 if ( $this->fld_ids ) {
282 $vals['pageid'] = intval( $row->rev_page );
283 $vals['revid'] = intval( $row->rev_id );
284 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
285 }
286
287 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
288
289 if ( $this->fld_title ) {
290 ApiQueryBase::addTitleInfo( $vals, $title );
291 }
292
293 if ( $this->fld_timestamp ) {
294 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
295 }
296
297 if ( $this->fld_flags ) {
298 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
299 $vals['new'] = '';
300 }
301 if ( $row->rev_minor_edit ) {
302 $vals['minor'] = '';
303 }
304 if ( $row->page_latest == $row->rev_id ) {
305 $vals['top'] = '';
306 }
307 }
308
309 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
310 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
311 $vals['commenthidden'] = '';
312 } else {
313 if ( $this->fld_comment ) {
314 $vals['comment'] = $row->rev_comment;
315 }
316
317 if ( $this->fld_parsedcomment ) {
318 global $wgUser;
319 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->rev_comment, $title );
320 }
321 }
322 }
323
324 if ( $this->fld_patrolled && $row->rc_patrolled ) {
325 $vals['patrolled'] = '';
326 }
327
328 if ( $this->fld_size && !is_null( $row->rev_len ) ) {
329 $vals['size'] = intval( $row->rev_len );
330 }
331
332 if ( $this->fld_tags ) {
333 if ( $row->ts_tags ) {
334 $tags = explode( ',', $row->ts_tags );
335 $this->getResult()->setIndexedTagName( $tags, 'tag' );
336 $vals['tags'] = $tags;
337 } else {
338 $vals['tags'] = array();
339 }
340 }
341
342 return $vals;
343 }
344
345 private function continueStr( $row ) {
346 return $row->rev_user_text . '|' .
347 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
348 }
349
350 public function getAllowedParams() {
351 return array(
352 'limit' => array(
353 ApiBase::PARAM_DFLT => 10,
354 ApiBase::PARAM_TYPE => 'limit',
355 ApiBase::PARAM_MIN => 1,
356 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
357 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
358 ),
359 'start' => array(
360 ApiBase::PARAM_TYPE => 'timestamp'
361 ),
362 'end' => array(
363 ApiBase::PARAM_TYPE => 'timestamp'
364 ),
365 'continue' => null,
366 'user' => array(
367 ApiBase::PARAM_ISMULTI => true
368 ),
369 'userprefix' => null,
370 'dir' => array(
371 ApiBase::PARAM_DFLT => 'older',
372 ApiBase::PARAM_TYPE => array(
373 'newer',
374 'older'
375 )
376 ),
377 'namespace' => array(
378 ApiBase::PARAM_ISMULTI => true,
379 ApiBase::PARAM_TYPE => 'namespace'
380 ),
381 'prop' => array(
382 ApiBase::PARAM_ISMULTI => true,
383 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
384 ApiBase::PARAM_TYPE => array(
385 'ids',
386 'title',
387 'timestamp',
388 'comment',
389 'parsedcomment',
390 'size',
391 'flags',
392 'patrolled',
393 'tags'
394 )
395 ),
396 'show' => array(
397 ApiBase::PARAM_ISMULTI => true,
398 ApiBase::PARAM_TYPE => array(
399 'minor',
400 '!minor',
401 'patrolled',
402 '!patrolled',
403 )
404 ),
405 'tag' => null,
406 );
407 }
408
409 public function getParamDescription() {
410 global $wgRCMaxAge;
411 $p = $this->getModulePrefix();
412 return array(
413 'limit' => 'The maximum number of contributions to return',
414 'start' => 'The start timestamp to return from',
415 'end' => 'The end timestamp to return to',
416 'continue' => 'When more results are available, use this to continue',
417 'user' => 'The users to retrieve contributions for',
418 'userprefix' => "Retrieve contibutions for all users whose names begin with this value. Overrides {$p}user",
419 'dir' => 'The direction to search (older or newer)',
420 'namespace' => 'Only list contributions in these namespaces',
421 'prop' => 'Include additional pieces of information',
422 'show' => array( "Show only items that meet this criteria, e.g. non minor edits only: {$p}show=!minor",
423 "NOTE: if {$p}show=patrolled or {$p}show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown", ),
424 'tag' => 'Only list revisions tagged with this tag',
425 );
426 }
427
428 public function getDescription() {
429 return 'Get all edits by a user';
430 }
431
432 public function getPossibleErrors() {
433 return array_merge( parent::getPossibleErrors(), array(
434 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
435 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
436 array( 'show' ),
437 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
438 ) );
439 }
440
441 protected function getExamples() {
442 return array(
443 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
444 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
445 );
446 }
447
448 public function getVersion() {
449 return __CLASS__ . ': $Id$';
450 }
451 }