Merge "Added another size limit check to Job::toString"
[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( ApiQuery $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 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
47
48 $prop = array_flip( $params['prop'] );
49
50 $this->fld_ids = isset( $prop['ids'] );
51 $this->fld_title = isset( $prop['title'] );
52 $this->fld_type = isset( $prop['type'] );
53 $this->fld_action = isset( $prop['action'] );
54 $this->fld_user = isset( $prop['user'] );
55 $this->fld_userid = isset( $prop['userid'] );
56 $this->fld_timestamp = isset( $prop['timestamp'] );
57 $this->fld_comment = isset( $prop['comment'] );
58 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
59 $this->fld_details = isset( $prop['details'] );
60 $this->fld_tags = isset( $prop['tags'] );
61
62 $hideLogs = LogEventsList::getExcludeClause( $db, 'user', $this->getUser() );
63 if ( $hideLogs !== false ) {
64 $this->addWhere( $hideLogs );
65 }
66
67 // Order is significant here
68 $this->addTables( array( 'logging', 'user', 'page' ) );
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_id',
78 'log_type',
79 'log_action',
80 'log_timestamp',
81 'log_deleted',
82 ) );
83
84 $this->addFieldsIf( 'page_id', $this->fld_ids );
85 // log_page is the page_id saved at log time, whereas page_id is from a
86 // join at query time. This leads to different results in various
87 // scenarios, e.g. deletion, recreation.
88 $this->addFieldsIf( 'log_page', $this->fld_ids );
89 $this->addFieldsIf( array( 'log_user', 'log_user_text', 'user_name' ), $this->fld_user );
90 $this->addFieldsIf( 'log_user', $this->fld_userid );
91 $this->addFieldsIf(
92 array( 'log_namespace', 'log_title' ),
93 $this->fld_title || $this->fld_parsedcomment
94 );
95 $this->addFieldsIf( 'log_comment', $this->fld_comment || $this->fld_parsedcomment );
96 $this->addFieldsIf( 'log_params', $this->fld_details );
97
98 if ( $this->fld_tags ) {
99 $this->addTables( 'tag_summary' );
100 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
101 $this->addFields( 'ts_tags' );
102 }
103
104 if ( !is_null( $params['tag'] ) ) {
105 $this->addTables( 'change_tag' );
106 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN',
107 array( 'log_id=ct_log_id' ) ) ) );
108 $this->addWhereFld( 'ct_tag', $params['tag'] );
109 }
110
111 if ( !is_null( $params['action'] ) ) {
112 // Do validation of action param, list of allowed actions can contains wildcards
113 // Allow the param, when the actions is in the list or a wildcard version is listed.
114 $logAction = $params['action'];
115 if ( strpos( $logAction, '/' ) === false ) {
116 // all items in the list have a slash
117 $valid = false;
118 } else {
119 $logActions = array_flip( $this->getAllowedLogActions() );
120 list( $type, $action ) = explode( '/', $logAction, 2 );
121 $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type . '/*'] );
122 }
123
124 if ( !$valid ) {
125 $valueName = $this->encodeParamName( 'action' );
126 $this->dieUsage(
127 "Unrecognized value for parameter '$valueName': {$logAction}",
128 "unknown_$valueName"
129 );
130 }
131
132 $this->addWhereFld( 'log_type', $type );
133 $this->addWhereFld( 'log_action', $action );
134 } elseif ( !is_null( $params['type'] ) ) {
135 $this->addWhereFld( 'log_type', $params['type'] );
136 }
137
138 $this->addTimestampWhereRange(
139 'log_timestamp',
140 $params['dir'],
141 $params['start'],
142 $params['end']
143 );
144 // Include in ORDER BY for uniqueness
145 $this->addWhereRange( 'log_id', $params['dir'], null, null );
146
147 if ( !is_null( $params['continue'] ) ) {
148 $cont = explode( '|', $params['continue'] );
149 $this->dieContinueUsageIf( count( $cont ) != 2 );
150 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
151 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
152 $continueId = (int)$cont[1];
153 $this->dieContinueUsageIf( $continueId != $cont[1] );
154 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
155 "(log_timestamp = $continueTimestamp AND " .
156 "log_id $op= $continueId)"
157 );
158 }
159
160 $limit = $params['limit'];
161 $this->addOption( 'LIMIT', $limit + 1 );
162
163 $user = $params['user'];
164 if ( !is_null( $user ) ) {
165 $userid = User::idFromName( $user );
166 if ( $userid ) {
167 $this->addWhereFld( 'log_user', $userid );
168 } else {
169 $this->addWhereFld( 'log_user_text', IP::sanitizeIP( $user ) );
170 }
171 }
172
173 $title = $params['title'];
174 if ( !is_null( $title ) ) {
175 $titleObj = Title::newFromText( $title );
176 if ( is_null( $titleObj ) ) {
177 $this->dieUsage( "Bad title value '$title'", 'param_title' );
178 }
179 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
180 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
181 }
182
183 if ( $params['namespace'] !== null ) {
184 $this->addWhereFld( 'log_namespace', $params['namespace'] );
185 }
186
187 $prefix = $params['prefix'];
188
189 if ( !is_null( $prefix ) ) {
190 if ( $this->getConfig()->get( 'MiserMode' ) ) {
191 $this->dieUsage( 'Prefix search disabled in Miser Mode', 'prefixsearchdisabled' );
192 }
193
194 $title = Title::newFromText( $prefix );
195 if ( is_null( $title ) ) {
196 $this->dieUsage( "Bad title value '$prefix'", 'param_prefix' );
197 }
198 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
199 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
200 }
201
202 // Paranoia: avoid brute force searches (bug 17342)
203 if ( $params['namespace'] !== null || !is_null( $title ) || !is_null( $user ) ) {
204 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
205 $titleBits = LogPage::DELETED_ACTION;
206 $userBits = LogPage::DELETED_USER;
207 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
208 $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
209 $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED;
210 } else {
211 $titleBits = 0;
212 $userBits = 0;
213 }
214 if ( ( $params['namespace'] !== null || !is_null( $title ) ) && $titleBits ) {
215 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
216 }
217 if ( !is_null( $user ) && $userBits ) {
218 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
219 }
220 }
221
222 $count = 0;
223 $res = $this->select( __METHOD__ );
224 $result = $this->getResult();
225 foreach ( $res as $row ) {
226 if ( ++$count > $limit ) {
227 // We've reached the one extra which shows that there are
228 // additional pages to be had. Stop here...
229 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
230 break;
231 }
232
233 $vals = $this->extractRowInfo( $row );
234 if ( !$vals ) {
235 continue;
236 }
237 $fit = $result->addValue( array( 'query', $this->getModuleName() ), null, $vals );
238 if ( !$fit ) {
239 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
240 break;
241 }
242 }
243 $result->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
244 }
245
246 /**
247 * @param ApiResult $result
248 * @param array $vals
249 * @param string $params
250 * @param string $type
251 * @param string $action
252 * @param string $ts
253 * @param bool $legacy
254 * @return array
255 */
256 public static function addLogParams( $result, &$vals, $params, $type,
257 $action, $ts, $legacy = false
258 ) {
259 switch ( $type ) {
260 case 'move':
261 if ( $legacy ) {
262 $targetKey = 0;
263 $noredirKey = 1;
264 } else {
265 $targetKey = '4::target';
266 $noredirKey = '5::noredir';
267 }
268
269 if ( isset( $params[$targetKey] ) ) {
270 $title = Title::newFromText( $params[$targetKey] );
271 if ( $title ) {
272 $vals2 = array();
273 ApiQueryBase::addTitleInfo( $vals2, $title, 'new_' );
274 $vals[$type] = $vals2;
275 }
276 }
277 if ( isset( $params[$noredirKey] ) && $params[$noredirKey] ) {
278 $vals[$type]['suppressedredirect'] = '';
279 }
280 $params = null;
281 break;
282 case 'patrol':
283 if ( $legacy ) {
284 $cur = 0;
285 $prev = 1;
286 $auto = 2;
287 } else {
288 $cur = '4::curid';
289 $prev = '5::previd';
290 $auto = '6::auto';
291 }
292 $vals2 = array();
293 $vals2['cur'] = $params[$cur];
294 $vals2['prev'] = $params[$prev];
295 $vals2['auto'] = $params[$auto];
296 $vals[$type] = $vals2;
297 $params = null;
298 break;
299 case 'rights':
300 $vals2 = array();
301 if ( $legacy ) {
302 list( $vals2['old'], $vals2['new'] ) = $params;
303 } else {
304 $vals2['new'] = implode( ', ', $params['5::newgroups'] );
305 $vals2['old'] = implode( ', ', $params['4::oldgroups'] );
306 }
307 $vals[$type] = $vals2;
308 $params = null;
309 break;
310 case 'block':
311 if ( $action == 'unblock' ) {
312 break;
313 }
314 $vals2 = array();
315 list( $vals2['duration'], $vals2['flags'] ) = $params;
316
317 // Indefinite blocks have no expiry time
318 if ( SpecialBlock::parseExpiryInput( $params[0] ) !== wfGetDB( DB_SLAVE )->getInfinity() ) {
319 $vals2['expiry'] = wfTimestamp( TS_ISO_8601,
320 strtotime( $params[0], wfTimestamp( TS_UNIX, $ts ) ) );
321 }
322 $vals[$type] = $vals2;
323 $params = null;
324 break;
325 case 'upload':
326 if ( isset( $params['img_timestamp'] ) ) {
327 $params['img_timestamp'] = wfTimestamp( TS_ISO_8601, $params['img_timestamp'] );
328 }
329 break;
330 }
331 if ( !is_null( $params ) ) {
332 $logParams = array();
333 // Keys like "4::paramname" can't be used for output so we change them to "paramname"
334 foreach ( $params as $key => $value ) {
335 if ( strpos( $key, ':' ) === false ) {
336 $logParams[$key] = $value;
337 continue;
338 }
339 $logParam = explode( ':', $key, 3 );
340 $logParams[$logParam[2]] = $value;
341 }
342 $result->setIndexedTagName( $logParams, 'param' );
343 $result->setIndexedTagName_recursive( $logParams, 'param' );
344 $vals = array_merge( $vals, $logParams );
345 }
346
347 return $vals;
348 }
349
350 private function extractRowInfo( $row ) {
351 $logEntry = DatabaseLogEntry::newFromRow( $row );
352 $vals = array();
353 $anyHidden = false;
354 $user = $this->getUser();
355
356 if ( $this->fld_ids ) {
357 $vals['logid'] = intval( $row->log_id );
358 }
359
360 if ( $this->fld_title || $this->fld_parsedcomment ) {
361 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
362 }
363
364 if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) {
365 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
366 $vals['actionhidden'] = '';
367 $anyHidden = true;
368 }
369 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) {
370 if ( $this->fld_title ) {
371 ApiQueryBase::addTitleInfo( $vals, $title );
372 }
373 if ( $this->fld_ids ) {
374 $vals['pageid'] = intval( $row->page_id );
375 $vals['logpage'] = intval( $row->log_page );
376 }
377 if ( $this->fld_details && $row->log_params !== '' ) {
378 self::addLogParams(
379 $this->getResult(),
380 $vals,
381 $logEntry->getParameters(),
382 $logEntry->getType(),
383 $logEntry->getSubtype(),
384 $logEntry->getTimestamp(),
385 $logEntry->isLegacy()
386 );
387 }
388 }
389 }
390
391 if ( $this->fld_type || $this->fld_action ) {
392 $vals['type'] = $row->log_type;
393 $vals['action'] = $row->log_action;
394 }
395
396 if ( $this->fld_user || $this->fld_userid ) {
397 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
398 $vals['userhidden'] = '';
399 $anyHidden = true;
400 }
401 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
402 if ( $this->fld_user ) {
403 $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
404 }
405 if ( $this->fld_userid ) {
406 $vals['userid'] = intval( $row->log_user );
407 }
408
409 if ( !$row->log_user ) {
410 $vals['anon'] = '';
411 }
412 }
413 }
414 if ( $this->fld_timestamp ) {
415 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
416 }
417
418 if ( ( $this->fld_comment || $this->fld_parsedcomment ) && isset( $row->log_comment ) ) {
419 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
420 $vals['commenthidden'] = '';
421 $anyHidden = true;
422 }
423 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
424 if ( $this->fld_comment ) {
425 $vals['comment'] = $row->log_comment;
426 }
427
428 if ( $this->fld_parsedcomment ) {
429 $vals['parsedcomment'] = Linker::formatComment( $row->log_comment, $title );
430 }
431 }
432 }
433
434 if ( $this->fld_tags ) {
435 if ( $row->ts_tags ) {
436 $tags = explode( ',', $row->ts_tags );
437 $this->getResult()->setIndexedTagName( $tags, 'tag' );
438 $vals['tags'] = $tags;
439 } else {
440 $vals['tags'] = array();
441 }
442 }
443
444 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
445 $vals['suppressed'] = '';
446 }
447
448 return $vals;
449 }
450
451 /**
452 * @return array
453 */
454 private function getAllowedLogActions() {
455 $config = $this->getConfig();
456 return array_keys( array_merge( $config->get( 'LogActions' ), $config->get( 'LogActionsHandlers' ) ) );
457 }
458
459 public function getCacheMode( $params ) {
460 if ( $this->userCanSeeRevDel() ) {
461 return 'private';
462 }
463 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
464 // formatComment() calls wfMessage() among other things
465 return 'anon-public-user-private';
466 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
467 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
468 ) { // Output can only contain public data.
469 return 'public';
470 } else {
471 return 'anon-public-user-private';
472 }
473 }
474
475 public function getAllowedParams( $flags = 0 ) {
476 $config = $this->getConfig();
477 $ret = array(
478 'prop' => array(
479 ApiBase::PARAM_ISMULTI => true,
480 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
481 ApiBase::PARAM_TYPE => array(
482 'ids',
483 'title',
484 'type',
485 'user',
486 'userid',
487 'timestamp',
488 'comment',
489 'parsedcomment',
490 'details',
491 'tags'
492 )
493 ),
494 'type' => array(
495 ApiBase::PARAM_TYPE => $config->get( 'LogTypes' )
496 ),
497 'action' => array(
498 // validation on request is done in execute()
499 ApiBase::PARAM_TYPE => ( $flags & ApiBase::GET_VALUES_FOR_HELP )
500 ? $this->getAllowedLogActions()
501 : null
502 ),
503 'start' => array(
504 ApiBase::PARAM_TYPE => 'timestamp'
505 ),
506 'end' => array(
507 ApiBase::PARAM_TYPE => 'timestamp'
508 ),
509 'dir' => array(
510 ApiBase::PARAM_DFLT => 'older',
511 ApiBase::PARAM_TYPE => array(
512 'newer',
513 'older'
514 ),
515 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
516 ),
517 'user' => null,
518 'title' => null,
519 'namespace' => array(
520 ApiBase::PARAM_TYPE => 'namespace'
521 ),
522 'prefix' => array(),
523 'tag' => null,
524 'limit' => array(
525 ApiBase::PARAM_DFLT => 10,
526 ApiBase::PARAM_TYPE => 'limit',
527 ApiBase::PARAM_MIN => 1,
528 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
529 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
530 ),
531 'continue' => array(
532 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
533 ),
534 );
535
536 if ( $config->get( 'MiserMode' ) ) {
537 $ret['prefix'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
538 }
539
540 return $ret;
541 }
542
543 public function getExamplesMessages() {
544 return array(
545 'action=query&list=logevents'
546 => 'apihelp-query+logevents-example-simple',
547 );
548 }
549
550 public function getHelpUrls() {
551 return 'https://www.mediawiki.org/wiki/API:Logevents';
552 }
553 }