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