Merge "Fix history and add section tabs being collapsed on RTL wikis"
[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->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 * @param $legacy bool
199 * @return array
200 */
201 public static function addLogParams( $result, &$vals, $params, $type, $action, $ts, $legacy = false ) {
202 switch ( $type ) {
203 case 'move':
204 if ( $legacy ) {
205 $targetKey = 0;
206 $noredirKey = 1;
207 } else {
208 $targetKey = '4::target';
209 $noredirKey = '5::noredir';
210 }
211
212 if ( isset( $params[$targetKey] ) ) {
213 $title = Title::newFromText( $params[$targetKey] );
214 if ( $title ) {
215 $vals2 = array();
216 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
217 $vals[$type] = $vals2;
218 }
219 }
220 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
221 $vals[$type]['suppressedredirect'] = '';
222 }
223 $params = null;
224 break;
225 case 'patrol':
226 if ( $legacy ) {
227 $cur = 0;
228 $prev = 1;
229 $auto = 2;
230 } else {
231 $cur = '4::curid';
232 $prev = '5::previd';
233 $auto = '6::auto';
234 }
235 $vals2 = array();
236 $vals2['cur'] = $params[$cur];
237 $vals2['prev'] = $params[$prev];
238 $vals2['auto'] = $params[$auto];
239 $vals[$type] = $vals2;
240 $params = null;
241 break;
242 case 'rights':
243 $vals2 = array();
244 if ( $legacy ) {
245 list( $vals2['old'], $vals2['new'] ) = $params;
246 } else {
247 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
248 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
249 }
250 $vals[$type] = $vals2;
251 $params = null;
252 break;
253 case 'block':
254 if ( $action == 'unblock' ) {
255 break;
256 }
257 $vals2 = array();
258 list( $vals2['duration'], $vals2['flags'] ) = $params;
259
260 // Indefinite blocks have no expiry time
261 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
262 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
263 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
264 }
265 $vals[$type] = $vals2;
266 $params = null;
267 break;
268 case 'upload':
269 if ( isset( $params['img_timestamp'] ) ) {
270 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601, $params['img_timestamp'] );
271 }
272 break;
273 }
274 if ( !is_null( $params ) ) {
275 $logParams = array();
276 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
277 foreach ( $params as $key => $value ) {
278 if ( strpos( $key, ':' ) === false ) {
279 $logParams[$key] = $value;
280 continue;
281 }
282 $logParam = explode( ':', $key, 3 );
283 $logParams[$logParam[2]] = $value;
284 }
285 $result->setIndexedTagName( $logParams, 'param' );
286 $result->setIndexedTagName_recursive( $logParams, 'param' );
287 $vals = array_merge( $vals, $logParams );
288 }
289 return $vals;
290 }
291
292 private function extractRowInfo( $row ) {
293 $logEntry = DatabaseLogEntry::newFromRow( $row );
294 $vals = array();
295
296 if ( $this->fld_ids ) {
297 $vals['logid'] = intval( $row->log_id );
298 $vals['pageid'] = intval( $row->page_id );
299 }
300
301 if ( $this->fld_title || $this->fld_parsedcomment ) {
302 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
303 }
304
305 if ( $this->fld_title ) {
306 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
307 $vals['actionhidden'] = '';
308 } else {
309 ApiQueryBase::addTitleInfo( $vals, $title );
310 }
311 }
312
313 if ( $this->fld_type || $this->fld_action ) {
314 $vals['type'] = $row->log_type;
315 $vals['action'] = $row->log_action;
316 }
317
318 if ( $this->fld_details && $row->log_params !== '' ) {
319 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
320 $vals['actionhidden'] = '';
321 } else {
322 self::addLogParams(
323 $this->getResult(),
324 $vals,
325 $logEntry->getParameters(),
326 $logEntry->getType(),
327 $logEntry->getSubtype(),
328 $logEntry->getTimestamp(),
329 $logEntry->isLegacy()
330 );
331 }
332 }
333
334 if ( $this->fld_user || $this->fld_userid ) {
335 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
336 $vals['userhidden'] = '';
337 } else {
338 if ( $this->fld_user ) {
339 $vals['user'] = $row->user_name;
340 }
341 if ( $this->fld_userid ) {
342 $vals['userid'] = $row->user_id;
343 }
344
345 if ( !$row->log_user ) {
346 $vals['anon'] = '';
347 }
348 }
349 }
350 if ( $this->fld_timestamp ) {
351 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
352 }
353
354 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
355 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
356 $vals['commenthidden'] = '';
357 } else {
358 if ( $this->fld_comment ) {
359 $vals['comment'] = $row->log_comment;
360 }
361
362 if ( $this->fld_parsedcomment ) {
363 $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
364 }
365 }
366 }
367
368 if ( $this->fld_tags ) {
369 if ( $row->ts_tags ) {
370 $tags = explode( ',', $row->ts_tags );
371 $this->getResult()->setIndexedTagName( $tags, 'tag' );
372 $vals['tags'] = $tags;
373 } else {
374 $vals['tags'] = array();
375 }
376 }
377
378 return $vals;
379 }
380
381 public function getCacheMode( $params ) {
382 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
383 // formatComment() calls wfMessage() among other things
384 return 'anon-public-user-private';
385 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
386 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
387 ) { // Output can only contain public data.
388 return 'public';
389 } else {
390 return 'anon-public-user-private';
391 }
392 }
393
394 public function getAllowedParams() {
395 global $wgLogTypes, $wgLogActions, $wgLogActionsHandlers;
396 return array(
397 'prop' => array(
398 ApiBase::PARAM_ISMULTI => true,
399 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
400 ApiBase::PARAM_TYPE => array(
401 'ids',
402 'title',
403 'type',
404 'user',
405 'userid',
406 'timestamp',
407 'comment',
408 'parsedcomment',
409 'details',
410 'tags'
411 )
412 ),
413 'type' => array(
414 ApiBase::PARAM_TYPE => $wgLogTypes
415 ),
416 'action' => array(
417 ApiBase::PARAM_TYPE => array_keys( array_merge( $wgLogActions, $wgLogActionsHandlers ) )
418 ),
419 'start' => array(
420 ApiBase::PARAM_TYPE => 'timestamp'
421 ),
422 'end' => array(
423 ApiBase::PARAM_TYPE => 'timestamp'
424 ),
425 'dir' => array(
426 ApiBase::PARAM_DFLT => 'older',
427 ApiBase::PARAM_TYPE => array(
428 'newer',
429 'older'
430 )
431 ),
432 'user' => null,
433 'title' => null,
434 'prefix' => null,
435 'tag' => null,
436 'limit' => array(
437 ApiBase::PARAM_DFLT => 10,
438 ApiBase::PARAM_TYPE => 'limit',
439 ApiBase::PARAM_MIN => 1,
440 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
441 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
442 )
443 );
444 }
445
446 public function getParamDescription() {
447 $p = $this->getModulePrefix();
448 return array(
449 'prop' => array(
450 'Which properties to get',
451 ' ids - Adds the ID of the log event',
452 ' title - Adds the title of the page for the log event',
453 ' type - Adds the type of log event',
454 ' user - Adds the user responsible for the log event',
455 ' userid - Adds the user ID who was responsible for the log event',
456 ' timestamp - Adds the timestamp for the event',
457 ' comment - Adds the comment of the event',
458 ' parsedcomment - Adds the parsed comment of the event',
459 ' details - Lists additional details about the event',
460 ' tags - Lists tags for the event',
461 ),
462 'type' => 'Filter log entries to only this type',
463 'action' => "Filter log actions to only this type. Overrides {$p}type",
464 'start' => 'The timestamp to start enumerating from',
465 'end' => 'The timestamp to end enumerating',
466 'dir' => $this->getDirectionDescription( $p ),
467 'user' => 'Filter entries to those made by the given user',
468 'title' => 'Filter entries to those related to a page',
469 'prefix' => 'Filter entries that start with this prefix. Disabled in Miser Mode',
470 'limit' => 'How many total event entries to return',
471 'tag' => 'Only list event entries tagged with this tag',
472 );
473 }
474
475 public function getResultProperties() {
476 global $wgLogTypes;
477 return array(
478 'ids' => array(
479 'logid' => 'integer',
480 'pageid' => 'integer'
481 ),
482 'title' => array(
483 'ns' => 'namespace',
484 'title' => 'string'
485 ),
486 'type' => array(
487 'type' => array(
488 ApiBase::PROP_TYPE => $wgLogTypes
489 ),
490 'action' => 'string'
491 ),
492 'details' => array(
493 'actionhidden' => 'boolean'
494 ),
495 'user' => array(
496 'userhidden' => 'boolean',
497 'user' => array(
498 ApiBase::PROP_TYPE => 'string',
499 ApiBase::PROP_NULLABLE => true
500 ),
501 'anon' => 'boolean'
502 ),
503 'userid' => array(
504 'userhidden' => 'boolean',
505 'userid' => array(
506 ApiBase::PROP_TYPE => 'integer',
507 ApiBase::PROP_NULLABLE => true
508 ),
509 'anon' => 'boolean'
510 ),
511 'timestamp' => array(
512 'timestamp' => 'timestamp'
513 ),
514 'comment' => array(
515 'commenthidden' => 'boolean',
516 'comment' => array(
517 ApiBase::PROP_TYPE => 'string',
518 ApiBase::PROP_NULLABLE => true
519 )
520 ),
521 'parsedcomment' => array(
522 'commenthidden' => 'boolean',
523 'parsedcomment' => array(
524 ApiBase::PROP_TYPE => 'string',
525 ApiBase::PROP_NULLABLE => true
526 )
527 )
528 );
529 }
530
531 public function getDescription() {
532 return 'Get events from logs';
533 }
534
535 public function getPossibleErrors() {
536 return array_merge( parent::getPossibleErrors(), array(
537 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
538 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
539 array( 'code' => 'param_prefix', 'info' => 'Bad title value \'prefix\'' ),
540 array( 'code' => 'prefixsearchdisabled', 'info' => 'Prefix search disabled in Miser Mode' ),
541 ) );
542 }
543
544 public function getExamples() {
545 return array(
546 'api.php?action=query&list=logevents'
547 );
548 }
549
550 public function getHelpUrls() {
551 return 'https://www.mediawiki.org/wiki/API:Logevents';
552 }
553 }