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