Merge "Revert "Log the reason why revision->getContent() returns null""
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.php
1 <?php
2 /**
3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
19 *
20 * @file
21 */
22
23 /**
24 * Query action to List the log events, with optional filtering by various parameters.
25 *
26 * @ingroup API
27 */
28 class ApiQueryLogEvents extends ApiQueryBase {
29
30 private $commentStore;
31
32 public function __construct( ApiQuery $query, $moduleName ) {
33 parent::__construct( $query, $moduleName, 'le' );
34 }
35
36 private $fld_ids = false, $fld_title = false, $fld_type = false,
37 $fld_user = false, $fld_userid = false,
38 $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
39 $fld_details = false, $fld_tags = false;
40
41 public function execute() {
42 $params = $this->extractRequestParams();
43 $db = $this->getDB();
44 $this->commentStore = CommentStore::getStore();
45 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
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_user = isset( $prop['user'] );
53 $this->fld_userid = isset( $prop['userid'] );
54 $this->fld_timestamp = isset( $prop['timestamp'] );
55 $this->fld_comment = isset( $prop['comment'] );
56 $this->fld_parsedcomment = isset( $prop['parsedcomment'] );
57 $this->fld_details = isset( $prop['details'] );
58 $this->fld_tags = isset( $prop['tags'] );
59
60 $hideLogs = LogEventsList::getExcludeClause( $db, 'user', $this->getUser() );
61 if ( $hideLogs !== false ) {
62 $this->addWhere( $hideLogs );
63 }
64
65 $actorMigration = ActorMigration::newMigration();
66 $actorQuery = $actorMigration->getJoin( 'log_user' );
67 $this->addTables( 'logging' );
68 $this->addTables( $actorQuery['tables'] );
69 $this->addTables( [ 'user', 'page' ] );
70 $this->addJoinConds( $actorQuery['joins'] );
71 $this->addJoinConds( [
72 'user' => [ 'LEFT JOIN',
73 'user_id=' . $actorQuery['fields']['log_user'] ],
74 'page' => [ 'LEFT JOIN',
75 [ 'log_namespace=page_namespace',
76 'log_title=page_title' ] ] ] );
77
78 $this->addFields( [
79 'log_id',
80 'log_type',
81 'log_action',
82 'log_timestamp',
83 'log_deleted',
84 ] );
85
86 $this->addFieldsIf( 'page_id', $this->fld_ids );
87 // log_page is the page_id saved at log time, whereas page_id is from a
88 // join at query time. This leads to different results in various
89 // scenarios, e.g. deletion, recreation.
90 $this->addFieldsIf( 'log_page', $this->fld_ids );
91 $this->addFieldsIf( $actorQuery['fields'] + [ 'user_name' ], $this->fld_user );
92 $this->addFieldsIf( $actorQuery['fields'], $this->fld_userid );
93 $this->addFieldsIf(
94 [ 'log_namespace', 'log_title' ],
95 $this->fld_title || $this->fld_parsedcomment
96 );
97 $this->addFieldsIf( 'log_params', $this->fld_details );
98
99 if ( $this->fld_comment || $this->fld_parsedcomment ) {
100 $commentQuery = $this->commentStore->getJoin( 'log_comment' );
101 $this->addTables( $commentQuery['tables'] );
102 $this->addFields( $commentQuery['fields'] );
103 $this->addJoinConds( $commentQuery['joins'] );
104 }
105
106 if ( $this->fld_tags ) {
107 $this->addTables( 'tag_summary' );
108 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', 'log_id=ts_log_id' ] ] );
109 $this->addFields( 'ts_tags' );
110 }
111
112 if ( !is_null( $params['tag'] ) ) {
113 $this->addTables( 'change_tag' );
114 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN',
115 [ 'log_id=ct_log_id' ] ] ] );
116 $this->addWhereFld( 'ct_tag', $params['tag'] );
117 }
118
119 if ( !is_null( $params['action'] ) ) {
120 // Do validation of action param, list of allowed actions can contains wildcards
121 // Allow the param, when the actions is in the list or a wildcard version is listed.
122 $logAction = $params['action'];
123 if ( strpos( $logAction, '/' ) === false ) {
124 // all items in the list have a slash
125 $valid = false;
126 } else {
127 $logActions = array_flip( $this->getAllowedLogActions() );
128 list( $type, $action ) = explode( '/', $logAction, 2 );
129 $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type . '/*'] );
130 }
131
132 if ( !$valid ) {
133 $encParamName = $this->encodeParamName( 'action' );
134 $this->dieWithError(
135 [ 'apierror-unrecognizedvalue', $encParamName, wfEscapeWikiText( $logAction ) ],
136 "unknown_$encParamName"
137 );
138 }
139
140 $this->addWhereFld( 'log_type', $type );
141 $this->addWhereFld( 'log_action', $action );
142 } elseif ( !is_null( $params['type'] ) ) {
143 $this->addWhereFld( 'log_type', $params['type'] );
144 }
145
146 $this->addTimestampWhereRange(
147 'log_timestamp',
148 $params['dir'],
149 $params['start'],
150 $params['end']
151 );
152 // Include in ORDER BY for uniqueness
153 $this->addWhereRange( 'log_id', $params['dir'], null, null );
154
155 if ( !is_null( $params['continue'] ) ) {
156 $cont = explode( '|', $params['continue'] );
157 $this->dieContinueUsageIf( count( $cont ) != 2 );
158 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
159 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
160 $continueId = (int)$cont[1];
161 $this->dieContinueUsageIf( $continueId != $cont[1] );
162 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
163 "(log_timestamp = $continueTimestamp AND " .
164 "log_id $op= $continueId)"
165 );
166 }
167
168 $limit = $params['limit'];
169 $this->addOption( 'LIMIT', $limit + 1 );
170
171 $user = $params['user'];
172 if ( !is_null( $user ) ) {
173 // Note the joins in $q are the same as those from ->getJoin() above
174 // so we only need to add 'conds' here.
175 // Don't query by user ID here, it might be able to use the
176 // log_user_text_time or log_user_text_type_time index.
177 $q = $actorMigration->getWhere(
178 $db, 'log_user', User::newFromName( $params['user'], false ), false
179 );
180 $this->addWhere( $q['conds'] );
181 }
182
183 $title = $params['title'];
184 if ( !is_null( $title ) ) {
185 $titleObj = Title::newFromText( $title );
186 if ( is_null( $titleObj ) ) {
187 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
188 }
189 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
190 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
191 }
192
193 if ( $params['namespace'] !== null ) {
194 $this->addWhereFld( 'log_namespace', $params['namespace'] );
195 }
196
197 $prefix = $params['prefix'];
198
199 if ( !is_null( $prefix ) ) {
200 if ( $this->getConfig()->get( 'MiserMode' ) ) {
201 $this->dieWithError( 'apierror-prefixsearchdisabled' );
202 }
203
204 $title = Title::newFromText( $prefix );
205 if ( is_null( $title ) ) {
206 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $prefix ) ] );
207 }
208 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
209 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
210 }
211
212 // Paranoia: avoid brute force searches (T19342)
213 if ( $params['namespace'] !== null || !is_null( $title ) || !is_null( $user ) ) {
214 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
215 $titleBits = LogPage::DELETED_ACTION;
216 $userBits = LogPage::DELETED_USER;
217 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
218 $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
219 $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED;
220 } else {
221 $titleBits = 0;
222 $userBits = 0;
223 }
224 if ( ( $params['namespace'] !== null || !is_null( $title ) ) && $titleBits ) {
225 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
226 }
227 if ( !is_null( $user ) && $userBits ) {
228 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
229 }
230 }
231
232 $count = 0;
233 $res = $this->select( __METHOD__ );
234 $result = $this->getResult();
235 foreach ( $res as $row ) {
236 if ( ++$count > $limit ) {
237 // We've reached the one extra which shows that there are
238 // additional pages to be had. Stop here...
239 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
240 break;
241 }
242
243 $vals = $this->extractRowInfo( $row );
244 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
245 if ( !$fit ) {
246 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
247 break;
248 }
249 }
250 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
251 }
252
253 /**
254 * @deprecated since 1.25 Use LogFormatter::formatParametersForApi instead
255 * @param ApiResult $result
256 * @param array &$vals
257 * @param string $params
258 * @param string $type
259 * @param string $action
260 * @param string $ts
261 * @param bool $legacy
262 * @return array
263 */
264 public static function addLogParams( $result, &$vals, $params, $type,
265 $action, $ts, $legacy = false
266 ) {
267 wfDeprecated( __METHOD__, '1.25' );
268
269 $entry = new ManualLogEntry( $type, $action );
270 $entry->setParameters( $params );
271 $entry->setTimestamp( $ts );
272 $entry->setLegacy( $legacy );
273 $formatter = LogFormatter::newFromEntry( $entry );
274 $vals['params'] = $formatter->formatParametersForApi();
275
276 return $vals;
277 }
278
279 private function extractRowInfo( $row ) {
280 $logEntry = DatabaseLogEntry::newFromRow( $row );
281 $vals = [
282 ApiResult::META_TYPE => 'assoc',
283 ];
284 $anyHidden = false;
285 $user = $this->getUser();
286
287 if ( $this->fld_ids ) {
288 $vals['logid'] = intval( $row->log_id );
289 }
290
291 if ( $this->fld_title || $this->fld_parsedcomment ) {
292 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
293 }
294
295 if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) {
296 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
297 $vals['actionhidden'] = true;
298 $anyHidden = true;
299 }
300 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) {
301 if ( $this->fld_title ) {
302 ApiQueryBase::addTitleInfo( $vals, $title );
303 }
304 if ( $this->fld_ids ) {
305 $vals['pageid'] = intval( $row->page_id );
306 $vals['logpage'] = intval( $row->log_page );
307 }
308 if ( $this->fld_details ) {
309 $vals['params'] = LogFormatter::newFromEntry( $logEntry )->formatParametersForApi();
310 }
311 }
312 }
313
314 if ( $this->fld_type ) {
315 $vals['type'] = $row->log_type;
316 $vals['action'] = $row->log_action;
317 }
318
319 if ( $this->fld_user || $this->fld_userid ) {
320 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
321 $vals['userhidden'] = true;
322 $anyHidden = true;
323 }
324 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
325 if ( $this->fld_user ) {
326 $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
327 }
328 if ( $this->fld_userid ) {
329 $vals['userid'] = intval( $row->log_user );
330 }
331
332 if ( !$row->log_user ) {
333 $vals['anon'] = true;
334 }
335 }
336 }
337 if ( $this->fld_timestamp ) {
338 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
339 }
340
341 if ( $this->fld_comment || $this->fld_parsedcomment ) {
342 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
343 $vals['commenthidden'] = true;
344 $anyHidden = true;
345 }
346 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
347 $comment = $this->commentStore->getComment( 'log_comment', $row )->text;
348 if ( $this->fld_comment ) {
349 $vals['comment'] = $comment;
350 }
351
352 if ( $this->fld_parsedcomment ) {
353 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
354 }
355 }
356 }
357
358 if ( $this->fld_tags ) {
359 if ( $row->ts_tags ) {
360 $tags = explode( ',', $row->ts_tags );
361 ApiResult::setIndexedTagName( $tags, 'tag' );
362 $vals['tags'] = $tags;
363 } else {
364 $vals['tags'] = [];
365 }
366 }
367
368 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
369 $vals['suppressed'] = true;
370 }
371
372 return $vals;
373 }
374
375 /**
376 * @return array
377 */
378 private function getAllowedLogActions() {
379 $config = $this->getConfig();
380 return array_keys( array_merge(
381 $config->get( 'LogActions' ),
382 $config->get( 'LogActionsHandlers' )
383 ) );
384 }
385
386 public function getCacheMode( $params ) {
387 if ( $this->userCanSeeRevDel() ) {
388 return 'private';
389 }
390 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
391 // formatComment() calls wfMessage() among other things
392 return 'anon-public-user-private';
393 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
394 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
395 ) { // Output can only contain public data.
396 return 'public';
397 } else {
398 return 'anon-public-user-private';
399 }
400 }
401
402 public function getAllowedParams( $flags = 0 ) {
403 $config = $this->getConfig();
404 $ret = [
405 'prop' => [
406 ApiBase::PARAM_ISMULTI => true,
407 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
408 ApiBase::PARAM_TYPE => [
409 'ids',
410 'title',
411 'type',
412 'user',
413 'userid',
414 'timestamp',
415 'comment',
416 'parsedcomment',
417 'details',
418 'tags'
419 ],
420 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
421 ],
422 'type' => [
423 ApiBase::PARAM_TYPE => $config->get( 'LogTypes' )
424 ],
425 'action' => [
426 // validation on request is done in execute()
427 ApiBase::PARAM_TYPE => ( $flags & ApiBase::GET_VALUES_FOR_HELP )
428 ? $this->getAllowedLogActions()
429 : null
430 ],
431 'start' => [
432 ApiBase::PARAM_TYPE => 'timestamp'
433 ],
434 'end' => [
435 ApiBase::PARAM_TYPE => 'timestamp'
436 ],
437 'dir' => [
438 ApiBase::PARAM_DFLT => 'older',
439 ApiBase::PARAM_TYPE => [
440 'newer',
441 'older'
442 ],
443 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
444 ],
445 'user' => [
446 ApiBase::PARAM_TYPE => 'user',
447 ],
448 'title' => null,
449 'namespace' => [
450 ApiBase::PARAM_TYPE => 'namespace',
451 ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
452 ],
453 'prefix' => [],
454 'tag' => null,
455 'limit' => [
456 ApiBase::PARAM_DFLT => 10,
457 ApiBase::PARAM_TYPE => 'limit',
458 ApiBase::PARAM_MIN => 1,
459 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
460 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
461 ],
462 'continue' => [
463 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
464 ],
465 ];
466
467 if ( $config->get( 'MiserMode' ) ) {
468 $ret['prefix'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
469 }
470
471 return $ret;
472 }
473
474 protected function getExamplesMessages() {
475 return [
476 'action=query&list=logevents'
477 => 'apihelp-query+logevents-example-simple',
478 ];
479 }
480
481 public function getHelpUrls() {
482 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logevents';
483 }
484 }