API:
[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 <FirstnameLastname@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 * @addtogroup API
33 */
34 class ApiQueryLogEvents extends ApiQueryBase {
35
36 public function __construct($query, $moduleName) {
37 parent :: __construct($query, $moduleName, 'le');
38 }
39
40 public function execute() {
41 $limit = $type = $start = $end = $dir = $user = $title = null;
42 extract($this->extractRequestParams());
43
44 $db = $this->getDB();
45
46 list($tbl_logging, $tbl_page, $tbl_user) = $db->tableNamesN('logging', 'page', 'user');
47
48 $this->addOption('STRAIGHT_JOIN');
49 $this->addTables("$tbl_logging LEFT OUTER JOIN $tbl_page ON " .
50 "log_namespace=page_namespace AND log_title=page_title " .
51 "INNER JOIN $tbl_user ON user_id=log_user");
52
53 $this->addFields(array (
54 'log_type',
55 'log_action',
56 'log_timestamp',
57 'log_user',
58 'user_name',
59 'log_namespace',
60 'log_title',
61 'page_id',
62 'log_comment',
63 'log_params'
64 ));
65
66 $this->addWhereFld('log_type', $type);
67 $this->addWhereRange('log_timestamp', $dir, $start, $end);
68 $this->addOption('LIMIT', $limit +1);
69
70 if (!is_null($user)) {
71 $userid = $db->selectField('user', 'user_id', array (
72 'user_name' => $user
73 ));
74 if (!$userid)
75 $this->dieUsage("User name $user not found", 'param_user');
76 $this->addWhereFld('log_user', $userid);
77 }
78
79 if (!is_null($title)) {
80 $titleObj = Title :: newFromText($title);
81 if (is_null($titleObj))
82 $this->dieUsage("Bad title value '$title'", 'param_title');
83 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
84 $this->addWhereFld('log_title', $titleObj->getDBkey());
85 }
86
87 $data = array ();
88 $count = 0;
89 $res = $this->select(__METHOD__);
90 while ($row = $db->fetchObject($res)) {
91 if (++ $count > $limit) {
92 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
93 $this->setContinueEnumParameter('start', $row->log_timestamp);
94 break;
95 }
96
97 $vals = $this->extractRowInfo($row);
98 if($vals)
99 $data[] = $vals;
100 }
101 $db->freeResult($res);
102
103 $this->getResult()->setIndexedTagName($data, 'item');
104 $this->getResult()->addValue('query', $this->getModuleName(), $data);
105 }
106
107 private function extractRowInfo($row) {
108 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
109 if (!$title->userCanRead())
110 return false;
111
112 $vals = array();
113
114 $vals['pageid'] = intval($row->page_id);
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 ?>