stylize.php on API code
[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_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_size = isset( $prop['size'] );
56 $this->fld_flags = isset( $prop['flags'] );
57 $this->fld_timestamp = isset( $prop['timestamp'] );
58 $this->fld_patrolled = isset( $prop['patrolled'] );
59 $this->fld_tags = isset( $prop['tags'] );
60
61 // TODO: if the query is going only against the revision table, should this be done?
62 $this->selectNamedDB( 'contributions', DB_SLAVE, 'contributions' );
63 $db = $this->getDB();
64
65 if ( isset( $this->params['userprefix'] ) )
66 {
67 $this->prefixMode = true;
68 $this->multiUserMode = true;
69 $this->userprefix = $this->params['userprefix'];
70 }
71 else
72 {
73 $this->usernames = array();
74 if ( !is_array( $this->params['user'] ) )
75 $this->params['user'] = array( $this->params['user'] );
76 if ( !count( $this->params['user'] ) )
77 $this->dieUsage( 'User parameter may not be empty.', 'param_user' );
78 foreach ( $this->params['user'] as $u )
79 $this->prepareUsername( $u );
80 $this->prefixMode = false;
81 $this->multiUserMode = ( count( $this->params['user'] ) > 1 );
82 }
83 $this->prepareQuery();
84
85 // Do the actual query.
86 $res = $this->select( __METHOD__ );
87
88 // Initialise some variables
89 $count = 0;
90 $limit = $this->params['limit'];
91
92 // Fetch each row
93 while ( $row = $db->fetchObject( $res ) ) {
94 if ( ++ $count > $limit ) {
95 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
96 if ( $this->multiUserMode )
97 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
98 else
99 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
100 break;
101 }
102
103 $vals = $this->extractRowInfo( $row );
104 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
105 if ( !$fit )
106 {
107 if ( $this->multiUserMode )
108 $this->setContinueEnumParameter( 'continue', $this->continueStr( $row ) );
109 else
110 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->rev_timestamp ) );
111 break;
112 }
113 }
114
115 // Free the database record so the connection can get on with other stuff
116 $db->freeResult( $res );
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 {
154 $continue = explode( '|', $this->params['continue'] );
155 if ( count( $continue ) != 2 )
156 $this->dieUsage( "Invalid continue param. You should pass the original " .
157 "value returned by the previous query", "_badcontinue" );
158 $encUser = $this->getDB()->strencode( $continue[0] );
159 $encTS = wfTimestamp( TS_MW, $continue[1] );
160 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
161 $this->addWhere( "rev_user_text $op '$encUser' OR " .
162 "(rev_user_text = '$encUser' AND " .
163 "rev_timestamp $op= '$encTS')" );
164 }
165
166 if ( !$wgUser->isAllowed( 'hideuser' ) )
167 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
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 // ... and in the specified timeframe.
174 // Ensure the same sort order for rev_user_text and rev_timestamp
175 // so our query is indexed
176 if ( $this->multiUserMode )
177 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
178 $this->addWhereRange( 'rev_timestamp',
179 $this->params['dir'], $this->params['start'], $this->params['end'] );
180 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
181
182 $show = $this->params['show'];
183 if ( !is_null( $show ) ) {
184 $show = array_flip( $show );
185 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
186 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) )
187 $this->dieUsage( "Incorrect parameter - mutually exclusive values may not be supplied", 'show' );
188
189 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
190 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
191 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
192 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
193 }
194 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
195 $index['revision'] = 'usertext_timestamp';
196
197 // Mandatory fields: timestamp allows request continuation
198 // ns+title checks if the user has access rights for this page
199 // user_text is necessary if multiple users were specified
200 $this->addFields( array(
201 'rev_timestamp',
202 'page_namespace',
203 'page_title',
204 'rev_user_text',
205 'rev_deleted'
206 ) );
207
208 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
209 $this->fld_patrolled )
210 {
211 global $wgUser;
212 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() )
213 $this->dieUsage( "You need the patrol right to request the patrolled flag", 'permissiondenied' );
214 // Use a redundant join condition on both
215 // timestamp and ID so we can use the timestamp
216 // index
217 $index['recentchanges'] = 'rc_user_text';
218 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) )
219 {
220 // Put the tables in the right order for
221 // STRAIGHT_JOIN
222 $tables = array( 'revision', 'recentchanges', 'page' );
223 $this->addOption( 'STRAIGHT_JOIN' );
224 $this->addWhere( 'rc_user_text=rev_user_text' );
225 $this->addWhere( 'rc_timestamp=rev_timestamp' );
226 $this->addWhere( 'rc_this_oldid=rev_id' );
227 }
228 else
229 {
230 $tables[] = 'recentchanges';
231 $this->addJoinConds( array( 'recentchanges' => array(
232 'LEFT JOIN', array(
233 'rc_user_text=rev_user_text',
234 'rc_timestamp=rev_timestamp',
235 'rc_this_oldid=rev_id' ) ) ) );
236 }
237 }
238
239 $this->addTables( $tables );
240 $this->addOption( 'USE INDEX', $index );
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 );
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 ( !is_null( $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 }
263 }
264
265 /**
266 * Extract fields from the database row and append them to a result array
267 */
268 private function extractRowInfo( $row ) {
269
270 $vals = array();
271
272 $vals['user'] = $row->rev_user_text;
273 if ( $row->rev_deleted & Revision::DELETED_USER )
274 $vals['userhidden'] = '';
275 if ( $this->fld_ids ) {
276 $vals['pageid'] = intval( $row->rev_page );
277 $vals['revid'] = intval( $row->rev_id );
278 // $vals['textid'] = intval($row->rev_text_id); // todo: Should this field be exposed?
279 }
280
281 if ( $this->fld_title )
282 ApiQueryBase :: addTitleInfo( $vals,
283 Title :: makeTitle( $row->page_namespace, $row->page_title ) );
284
285 if ( $this->fld_timestamp )
286 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
287
288 if ( $this->fld_flags ) {
289 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) )
290 $vals['new'] = '';
291 if ( $row->rev_minor_edit )
292 $vals['minor'] = '';
293 if ( $row->page_latest == $row->rev_id )
294 $vals['top'] = '';
295 }
296
297 if ( $this->fld_comment && isset( $row->rev_comment ) ) {
298 if ( $row->rev_deleted & Revision::DELETED_COMMENT )
299 $vals['commenthidden'] = '';
300 else
301 $vals['comment'] = $row->rev_comment;
302 }
303
304 if ( $this->fld_patrolled && $row->rc_patrolled )
305 $vals['patrolled'] = '';
306
307 if ( $this->fld_size && !is_null( $row->rev_len ) )
308 $vals['size'] = intval( $row->rev_len );
309
310 if ( $this->fld_tags ) {
311 if ( $row->ts_tags ) {
312 $tags = explode( ',', $row->ts_tags );
313 $this->getResult()->setIndexedTagName( $tags, 'tag' );
314 $vals['tags'] = $tags;
315 } else {
316 $vals['tags'] = array();
317 }
318 }
319
320 return $vals;
321 }
322
323 private function continueStr( $row )
324 {
325 return $row->rev_user_text . '|' .
326 wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
327 }
328
329 public function getAllowedParams() {
330 return array (
331 'limit' => array (
332 ApiBase :: PARAM_DFLT => 10,
333 ApiBase :: PARAM_TYPE => 'limit',
334 ApiBase :: PARAM_MIN => 1,
335 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
336 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
337 ),
338 'start' => array (
339 ApiBase :: PARAM_TYPE => 'timestamp'
340 ),
341 'end' => array (
342 ApiBase :: PARAM_TYPE => 'timestamp'
343 ),
344 'continue' => null,
345 'user' => array (
346 ApiBase :: PARAM_ISMULTI => true
347 ),
348 'userprefix' => null,
349 'dir' => array (
350 ApiBase :: PARAM_DFLT => 'older',
351 ApiBase :: PARAM_TYPE => array (
352 'newer',
353 'older'
354 )
355 ),
356 'namespace' => array (
357 ApiBase :: PARAM_ISMULTI => true,
358 ApiBase :: PARAM_TYPE => 'namespace'
359 ),
360 'prop' => array (
361 ApiBase :: PARAM_ISMULTI => true,
362 ApiBase :: PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
363 ApiBase :: PARAM_TYPE => array (
364 'ids',
365 'title',
366 'timestamp',
367 'comment',
368 'size',
369 'flags',
370 'patrolled',
371 'tags'
372 )
373 ),
374 'show' => array (
375 ApiBase :: PARAM_ISMULTI => true,
376 ApiBase :: PARAM_TYPE => array (
377 'minor',
378 '!minor',
379 'patrolled',
380 '!patrolled',
381 )
382 ),
383 );
384 }
385
386 public function getParamDescription() {
387 return array (
388 'limit' => 'The maximum number of contributions to return.',
389 'start' => 'The start timestamp to return from.',
390 'end' => 'The end timestamp to return to.',
391 'continue' => 'When more results are available, use this to continue.',
392 'user' => 'The user to retrieve contributions for.',
393 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
394 'dir' => 'The direction to search (older or newer).',
395 'namespace' => 'Only list contributions in these namespaces',
396 'prop' => 'Include additional pieces of information',
397 'show' => array( 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
398 'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown', ),
399 );
400 }
401
402 public function getDescription() {
403 return 'Get all edits by a user';
404 }
405
406 protected function getExamples() {
407 return array (
408 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
409 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
410 );
411 }
412
413 public function getVersion() {
414 return __CLASS__ . ': $Id$';
415 }
416 }