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