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