(bug 19004) Added support for tags to the API. Patch by Matthew Britton.
[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 $this->fld_tags = in_array('tags', $prop);
55
56 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
57
58 $hideLogs = LogEventsList::getExcludeClause($db);
59 if($hideLogs !== false)
60 $this->addWhere($hideLogs);
61
62 // Order is significant here
63 $this->addTables(array('logging', 'user', 'page'));
64 $this->addOption('STRAIGHT_JOIN');
65 $this->addJoinConds(array(
66 'user' => array('JOIN',
67 'user_id=log_user'),
68 'page' => array('LEFT JOIN',
69 array( 'log_namespace=page_namespace',
70 'log_title=page_title'))));
71 $index = 'times'; // default, may change
72
73 $this->addFields(array (
74 'log_type',
75 'log_action',
76 'log_timestamp',
77 'log_deleted',
78 ));
79
80 $this->addFieldsIf('log_id', $this->fld_ids);
81 $this->addFieldsIf('page_id', $this->fld_ids);
82 $this->addFieldsIf('log_user', $this->fld_user);
83 $this->addFieldsIf('user_name', $this->fld_user);
84 $this->addFieldsIf('log_namespace', $this->fld_title);
85 $this->addFieldsIf('log_title', $this->fld_title);
86 $this->addFieldsIf('log_comment', $this->fld_comment);
87 $this->addFieldsIf('log_params', $this->fld_details);
88
89 if($this->fld_tags || !is_null($params['tag'])) {
90 $this->addTables('tag_summary');
91 $this->addJoinConds(array('tag_summary' => array('LEFT JOIN', 'log_id=ts_log_id')));
92 $this->addFields('ts_tags');
93 }
94
95 if( !is_null($params['tag']) ) {
96 $this->addWhereFld('ts_tags', $params['tag']);
97 }
98
99 if( !is_null($params['type']) ) {
100 $this->addWhereFld('log_type', $params['type']);
101 $index = 'type_time';
102 }
103
104 $this->addWhereRange('log_timestamp', $params['dir'], $params['start'], $params['end']);
105
106 $limit = $params['limit'];
107 $this->addOption('LIMIT', $limit +1);
108
109 $user = $params['user'];
110 if (!is_null($user)) {
111 $userid = User::idFromName($user);
112 if (!$userid)
113 $this->dieUsage("User name $user not found", 'param_user');
114 $this->addWhereFld('log_user', $userid);
115 $index = 'user_time';
116 }
117
118 $title = $params['title'];
119 if (!is_null($title)) {
120 $titleObj = Title :: newFromText($title);
121 if (is_null($titleObj))
122 $this->dieUsage("Bad title value '$title'", 'param_title');
123 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
124 $this->addWhereFld('log_title', $titleObj->getDBkey());
125
126 // Use the title index in preference to the user index if there is a conflict
127 $index = is_null($user) ? 'page_time' : array('page_time','user_time');
128 }
129
130 $this->addOption( 'USE INDEX', array( 'logging' => $index ) );
131
132 // Paranoia: avoid brute force searches (bug 17342)
133 if (!is_null($title)) {
134 $this->addWhere($db->bitAnd('log_deleted', LogPage::DELETED_ACTION) . ' = 0');
135 }
136 if (!is_null($user)) {
137 $this->addWhere($db->bitAnd('log_deleted', LogPage::DELETED_USER) . ' = 0');
138 }
139
140 $count = 0;
141 $res = $this->select(__METHOD__);
142 while ($row = $db->fetchObject($res)) {
143 if (++ $count > $limit) {
144 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
145 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
146 break;
147 }
148
149 $vals = $this->extractRowInfo($row);
150 if(!$vals)
151 continue;
152 $fit = $this->getResult()->addValue(array('query', $this->getModuleName()), null, $vals);
153 if(!$fit)
154 {
155 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
156 break;
157 }
158 }
159 $db->freeResult($res);
160
161 $this->getResult()->setIndexedTagName_internal(array('query', $this->getModuleName()), 'item');
162 }
163
164 public static function addLogParams($result, &$vals, $params, $type, $ts) {
165 $params = explode("\n", $params);
166 switch ($type) {
167 case 'move':
168 if (isset ($params[0])) {
169 $title = Title :: newFromText($params[0]);
170 if ($title) {
171 $vals2 = array();
172 ApiQueryBase :: addTitleInfo($vals2, $title, "new_");
173 $vals[$type] = $vals2;
174 }
175 }
176 if (isset ($params[1]) && $params[1]) {
177 $vals[$type]['suppressedredirect'] = '';
178 }
179 $params = null;
180 break;
181 case 'patrol':
182 $vals2 = array();
183 list( $vals2['cur'], $vals2['prev'], $vals2['auto'] ) = $params;
184 $vals[$type] = $vals2;
185 $params = null;
186 break;
187 case 'rights':
188 $vals2 = array();
189 list( $vals2['old'], $vals2['new'] ) = $params;
190 $vals[$type] = $vals2;
191 $params = null;
192 break;
193 case 'block':
194 $vals2 = array();
195 list( $vals2['duration'], $vals2['flags'] ) = $params;
196 $vals2['expiry'] = wfTimestamp(TS_ISO_8601,
197 strtotime($params[0], wfTimestamp(TS_UNIX, $ts)));
198 $vals[$type] = $vals2;
199 $params = null;
200 break;
201 }
202 if (!is_null($params)) {
203 $result->setIndexedTagName($params, 'param');
204 $vals = array_merge($vals, $params);
205 }
206 return $vals;
207 }
208
209 private function extractRowInfo($row) {
210 $vals = array();
211
212 if ($this->fld_ids) {
213 $vals['logid'] = intval($row->log_id);
214 $vals['pageid'] = intval($row->page_id);
215 }
216
217 if ($this->fld_title) {
218 if (LogEventsList::isDeleted($row, LogPage::DELETED_ACTION)) {
219 $vals['actionhidden'] = '';
220 } else {
221 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
222 ApiQueryBase :: addTitleInfo($vals, $title);
223 }
224 }
225
226 if ($this->fld_type) {
227 $vals['type'] = $row->log_type;
228 $vals['action'] = $row->log_action;
229 }
230
231 if ($this->fld_details && $row->log_params !== '') {
232 if (LogEventsList::isDeleted($row, LogPage::DELETED_ACTION)) {
233 $vals['actionhidden'] = '';
234 } else {
235 self::addLogParams($this->getResult(), $vals,
236 $row->log_params, $row->log_type,
237 $row->log_timestamp);
238 }
239 }
240
241 if ($this->fld_user) {
242 if (LogEventsList::isDeleted($row, LogPage::DELETED_USER)) {
243 $vals['userhidden'] = '';
244 } else {
245 $vals['user'] = $row->user_name;
246 if(!$row->log_user)
247 $vals['anon'] = '';
248 }
249 }
250 if ($this->fld_timestamp) {
251 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
252 }
253 if ($this->fld_comment && isset($row->log_comment)) {
254 if (LogEventsList::isDeleted($row, LogPage::DELETED_COMMENT)) {
255 $vals['commenthidden'] = '';
256 } else {
257 $vals['comment'] = $row->log_comment;
258 }
259 }
260
261 if ($this->fld_tags && isset($row->ts_tags)) {
262 $vals['tags'] = $row->ts_tags;
263 }
264
265 return $vals;
266 }
267
268
269 public function getAllowedParams() {
270 global $wgLogTypes;
271 return array (
272 'prop' => array (
273 ApiBase :: PARAM_ISMULTI => true,
274 ApiBase :: PARAM_DFLT => 'ids|title|type|user|timestamp|comment|details',
275 ApiBase :: PARAM_TYPE => array (
276 'ids',
277 'title',
278 'type',
279 'user',
280 'timestamp',
281 'comment',
282 'details',
283 'tags'
284 )
285 ),
286 'type' => array (
287 ApiBase :: PARAM_TYPE => $wgLogTypes
288 ),
289 'start' => array (
290 ApiBase :: PARAM_TYPE => 'timestamp'
291 ),
292 'end' => array (
293 ApiBase :: PARAM_TYPE => 'timestamp'
294 ),
295 'dir' => array (
296 ApiBase :: PARAM_DFLT => 'older',
297 ApiBase :: PARAM_TYPE => array (
298 'newer',
299 'older'
300 )
301 ),
302 'user' => null,
303 'title' => null,
304 'tag' => null,
305 'limit' => array (
306 ApiBase :: PARAM_DFLT => 10,
307 ApiBase :: PARAM_TYPE => 'limit',
308 ApiBase :: PARAM_MIN => 1,
309 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
310 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
311 )
312 );
313 }
314
315 public function getParamDescription() {
316 return array (
317 'prop' => 'Which properties to get',
318 'type' => 'Filter log entries to only this type(s)',
319 'start' => 'The timestamp to start enumerating from.',
320 'end' => 'The timestamp to end enumerating.',
321 'dir' => 'In which direction to enumerate.',
322 'user' => 'Filter entries to those made by the given user.',
323 'title' => 'Filter entries to those related to a page.',
324 'tag' => 'Only list entries with this tag',
325 'limit' => 'How many total event entries to return.'
326 );
327 }
328
329 public function getDescription() {
330 return 'Get events from logs.';
331 }
332
333 protected function getExamples() {
334 return array (
335 'api.php?action=query&list=logevents'
336 );
337 }
338
339 public function getVersion() {
340 return __CLASS__ . ': $Id$';
341 }
342 }