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