* (bug 29063) When viewing list=recentchanges&rcprop=loginfo for an unblock entry...
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.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 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
30 }
31
32 /**
33 * Query action to List the log events, with optional filtering by various parameters.
34 *
35 * @ingroup API
36 */
37 class ApiQueryLogEvents extends ApiQueryBase {
38
39 public function __construct( $query, $moduleName ) {
40 parent::__construct( $query, $moduleName, 'le' );
41 }
42
43 private $fld_ids = false, $fld_title = false, $fld_type = false,
44 $fld_action = false, $fld_user = false, $fld_userid = false,
45 $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
46 $fld_details = false, $fld_tags = false;
47
48 public function execute() {
49 $params = $this->extractRequestParams();
50 $db = $this->getDB();
51
52 $prop = array_flip( $params['prop'] );
53
54 $this->fld_ids = isset( $prop['ids'] );
55 $this->fld_title = isset( $prop['title'] );
56 $this->fld_type = isset( $prop['type'] );
57 $this->fld_action = isset ( $prop['action'] );
58 $this->fld_user = isset( $prop['user'] );
59 $this->fld_userid = isset( $prop['userid'] );
60 $this->fld_timestamp = isset( $prop['timestamp'] );
61 $this->fld_comment = isset( $prop['comment'] );
62 $this->fld_parsedcomment = isset ( $prop['parsedcomment'] );
63 $this->fld_details = isset( $prop['details'] );
64 $this->fld_tags = isset( $prop['tags'] );
65
66 $hideLogs = LogEventsList::getExcludeClause( $db );
67 if ( $hideLogs !== false ) {
68 $this->addWhere( $hideLogs );
69 }
70
71 // Order is significant here
72 $this->addTables( array( 'logging', 'user', 'page' ) );
73 $this->addOption( 'STRAIGHT_JOIN' );
74 $this->addJoinConds( array(
75 'user' => array( 'JOIN',
76 'user_id=log_user' ),
77 'page' => array( 'LEFT JOIN',
78 array( 'log_namespace=page_namespace',
79 'log_title=page_title' ) ) ) );
80 $index = array( 'logging' => 'times' ); // default, may change
81
82 $this->addFields( array(
83 'log_type',
84 'log_action',
85 'log_timestamp',
86 'log_deleted',
87 ) );
88
89 $this->addFieldsIf( 'log_id', $this->fld_ids );
90 $this->addFieldsIf( 'page_id', $this->fld_ids );
91 $this->addFieldsIf( 'log_user', $this->fld_user );
92 $this->addFieldsIf( 'user_name', $this->fld_user );
93 $this->addFieldsIf( 'user_id', $this->fld_userid );
94 $this->addFieldsIf( 'log_namespace', $this->fld_title || $this->fld_parsedcomment );
95 $this->addFieldsIf( 'log_title', $this->fld_title || $this->fld_parsedcomment );
96 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
97 $this->addFieldsIf( 'log_params', $this->fld_details );
98
99 if ( $this->fld_tags ) {
100 $this->addTables( 'tag_summary' );
101 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
102 $this->addFields( 'ts_tags' );
103 }
104
105 if ( !is_null( $params['tag'] ) ) {
106 $this->addTables( 'change_tag' );
107 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
108 $this->addWhereFld( 'ct_tag', $params['tag'] );
109 global $wgOldChangeTagsIndex;
110 $index['change_tag'] = $wgOldChangeTagsIndex ? 'ct_tag' : 'change_tag_tag_id';
111 }
112
113 if ( !is_null( $params['action'] ) ) {
114 list( $type, $action ) = explode( '/', $params['action'] );
115 $this->addWhereFld( 'log_type', $type );
116 $this->addWhereFld( 'log_action', $action );
117 }
118 else if ( !is_null( $params['type'] ) ) {
119 $this->addWhereFld( 'log_type', $params['type'] );
120 $index['logging'] = 'type_time';
121 }
122
123 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
124
125 $limit = $params['limit'];
126 $this->addOption( 'LIMIT', $limit + 1 );
127
128 $user = $params['user'];
129 if ( !is_null( $user ) ) {
130 $userid = User::idFromName( $user );
131 if ( !$userid ) {
132 $this->dieUsage( "User name $user not found", 'param_user' );
133 }
134 $this->addWhereFld( 'log_user', $userid );
135 $index['logging'] = 'user_time';
136 }
137
138 $title = $params['title'];
139 if ( !is_null( $title ) ) {
140 $titleObj = Title::newFromText( $title );
141 if ( is_null( $titleObj ) ) {
142 $this->dieUsage( "Bad title value '$title'", 'param_title' );
143 }
144 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
145 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
146
147 // Use the title index in preference to the user index if there is a conflict
148 $index['logging'] = is_null( $user ) ? 'page_time' : array( 'page_time', 'user_time' );
149 }
150
151 $prefix = $params['prefix'];
152
153 if ( !is_null( $prefix ) ) {
154 global $wgMiserMode;
155 if ( $wgMiserMode ) {
156 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
157 }
158
159 $title = Title::newFromText( $prefix );
160 if ( is_null( $title ) ) {
161 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
162 }
163 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
164 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
165 }
166
167 $this->addOption( 'USE INDEX', $index );
168
169 // Paranoia: avoid brute force searches (bug 17342)
170 if ( !is_null( $title ) ) {
171 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_ACTION ) . ' = 0' );
172 }
173 if ( !is_null( $user ) ) {
174 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage::DELETED_USER ) . ' = 0' );
175 }
176
177 $count = 0;
178 $res = $this->select( __METHOD__ );
179 foreach ( $res as $row ) {
180 if ( ++ $count > $limit ) {
181 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
182 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
183 break;
184 }
185
186 $vals = $this->extractRowInfo( $row );
187 if ( !$vals ) {
188 continue;
189 }
190 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
191 if ( !$fit ) {
192 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
193 break;
194 }
195 }
196 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
197 }
198
199 /**
200 * @param $result ApiResult
201 * @param $vals array
202 * @param $params string
203 * @param $type string
204 * @param $action string
205 * @param $ts
206 * @return array
207 */
208 public static function addLogParams( $result, &$vals, $params, $type, $action, $ts ) {
209 $params = explode( "\n", $params );
210 switch ( $type ) {
211 case 'move':
212 if ( isset( $params[0] ) ) {
213 $title = Title::newFromText( $params[0] );
214 if ( $title ) {
215 $vals2 = array();
216 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
217 $vals[$type] = $vals2;
218 }
219 }
220 if ( isset( $params[1] ) && $params[1] ) {
221 $vals[$type]['suppressedredirect'] = '';
222 }
223 $params = null;
224 break;
225 case 'patrol':
226 $vals2 = array();
227 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
228 $vals[$type] = $vals2;
229 $params = null;
230 break;
231 case 'rights':
232 $vals2 = array();
233 list( $vals2['old'], $vals2['new'] ) = $params;
234 $vals[$type] = $vals2;
235 $params = null;
236 break;
237 case 'block':
238 if ( $action == 'unblock' ) {
239 break;
240 }
241 $vals2 = array();
242 list( $vals2['duration'], $vals2['flags'] ) = $params;
243
244 // Indefinite blocks have no expiry time
245 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
246 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
247 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
248 }
249 $vals[$type] = $vals2;
250 $params = null;
251 break;
252 }
253 if ( !is_null( $params ) ) {
254 $result->setIndexedTagName( $params, 'param' );
255 $vals = array_merge( $vals, $params );
256 }
257 return $vals;
258 }
259
260 private function extractRowInfo( $row ) {
261 $vals = array();
262
263 if ( $this->fld_ids ) {
264 $vals['logid'] = intval( $row->log_id );
265 $vals['pageid'] = intval( $row->page_id );
266 }
267
268 if ( $this->fld_title || $this->fld_parsedcomment ) {
269 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
270 }
271
272 if ( $this->fld_title ) {
273 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
274 $vals['actionhidden'] = '';
275 } else {
276 ApiQueryBase::addTitleInfo( $vals, $title );
277 }
278 }
279
280 if ( $this->fld_type || $this->fld_action ) {
281 $vals['type'] = $row->log_type;
282 $vals['action'] = $row->log_action;
283 }
284
285 if ( $this->fld_details && $row->log_params !== '' ) {
286 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
287 $vals['actionhidden'] = '';
288 } else {
289 self::addLogParams(
290 $this->getResult(),
291 $vals,
292 $row->log_params,
293 $row->log_type,
294 $row->log_action,
295 $row->log_timestamp
296 );
297 }
298 }
299
300 if ( $this->fld_user || $this->fld_userid ) {
301 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
302 $vals['userhidden'] = '';
303 } else {
304 if ( $this->fld_user ) {
305 $vals['user'] = $row->user_name;
306 }
307 if ( $this->fld_userid ) {
308 $vals['userid'] = $row->user_id;
309 }
310
311 if ( !$row->log_user ) {
312 $vals['anon'] = '';
313 }
314 }
315 }
316 if ( $this->fld_timestamp ) {
317 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
318 }
319
320 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
321 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
322 $vals['commenthidden'] = '';
323 } else {
324 if ( $this->fld_comment ) {
325 $vals['comment'] = $row->log_comment;
326 }
327
328 if ( $this->fld_parsedcomment ) {
329 global $wgUser;
330 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment, $title );
331 }
332 }
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 public function getCacheMode( $params ) {
349 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
350 // formatComment() calls wfMsg() among other things
351 return 'anon-public-user-private';
352 } else {
353 return 'public';
354 }
355 }
356
357 public function getAllowedParams() {
358 global $wgLogTypes, $wgLogActions;
359 return array(
360 'prop' => array(
361 ApiBase::PARAM_ISMULTI => true,
362 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
363 ApiBase::PARAM_TYPE => array(
364 'ids',
365 'title',
366 'type',
367 'user',
368 'userid',
369 'timestamp',
370 'comment',
371 'parsedcomment',
372 'details',
373 'tags'
374 )
375 ),
376 'type' => array(
377 ApiBase::PARAM_TYPE => $wgLogTypes
378 ),
379 'action' => array(
380 ApiBase::PARAM_TYPE => array_keys( $wgLogActions )
381 ),
382 'start' => array(
383 ApiBase::PARAM_TYPE => 'timestamp'
384 ),
385 'end' => array(
386 ApiBase::PARAM_TYPE => 'timestamp'
387 ),
388 'dir' => array(
389 ApiBase::PARAM_DFLT => 'older',
390 ApiBase::PARAM_TYPE => array(
391 'newer',
392 'older'
393 )
394 ),
395 'user' => null,
396 'title' => null,
397 'prefix' => null,
398 'tag' => null,
399 'limit' => array(
400 ApiBase::PARAM_DFLT => 10,
401 ApiBase::PARAM_TYPE => 'limit',
402 ApiBase::PARAM_MIN => 1,
403 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
404 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
405 )
406 );
407 }
408
409 public function getParamDescription() {
410 $p = $this->getModulePrefix();
411 return array(
412 'prop' => array(
413 'Which properties to get',
414 ' ids - Adds the ID of the log event',
415 ' title - Adds the title of the page for the log event',
416 ' type - Adds the type of log event',
417 ' user - Adds the user responsible for the log event',
418 ' userid - Adds the user ID who was responsible for the log event',
419 ' timestamp - Adds the timestamp for the event',
420 ' comment - Adds the comment of the event',
421 ' parsedcomment - Adds the parsed comment of the event',
422 ' details - Lists addtional details about the event',
423 ' tags - Lists tags for the event',
424 ),
425 'type' => 'Filter log entries to only this type(s)',
426 'action' => "Filter log actions to only this type. Overrides {$p}type",
427 'start' => 'The timestamp to start enumerating from',
428 'end' => 'The timestamp to end enumerating',
429 'dir' => $this->getDirectionDescription( $p ),
430 'user' => 'Filter entries to those made by the given user',
431 'title' => 'Filter entries to those related to a page',
432 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
433 'limit' => 'How many total event entries to return',
434 'tag' => 'Only list event entries tagged with this tag',
435 );
436 }
437
438 public function getDescription() {
439 return 'Get events from logs';
440 }
441
442 public function getPossibleErrors() {
443 return array_merge( parent::getPossibleErrors(), array(
444 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
445 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
446 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
447 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
448 ) );
449 }
450
451 protected function getExamples() {
452 return array(
453 'api.php?action=query&list=logevents'
454 );
455 }
456
457 public function getVersion() {
458 return __CLASS__ . ': $Id$';
459 }
460 }