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