5 * Created on Oct 16, 2006
7 * Copyright © 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( 'ApiQueryBase.php' );
33 * Query action to List the log events, with optional filtering by various parameters.
37 class ApiQueryLogEvents
extends ApiQueryBase
{
39 public function __construct( $query, $moduleName ) {
40 parent
::__construct( $query, $moduleName, 'le' );
43 private $fld_ids = false, $fld_title = false, $fld_type = false,
44 $fld_action = false, $fld_user = false, $fld_userid = false,
45 $fld_timestamp = false, $fld_comment = false, $fld_parsedcomment = false,
46 $fld_details = false, $fld_tags = false;
48 public function execute() {
49 $params = $this->extractRequestParams();
52 $prop = array_flip( $params['prop'] );
54 $this->fld_ids
= isset( $prop['ids'] );
55 $this->fld_title
= isset( $prop['title'] );
56 $this->fld_type
= isset( $prop['type'] );
57 $this->fld_action
= isset ( $prop['action'] );
58 $this->fld_user
= isset( $prop['user'] );
59 $this->fld_userid
= isset( $prop['userid'] );
60 $this->fld_timestamp
= isset( $prop['timestamp'] );
61 $this->fld_comment
= isset( $prop['comment'] );
62 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
63 $this->fld_details
= isset( $prop['details'] );
64 $this->fld_tags
= isset( $prop['tags'] );
66 $hideLogs = LogEventsList
::getExcludeClause( $db );
67 if ( $hideLogs !== false ) {
68 $this->addWhere( $hideLogs );
71 // Order is significant here
72 $this->addTables( array( 'logging', 'user', 'page' ) );
73 $this->addOption( 'STRAIGHT_JOIN' );
74 $this->addJoinConds( array(
75 'user' => array( 'JOIN',
77 'page' => array( 'LEFT JOIN',
78 array( 'log_namespace=page_namespace',
79 'log_title=page_title' ) ) ) );
80 $index = array( 'logging' => 'times' ); // default, may change
82 $this->addFields( array(
89 $this->addFieldsIf( 'log_id', $this->fld_ids
);
90 $this->addFieldsIf( 'page_id', $this->fld_ids
);
91 $this->addFieldsIf( 'log_user', $this->fld_user
);
92 $this->addFieldsIf( 'user_name', $this->fld_user
);
93 $this->addFieldsIf( 'user_id', $this->fld_userid
);
94 $this->addFieldsIf( 'log_namespace', $this->fld_title ||
$this->fld_parsedcomment
);
95 $this->addFieldsIf( 'log_title', $this->fld_title ||
$this->fld_parsedcomment
);
96 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
97 $this->addFieldsIf( 'log_params', $this->fld_details
);
99 if ( $this->fld_tags
) {
100 $this->addTables( 'tag_summary' );
101 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
102 $this->addFields( 'ts_tags' );
105 if ( !is_null( $params['tag'] ) ) {
106 $this->addTables( 'change_tag' );
107 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
108 $this->addWhereFld( 'ct_tag', $params['tag'] );
109 global $wgOldChangeTagsIndex;
110 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
113 if ( !is_null( $params['action'] ) ) {
114 list( $type, $action ) = explode( '/', $params['action'] );
115 $this->addWhereFld( 'log_type', $type );
116 $this->addWhereFld( 'log_action', $action );
118 else if ( !is_null( $params['type'] ) ) {
119 $this->addWhereFld( 'log_type', $params['type'] );
120 $index['logging'] = 'type_time';
123 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
125 $limit = $params['limit'];
126 $this->addOption( 'LIMIT', $limit +
1 );
128 $user = $params['user'];
129 if ( !is_null( $user ) ) {
130 $userid = User
::idFromName( $user );
132 $this->dieUsage( "User name $user not found", 'param_user' );
134 $this->addWhereFld( 'log_user', $userid );
135 $index['logging'] = 'user_time';
138 $title = $params['title'];
139 if ( !is_null( $title ) ) {
140 $titleObj = Title
::newFromText( $title );
141 if ( is_null( $titleObj ) ) {
142 $this->dieUsage( "Bad title value '$title'", 'param_title' );
144 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
145 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
147 // Use the title index in preference to the user index if there is a conflict
148 $index['logging'] = is_null( $user ) ?
'page_time' : array( 'page_time', 'user_time' );
151 $this->addOption( 'USE INDEX', $index );
153 // Paranoia: avoid brute force searches (bug 17342)
154 if ( !is_null( $title ) ) {
155 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0' );
157 if ( !is_null( $user ) ) {
158 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0' );
162 $res = $this->select( __METHOD__
);
163 foreach ( $res as $row ) {
164 if ( ++
$count > $limit ) {
165 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
166 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
170 $vals = $this->extractRowInfo( $row );
174 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
176 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
180 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
185 * @param $result ApiResult
192 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
193 $params = explode( "\n", $params );
196 if ( isset( $params[0] ) ) {
197 $title = Title
::newFromText( $params[0] );
200 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
201 $vals[$type] = $vals2;
204 if ( isset( $params[1] ) && $params[1] ) {
205 $vals[$type]['suppressedredirect'] = '';
211 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
212 $vals[$type] = $vals2;
217 list( $vals2['old'], $vals2['new'] ) = $params;
218 $vals[$type] = $vals2;
223 list( $vals2['duration'], $vals2['flags'] ) = $params;
225 // Indefinite blocks have no expiry time
226 if ( Block
::parseExpiryInput( $params[0] ) !== Block
::infinity() ) {
227 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
228 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
230 $vals[$type] = $vals2;
234 if ( !is_null( $params ) ) {
235 $result->setIndexedTagName( $params, 'param' );
236 $vals = array_merge( $vals, $params );
241 private function extractRowInfo( $row ) {
244 if ( $this->fld_ids
) {
245 $vals['logid'] = intval( $row->log_id
);
246 $vals['pageid'] = intval( $row->page_id
);
249 if ( $this->fld_title ||
$this->fld_parsedcomment
) {
250 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
253 if ( $this->fld_title
) {
254 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
255 $vals['actionhidden'] = '';
257 ApiQueryBase
::addTitleInfo( $vals, $title );
261 if ( $this->fld_type ||
$this->fld_action
) {
262 $vals['type'] = $row->log_type
;
263 $vals['action'] = $row->log_action
;
266 if ( $this->fld_details
&& $row->log_params
!== '' ) {
267 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
268 $vals['actionhidden'] = '';
271 $this->getResult(), $vals,
272 $row->log_params
, $row->log_type
,
278 if ( $this->fld_user ||
$this->fld_userid
) {
279 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
280 $vals['userhidden'] = '';
282 if ( $this->fld_user
) {
283 $vals['user'] = $row->user_name
;
285 if ( $this->fld_userid
) {
286 $vals['userid'] = $row->user_id
;
289 if ( !$row->log_user
) {
294 if ( $this->fld_timestamp
) {
295 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
298 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
299 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
300 $vals['commenthidden'] = '';
302 if ( $this->fld_comment
) {
303 $vals['comment'] = $row->log_comment
;
306 if ( $this->fld_parsedcomment
) {
308 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment
, $title );
313 if ( $this->fld_tags
) {
314 if ( $row->ts_tags
) {
315 $tags = explode( ',', $row->ts_tags
);
316 $this->getResult()->setIndexedTagName( $tags, 'tag' );
317 $vals['tags'] = $tags;
319 $vals['tags'] = array();
326 public function getCacheMode( $params ) {
327 if ( !is_null( $params['prop'] ) && in_array( 'parsedcomment', $params['prop'] ) ) {
328 // formatComment() calls wfMsg() among other things
329 return 'anon-public-user-private';
335 public function getAllowedParams() {
336 global $wgLogTypes, $wgLogActions;
339 ApiBase
::PARAM_ISMULTI
=> true,
340 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
341 ApiBase
::PARAM_TYPE
=> array(
355 ApiBase
::PARAM_TYPE
=> $wgLogTypes
358 ApiBase
::PARAM_TYPE
=> array_keys( $wgLogActions )
361 ApiBase
::PARAM_TYPE
=> 'timestamp'
364 ApiBase
::PARAM_TYPE
=> 'timestamp'
367 ApiBase
::PARAM_DFLT
=> 'older',
368 ApiBase
::PARAM_TYPE
=> array(
377 ApiBase
::PARAM_DFLT
=> 10,
378 ApiBase
::PARAM_TYPE
=> 'limit',
379 ApiBase
::PARAM_MIN
=> 1,
380 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
381 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
386 public function getParamDescription() {
389 'Which properties to get',
390 ' ids - Adds the ID of the log event',
391 ' title - Adds the title of the page for the log event',
392 ' type - Adds the type of log event',
393 ' user - Adds the user responsible for the log event',
394 ' userid - Adds the user ID who was responsible for the log event',
395 ' timestamp - Adds the timestamp for the event',
396 ' comment - Adds the comment of the event',
397 ' parsedcomment - Adds the parsed comment of the event',
398 ' details - Lists addtional details about the event',
399 ' tags - Lists tags for the event',
401 'type' => 'Filter log entries to only this type(s)',
402 'action' => "Filter log actions to only this type. Overrides {$this->getModulePrefix()}type",
403 'start' => 'The timestamp to start enumerating from',
404 'end' => 'The timestamp to end enumerating',
405 'dir' => 'In which direction to enumerate',
406 'user' => 'Filter entries to those made by the given user',
407 'title' => 'Filter entries to those related to a page',
408 'limit' => 'How many total event entries to return',
409 'tag' => 'Only list event entries tagged with this tag',
413 public function getDescription() {
414 return 'Get events from logs';
417 public function getPossibleErrors() {
418 return array_merge( parent
::getPossibleErrors(), array(
419 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
420 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
424 protected function getExamples() {
426 'api.php?action=query&list=logevents'
430 public function getVersion() {
431 return __CLASS__
. ': $Id$';