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