Merge "Revert "selenium: add new message banner test to user spec""
[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 = new CommentStore( 'log_comment' );
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 // Order is significant here
66 $this->addTables( [ 'logging', 'user', 'page' ] );
67 $this->addJoinConds( [
68 'user' => [ 'LEFT JOIN',
69 'user_id=log_user' ],
70 'page' => [ 'LEFT JOIN',
71 [ 'log_namespace=page_namespace',
72 'log_title=page_title' ] ] ] );
73
74 $this->addFields( [
75 'log_id',
76 'log_type',
77 'log_action',
78 'log_timestamp',
79 'log_deleted',
80 ] );
81
82 $this->addFieldsIf( 'page_id', $this->fld_ids );
83 // log_page is the page_id saved at log time, whereas page_id is from a
84 // join at query time. This leads to different results in various
85 // scenarios, e.g. deletion, recreation.
86 $this->addFieldsIf( 'log_page', $this->fld_ids );
87 $this->addFieldsIf( [ 'log_user', 'log_user_text', 'user_name' ], $this->fld_user );
88 $this->addFieldsIf( 'log_user', $this->fld_userid );
89 $this->addFieldsIf(
90 [ 'log_namespace', 'log_title' ],
91 $this->fld_title || $this->fld_parsedcomment
92 );
93 $this->addFieldsIf( 'log_params', $this->fld_details );
94
95 if ( $this->fld_comment || $this->fld_parsedcomment ) {
96 $commentQuery = $this->commentStore->getJoin();
97 $this->addTables( $commentQuery['tables'] );
98 $this->addFields( $commentQuery['fields'] );
99 $this->addJoinConds( $commentQuery['joins'] );
100 }
101
102 if ( $this->fld_tags ) {
103 $this->addTables( 'tag_summary' );
104 $this->addJoinConds( [ 'tag_summary' => [ 'LEFT JOIN', 'log_id=ts_log_id' ] ] );
105 $this->addFields( 'ts_tags' );
106 }
107
108 if ( !is_null( $params['tag'] ) ) {
109 $this->addTables( 'change_tag' );
110 $this->addJoinConds( [ 'change_tag' => [ 'INNER JOIN',
111 [ 'log_id=ct_log_id' ] ] ] );
112 $this->addWhereFld( 'ct_tag', $params['tag'] );
113 }
114
115 if ( !is_null( $params['action'] ) ) {
116 // Do validation of action param, list of allowed actions can contains wildcards
117 // Allow the param, when the actions is in the list or a wildcard version is listed.
118 $logAction = $params['action'];
119 if ( strpos( $logAction, '/' ) === false ) {
120 // all items in the list have a slash
121 $valid = false;
122 } else {
123 $logActions = array_flip( $this->getAllowedLogActions() );
124 list( $type, $action ) = explode( '/', $logAction, 2 );
125 $valid = isset( $logActions[$logAction] ) || isset( $logActions[$type . '/*'] );
126 }
127
128 if ( !$valid ) {
129 $encParamName = $this->encodeParamName( 'action' );
130 $this->dieWithError(
131 [ 'apierror-unrecognizedvalue', $encParamName, wfEscapeWikiText( $logAction ) ],
132 "unknown_$encParamName"
133 );
134 }
135
136 $this->addWhereFld( 'log_type', $type );
137 $this->addWhereFld( 'log_action', $action );
138 } elseif ( !is_null( $params['type'] ) ) {
139 $this->addWhereFld( 'log_type', $params['type'] );
140 }
141
142 $this->addTimestampWhereRange(
143 'log_timestamp',
144 $params['dir'],
145 $params['start'],
146 $params['end']
147 );
148 // Include in ORDER BY for uniqueness
149 $this->addWhereRange( 'log_id', $params['dir'], null, null );
150
151 if ( !is_null( $params['continue'] ) ) {
152 $cont = explode( '|', $params['continue'] );
153 $this->dieContinueUsageIf( count( $cont ) != 2 );
154 $op = ( $params['dir'] === 'newer' ? '>' : '<' );
155 $continueTimestamp = $db->addQuotes( $db->timestamp( $cont[0] ) );
156 $continueId = (int)$cont[1];
157 $this->dieContinueUsageIf( $continueId != $cont[1] );
158 $this->addWhere( "log_timestamp $op $continueTimestamp OR " .
159 "(log_timestamp = $continueTimestamp AND " .
160 "log_id $op= $continueId)"
161 );
162 }
163
164 $limit = $params['limit'];
165 $this->addOption( 'LIMIT', $limit + 1 );
166
167 $user = $params['user'];
168 if ( !is_null( $user ) ) {
169 $userid = User::idFromName( $user );
170 if ( $userid ) {
171 $this->addWhereFld( 'log_user', $userid );
172 } else {
173 $this->addWhereFld( 'log_user_text', $user );
174 }
175 }
176
177 $title = $params['title'];
178 if ( !is_null( $title ) ) {
179 $titleObj = Title::newFromText( $title );
180 if ( is_null( $titleObj ) ) {
181 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $title ) ] );
182 }
183 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
184 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
185 }
186
187 if ( $params['namespace'] !== null ) {
188 $this->addWhereFld( 'log_namespace', $params['namespace'] );
189 }
190
191 $prefix = $params['prefix'];
192
193 if ( !is_null( $prefix ) ) {
194 if ( $this->getConfig()->get( 'MiserMode' ) ) {
195 $this->dieWithError( 'apierror-prefixsearchdisabled' );
196 }
197
198 $title = Title::newFromText( $prefix );
199 if ( is_null( $title ) ) {
200 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $prefix ) ] );
201 }
202 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
203 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
204 }
205
206 // Paranoia: avoid brute force searches (T19342)
207 if ( $params['namespace'] !== null || !is_null( $title ) || !is_null( $user ) ) {
208 if ( !$this->getUser()->isAllowed( 'deletedhistory' ) ) {
209 $titleBits = LogPage::DELETED_ACTION;
210 $userBits = LogPage::DELETED_USER;
211 } elseif ( !$this->getUser()->isAllowedAny( 'suppressrevision', 'viewsuppressed' ) ) {
212 $titleBits = LogPage::DELETED_ACTION | LogPage::DELETED_RESTRICTED;
213 $userBits = LogPage::DELETED_USER | LogPage::DELETED_RESTRICTED;
214 } else {
215 $titleBits = 0;
216 $userBits = 0;
217 }
218 if ( ( $params['namespace'] !== null || !is_null( $title ) ) && $titleBits ) {
219 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
220 }
221 if ( !is_null( $user ) && $userBits ) {
222 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
223 }
224 }
225
226 $count = 0;
227 $res = $this->select( __METHOD__ );
228 $result = $this->getResult();
229 foreach ( $res as $row ) {
230 if ( ++$count > $limit ) {
231 // We've reached the one extra which shows that there are
232 // additional pages to be had. Stop here...
233 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
234 break;
235 }
236
237 $vals = $this->extractRowInfo( $row );
238 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
239 if ( !$fit ) {
240 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
241 break;
242 }
243 }
244 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
245 }
246
247 /**
248 * @deprecated since 1.25 Use LogFormatter::formatParametersForApi instead
249 * @param ApiResult $result
250 * @param array &$vals
251 * @param string $params
252 * @param string $type
253 * @param string $action
254 * @param string $ts
255 * @param bool $legacy
256 * @return array
257 */
258 public static function addLogParams( $result, &$vals, $params, $type,
259 $action, $ts, $legacy = false
260 ) {
261 wfDeprecated( __METHOD__, '1.25' );
262
263 $entry = new ManualLogEntry( $type, $action );
264 $entry->setParameters( $params );
265 $entry->setTimestamp( $ts );
266 $entry->setLegacy( $legacy );
267 $formatter = LogFormatter::newFromEntry( $entry );
268 $vals['params'] = $formatter->formatParametersForApi();
269
270 return $vals;
271 }
272
273 private function extractRowInfo( $row ) {
274 $logEntry = DatabaseLogEntry::newFromRow( $row );
275 $vals = [
276 ApiResult::META_TYPE => 'assoc',
277 ];
278 $anyHidden = false;
279 $user = $this->getUser();
280
281 if ( $this->fld_ids ) {
282 $vals['logid'] = intval( $row->log_id );
283 }
284
285 if ( $this->fld_title || $this->fld_parsedcomment ) {
286 $title = Title::makeTitle( $row->log_namespace, $row->log_title );
287 }
288
289 if ( $this->fld_title || $this->fld_ids || $this->fld_details && $row->log_params !== '' ) {
290 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_ACTION ) ) {
291 $vals['actionhidden'] = true;
292 $anyHidden = true;
293 }
294 if ( LogEventsList::userCan( $row, LogPage::DELETED_ACTION, $user ) ) {
295 if ( $this->fld_title ) {
296 ApiQueryBase::addTitleInfo( $vals, $title );
297 }
298 if ( $this->fld_ids ) {
299 $vals['pageid'] = intval( $row->page_id );
300 $vals['logpage'] = intval( $row->log_page );
301 }
302 if ( $this->fld_details ) {
303 $vals['params'] = LogFormatter::newFromEntry( $logEntry )->formatParametersForApi();
304 }
305 }
306 }
307
308 if ( $this->fld_type ) {
309 $vals['type'] = $row->log_type;
310 $vals['action'] = $row->log_action;
311 }
312
313 if ( $this->fld_user || $this->fld_userid ) {
314 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_USER ) ) {
315 $vals['userhidden'] = true;
316 $anyHidden = true;
317 }
318 if ( LogEventsList::userCan( $row, LogPage::DELETED_USER, $user ) ) {
319 if ( $this->fld_user ) {
320 $vals['user'] = $row->user_name === null ? $row->log_user_text : $row->user_name;
321 }
322 if ( $this->fld_userid ) {
323 $vals['userid'] = intval( $row->log_user );
324 }
325
326 if ( !$row->log_user ) {
327 $vals['anon'] = true;
328 }
329 }
330 }
331 if ( $this->fld_timestamp ) {
332 $vals['timestamp'] = wfTimestamp( TS_ISO_8601, $row->log_timestamp );
333 }
334
335 if ( $this->fld_comment || $this->fld_parsedcomment ) {
336 if ( LogEventsList::isDeleted( $row, LogPage::DELETED_COMMENT ) ) {
337 $vals['commenthidden'] = true;
338 $anyHidden = true;
339 }
340 if ( LogEventsList::userCan( $row, LogPage::DELETED_COMMENT, $user ) ) {
341 $comment = $this->commentStore->getComment( $row )->text;
342 if ( $this->fld_comment ) {
343 $vals['comment'] = $comment;
344 }
345
346 if ( $this->fld_parsedcomment ) {
347 $vals['parsedcomment'] = Linker::formatComment( $comment, $title );
348 }
349 }
350 }
351
352 if ( $this->fld_tags ) {
353 if ( $row->ts_tags ) {
354 $tags = explode( ',', $row->ts_tags );
355 ApiResult::setIndexedTagName( $tags, 'tag' );
356 $vals['tags'] = $tags;
357 } else {
358 $vals['tags'] = [];
359 }
360 }
361
362 if ( $anyHidden && LogEventsList::isDeleted( $row, LogPage::DELETED_RESTRICTED ) ) {
363 $vals['suppressed'] = true;
364 }
365
366 return $vals;
367 }
368
369 /**
370 * @return array
371 */
372 private function getAllowedLogActions() {
373 $config = $this->getConfig();
374 return array_keys( array_merge(
375 $config->get( 'LogActions' ),
376 $config->get( 'LogActionsHandlers' )
377 ) );
378 }
379
380 public function getCacheMode( $params ) {
381 if ( $this->userCanSeeRevDel() ) {
382 return 'private';
383 }
384 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
385 // formatComment() calls wfMessage() among other things
386 return 'anon-public-user-private';
387 } elseif ( LogEventsList::getExcludeClause( $this->getDB(), 'user', $this->getUser() )
388 === LogEventsList::getExcludeClause( $this->getDB(), 'public' )
389 ) { // Output can only contain public data.
390 return 'public';
391 } else {
392 return 'anon-public-user-private';
393 }
394 }
395
396 public function getAllowedParams( $flags = 0 ) {
397 $config = $this->getConfig();
398 $ret = [
399 'prop' => [
400 ApiBase::PARAM_ISMULTI => true,
401 ApiBase::PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
402 ApiBase::PARAM_TYPE => [
403 'ids',
404 'title',
405 'type',
406 'user',
407 'userid',
408 'timestamp',
409 'comment',
410 'parsedcomment',
411 'details',
412 'tags'
413 ],
414 ApiBase::PARAM_HELP_MSG_PER_VALUE => [],
415 ],
416 'type' => [
417 ApiBase::PARAM_TYPE => $config->get( 'LogTypes' )
418 ],
419 'action' => [
420 // validation on request is done in execute()
421 ApiBase::PARAM_TYPE => ( $flags & ApiBase::GET_VALUES_FOR_HELP )
422 ? $this->getAllowedLogActions()
423 : null
424 ],
425 'start' => [
426 ApiBase::PARAM_TYPE => 'timestamp'
427 ],
428 'end' => [
429 ApiBase::PARAM_TYPE => 'timestamp'
430 ],
431 'dir' => [
432 ApiBase::PARAM_DFLT => 'older',
433 ApiBase::PARAM_TYPE => [
434 'newer',
435 'older'
436 ],
437 ApiBase::PARAM_HELP_MSG => 'api-help-param-direction',
438 ],
439 'user' => [
440 ApiBase::PARAM_TYPE => 'user',
441 ],
442 'title' => null,
443 'namespace' => [
444 ApiBase::PARAM_TYPE => 'namespace',
445 ApiBase::PARAM_EXTRA_NAMESPACES => [ NS_MEDIA, NS_SPECIAL ],
446 ],
447 'prefix' => [],
448 'tag' => null,
449 'limit' => [
450 ApiBase::PARAM_DFLT => 10,
451 ApiBase::PARAM_TYPE => 'limit',
452 ApiBase::PARAM_MIN => 1,
453 ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
454 ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2
455 ],
456 'continue' => [
457 ApiBase::PARAM_HELP_MSG => 'api-help-param-continue',
458 ],
459 ];
460
461 if ( $config->get( 'MiserMode' ) ) {
462 $ret['prefix'][ApiBase::PARAM_HELP_MSG] = 'api-help-param-disabled-in-miser-mode';
463 }
464
465 return $ret;
466 }
467
468 protected function getExamplesMessages() {
469 return [
470 'action=query&list=logevents'
471 => 'apihelp-query+logevents-example-simple',
472 ];
473 }
474
475 public function getHelpUrls() {
476 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logevents';
477 }
478 }