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
= in_array('ids', $prop);
48 $this->fld_title
= in_array('title', $prop);
49 $this->fld_type
= in_array('type', $prop);
50 $this->fld_user
= in_array('user', $prop);
51 $this->fld_timestamp
= in_array('timestamp', $prop);
52 $this->fld_comment
= in_array('comment', $prop);
53 $this->fld_details
= in_array('details', $prop);
55 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
57 $hideLogs = LogEventsList
::getExcludeClause($db);
58 if($hideLogs !== false)
59 $this->addWhere($hideLogs);
61 // Order is significant here
62 $this->addTables(array('user', 'page', 'logging'));
63 $this->addJoinConds(array(
64 'page' => array('LEFT JOIN',
65 array( 'log_namespace=page_namespace',
66 'log_title=page_title'))));
67 $this->addWhere('user_id=log_user');
68 $index = 'times'; // default, may change
70 $this->addFields(array (
77 $this->addFieldsIf('log_id', $this->fld_ids
);
78 $this->addFieldsIf('page_id', $this->fld_ids
);
79 $this->addFieldsIf('log_user', $this->fld_user
);
80 $this->addFieldsIf('user_name', $this->fld_user
);
81 $this->addFieldsIf('log_namespace', $this->fld_title
);
82 $this->addFieldsIf('log_title', $this->fld_title
);
83 $this->addFieldsIf('log_comment', $this->fld_comment
);
84 $this->addFieldsIf('log_params', $this->fld_details
);
86 if( !is_null($params['type']) ) {
87 $this->addWhereFld('log_type', $params['type']);
91 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
93 $limit = $params['limit'];
94 $this->addOption('LIMIT', $limit +
1);
97 $user = $params['user'];
98 if (!is_null($user)) {
99 $userid = User
::idFromName($user);
101 $this->dieUsage("User name $user not found", 'param_user');
102 $this->addWhereFld('log_user', $userid);
103 $index = 'user_time';
106 $title = $params['title'];
107 if (!is_null($title)) {
108 $titleObj = Title
:: newFromText($title);
109 if (is_null($titleObj))
110 $this->dieUsage("Bad title value '$title'", 'param_title');
111 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
112 $this->addWhereFld('log_title', $titleObj->getDBkey());
114 // Use the title index in preference to the user index if there is a conflict
115 $index = 'page_time';
118 $this->addOption( 'USE INDEX', array( 'logging' => $index ) );
120 // Paranoia: avoid brute force searches (bug 17342)
121 if (!is_null($title)) {
122 $this->addWhere('log_deleted & ' . LogPage
::DELETED_ACTION
. ' = 0');
124 if (!is_null($user)) {
125 $this->addWhere('log_deleted & ' . LogPage
::DELETED_USER
. ' = 0');
129 $res = $this->select(__METHOD__
);
130 while ($row = $db->fetchObject($res)) {
131 if (++
$count > $limit) {
132 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
133 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->log_timestamp
));
137 $vals = $this->extractRowInfo($row);
140 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
143 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->log_timestamp
));
147 $db->freeResult($res);
149 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
152 public static function addLogParams($result, &$vals, $params, $type, $ts) {
153 $params = explode("\n", $params);
156 if (isset ($params[0])) {
157 $title = Title
:: newFromText($params[0]);
160 ApiQueryBase
:: addTitleInfo($vals2, $title, "new_");
161 $vals[$type] = $vals2;
168 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
169 $vals[$type] = $vals2;
174 list( $vals2['old'], $vals2['new'] ) = $params;
175 $vals[$type] = $vals2;
180 list( $vals2['duration'], $vals2['flags'] ) = $params;
181 $vals2['expiry'] = wfTimestamp(TS_ISO_8601
,
182 strtotime($params[0], wfTimestamp(TS_UNIX
, $ts)));
183 $vals[$type] = $vals2;
187 if (!is_null($params)) {
188 $result->setIndexedTagName($params, 'param');
189 $vals = array_merge($vals, $params);
194 private function extractRowInfo($row) {
197 if ($this->fld_ids
) {
198 $vals['logid'] = intval($row->log_id
);
199 $vals['pageid'] = intval($row->page_id
);
202 if ($this->fld_title
) {
203 if (LogEventsList
::isDeleted($row, LogPage
::DELETED_ACTION
)) {
204 $vals['actionhidden'] = '';
206 $title = Title
:: makeTitle($row->log_namespace
, $row->log_title
);
207 ApiQueryBase
:: addTitleInfo($vals, $title);
211 if ($this->fld_type
) {
212 $vals['type'] = $row->log_type
;
213 $vals['action'] = $row->log_action
;
216 if ($this->fld_details
&& $row->log_params
!== '') {
217 if (LogEventsList
::isDeleted($row, LogPage
::DELETED_ACTION
)) {
218 $vals['actionhidden'] = '';
220 self
::addLogParams($this->getResult(), $vals,
221 $row->log_params
, $row->log_type
,
222 $row->log_timestamp
);
226 if ($this->fld_user
) {
227 if (LogEventsList
::isDeleted($row, LogPage
::DELETED_USER
)) {
228 $vals['userhidden'] = '';
230 $vals['user'] = $row->user_name
;
235 if ($this->fld_timestamp
) {
236 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->log_timestamp
);
238 if ($this->fld_comment
&& isset($row->log_comment
)) {
239 if (LogEventsList
::isDeleted($row, LogPage
::DELETED_COMMENT
)) {
240 $vals['commenthidden'] = '';
242 $vals['comment'] = $row->log_comment
;
250 public function getAllowedParams() {
254 ApiBase
:: PARAM_ISMULTI
=> true,
255 ApiBase
:: PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
256 ApiBase
:: PARAM_TYPE
=> array (
267 ApiBase
:: PARAM_TYPE
=> $wgLogTypes
270 ApiBase
:: PARAM_TYPE
=> 'timestamp'
273 ApiBase
:: PARAM_TYPE
=> 'timestamp'
276 ApiBase
:: PARAM_DFLT
=> 'older',
277 ApiBase
:: PARAM_TYPE
=> array (
285 ApiBase
:: PARAM_DFLT
=> 10,
286 ApiBase
:: PARAM_TYPE
=> 'limit',
287 ApiBase
:: PARAM_MIN
=> 1,
288 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
289 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
294 public function getParamDescription() {
296 'prop' => 'Which properties to get',
297 'type' => 'Filter log entries to only this type(s)',
298 'start' => 'The timestamp to start enumerating from.',
299 'end' => 'The timestamp to end enumerating.',
300 'dir' => 'In which direction to enumerate.',
301 'user' => 'Filter entries to those made by the given user.',
302 'title' => 'Filter entries to those related to a page.',
303 'limit' => 'How many total event entries to return.'
307 public function getDescription() {
308 return 'Get events from logs.';
311 protected function getExamples() {
313 'api.php?action=query&list=logevents'
317 public function getVersion() {
318 return __CLASS__
. ': $Id$';