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