decde3e9699ef3abe8373f48a7d4ee261852244c
[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 $this->setTablesAsExpression("$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 $this->addFields(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 $this->addWhereFld('log_type', $type);
63 $this->addWhereRange('log_timestamp', $dir, $start, $end);
64 $this->addOption('LIMIT', $limit +1);
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 $this->addWhereFld('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 $this->addWhereFld('log_namespace', $titleObj->getNamespace());
80 $this->addWhereFld('log_title', $titleObj->getDBkey());
81 }
82
83 $data = array ();
84 $count = 0;
85 $res = $this->select(__METHOD__);
86 while ($row = $db->fetchObject($res)) {
87 if (++ $count > $limit) {
88 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
89 $this->setContinueEnumParameter('start', ApiQueryBase :: keyToTitle($row->log_timestamp));
90 break;
91 }
92
93 $vals = array (
94 'action' => "$row->log_type/$row->log_action",
95 'timestamp' => $row->log_timestamp,
96 'comment' => $row->log_comment,
97 'pageid' => intval($row->page_id
98 ));
99
100 $title = Title :: makeTitle($row->log_namespace, $row->log_title);
101 $vals['ns'] = $title->getNamespace();
102 $vals['title'] = $title->getPrefixedText();
103
104 if ($row->log_params !== '') {
105 $params = explode("\n", $row->log_params);
106 if ($row->log_type == 'move' && isset ($params[0])) {
107 $destTitle = Title :: newFromText($params[0]);
108 if ($destTitle) {
109 $vals['tons'] = $destTitle->getNamespace();
110 $vals['totitle'] = $destTitle->getPrefixedText();
111 $params = null;
112 }
113 }
114
115 if (!empty ($params)) {
116 $this->getResult()->setIndexedTagName($params, 'param');
117 $vals = array_merge($vals, $params);
118 }
119 }
120
121 if (!$row->log_user)
122 $vals['anon'] = '';
123 $vals['user'] = $row->user_name;
124
125 $data[] = $vals;
126 }
127 $db->freeResult($res);
128
129 $this->getResult()->setIndexedTagName($data, 'item');
130 $this->getResult()->addValue('query', $this->getModuleName(), $data);
131 }
132
133 protected function getAllowedParams() {
134 return array (
135 'limit' => array (
136 ApiBase :: PARAM_DFLT => 10,
137 ApiBase :: PARAM_TYPE => 'limit',
138 ApiBase :: PARAM_MIN => 1,
139 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_BIG1,
140 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
141 ),
142 'type' => array (
143 ApiBase :: PARAM_ISMULTI => true,
144 ApiBase :: PARAM_TYPE => array (
145 'block',
146 'protect',
147 'rights',
148 'delete',
149 'upload',
150 'move',
151 'import',
152 'renameuser',
153 'newusers',
154 'makebot'
155 )
156 ),
157 'start' => array (
158 ApiBase :: PARAM_TYPE => 'timestamp'
159 ),
160 'end' => array (
161 ApiBase :: PARAM_TYPE => 'timestamp'
162 ),
163 'dir' => array (
164 ApiBase :: PARAM_DFLT => 'older',
165 ApiBase :: PARAM_TYPE => array (
166 'newer',
167 'older'
168 )
169 ),
170 'user' => null,
171 'title' => null
172 );
173 }
174
175 protected function getParamDescription() {
176 return array (
177 'limit' => '',
178 'type' => '',
179 'start' => '',
180 'end' => '',
181 'dir' => '',
182 'user' => '',
183 'title' => ''
184 );
185 }
186
187 protected function getDescription() {
188 return 'Get events from logs.';
189 }
190
191 protected function getExamples() {
192 return array (
193 'api.php?action=query&list=logevents'
194 );
195 }
196
197 public function getVersion() {
198 return __CLASS__ . ': $Id$';
199 }
200 }
201 ?>