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