4 * Created on Oct 16, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License along
21 * with this program; if not, write to the Free Software Foundation, Inc.,
22 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 * http://www.gnu.org/copyleft/gpl.html
26 if ( !defined( 'MEDIAWIKI' ) ) {
27 // Eclipse helper - will be ignored in production
28 require_once ( 'ApiQueryBase.php' );
32 * Query action to List the log events, with optional filtering by various parameters.
36 class ApiQueryLogEvents
extends ApiQueryBase
{
38 public function __construct( $query, $moduleName ) {
39 parent
:: __construct( $query, $moduleName, 'le' );
42 public function execute() {
43 $params = $this->extractRequestParams();
46 $prop = $params['prop'];
47 $this->fld_ids
= isset( $prop['ids'] );
48 $this->fld_title
= isset( $prop['title'] );
49 $this->fld_type
= isset( $prop['type'] );
50 $this->fld_user
= isset( $prop['user'] );
51 $this->fld_timestamp
= isset( $prop['timestamp'] );
52 $this->fld_comment
= isset( $prop['comment'] );
53 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
54 $this->fld_details
= isset( $prop['details'] );
55 $this->fld_tags
= isset( $prop['tags'] );
57 list( $tbl_logging, $tbl_page, $tbl_user ) = $db->tableNamesN( 'logging', 'page', 'user' );
59 $hideLogs = LogEventsList
::getExcludeClause( $db );
60 if ( $hideLogs !== false )
61 $this->addWhere( $hideLogs );
63 // Order is significant here
64 $this->addTables( array( 'logging', 'user', 'page' ) );
65 $this->addOption( 'STRAIGHT_JOIN' );
66 $this->addJoinConds( array(
67 'user' => array( 'JOIN',
69 'page' => array( 'LEFT JOIN',
70 array( 'log_namespace=page_namespace',
71 'log_title=page_title' ) ) ) );
72 $index['logging'] = 'times'; // default, may change
74 $this->addFields( array (
81 $this->addFieldsIf( 'log_id', $this->fld_ids
);
82 $this->addFieldsIf( 'page_id', $this->fld_ids
);
83 $this->addFieldsIf( 'log_user', $this->fld_user
);
84 $this->addFieldsIf( 'user_name', $this->fld_user
);
85 $this->addFieldsIf( 'log_namespace', $this->fld_title
);
86 $this->addFieldsIf( 'log_title', $this->fld_title
);
87 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
88 $this->addFieldsIf( 'log_params', $this->fld_details
);
90 if ( $this->fld_tags
) {
91 $this->addTables( 'tag_summary' );
92 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
93 $this->addFields( 'ts_tags' );
96 if ( !is_null( $params['tag'] ) ) {
97 $this->addTables( 'change_tag' );
98 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
99 $this->addWhereFld( 'ct_tag', $params['tag'] );
100 global $wgOldChangeTagsIndex;
101 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
104 if ( !is_null( $params['type'] ) ) {
105 $this->addWhereFld( 'log_type', $params['type'] );
106 $index['logging'] = 'type_time';
109 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
111 $limit = $params['limit'];
112 $this->addOption( 'LIMIT', $limit +
1 );
114 $user = $params['user'];
115 if ( !is_null( $user ) ) {
116 $userid = User
::idFromName( $user );
118 $this->dieUsage( "User name $user not found", 'param_user' );
119 $this->addWhereFld( 'log_user', $userid );
120 $index['logging'] = 'user_time';
123 $title = $params['title'];
124 if ( !is_null( $title ) ) {
125 $titleObj = Title
:: newFromText( $title );
126 if ( is_null( $titleObj ) )
127 $this->dieUsage( "Bad title value '$title'", 'param_title' );
128 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
129 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
131 // Use the title index in preference to the user index if there is a conflict
132 $index['logging'] = is_null( $user ) ?
'page_time' : array( 'page_time', 'user_time' );
135 $this->addOption( 'USE INDEX', $index );
137 // Paranoia: avoid brute force searches (bug 17342)
138 if ( !is_null( $title ) ) {
139 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0' );
141 if ( !is_null( $user ) ) {
142 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0' );
146 $res = $this->select( __METHOD__
);
147 while ( $row = $db->fetchObject( $res ) ) {
148 if ( ++
$count > $limit ) {
149 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
150 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
154 $vals = $this->extractRowInfo( $row );
157 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
160 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
164 $db->freeResult( $res );
166 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
169 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
170 $params = explode( "\n", $params );
173 if ( isset ( $params[0] ) ) {
174 $title = Title
:: newFromText( $params[0] );
177 ApiQueryBase
:: addTitleInfo( $vals2, $title, "new_" );
178 $vals[$type] = $vals2;
181 if ( isset ( $params[1] ) && $params[1] ) {
182 $vals[$type]['suppressedredirect'] = '';
188 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
189 $vals[$type] = $vals2;
194 list( $vals2['old'], $vals2['new'] ) = $params;
195 $vals[$type] = $vals2;
200 list( $vals2['duration'], $vals2['flags'] ) = $params;
201 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
202 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
203 $vals[$type] = $vals2;
207 if ( !is_null( $params ) ) {
208 $result->setIndexedTagName( $params, 'param' );
209 $vals = array_merge( $vals, $params );
214 private function extractRowInfo( $row ) {
217 if ( $this->fld_ids
) {
218 $vals['logid'] = intval( $row->log_id
);
219 $vals['pageid'] = intval( $row->page_id
);
222 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
224 if ( $this->fld_title
) {
225 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
226 $vals['actionhidden'] = '';
228 ApiQueryBase
::addTitleInfo( $vals, $title );
232 if ( $this->fld_type
) {
233 $vals['type'] = $row->log_type
;
234 $vals['action'] = $row->log_action
;
237 if ( $this->fld_details
&& $row->log_params
!== '' ) {
238 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
239 $vals['actionhidden'] = '';
241 self
::addLogParams( $this->getResult(), $vals,
242 $row->log_params
, $row->log_type
,
243 $row->log_timestamp
);
247 if ( $this->fld_user
) {
248 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
249 $vals['userhidden'] = '';
251 $vals['user'] = $row->user_name
;
252 if ( !$row->log_user
)
256 if ( $this->fld_timestamp
) {
257 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
260 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
261 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
262 $vals['commenthidden'] = '';
264 if ( $this->fld_comment
)
265 $vals['comment'] = $row->log_comment
;
267 if ( $this->fld_parsedcomment
) {
269 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment
, $title );
274 if ( $this->fld_tags
) {
275 if ( $row->ts_tags
) {
276 $tags = explode( ',', $row->ts_tags
);
277 $this->getResult()->setIndexedTagName( $tags, 'tag' );
278 $vals['tags'] = $tags;
280 $vals['tags'] = array();
288 public function getAllowedParams() {
292 ApiBase
:: PARAM_ISMULTI
=> true,
293 ApiBase
:: PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
294 ApiBase
:: PARAM_TYPE
=> array (
307 ApiBase
:: PARAM_TYPE
=> $wgLogTypes
310 ApiBase
:: PARAM_TYPE
=> 'timestamp'
313 ApiBase
:: PARAM_TYPE
=> 'timestamp'
316 ApiBase
:: PARAM_DFLT
=> 'older',
317 ApiBase
:: PARAM_TYPE
=> array (
326 ApiBase
:: PARAM_DFLT
=> 10,
327 ApiBase
:: PARAM_TYPE
=> 'limit',
328 ApiBase
:: PARAM_MIN
=> 1,
329 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
330 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
335 public function getParamDescription() {
337 'prop' => 'Which properties to get',
338 'type' => 'Filter log entries to only this type(s)',
339 'start' => 'The timestamp to start enumerating from.',
340 'end' => 'The timestamp to end enumerating.',
341 'dir' => 'In which direction to enumerate.',
342 'user' => 'Filter entries to those made by the given user.',
343 'title' => 'Filter entries to those related to a page.',
344 'limit' => 'How many total event entries to return.',
345 'tag' => 'Only list event entries tagged with this tag.',
349 public function getDescription() {
350 return 'Get events from logs.';
353 protected function getExamples() {
355 'api.php?action=query&list=logevents'
359 public function getVersion() {
360 return __CLASS__
. ': $Id$';