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 $this->addOption('STRAIGHT_JOIN');
58 $this->addTables("$tbl_logging LEFT OUTER JOIN $tbl_page ON " .
59 "log_namespace=page_namespace AND log_title=page_title " .
60 "INNER JOIN $tbl_user ON user_id=log_user");
62 $this->addFields(array (
68 // FIXME: Fake out log_id for now until the column is live on Wikimedia
69 // $this->addFieldsIf('log_id', $this->fld_ids);
70 $this->addFieldsIf('page_id', $this->fld_ids
);
71 $this->addFieldsIf('log_user', $this->fld_user
);
72 $this->addFieldsIf('user_name', $this->fld_user
);
73 $this->addFieldsIf('log_namespace', $this->fld_title
);
74 $this->addFieldsIf('log_title', $this->fld_title
);
75 $this->addFieldsIf('log_comment', $this->fld_comment
);
76 $this->addFieldsIf('log_params', $this->fld_details
);
79 $this->addWhereFld('log_deleted', 0);
80 $this->addWhereFld('log_type', $params['type']);
81 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
83 $limit = $params['limit'];
84 $this->addOption('LIMIT', $limit +
1);
86 $user = $params['user'];
87 if (!is_null($user)) {
88 $userid = $db->selectField('user', 'user_id', array (
92 $this->dieUsage("User name $user not found", 'param_user');
93 $this->addWhereFld('log_user', $userid);
96 $title = $params['title'];
97 if (!is_null($title)) {
98 $titleObj = Title
:: newFromText($title);
99 if (is_null($titleObj))
100 $this->dieUsage("Bad title value '$title'", 'param_title');
101 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
102 $this->addWhereFld('log_title', $titleObj->getDBkey());
107 $res = $this->select(__METHOD__
);
108 while ($row = $db->fetchObject($res)) {
109 if (++
$count > $limit) {
110 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
111 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->log_timestamp
));
115 $vals = $this->extractRowInfo($row);
119 $db->freeResult($res);
121 $this->getResult()->setIndexedTagName($data, 'item');
122 $this->getResult()->addValue('query', $this->getModuleName(), $data);
125 private function extractRowInfo($row) {
128 if ($this->fld_ids
) {
129 // FIXME: Fake out log_id for now until the column is live on Wikimedia
130 // $vals['logid'] = intval($row->log_id);
132 $vals['pageid'] = intval($row->page_id
);
135 if ($this->fld_title
) {
136 $title = Title
:: makeTitle($row->log_namespace
, $row->log_title
);
137 ApiQueryBase
:: addTitleInfo($vals, $title);
140 if ($this->fld_type
) {
141 $vals['type'] = $row->log_type
;
142 $vals['action'] = $row->log_action
;
145 if ($this->fld_details
&& $row->log_params
!== '') {
146 $params = explode("\n", $row->log_params
);
147 switch ($row->log_type
) {
149 if (isset ($params[0])) {
150 $title = Title
:: newFromText($params[0]);
153 ApiQueryBase
:: addTitleInfo($vals2, $title, "new_");
154 $vals[$row->log_type
] = $vals2;
161 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
162 $vals[$row->log_type
] = $vals2;
167 list( $vals2['old'], $vals2['new'] ) = $params;
168 $vals[$row->log_type
] = $vals2;
173 list( $vals2['duration'], $vals2['flags'] ) = $params;
174 $vals[$row->log_type
] = $vals2;
179 if (isset($params)) {
180 $this->getResult()->setIndexedTagName($params, 'param');
181 $vals = array_merge($vals, $params);
185 if ($this->fld_user
) {
186 $vals['user'] = $row->user_name
;
190 if ($this->fld_timestamp
) {
191 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->log_timestamp
);
193 if ($this->fld_comment
&& !empty ($row->log_comment
)) {
194 $vals['comment'] = $row->log_comment
;
201 public function getAllowedParams() {
205 ApiBase
:: PARAM_ISMULTI
=> true,
206 ApiBase
:: PARAM_DFLT
=> 'ids|title|type|user|timestamp|comment|details',
207 ApiBase
:: PARAM_TYPE
=> array (
218 ApiBase
:: PARAM_ISMULTI
=> true,
219 ApiBase
:: PARAM_TYPE
=> $wgLogTypes
222 ApiBase
:: PARAM_TYPE
=> 'timestamp'
225 ApiBase
:: PARAM_TYPE
=> 'timestamp'
228 ApiBase
:: PARAM_DFLT
=> 'older',
229 ApiBase
:: PARAM_TYPE
=> array (
237 ApiBase
:: PARAM_DFLT
=> 10,
238 ApiBase
:: PARAM_TYPE
=> 'limit',
239 ApiBase
:: PARAM_MIN
=> 1,
240 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
241 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
246 public function getParamDescription() {
248 'prop' => 'Which properties to get',
249 'type' => 'Filter log entries to only this type(s)',
250 'start' => 'The timestamp to start enumerating from.',
251 'end' => 'The timestamp to end enumerating.',
252 'dir' => 'In which direction to enumerate.',
253 'user' => 'Filter entries to those made by the given user.',
254 'title' => 'Filter entries to those related to a page.',
255 'limit' => 'How many total event entries to return.'
259 public function getDescription() {
260 return 'Get events from logs.';
263 protected function getExamples() {
265 'api.php?action=query&list=logevents'
269 public function getVersion() {
270 return __CLASS__
. ': $Id$';