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