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