log_id here too
[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')); // default, may change
69
70 $this->addFields(array (
71 'log_type',
72 'log_action',
73 'log_timestamp',
74 ));
75
76 $this->addFieldsIf('log_id', $this->fld_ids);
77 $this->addFieldsIf('page_id', $this->fld_ids);
78 $this->addFieldsIf('log_user', $this->fld_user);
79 $this->addFieldsIf('user_name', $this->fld_user);
80 $this->addFieldsIf('log_namespace', $this->fld_title);
81 $this->addFieldsIf('log_title', $this->fld_title);
82 $this->addFieldsIf('log_comment', $this->fld_comment);
83 $this->addFieldsIf('log_params', $this->fld_details);
84
85 $this->addWhereFld('log_deleted', 0);
86
87 if( !is_null($params['type']) ) {
88 $this->addWhereFld('log_type', $params['type']);
89 $this->addOption('USE INDEX', array('logging' => array('type_time')));
90 }
91
92 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
93
94 $limit = $params['limit'];
95 $this->addOption('LIMIT', $limit +1);
96
97 $user = $params['user'];
98 if (!is_null($user)) {
99 $userid = $db->selectField('user', 'user_id', array (
100 'user_name' => $user
101 ));
102 if (!$userid)
103 $this->dieUsage("User name $user not found", 'param_user');
104 $this->addWhereFld('log_user', $userid);
105 $this->addOption('USE INDEX', array('logging' => array('user_time','page_time')));
106 }
107
108 $title = $params['title'];
109 if (!is_null($title)) {
110 $titleObj = Title :: newFromText($title);
111 if (is_null($titleObj))
112 $this->dieUsage("Bad title value '$title'", 'param_title');
113 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
114 $this->addWhereFld('log_title', $titleObj->getDBkey());
115 $this->addOption('USE INDEX', array('logging' => array('user_time','page_time')));
116 }
117
118 $data = array ();
119 $count = 0;
120 $res = $this->select(__METHOD__);
121 while ($row = $db->fetchObject($res)) {
122 if (++ $count > $limit) {
123 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
124 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
125 break;
126 }
127
128 $vals = $this->extractRowInfo($row);
129 if($vals)
130 $data[] = $vals;
131 }
132 $db->freeResult($res);
133
134 $this->getResult()->setIndexedTagName($data, 'item');
135 $this->getResult()->addValue('query', $this->getModuleName(), $data);
136 }
137
138 private function extractRowInfo($row) {
139 $vals = array();
140
141 if ($this->fld_ids) {
142 $vals['logid'] = intval($row->log_id);
143 $vals['pageid'] = intval($row->page_id);
144 }
145
146 if ($this->fld_title) {
147 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
148 ApiQueryBase :: addTitleInfo($vals, $title);
149 }
150
151 if ($this->fld_type) {
152 $vals['type'] = $row->log_type;
153 $vals['action'] = $row->log_action;
154 }
155
156 if ($this->fld_details && $row->log_params !== '') {
157 $params = explode("\n", $row->log_params);
158 switch ($row->log_type) {
159 case 'move':
160 if (isset ($params[0])) {
161 $title = Title :: newFromText($params[0]);
162 if ($title) {
163 $vals2 = array();
164 ApiQueryBase :: addTitleInfo($vals2, $title, "new_");
165 $vals[$row->log_type] = $vals2;
166 $params = null;
167 }
168 }
169 break;
170 case 'patrol':
171 $vals2 = array();
172 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
173 $vals[$row->log_type] = $vals2;
174 $params = null;
175 break;
176 case 'rights':
177 $vals2 = array();
178 list( $vals2['old'], $vals2['new'] ) = $params;
179 $vals[$row->log_type] = $vals2;
180 $params = null;
181 break;
182 case 'block':
183 $vals2 = array();
184 list( $vals2['duration'], $vals2['flags'] ) = $params;
185 $vals[$row->log_type] = $vals2;
186 $params = null;
187 break;
188 }
189
190 if (isset($params)) {
191 $this->getResult()->setIndexedTagName($params, 'param');
192 $vals = array_merge($vals, $params);
193 }
194 }
195
196 if ($this->fld_user) {
197 $vals['user'] = $row->user_name;
198 if(!$row->log_user)
199 $vals['anon'] = '';
200 }
201 if ($this->fld_timestamp) {
202 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
203 }
204 if ($this->fld_comment && !empty ($row->log_comment)) {
205 $vals['comment'] = $row->log_comment;
206 }
207
208 return $vals;
209 }
210
211
212 public function getAllowedParams() {
213 global $wgLogTypes;
214 return array (
215 'prop' => array (
216 ApiBase :: PARAM_ISMULTI => true,
217 ApiBase :: PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
218 ApiBase :: PARAM_TYPE => array (
219 'ids',
220 'title',
221 'type',
222 'user',
223 'timestamp',
224 'comment',
225 'details',
226 )
227 ),
228 'type' => array (
229 ApiBase :: PARAM_TYPE => $wgLogTypes
230 ),
231 'start' => array (
232 ApiBase :: PARAM_TYPE => 'timestamp'
233 ),
234 'end' => array (
235 ApiBase :: PARAM_TYPE => 'timestamp'
236 ),
237 'dir' => array (
238 ApiBase :: PARAM_DFLT => 'older',
239 ApiBase :: PARAM_TYPE => array (
240 'newer',
241 'older'
242 )
243 ),
244 'user' => null,
245 'title' => null,
246 'limit' => array (
247 ApiBase :: PARAM_DFLT => 10,
248 ApiBase :: PARAM_TYPE => 'limit',
249 ApiBase :: PARAM_MIN => 1,
250 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
251 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
252 )
253 );
254 }
255
256 public function getParamDescription() {
257 return array (
258 'prop' => 'Which properties to get',
259 'type' => 'Filter log entries to only this type(s)',
260 'start' => 'The timestamp to start enumerating from.',
261 'end' => 'The timestamp to end enumerating.',
262 'dir' => 'In which direction to enumerate.',
263 'user' => 'Filter entries to those made by the given user.',
264 'title' => 'Filter entries to those related to a page.',
265 'limit' => 'How many total event entries to return.'
266 );
267 }
268
269 public function getDescription() {
270 return 'Get events from logs.';
271 }
272
273 protected function getExamples() {
274 return array (
275 'api.php?action=query&list=logevents'
276 );
277 }
278
279 public function getVersion() {
280 return __CLASS__ . ': $Id$';
281 }
282 }