Rewrite ajaxwatch.js to use the API watch action, and JQuery. Allows it to be used...
[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 * 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 $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 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 }
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 // Free the database record so the connection can get on with other stuff
118 $db->freeResult( $res );
119
120 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
121 }
122
123 /**
124 * Validate the 'user' parameter and set the value to compare
125 * against `revision`.`rev_user_text`
126 */
127 private function prepareUsername( $user ) {
128 if ( !is_null( $user ) && $user !== '' ) {
129 $name = User::isIP( $user )
130 ? $user
131 : User::getCanonicalName( $user, 'valid' );
132 if ( $name === false ) {
133 $this->dieUsage( "User name {$user} is not valid", 'param_user' );
134 } else {
135 $this->usernames[] = $name;
136 }
137 } else {
138 $this->dieUsage( 'User parameter may not be empty', 'param_user' );
139 }
140 }
141
142 /**
143 * Prepares the query and returns the limit of rows requested
144 */
145 private function prepareQuery() {
146 // We're after the revision table, and the corresponding page
147 // row for anything we retrieve. We may also need the
148 // recentchanges row and/or tag summary row.
149 global $wgUser;
150 $tables = array( 'page', 'revision' ); // Order may change
151 $this->addWhere( 'page_id=rev_page' );
152
153 // Handle continue parameter
154 if ( $this->multiUserMode && !is_null( $this->params['continue'] ) ) {
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 }
160 $encUser = $this->getDB()->strencode( $continue[0] );
161 $encTS = wfTimestamp( TS_MW, $continue[1] );
162 $op = ( $this->params['dir'] == 'older' ? '<' : '>' );
163 $this->addWhere(
164 "rev_user_text $op '$encUser' OR " .
165 "(rev_user_text = '$encUser' AND " .
166 "rev_timestamp $op= '$encTS')"
167 );
168 }
169
170 if ( !$wgUser->isAllowed( 'hideuser' ) ) {
171 $this->addWhere( $this->getDB()->bitAnd( 'rev_deleted', Revision::DELETED_USER ) . ' = 0' );
172 }
173 // We only want pages by the specified users.
174 if ( $this->prefixMode ) {
175 $this->addWhere( 'rev_user_text' . $this->getDB()->buildLike( $this->userprefix, $this->getDB()->anyString() ) );
176 } else {
177 $this->addWhereFld( 'rev_user_text', $this->usernames );
178 }
179 // ... and in the specified timeframe.
180 // Ensure the same sort order for rev_user_text and rev_timestamp
181 // so our query is indexed
182 if ( $this->multiUserMode ) {
183 $this->addWhereRange( 'rev_user_text', $this->params['dir'], null, null );
184 }
185 $this->addWhereRange( 'rev_timestamp',
186 $this->params['dir'], $this->params['start'], $this->params['end'] );
187 $this->addWhereFld( 'page_namespace', $this->params['namespace'] );
188
189 $show = $this->params['show'];
190 if ( !is_null( $show ) ) {
191 $show = array_flip( $show );
192 if ( ( isset( $show['minor'] ) && isset( $show['!minor'] ) )
193 || ( isset( $show['patrolled'] ) && isset( $show['!patrolled'] ) ) )
194 {
195 $this->dieUsageMsg( array( 'show' ) );
196 }
197
198 $this->addWhereIf( 'rev_minor_edit = 0', isset( $show['!minor'] ) );
199 $this->addWhereIf( 'rev_minor_edit != 0', isset( $show['minor'] ) );
200 $this->addWhereIf( 'rc_patrolled = 0', isset( $show['!patrolled'] ) );
201 $this->addWhereIf( 'rc_patrolled != 0', isset( $show['patrolled'] ) );
202 }
203 $this->addOption( 'LIMIT', $this->params['limit'] + 1 );
204 $index = array( 'revision' => 'usertext_timestamp' );
205
206 // Mandatory fields: timestamp allows request continuation
207 // ns+title checks if the user has access rights for this page
208 // user_text is necessary if multiple users were specified
209 $this->addFields( array(
210 'rev_timestamp',
211 'page_namespace',
212 'page_title',
213 'rev_user_text',
214 'rev_deleted'
215 ) );
216
217 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ||
218 $this->fld_patrolled )
219 {
220 global $wgUser;
221 if ( !$wgUser->useRCPatrol() && !$wgUser->useNPPatrol() ) {
222 $this->dieUsage( 'You need the patrol right to request the patrolled flag', 'permissiondenied' );
223 }
224 // Use a redundant join condition on both
225 // timestamp and ID so we can use the timestamp
226 // index
227 $index['recentchanges'] = 'rc_user_text';
228 if ( isset( $show['patrolled'] ) || isset( $show['!patrolled'] ) ) {
229 // Put the tables in the right order for
230 // STRAIGHT_JOIN
231 $tables = array( 'revision', 'recentchanges', 'page' );
232 $this->addOption( 'STRAIGHT_JOIN' );
233 $this->addWhere( 'rc_user_text=rev_user_text' );
234 $this->addWhere( 'rc_timestamp=rev_timestamp' );
235 $this->addWhere( 'rc_this_oldid=rev_id' );
236 } else {
237 $tables[] = 'recentchanges';
238 $this->addJoinConds( array( 'recentchanges' => array(
239 'LEFT JOIN', array(
240 'rc_user_text=rev_user_text',
241 'rc_timestamp=rev_timestamp',
242 'rc_this_oldid=rev_id' ) ) ) );
243 }
244 }
245
246 $this->addTables( $tables );
247 $this->addFieldsIf( 'rev_page', $this->fld_ids );
248 $this->addFieldsIf( 'rev_id', $this->fld_ids || $this->fld_flags );
249 $this->addFieldsIf( 'page_latest', $this->fld_flags );
250 // $this->addFieldsIf( 'rev_text_id', $this->fld_ids ); // Should this field be exposed?
251 $this->addFieldsIf( 'rev_comment', $this->fld_comment || $this->fld_parsedcomment );
252 $this->addFieldsIf( 'rev_len', $this->fld_size );
253 $this->addFieldsIf( 'rev_minor_edit', $this->fld_flags );
254 $this->addFieldsIf( 'rev_parent_id', $this->fld_flags );
255 $this->addFieldsIf( 'rc_patrolled', $this->fld_patrolled );
256
257 if ( $this->fld_tags ) {
258 $this->addTables( 'tag_summary' );
259 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', array( 'rev_id=ts_rev_id' ) ) ) );
260 $this->addFields( 'ts_tags' );
261 }
262
263 if ( isset( $this->params['tag'] ) ) {
264 $this->addTables( 'change_tag' );
265 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'rev_id=ct_rev_id' ) ) ) );
266 $this->addWhereFld( 'ct_tag', $this->params['tag'] );
267 global $wgOldChangeTagsIndex;
268 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
269 }
270
271 $this->addOption( 'USE INDEX', $index );
272 }
273
274 /**
275 * Extract fields from the database row and append them to a result array
276 */
277 private function extractRowInfo( $row ) {
278 $vals = array();
279
280 $vals['user'] = $row->rev_user_text;
281 if ( $row->rev_deleted & Revision::DELETED_USER ) {
282 $vals['userhidden'] = '';
283 }
284 if ( $this->fld_ids ) {
285 $vals['pageid'] = intval( $row->rev_page );
286 $vals['revid'] = intval( $row->rev_id );
287 // $vals['textid'] = intval( $row->rev_text_id ); // todo: Should this field be exposed?
288 }
289
290 $title = Title::makeTitle( $row->page_namespace, $row->page_title );
291
292 if ( $this->fld_title ) {
293 ApiQueryBase::addTitleInfo( $vals, $title );
294 }
295
296 if ( $this->fld_timestamp ) {
297 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->rev_timestamp );
298 }
299
300 if ( $this->fld_flags ) {
301 if ( $row->rev_parent_id == 0 && !is_null( $row->rev_parent_id ) ) {
302 $vals['new'] = '';
303 }
304 if ( $row->rev_minor_edit ) {
305 $vals['minor'] = '';
306 }
307 if ( $row->page_latest == $row->rev_id ) {
308 $vals['top'] = '';
309 }
310 }
311
312 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->rev_comment ) ) {
313 if ( $row->rev_deleted & Revision::DELETED_COMMENT ) {
314 $vals['commenthidden'] = '';
315 } else {
316 if ( $this->fld_comment ) {
317 $vals['comment'] = $row->rev_comment;
318 }
319
320 if ( $this->fld_parsedcomment ) {
321 global $wgUser;
322 $vals['parsedcomment'] = $wgUser->getSkin()->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 getAllowedParams() {
354 return array(
355 'limit' => array(
356 ApiBase::PARAM_DFLT => 10,
357 ApiBase::PARAM_TYPE => 'limit',
358 ApiBase::PARAM_MIN => 1,
359 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
360 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
361 ),
362 'start' => array(
363 ApiBase::PARAM_TYPE => 'timestamp'
364 ),
365 'end' => array(
366 ApiBase::PARAM_TYPE => 'timestamp'
367 ),
368 'continue' => null,
369 'user' => array(
370 ApiBase::PARAM_ISMULTI => true
371 ),
372 'userprefix' => null,
373 'dir' => array(
374 ApiBase::PARAM_DFLT => 'older',
375 ApiBase::PARAM_TYPE => array(
376 'newer',
377 'older'
378 )
379 ),
380 'namespace' => array(
381 ApiBase::PARAM_ISMULTI => true,
382 ApiBase::PARAM_TYPE => 'namespace'
383 ),
384 'prop' => array(
385 ApiBase::PARAM_ISMULTI => true,
386 ApiBase::PARAM_DFLT => 'ids|title|timestamp|comment|size|flags',
387 ApiBase::PARAM_TYPE => array(
388 'ids',
389 'title',
390 'timestamp',
391 'comment',
392 'parsedcomment',
393 'size',
394 'flags',
395 'patrolled',
396 'tags'
397 )
398 ),
399 'show' => array(
400 ApiBase::PARAM_ISMULTI => true,
401 ApiBase::PARAM_TYPE => array(
402 'minor',
403 '!minor',
404 'patrolled',
405 '!patrolled',
406 )
407 ),
408 'tag' => null,
409 );
410 }
411
412 public function getParamDescription() {
413 return array(
414 'limit' => 'The maximum number of contributions to return.',
415 'start' => 'The start timestamp to return from.',
416 'end' => 'The end timestamp to return to.',
417 'continue' => 'When more results are available, use this to continue.',
418 'user' => 'The user to retrieve contributions for.',
419 'userprefix' => 'Retrieve contibutions for all users whose names begin with this value. Overrides ucuser.',
420 'dir' => 'The direction to search (older or newer).',
421 'namespace' => 'Only list contributions in these namespaces',
422 'prop' => 'Include additional pieces of information',
423 'show' => array( 'Show only items that meet this criteria, e.g. non minor edits only: show=!minor',
424 'NOTE: if show=patrolled or show=!patrolled is set, revisions older than $wgRCMaxAge won\'t be shown', ),
425 'tag' => 'Only list revisions tagged with this tag',
426 );
427 }
428
429 public function getDescription() {
430 return 'Get all edits by a user';
431 }
432
433 public function getPossibleErrors() {
434 return array_merge( parent::getPossibleErrors(), array(
435 array( 'code' => 'param_user', 'info' => 'User parameter may not be empty.' ),
436 array( 'code' => 'param_user', 'info' => 'User name user is not valid' ),
437 array( 'show' ),
438 array( 'code' => 'permissiondenied', 'info' => 'You need the patrol right to request the patrolled flag' ),
439 ) );
440 }
441
442 protected function getExamples() {
443 return array(
444 'api.php?action=query&list=usercontribs&ucuser=YurikBot',
445 'api.php?action=query&list=usercontribs&ucuserprefix=217.121.114.',
446 );
447 }
448
449 public function getVersion() {
450 return __CLASS__ . ': $Id$';
451 }
452 }