9df96a2f2fd7e2471ff6da27da3d3cdb11523853
4 * Created on Oct 16, 2006
6 * API for MediaWiki 1.8+
8 * Copyright © 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 = array_flip( $params['prop'] );
48 $this->fld_ids
= isset( $prop['ids'] );
49 $this->fld_title
= isset( $prop['title'] );
50 $this->fld_type
= isset( $prop['type'] );
51 $this->fld_action
= isset ( $prop['action'] );
52 $this->fld_user
= isset( $prop['user'] );
53 $this->fld_timestamp
= isset( $prop['timestamp'] );
54 $this->fld_comment
= isset( $prop['comment'] );
55 $this->fld_parsedcomment
= isset ( $prop['parsedcomment'] );
56 $this->fld_details
= isset( $prop['details'] );
57 $this->fld_tags
= isset( $prop['tags'] );
59 list( $tbl_logging, $tbl_page, $tbl_user ) = $db->tableNamesN( 'logging', 'page', 'user' );
61 $hideLogs = LogEventsList
::getExcludeClause( $db );
62 if ( $hideLogs !== false ) {
63 $this->addWhere( $hideLogs );
66 // Order is significant here
67 $this->addTables( array( 'logging', 'user', 'page' ) );
68 $this->addOption( 'STRAIGHT_JOIN' );
69 $this->addJoinConds( array(
70 'user' => array( 'JOIN',
72 'page' => array( 'LEFT JOIN',
73 array( 'log_namespace=page_namespace',
74 'log_title=page_title' ) ) ) );
75 $index = array( 'logging' => 'times' ); // default, may change
77 $this->addFields( array(
84 $this->addFieldsIf( 'log_id', $this->fld_ids
);
85 $this->addFieldsIf( 'page_id', $this->fld_ids
);
86 $this->addFieldsIf( 'log_user', $this->fld_user
);
87 $this->addFieldsIf( 'user_name', $this->fld_user
);
88 $this->addFieldsIf( 'log_namespace', $this->fld_title
);
89 $this->addFieldsIf( 'log_title', $this->fld_title
);
90 $this->addFieldsIf( 'log_comment', $this->fld_comment ||
$this->fld_parsedcomment
);
91 $this->addFieldsIf( 'log_params', $this->fld_details
);
93 if ( $this->fld_tags
) {
94 $this->addTables( 'tag_summary' );
95 $this->addJoinConds( array( 'tag_summary' => array( 'LEFT JOIN', 'log_id=ts_log_id' ) ) );
96 $this->addFields( 'ts_tags' );
99 if ( !is_null( $params['tag'] ) ) {
100 $this->addTables( 'change_tag' );
101 $this->addJoinConds( array( 'change_tag' => array( 'INNER JOIN', array( 'log_id=ct_log_id' ) ) ) );
102 $this->addWhereFld( 'ct_tag', $params['tag'] );
103 global $wgOldChangeTagsIndex;
104 $index['change_tag'] = $wgOldChangeTagsIndex ?
'ct_tag' : 'change_tag_tag_id';
107 if ( !is_null( $params['action'] ) ) {
108 $this->addWhereFld( 'log_action', substr( $params['action'], strrpos( $params['action'], "/" ) +
1 ) );
110 else if ( !is_null( $params['type'] ) ) {
111 $this->addWhereFld( 'log_type', $params['type'] );
112 $index['logging'] = 'type_time';
115 $this->addWhereRange( 'log_timestamp', $params['dir'], $params['start'], $params['end'] );
117 $limit = $params['limit'];
118 $this->addOption( 'LIMIT', $limit +
1 );
120 $user = $params['user'];
121 if ( !is_null( $user ) ) {
122 $userid = User
::idFromName( $user );
124 $this->dieUsage( "User name $user not found", 'param_user' );
126 $this->addWhereFld( 'log_user', $userid );
127 $index['logging'] = 'user_time';
130 $title = $params['title'];
131 if ( !is_null( $title ) ) {
132 $titleObj = Title
::newFromText( $title );
133 if ( is_null( $titleObj ) ) {
134 $this->dieUsage( "Bad title value '$title'", 'param_title' );
136 $this->addWhereFld( 'log_namespace', $titleObj->getNamespace() );
137 $this->addWhereFld( 'log_title', $titleObj->getDBkey() );
139 // Use the title index in preference to the user index if there is a conflict
140 $index['logging'] = is_null( $user ) ?
'page_time' : array( 'page_time', 'user_time' );
143 $this->addOption( 'USE INDEX', $index );
145 // Paranoia: avoid brute force searches (bug 17342)
146 if ( !is_null( $title ) ) {
147 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_ACTION
) . ' = 0' );
149 if ( !is_null( $user ) ) {
150 $this->addWhere( $db->bitAnd( 'log_deleted', LogPage
::DELETED_USER
) . ' = 0' );
154 $res = $this->select( __METHOD__
);
155 while ( $row = $db->fetchObject( $res ) ) {
156 if ( ++
$count > $limit ) {
157 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
158 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
162 $vals = $this->extractRowInfo( $row );
166 $fit = $this->getResult()->addValue( array( 'query', $this->getModuleName() ), null, $vals );
168 $this->setContinueEnumParameter( 'start', wfTimestamp( TS_ISO_8601
, $row->log_timestamp
) );
172 $db->freeResult( $res );
174 $this->getResult()->setIndexedTagName_internal( array( 'query', $this->getModuleName() ), 'item' );
177 public static function addLogParams( $result, &$vals, $params, $type, $ts ) {
178 $params = explode( "\n", $params );
181 if ( isset( $params[0] ) ) {
182 $title = Title
::newFromText( $params[0] );
185 ApiQueryBase
::addTitleInfo( $vals2, $title, 'new_' );
186 $vals[$type] = $vals2;
189 if ( isset( $params[1] ) && $params[1] ) {
190 $vals[$type]['suppressedredirect'] = '';
196 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
197 $vals[$type] = $vals2;
202 list( $vals2['old'], $vals2['new'] ) = $params;
203 $vals[$type] = $vals2;
208 list( $vals2['duration'], $vals2['flags'] ) = $params;
209 $vals2['expiry'] = wfTimestamp( TS_ISO_8601
,
210 strtotime( $params[0], wfTimestamp( TS_UNIX
, $ts ) ) );
211 $vals[$type] = $vals2;
215 if ( !is_null( $params ) ) {
216 $result->setIndexedTagName( $params, 'param' );
217 $vals = array_merge( $vals, $params );
222 private function extractRowInfo( $row ) {
225 if ( $this->fld_ids
) {
226 $vals['logid'] = intval( $row->log_id
);
227 $vals['pageid'] = intval( $row->page_id
);
230 $title = Title
::makeTitle( $row->log_namespace
, $row->log_title
);
232 if ( $this->fld_title
) {
233 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
234 $vals['actionhidden'] = '';
236 ApiQueryBase
::addTitleInfo( $vals, $title );
240 if ( $this->fld_type ||
$this->fld_action
) {
241 $vals['type'] = $row->log_type
;
242 $vals['action'] = $row->log_action
;
245 if ( $this->fld_details
&& $row->log_params
!== '' ) {
246 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_ACTION
) ) {
247 $vals['actionhidden'] = '';
250 $this->getResult(), $vals,
251 $row->log_params
, $row->log_type
,
257 if ( $this->fld_user
) {
258 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_USER
) ) {
259 $vals['userhidden'] = '';
261 $vals['user'] = $row->user_name
;
262 if ( !$row->log_user
)
266 if ( $this->fld_timestamp
) {
267 $vals['timestamp'] = wfTimestamp( TS_ISO_8601
, $row->log_timestamp
);
270 if ( ( $this->fld_comment ||
$this->fld_parsedcomment
) && isset( $row->log_comment
) ) {
271 if ( LogEventsList
::isDeleted( $row, LogPage
::DELETED_COMMENT
) ) {
272 $vals['commenthidden'] = '';
274 if ( $this->fld_comment
) {
275 $vals['comment'] = $row->log_comment
;
278 if ( $this->fld_parsedcomment
) {
280 $vals['parsedcomment'] = $wgUser->getSkin()->formatComment( $row->log_comment
, $title );
285 if ( $this->fld_tags
) {
286 if ( $row->ts_tags
) {
287 $tags = explode( ',', $row->ts_tags
);
288 $this->getResult()->setIndexedTagName( $tags, 'tag' );
289 $vals['tags'] = $tags;
291 $vals['tags'] = array();
298 public function getAllowedParams() {
299 global $wgLogTypes, $wgLogActions;
302 ApiBase
::PARAM_ISMULTI
=> true,
303 ApiBase
::PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
304 ApiBase
::PARAM_TYPE
=> array(
317 ApiBase
::PARAM_TYPE
=> $wgLogTypes
320 ApiBase
::PARAM_TYPE
=> array_keys( $wgLogActions )
323 ApiBase
::PARAM_TYPE
=> 'timestamp'
326 ApiBase
::PARAM_TYPE
=> 'timestamp'
329 ApiBase
::PARAM_DFLT
=> 'older',
330 ApiBase
::PARAM_TYPE
=> array(
339 ApiBase
::PARAM_DFLT
=> 10,
340 ApiBase
::PARAM_TYPE
=> 'limit',
341 ApiBase
::PARAM_MIN
=> 1,
342 ApiBase
::PARAM_MAX
=> ApiBase
::LIMIT_BIG1
,
343 ApiBase
::PARAM_MAX2
=> ApiBase
::LIMIT_BIG2
348 public function getParamDescription() {
350 'prop' => 'Which properties to get',
351 'type' => 'Filter log entries to only this type(s)',
352 'action' => "Filter log actions to only this type. Overrides {$this->getModulePrefix()}type",
353 'start' => 'The timestamp to start enumerating from.',
354 'end' => 'The timestamp to end enumerating.',
355 'dir' => 'In which direction to enumerate.',
356 'user' => 'Filter entries to those made by the given user.',
357 'title' => 'Filter entries to those related to a page.',
358 'limit' => 'How many total event entries to return.',
359 'tag' => 'Only list event entries tagged with this tag.',
363 public function getDescription() {
364 return 'Get events from logs.';
367 public function getPossibleErrors() {
368 return array_merge( parent
::getPossibleErrors(), array(
369 array( 'code' => 'param_user', 'info' => 'User name $user not found' ),
370 array( 'code' => 'param_title', 'info' => 'Bad title value \'title\'' ),
374 protected function getExamples() {
376 'api.php?action=query&list=logevents'
380 public function getVersion() {
381 return __CLASS__
. ': $Id$';