7da11b02d8df6802270de622d9feb518af63b1cf
[lhc/web/wiklou.git] / includes / api / ApiQueryLogEvents.php
1 <?php
2
3
4 /*
5 * Created on Oct 16, 2006
6 *
7 * API for MediaWiki 1.8+
8 *
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
10 *
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along
22 * with this program; if not, write to the Free Software Foundation, Inc.,
23 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 * http://www.gnu.org/copyleft/gpl.html
25 */
26
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
30 }
31
32 class ApiQueryLogEvents extends ApiQueryBase {
33
34 public function __construct($query, $moduleName) {
35 parent :: __construct($query, $moduleName, 'le');
36 }
37
38 public function execute() {
39 $limit = $type = $start = $end = $dir = $user = $title = null;
40 extract($this->extractRequestParams());
41
42 $db = $this->getDB();
43
44 extract($db->tableNames('logging', 'page', 'user'), EXTR_PREFIX_ALL, 'tbl');
45 $tables = "$tbl_logging LEFT OUTER JOIN $tbl_page ON " .
46 "log_namespace=page_namespace AND log_title=page_title " .
47 "INNER JOIN $tbl_user ON user_id=log_user";
48
49 $fields = array (
50 'log_type',
51 'log_action',
52 'log_timestamp',
53 'log_user',
54 'user_name',
55 'log_namespace',
56 'log_title',
57 'page_id',
58 'log_comment',
59 'log_params'
60 );
61
62 $where = array ();
63 if (!is_null($type))
64 $where['log_type'] = $type;
65
66 if (!is_null($user)) {
67 $userid = $db->selectField('user', 'user_id', array (
68 'user_name' => $user
69 ));
70 if (!$userid)
71 $this->dieUsage("User name $user not found", 'param_user');
72 $where['log_user'] = $userid;
73 }
74
75 if (!is_null($title)) {
76 $titleObj = Title :: newFromText($title);
77 if (is_null($titleObj))
78 $this->dieUsage("Bad title value '$title'", 'param_title');
79 $where['log_namespace'] = $titleObj->getNamespace();
80 $where['log_title'] = $titleObj->getDBkey();
81 }
82
83 $dirNewer = ($dir === 'newer');
84 $before = ($dirNewer ? '<=' : '>=');
85 $after = ($dirNewer ? '>=' : '<=');
86
87 if (!is_null($start))
88 $where[] = 'log_timestamp' . $after . $db->addQuotes($start);
89 if (!is_null($end))
90 $where[] = 'log_timestamp' . $before . $db->addQuotes($end);
91
92 $options = array (
93 'LIMIT' => $limit +1,
94 'ORDER BY' => 'log_timestamp' . ($dirNewer ? '' : ' DESC'
95 ));
96
97 $this->profileDBIn();
98 $res = $db->select($tables, $fields, $where, __METHOD__, $options);
99 $this->profileDBOut();
100
101 $data = array ();
102 $count = 0;
103 while ($row = $db->fetchObject($res)) {
104 if (++ $count > $limit) {
105 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
106 $this->setContinueEnumParameter('start', ApiQueryBase :: keyToTitle($row->log_timestamp));
107 break;
108 }
109
110 $vals = array (
111 'action' => "$row->log_type/$row->log_action",
112 'timestamp' => $row->log_timestamp,
113 'comment' => $row->log_comment,
114 'pageid' => intval($row->page_id
115 ));
116
117 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
118 $vals['ns'] = $title->getNamespace();
119 $vals['title'] = $title->getPrefixedText();
120
121 if ($row->log_params !== '') {
122 $params = explode("\n", $row->log_params);
123 if ($row->log_type == 'move' && isset ($params[0])) {
124 $destTitle = Title :: newFromText($params[0]);
125 if ($destTitle) {
126 $vals['tons'] = $destTitle->getNamespace();
127 $vals['totitle'] = $destTitle->getPrefixedText();
128 $params = null;
129 }
130 }
131
132 if(!empty($params)) {
133 ApiResult :: setIndexedTagName($params, 'param');
134 $vals = array_merge($vals, $params);
135 }
136 }
137
138 if (!$row->log_user)
139 $vals['anon'] = '';
140 $vals['user'] = $row->user_name;
141
142 $data[] = $vals;
143 }
144 $db->freeResult($res);
145
146 ApiResult :: setIndexedTagName($data, 'item');
147 $this->getResult()->addValue('query', $this->getModuleName(), $data);
148 }
149
150 protected function getAllowedParams() {
151 return array (
152 'limit' => array (
153 ApiBase :: PARAM_DFLT => 10,
154 ApiBase :: PARAM_TYPE => 'limit',
155 ApiBase :: PARAM_MIN => 1,
156 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
157 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
158 ),
159 'type' => array (
160 ApiBase :: PARAM_ISMULTI => true,
161 ApiBase :: PARAM_TYPE => array (
162 'block',
163 'protect',
164 'rights',
165 'delete',
166 'upload',
167 'move',
168 'import',
169 'renameuser',
170 'newusers',
171 'makebot'
172 )
173 ),
174 'start' => array (
175 ApiBase :: PARAM_TYPE => 'timestamp'
176 ),
177 'end' => array (
178 ApiBase :: PARAM_TYPE => 'timestamp'
179 ),
180 'dir' => array (
181 ApiBase :: PARAM_DFLT => 'older',
182 ApiBase :: PARAM_TYPE => array (
183 'newer',
184 'older'
185 )
186 ),
187 'user' => null,
188 'title' => null
189 );
190 }
191
192 protected function getParamDescription() {
193 return array (
194 'limit' => '',
195 'type' => '',
196 'start' => '',
197 'end' => '',
198 'dir' => '',
199 'user' => '',
200 'title' => ''
201 );
202 }
203
204 protected function getDescription() {
205 return 'Get events from logs.';
206 }
207
208 protected function getExamples() {
209 return array (
210 'api.php?action=query&list=logevents'
211 );
212 }
213
214 public function getVersion() {
215 return __CLASS__ . ': $Id$';
216 }
217 }
218 ?>