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