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