3 * Copyright © 2006 Yuri Astrakhan "<Firstname><Lastname>@gmail.com"
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.
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.
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
24 * Query action to List the log events, with optional filtering by various parameters.
28 class ApiQueryLogEvents
extends ApiQueryBase
{
30 private $commentStore;
32 public function __construct( ApiQuery
$query, $moduleName ) {
33 parent
::__construct( $query, $moduleName, 'le' );
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;
41 public function execute() {
42 $params = $this->extractRequestParams();
44 $this->commentStore
= new CommentStore( 'log_comment' );
45 $this->requireMaxOneParameter( $params, 'title', 'prefix', 'namespace' );
47 $prop = array_flip( $params['prop'] );
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'] );
60 $hideLogs = LogEventsList
::getExcludeClause( $db, 'user', $this->getUser() );
61 if ( $hideLogs !== false ) {
62 $this->addWhere( $hideLogs );
65 // Order is significant here
66 $this->addTables( [ 'logging', 'user', 'page' ] );
67 $this->addJoinConds( [
68 'user' => [ 'LEFT JOIN',
70 'page' => [ 'LEFT JOIN',
71 [ 'log_namespace=page_namespace',
72 'log_title=page_title' ] ] ] );
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
);
90 [ 'log_namespace', 'log_title' ],
91 $this->fld_title ||
$this->fld_parsedcomment
93 $this->addFieldsIf( 'log_params', $this->fld_details
);
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'] );
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' );
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'] );
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
123 $logActions = array_flip( $this->getAllowedLogActions() );
124 list( $type, $action ) = explode( '/', $logAction, 2 );
125 $valid = isset( $logActions[$logAction] ) ||
isset( $logActions[$type . '/*'] );
129 $encParamName = $this->encodeParamName( 'action' );
131 [ 'apierror-unrecognizedvalue', $encParamName, wfEscapeWikiText( $logAction ) ],
132 "unknown_$encParamName"
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'] );
142 $this->addTimestampWhereRange(
148 // Include in ORDER BY for uniqueness
149 $this->addWhereRange( 'log_id', $params['dir'], null, null );
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)"
164 $limit = $params['limit'];
165 $this->addOption( 'LIMIT', $limit +
1 );
167 $user = $params['user'];
168 if ( !is_null( $user ) ) {
169 $userid = User
::idFromName( $user );
171 $this->addWhereFld( 'log_user', $userid );
173 $this->addWhereFld( 'log_user_text', $user );
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 ) ] );
183 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
184 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
187 if ( $params['namespace'] !== null ) {
188 $this->addWhereFld( 'log_namespace', $params['namespace'] );
191 $prefix = $params['prefix'];
193 if ( !is_null( $prefix ) ) {
194 if ( $this->getConfig()->get( 'MiserMode' ) ) {
195 $this->dieWithError( 'apierror-prefixsearchdisabled' );
198 $title = Title
::newFromText( $prefix );
199 if ( is_null( $title ) ) {
200 $this->dieWithError( [ 'apierror-invalidtitle', wfEscapeWikiText( $prefix ) ] );
202 $this->addWhereFld( 'log_namespace', $title->getNamespace() );
203 $this->addWhere( 'log_title ' . $db->buildLike( $title->getDBkey(), $db->anyString() ) );
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
;
218 if ( ( $params['namespace'] !== null ||
!is_null( $title ) ) && $titleBits ) {
219 $this->addWhere( $db->bitAnd( 'log_deleted', $titleBits ) . " != $titleBits" );
221 if ( !is_null( $user ) && $userBits ) {
222 $this->addWhere( $db->bitAnd( 'log_deleted', $userBits ) . " != $userBits" );
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" );
237 $vals = $this->extractRowInfo( $row );
238 $fit = $result->addValue( [ 'query', $this->getModuleName() ], null, $vals );
240 $this->setContinueEnumParameter( 'continue', "$row->log_timestamp|$row->log_id" );
244 $result->addIndexedTagName( [ 'query', $this->getModuleName() ], 'item' );
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
255 * @param bool $legacy
258 public static function addLogParams( $result, &$vals, $params, $type,
259 $action, $ts, $legacy = false
261 wfDeprecated( __METHOD__
, '1.25' );
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();
273 private function extractRowInfo( $row ) {
274 $logEntry = DatabaseLogEntry
::newFromRow( $row );
276 ApiResult
::META_TYPE
=> 'assoc',
279 $user = $this->getUser();
281 if ( $this->fld_ids
) {
282 $vals['logid'] = intval( $row->log_id
);
285 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
286 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
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;
294 if ( LogEventsList
::userCan( $row, LogPage
::DELETED_ACTION
, $user ) ) {
295 if ( $this->fld_title
) {
296 ApiQueryBase
::addTitleInfo( $vals, $title );
298 if ( $this->fld_ids
) {
299 $vals['pageid'] = intval( $row->page_id
);
300 $vals['logpage'] = intval( $row->log_page
);
302 if ( $this->fld_details
) {
303 $vals['params'] = LogFormatter
::newFromEntry( $logEntry )->formatParametersForApi();
308 if ( $this->fld_type
) {
309 $vals['type'] = $row->log_type
;
310 $vals['action'] = $row->log_action
;
313 if ( $this->fld_user ||
$this->fld_userid
) {
314 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
315 $vals['userhidden'] = true;
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
;
322 if ( $this->fld_userid
) {
323 $vals['userid'] = intval( $row->log_user
);
326 if ( !$row->log_user
) {
327 $vals['anon'] = true;
331 if ( $this->fld_timestamp
) {
332 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
335 if ( $this->fld_comment ||
$this->fld_parsedcomment
) {
336 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
337 $vals['commenthidden'] = true;
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;
346 if ( $this->fld_parsedcomment
) {
347 $vals['parsedcomment'] = Linker
::formatComment( $comment, $title );
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;
362 if ( $anyHidden && LogEventsList
::isDeleted( $row, LogPage
::DELETED_RESTRICTED
) ) {
363 $vals['suppressed'] = true;
372 private function getAllowedLogActions() {
373 $config = $this->getConfig();
374 return array_keys( array_merge(
375 $config->get( 'LogActions' ),
376 $config->get( 'LogActionsHandlers' )
380 public function getCacheMode( $params ) {
381 if ( $this->userCanSeeRevDel() ) {
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.
392 return 'anon-public-user-private';
396 public function getAllowedParams( $flags = 0 ) {
397 $config = $this->getConfig();
400 ApiBase
::PARAM_ISMULTI
=> true,
401 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
402 ApiBase
::PARAM_TYPE
=> [
414 ApiBase
::PARAM_HELP_MSG_PER_VALUE
=> [],
417 ApiBase
::PARAM_TYPE
=> $config->get( 'LogTypes' )
420 // validation on request is done in execute()
421 ApiBase
::PARAM_TYPE
=> ( $flags & ApiBase
::GET_VALUES_FOR_HELP
)
422 ?
$this->getAllowedLogActions()
426 ApiBase
::PARAM_TYPE
=> 'timestamp'
429 ApiBase
::PARAM_TYPE
=> 'timestamp'
432 ApiBase
::PARAM_DFLT
=> 'older',
433 ApiBase
::PARAM_TYPE
=> [
437 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-direction',
440 ApiBase
::PARAM_TYPE
=> 'user',
444 ApiBase
::PARAM_TYPE
=> 'namespace',
445 ApiBase
::PARAM_EXTRA_NAMESPACES
=> [ NS_MEDIA
, NS_SPECIAL
],
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
457 ApiBase
::PARAM_HELP_MSG
=> 'api-help-param-continue',
461 if ( $config->get( 'MiserMode' ) ) {
462 $ret['prefix'][ApiBase
::PARAM_HELP_MSG
] = 'api-help-param-disabled-in-miser-mode';
468 protected function getExamplesMessages() {
470 'action=query&list=logevents'
471 => 'apihelp-query+logevents-example-simple',
475 public function getHelpUrls() {
476 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Logevents';