848c6ceb1d6ff992e585afadb48a5c6eecd05337
[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, 'user', $this->getUser() );
62 if ( $hideLogs !== false ) {
63 $this->addWhere( $hideLogs );
64 }
65
66 // Order is significant here
67 $this->addTables( array( 'logging', 'user', 'page' ) );
68 $this->addJoinConds( array(
69 'user' => array( 'LEFT JOIN',
70 'user_id=log_user' ),
71 'page' => array( 'LEFT JOIN',
72 array( 'log_namespace=page_namespace',
73 'log_title=page_title' ) ) ) );
74
75 $this->addFields( array(
76 'log_type',
77 'log_action',
78 'log_timestamp',
79 'log_deleted',
80 ) );
81
82 $this->addFieldsIf( array( 'log_id', 'page_id' ), $this->fld_ids );
83 $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user );
84 $this->addFieldsIf( 'log_user', $this->fld_userid );
85 $this->addFieldsIf(
86 array( 'log_namespace', 'log_title' ),
87 $this->fld_title || $this->fld_parsedcomment
88 );
89 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
90 $this->addFieldsIf( 'log_params', $this->fld_details );
91
92 if ( $this->fld_tags ) {
93 $this->addTables( 'tag_summary' );
94 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
95 $this->addFields( 'ts_tags' );
96 }
97
98 if ( !is_null( $params['tag'] ) ) {
99 $this->addTables( 'change_tag' );
100 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN',
101 array( 'log_id=ct_log_id' ) ) ) );
102 $this->addWhereFld( 'ct_tag', $params['tag'] );
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 }
112
113 $this->addTimestampWhereRange(
114 'log_timestamp',
115 $params['dir'],
116 $params['start'],
117 $params['end']
118 );
119
120 $limit = $params['limit'];
121 $this->addOption( 'LIMIT', $limit + 1 );
122
123 $user = $params['user'];
124 if ( !is_null( $user ) ) {
125 $userid = User::idFromName( $user );
126 if ( $userid ) {
127 $this->addWhereFld( 'log_user', $userid );
128 } else {
129 $this->addWhereFld( 'log_user_text', IP::sanitizeIP( $user ) );
130 }
131 }
132
133 $title = $params['title'];
134 if ( !is_null( $title ) ) {
135 $titleObj = Title::newFromText( $title );
136 if ( is_null( $titleObj ) ) {
137 $this->dieUsage( "Bad title value '$title'", 'param_title' );
138 }
139 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
140 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
141 }
142
143 $prefix = $params['prefix'];
144
145 if ( !is_null( $prefix ) ) {
146 global $wgMiserMode;
147 if ( $wgMiserMode ) {
148 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
149 }
150
151 $title = Title::newFromText( $prefix );
152 if ( is_null( $title ) ) {
153 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
154 }
155 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
156 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
157 }
158
159 // Paranoia: avoid brute force searches (bug 17342)
160 if ( !is_null( $title ) || !is_null( $user ) ) {
161 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
162 $titleBits = LogPage::DELETED_ACTION;
163 $userBits = LogPage::DELETED_USER;
164 } elseif ( !$this->getUser()->isAllowed( 'suppressrevision' ) ) {
165 $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
166 $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED;
167 } else {
168 $titleBits = 0;
169 $userBits = 0;
170 }
171 if ( !is_null( $title ) && $titleBits ) {
172 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
173 }
174 if ( !is_null( $user ) && $userBits ) {
175 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
176 }
177 }
178
179 $count = 0;
180 $res = $this->select( __METHOD__ );
181 $result = $this->getResult();
182 foreach ( $res as $row ) {
183 if ( ++$count > $limit ) {
184 // We've reached the one extra which shows that there are
185 // additional pages to be had. Stop here...
186 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
187 break;
188 }
189
190 $vals = $this->extractRowInfo( $row );
191 if ( !$vals ) {
192 continue;
193 }
194 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
195 if ( !$fit ) {
196 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601, $row->log_timestamp ) );
197 break;
198 }
199 }
200 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
201 }
202
203 /**
204 * @param $result ApiResult
205 * @param $vals array
206 * @param $params string
207 * @param $type string
208 * @param $action string
209 * @param $ts
210 * @param $legacy bool
211 * @return array
212 */
213 public static function addLogParams( $result, &$vals, $params, $type,
214 $action, $ts, $legacy = false
215 ) {
216 switch ( $type ) {
217 case 'move':
218 if ( $legacy ) {
219 $targetKey = 0;
220 $noredirKey = 1;
221 } else {
222 $targetKey = '4::target';
223 $noredirKey = '5::noredir';
224 }
225
226 if ( isset( $params[$targetKey] ) ) {
227 $title = Title::newFromText( $params[$targetKey] );
228 if ( $title ) {
229 $vals2 = array();
230 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
231 $vals[$type] = $vals2;
232 }
233 }
234 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
235 $vals[$type]['suppressedredirect'] = '';
236 }
237 $params = null;
238 break;
239 case 'patrol':
240 if ( $legacy ) {
241 $cur = 0;
242 $prev = 1;
243 $auto = 2;
244 } else {
245 $cur = '4::curid';
246 $prev = '5::previd';
247 $auto = '6::auto';
248 }
249 $vals2 = array();
250 $vals2['cur'] = $params[$cur];
251 $vals2['prev'] = $params[$prev];
252 $vals2['auto'] = $params[$auto];
253 $vals[$type] = $vals2;
254 $params = null;
255 break;
256 case 'rights':
257 $vals2 = array();
258 if ( $legacy ) {
259 list( $vals2['old'], $vals2['new'] ) = $params;
260 } else {
261 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
262 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
263 }
264 $vals[$type] = $vals2;
265 $params = null;
266 break;
267 case 'block':
268 if ( $action == 'unblock' ) {
269 break;
270 }
271 $vals2 = array();
272 list( $vals2['duration'], $vals2['flags'] ) = $params;
273
274 // Indefinite blocks have no expiry time
275 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
276 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
277 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
278 }
279 $vals[$type] = $vals2;
280 $params = null;
281 break;
282 case 'upload':
283 if ( isset( $params['img_timestamp'] ) ) {
284 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601, $params['img_timestamp'] );
285 }
286 break;
287 }
288 if ( !is_null( $params ) ) {
289 $logParams = array();
290 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
291 foreach ( $params as $key => $value ) {
292 if ( strpos( $key, ':' ) === false ) {
293 $logParams[$key] = $value;
294 continue;
295 }
296 $logParam = explode( ':', $key, 3 );
297 $logParams[$logParam[2]] = $value;
298 }
299 $result->setIndexedTagName( $logParams, 'param' );
300 $result->setIndexedTagName_recursive( $logParams, 'param' );
301 $vals = array_merge( $vals, $logParams );
302 }
303
304 return $vals;
305 }
306
307 private function extractRowInfo( $row ) {
308 $logEntry = DatabaseLogEntry::newFromRow( $row );
309 $vals = array();
310 $anyHidden = false;
311 $user = $this->getUser();
312
313 if ( $this->fld_ids ) {
314 $vals['logid'] = intval( $row->log_id );
315 }
316
317 if ( $this->fld_title || $this->fld_parsedcomment ) {
318 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
319 }
320
321 if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) {
322 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
323 $vals['actionhidden'] = '';
324 $anyHidden = true;
325 }
326 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) {
327 if ( $this->fld_title ) {
328 ApiQueryBase::addTitleInfo( $vals, $title );
329 }
330 if ( $this->fld_ids ) {
331 $vals['pageid'] = intval( $row->page_id );
332 }
333 if ( $this->fld_details && $row->log_params !== '' ) {
334 self::addLogParams(
335 $this->getResult(),
336 $vals,
337 $logEntry->getParameters(),
338 $logEntry->getType(),
339 $logEntry->getSubtype(),
340 $logEntry->getTimestamp(),
341 $logEntry->isLegacy()
342 );
343 }
344 }
345 }
346
347 if ( $this->fld_type || $this->fld_action ) {
348 $vals['type'] = $row->log_type;
349 $vals['action'] = $row->log_action;
350 }
351
352 if ( $this->fld_user || $this->fld_userid ) {
353 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
354 $vals['userhidden'] = '';
355 $anyHidden = true;
356 }
357 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
358 if ( $this->fld_user ) {
359 $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
360 }
361 if ( $this->fld_userid ) {
362 $vals['userid'] = $row->log_user;
363 }
364
365 if ( !$row->log_user ) {
366 $vals['anon'] = '';
367 }
368 }
369 }
370 if ( $this->fld_timestamp ) {
371 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
372 }
373
374 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
375 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
376 $vals['commenthidden'] = '';
377 $anyHidden = true;
378 }
379 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
380 if ( $this->fld_comment ) {
381 $vals['comment'] = $row->log_comment;
382 }
383
384 if ( $this->fld_parsedcomment ) {
385 $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
386 }
387 }
388 }
389
390 if ( $this->fld_tags ) {
391 if ( $row->ts_tags ) {
392 $tags = explode( ',', $row->ts_tags );
393 $this->getResult()->setIndexedTagName( $tags, 'tag' );
394 $vals['tags'] = $tags;
395 } else {
396 $vals['tags'] = array();
397 }
398 }
399
400 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
401 $vals['suppressed'] = '';
402 }
403
404 return $vals;
405 }
406
407 public function getCacheMode( $params ) {
408 if ( $this->userCanSeeRevDel() ) {
409 return 'private';
410 }
411 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
412 // formatComment() calls wfMessage() among other things
413 return 'anon-public-user-private';
414 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
415 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
416 ) { // Output can only contain public data.
417 return 'public';
418 } else {
419 return 'anon-public-user-private';
420 }
421 }
422
423 public function getAllowedParams() {
424 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
425
426 return array(
427 'prop' => array(
428 ApiBase::PARAM_ISMULTI => true,
429 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
430 ApiBase::PARAM_TYPE => array(
431 'ids',
432 'title',
433 'type',
434 'user',
435 'userid',
436 'timestamp',
437 'comment',
438 'parsedcomment',
439 'details',
440 'tags'
441 )
442 ),
443 'type' => array(
444 ApiBase::PARAM_TYPE => $wgLogTypes
445 ),
446 'action' => array(
447 ApiBase::PARAM_TYPE => array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
448 ),
449 'start' => array(
450 ApiBase::PARAM_TYPE => 'timestamp'
451 ),
452 'end' => array(
453 ApiBase::PARAM_TYPE => 'timestamp'
454 ),
455 'dir' => array(
456 ApiBase::PARAM_DFLT => 'older',
457 ApiBase::PARAM_TYPE => array(
458 'newer',
459 'older'
460 )
461 ),
462 'user' => null,
463 'title' => null,
464 'prefix' => null,
465 'tag' => null,
466 'limit' => array(
467 ApiBase::PARAM_DFLT => 10,
468 ApiBase::PARAM_TYPE => 'limit',
469 ApiBase::PARAM_MIN => 1,
470 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
471 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
472 )
473 );
474 }
475
476 public function getParamDescription() {
477 $p = $this->getModulePrefix();
478
479 return array(
480 'prop' => array(
481 'Which properties to get',
482 ' ids - Adds the ID of the log event',
483 ' title - Adds the title of the page for the log event',
484 ' type - Adds the type of log event',
485 ' user - Adds the user responsible for the log event',
486 ' userid - Adds the user ID who was responsible for the log event',
487 ' timestamp - Adds the timestamp for the event',
488 ' comment - Adds the comment of the event',
489 ' parsedcomment - Adds the parsed comment of the event',
490 ' details - Lists additional details about the event',
491 ' tags - Lists tags for the event',
492 ),
493 'type' => 'Filter log entries to only this type',
494 'action' => "Filter log actions to only this type. Overrides {$p}type",
495 'start' => 'The timestamp to start enumerating from',
496 'end' => 'The timestamp to end enumerating',
497 'dir' => $this->getDirectionDescription( $p ),
498 'user' => 'Filter entries to those made by the given user',
499 'title' => 'Filter entries to those related to a page',
500 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
501 'limit' => 'How many total event entries to return',
502 'tag' => 'Only list event entries tagged with this tag',
503 );
504 }
505
506 public function getResultProperties() {
507 global $wgLogTypes;
508
509 return array(
510 'ids' => array(
511 'logid' => 'integer',
512 'pageid' => 'integer'
513 ),
514 'title' => array(
515 'ns' => 'namespace',
516 'title' => 'string'
517 ),
518 'type' => array(
519 'type' => array(
520 ApiBase::PROP_TYPE => $wgLogTypes
521 ),
522 'action' => 'string'
523 ),
524 'details' => array(
525 'actionhidden' => 'boolean'
526 ),
527 'user' => array(
528 'userhidden' => 'boolean',
529 'user' => array(
530 ApiBase::PROP_TYPE => 'string',
531 ApiBase::PROP_NULLABLE => true
532 ),
533 'anon' => 'boolean'
534 ),
535 'userid' => array(
536 'userhidden' => 'boolean',
537 'userid' => array(
538 ApiBase::PROP_TYPE => 'integer',
539 ApiBase::PROP_NULLABLE => true
540 ),
541 'anon' => 'boolean'
542 ),
543 'timestamp' => array(
544 'timestamp' => 'timestamp'
545 ),
546 'comment' => array(
547 'commenthidden' => 'boolean',
548 'comment' => array(
549 ApiBase::PROP_TYPE => 'string',
550 ApiBase::PROP_NULLABLE => true
551 )
552 ),
553 'parsedcomment' => array(
554 'commenthidden' => 'boolean',
555 'parsedcomment' => array(
556 ApiBase::PROP_TYPE => 'string',
557 ApiBase::PROP_NULLABLE => true
558 )
559 )
560 );
561 }
562
563 public function getDescription() {
564 return 'Get events from logs';
565 }
566
567 public function getPossibleErrors() {
568 return array_merge( parent::getPossibleErrors(), array(
569 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
570 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
571 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
572 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
573 ) );
574 }
575
576 public function getExamples() {
577 return array(
578 'api.php?action=query&list=logevents'
579 );
580 }
581
582 public function getHelpUrls() {
583 return 'https://www.mediawiki.org/wiki/API:Logevents';
584 }
585 }