1d3ba3b9f31a02bd5f219995ae8f79ce33732b52
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.php
1 <?php
2
3 /*
4 * Created on Oct 16, 2006
5 *
6 * API for MediaWiki 1.8+
7 *
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
9 *
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.
14 *
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.
19 *
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
24 */
25
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
29 }
30
31 /**
32 * Query action to List the log events, with optional filtering by various parameters.
33 *
34 * @addtogroup API
35 */
36 class ApiQueryLogEvents extends ApiQueryBase {
37
38 public function __construct($query, $moduleName) {
39 parent :: __construct($query, $moduleName, 'le');
40 }
41
42 public function execute() {
43 $params = $this->extractRequestParams();
44 $db = $this->getDB();
45
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);
54
55 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
56
57 $hideLogs = LogEventsList::getExcludeClause($db);
58 if($hideLogs !== false)
59 $this->addWhere($hideLogs);
60
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 $this->addOption('USE INDEX', array('logging' => 'times'));
69
70 $this->addFields(array (
71 'log_type',
72 'log_action',
73 'log_timestamp',
74 ));
75
76 // FIXME: Fake out log_id for now until the column is live on Wikimedia
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);
85
86 $this->addWhereFld('log_deleted', 0);
87 $this->addWhereFld('log_type', $params['type']);
88 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
89
90 $limit = $params['limit'];
91 $this->addOption('LIMIT', $limit +1);
92
93 $user = $params['user'];
94 if (!is_null($user)) {
95 $userid = $db->selectField('user', 'user_id', array (
96 'user_name' => $user
97 ));
98 if (!$userid)
99 $this->dieUsage("User name $user not found", 'param_user');
100 $this->addWhereFld('log_user', $userid);
101 }
102
103 $title = $params['title'];
104 if (!is_null($title)) {
105 $titleObj = Title :: newFromText($title);
106 if (is_null($titleObj))
107 $this->dieUsage("Bad title value '$title'", 'param_title');
108 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
109 $this->addWhereFld('log_title', $titleObj->getDBkey());
110 }
111
112 $data = array ();
113 $count = 0;
114 $res = $this->select(__METHOD__);
115 while ($row = $db->fetchObject($res)) {
116 if (++ $count > $limit) {
117 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
118 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
119 break;
120 }
121
122 $vals = $this->extractRowInfo($row);
123 if($vals)
124 $data[] = $vals;
125 }
126 $db->freeResult($res);
127
128 $this->getResult()->setIndexedTagName($data, 'item');
129 $this->getResult()->addValue('query', $this->getModuleName(), $data);
130 }
131
132 private function extractRowInfo($row) {
133 $vals = array();
134
135 if ($this->fld_ids) {
136 // FIXME: Fake out log_id for now until the column is live on Wikimedia
137 // $vals['logid'] = intval($row->log_id);
138 $vals['logid'] = 0;
139 $vals['pageid'] = intval($row->page_id);
140 }
141
142 if ($this->fld_title) {
143 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
144 ApiQueryBase :: addTitleInfo($vals, $title);
145 }
146
147 if ($this->fld_type) {
148 $vals['type'] = $row->log_type;
149 $vals['action'] = $row->log_action;
150 }
151
152 if ($this->fld_details && $row->log_params !== '') {
153 $params = explode("\n", $row->log_params);
154 switch ($row->log_type) {
155 case 'move':
156 if (isset ($params[0])) {
157 $title = Title :: newFromText($params[0]);
158 if ($title) {
159 $vals2 = array();
160 ApiQueryBase :: addTitleInfo($vals2, $title, "new_");
161 $vals[$row->log_type] = $vals2;
162 $params = null;
163 }
164 }
165 break;
166 case 'patrol':
167 $vals2 = array();
168 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
169 $vals[$row->log_type] = $vals2;
170 $params = null;
171 break;
172 case 'rights':
173 $vals2 = array();
174 list( $vals2['old'], $vals2['new'] ) = $params;
175 $vals[$row->log_type] = $vals2;
176 $params = null;
177 break;
178 case 'block':
179 $vals2 = array();
180 list( $vals2['duration'], $vals2['flags'] ) = $params;
181 $vals[$row->log_type] = $vals2;
182 $params = null;
183 break;
184 }
185
186 if (isset($params)) {
187 $this->getResult()->setIndexedTagName($params, 'param');
188 $vals = array_merge($vals, $params);
189 }
190 }
191
192 if ($this->fld_user) {
193 $vals['user'] = $row->user_name;
194 if(!$row->log_user)
195 $vals['anon'] = '';
196 }
197 if ($this->fld_timestamp) {
198 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
199 }
200 if ($this->fld_comment && !empty ($row->log_comment)) {
201 $vals['comment'] = $row->log_comment;
202 }
203
204 return $vals;
205 }
206
207
208 public function getAllowedParams() {
209 global $wgLogTypes;
210 return array (
211 'prop' => array (
212 ApiBase :: PARAM_ISMULTI => true,
213 ApiBase :: PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
214 ApiBase :: PARAM_TYPE => array (
215 'ids',
216 'title',
217 'type',
218 'user',
219 'timestamp',
220 'comment',
221 'details',
222 )
223 ),
224 'type' => array (
225 ApiBase :: PARAM_TYPE => $wgLogTypes
226 ),
227 'start' => array (
228 ApiBase :: PARAM_TYPE => 'timestamp'
229 ),
230 'end' => array (
231 ApiBase :: PARAM_TYPE => 'timestamp'
232 ),
233 'dir' => array (
234 ApiBase :: PARAM_DFLT => 'older',
235 ApiBase :: PARAM_TYPE => array (
236 'newer',
237 'older'
238 )
239 ),
240 'user' => null,
241 'title' => null,
242 'limit' => array (
243 ApiBase :: PARAM_DFLT => 10,
244 ApiBase :: PARAM_TYPE => 'limit',
245 ApiBase :: PARAM_MIN => 1,
246 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
247 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
248 )
249 );
250 }
251
252 public function getParamDescription() {
253 return array (
254 'prop' => 'Which properties to get',
255 'type' => 'Filter log entries to only this type(s)',
256 'start' => 'The timestamp to start enumerating from.',
257 'end' => 'The timestamp to end enumerating.',
258 'dir' => 'In which direction to enumerate.',
259 'user' => 'Filter entries to those made by the given user.',
260 'title' => 'Filter entries to those related to a page.',
261 'limit' => 'How many total event entries to return.'
262 );
263 }
264
265 public function getDescription() {
266 return 'Get events from logs.';
267 }
268
269 protected function getExamples() {
270 return array (
271 'api.php?action=query&list=logevents'
272 );
273 }
274
275 public function getVersion() {
276 return __CLASS__ . ': $Id$';
277 }
278 }