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