API: Big change: Removed all userCanRead() checks per IRC discussion. Only rvprop...
[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 $limit = $type = $start = $end = $dir = $user = $title = null;
44 extract($this->extractRequestParams());
45
46 $db = $this->getDB();
47
48 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
49
50 $this->addOption('STRAIGHT_JOIN');
51 $this->addTables("$tbl_logging LEFT OUTER JOIN $tbl_page ON " .
52 "log_namespace=page_namespace AND log_title=page_title " .
53 "INNER JOIN $tbl_user ON user_id=log_user");
54
55 $this->addFields(array (
56 'log_type',
57 'log_action',
58 'log_timestamp',
59 'log_user',
60 'user_name',
61 'log_namespace',
62 'log_title',
63 'page_id',
64 'log_comment',
65 'log_params'
66 ));
67
68 $this->addWhereFld('log_deleted', 0);
69 $this->addWhereFld('log_type', $type);
70 $this->addWhereRange('log_timestamp', $dir, $start, $end);
71 $this->addOption('LIMIT', $limit +1);
72
73 if (!is_null($user)) {
74 $userid = $db->selectField('user', 'user_id', array (
75 'user_name' => $user
76 ));
77 if (!$userid)
78 $this->dieUsage("User name $user not found", 'param_user');
79 $this->addWhereFld('log_user', $userid);
80 }
81
82 if (!is_null($title)) {
83 $titleObj = Title :: newFromText($title);
84 if (is_null($titleObj))
85 $this->dieUsage("Bad title value '$title'", 'param_title');
86 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
87 $this->addWhereFld('log_title', $titleObj->getDBkey());
88 }
89
90 $data = array ();
91 $count = 0;
92 $res = $this->select(__METHOD__);
93 while ($row = $db->fetchObject($res)) {
94 if (++ $count > $limit) {
95 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
96 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->log_timestamp));
97 break;
98 }
99
100 $vals = $this->extractRowInfo($row);
101 if($vals)
102 $data[] = $vals;
103 }
104 $db->freeResult($res);
105
106 $this->getResult()->setIndexedTagName($data, 'item');
107 $this->getResult()->addValue('query', $this->getModuleName(), $data);
108 }
109
110 private function extractRowInfo($row) {
111 $vals = array();
112
113 $vals['pageid'] = intval($row->page_id);
114 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
115 ApiQueryBase :: addTitleInfo($vals, $title);
116 $vals['type'] = $row->log_type;
117 $vals['action'] = $row->log_action;
118
119 if ($row->log_params !== '') {
120 $params = explode("\n", $row->log_params);
121 switch ($row->log_type) {
122 case 'move':
123 if (isset ($params[0])) {
124 $title = Title :: newFromText($params[0]);
125 if ($title) {
126 ApiQueryBase :: addTitleInfo($vals, $title, "new_");
127 $params = null;
128 }
129 }
130 break;
131 case 'patrol':
132 list( $cur, $prev, $auto ) = $params;
133 $vals['patrol_prev'] = $prev;
134 $vals['patrol_cur'] = $cur;
135 $vals['patrol_auto'] = $auto;
136 $params = null;
137 break;
138 }
139
140 if (!empty ($params)) {
141 $this->getResult()->setIndexedTagName($params, 'param');
142 $vals = array_merge($vals, $params);
143 }
144 }
145
146 $vals['user'] = $row->user_name;
147 if(!$row->log_user)
148 $vals['anon'] = '';
149 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->log_timestamp);
150
151 if (!empty ($row->log_comment))
152 $vals['comment'] = $row->log_comment;
153
154 return $vals;
155 }
156
157
158 protected function getAllowedParams() {
159 global $wgLogTypes;
160 return array (
161 'type' => array (
162 ApiBase :: PARAM_ISMULTI => true,
163 ApiBase :: PARAM_TYPE => $wgLogTypes
164 ),
165 'start' => array (
166 ApiBase :: PARAM_TYPE => 'timestamp'
167 ),
168 'end' => array (
169 ApiBase :: PARAM_TYPE => 'timestamp'
170 ),
171 'dir' => array (
172 ApiBase :: PARAM_DFLT => 'older',
173 ApiBase :: PARAM_TYPE => array (
174 'newer',
175 'older'
176 )
177 ),
178 'user' => null,
179 'title' => null,
180 'limit' => array (
181 ApiBase :: PARAM_DFLT => 10,
182 ApiBase :: PARAM_TYPE => 'limit',
183 ApiBase :: PARAM_MIN => 1,
184 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
185 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
186 )
187 );
188 }
189
190 protected function getParamDescription() {
191 return array (
192 'type' => 'Filter log entries to only this type(s)',
193 'start' => 'The timestamp to start enumerating from.',
194 'end' => 'The timestamp to end enumerating.',
195 'dir' => 'In which direction to enumerate.',
196 'user' => 'Filter entries to those made by the given user.',
197 'title' => 'Filter entries to those related to a page.',
198 'limit' => 'How many total event entries to return.'
199 );
200 }
201
202 protected function getDescription() {
203 return 'Get events from logs.';
204 }
205
206 protected function getExamples() {
207 return array (
208 'api.php?action=query&list=logevents'
209 );
210 }
211
212 public function getVersion() {
213 return __CLASS__ . ': $Id$';
214 }
215 }
216