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