2268a8f72e4f5d1e8f4015248492e7afe150093c
[lhc/web/wiklou.git] / includes / api / ApiQueryRecentChanges.php
1 <?php
2
3 /*
4 * Created on Oct 19, 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 * A query action to enumerate the recent changes that were done to the wiki.
33 * Various filters are supported.
34 *
35 * @addtogroup API
36 */
37 class ApiQueryRecentChanges extends ApiQueryBase {
38
39 public function __construct($query, $moduleName) {
40 parent :: __construct($query, $moduleName, 'rc');
41 }
42
43 private $fld_comment = false, $fld_user = false, $fld_flags = false,
44 $fld_timestamp = false, $fld_title = false, $fld_ids = false,
45 $fld_sizes = false;
46
47 public function execute() {
48 $limit = $prop = $namespace = $show = $dir = $start = $end = null;
49 extract($this->extractRequestParams());
50
51 $this->addTables('recentchanges');
52 $this->addWhereRange('rc_timestamp', $dir, $start, $end);
53 $this->addWhereFld('rc_namespace', $namespace);
54 $this->addWhereFld('rc_deleted', 0);
55
56 if (!is_null($show)) {
57 $show = array_flip($show);
58 if ((isset ($show['minor']) && isset ($show['!minor'])) || (isset ($show['bot']) && isset ($show['!bot'])) || (isset ($show['anon']) && isset ($show['!anon'])))
59 $this->dieUsage("Incorrect parameter - mutually exclusive values may not be supplied", 'show');
60
61 $this->addWhereIf('rc_minor = 0', isset ($show['!minor']));
62 $this->addWhereIf('rc_minor != 0', isset ($show['minor']));
63 $this->addWhereIf('rc_bot = 0', isset ($show['!bot']));
64 $this->addWhereIf('rc_bot != 0', isset ($show['bot']));
65 $this->addWhereIf('rc_user = 0', isset ($show['anon']));
66 $this->addWhereIf('rc_user != 0', isset ($show['!anon']));
67 }
68
69 $this->addFields(array (
70 'rc_timestamp',
71 'rc_namespace',
72 'rc_title',
73 'rc_type',
74 'rc_moved_to_ns',
75 'rc_moved_to_title'
76 ));
77
78 if (!is_null($prop)) {
79 $prop = array_flip($prop);
80
81 $this->fld_comment = isset ($prop['comment']);
82 $this->fld_user = isset ($prop['user']);
83 $this->fld_flags = isset ($prop['flags']);
84 $this->fld_timestamp = isset ($prop['timestamp']);
85 $this->fld_title = isset ($prop['title']);
86 $this->fld_ids = isset ($prop['ids']);
87 $this->fld_sizes = isset ($prop['sizes']);
88
89 $this->addFieldsIf('rc_id', $this->fld_ids);
90 $this->addFieldsIf('rc_cur_id', $this->fld_ids);
91 $this->addFieldsIf('rc_this_oldid', $this->fld_ids);
92 $this->addFieldsIf('rc_last_oldid', $this->fld_ids);
93 $this->addFieldsIf('rc_comment', $this->fld_comment);
94 $this->addFieldsIf('rc_user', $this->fld_user);
95 $this->addFieldsIf('rc_user_text', $this->fld_user);
96 $this->addFieldsIf('rc_minor', $this->fld_flags);
97 $this->addFieldsIf('rc_bot', $this->fld_flags);
98 $this->addFieldsIf('rc_new', $this->fld_flags);
99 $this->addFieldsIf('rc_old_len', $this->fld_sizes);
100 $this->addFieldsIf('rc_new_len', $this->fld_sizes);
101 }
102
103 $this->addOption('LIMIT', $limit +1);
104 $this->addOption('USE INDEX', 'rc_timestamp');
105
106 $data = array ();
107 $count = 0;
108 $db = $this->getDB();
109 $res = $this->select(__METHOD__);
110
111 while ($row = $db->fetchObject($res)) {
112 if (++ $count > $limit) {
113 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
114 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601, $row->rc_timestamp));
115 break;
116 }
117
118 $vals = $this->extractRowInfo($row);
119 if($vals)
120 $data[] = $vals;
121 }
122 $db->freeResult($res);
123
124 $result = $this->getResult();
125 $result->setIndexedTagName($data, 'rc');
126 $result->addValue('query', $this->getModuleName(), $data);
127 }
128
129 private function extractRowInfo($row) {
130 $movedToTitle = false;
131 if (!empty($row->rc_moved_to_title))
132 $movedToTitle = Title :: makeTitle($row->rc_moved_to_ns, $row->rc_moved_to_title);
133
134 $title = Title :: makeTitle($row->rc_namespace, $row->rc_title);
135 $vals = array ();
136
137 $type = intval ( $row->rc_type );
138
139 /* Determine what kind of change this was. */
140 switch ( $type ) {
141 case RC_EDIT: $vals['type'] = 'edit'; break;
142 case RC_NEW: $vals['type'] = 'new'; break;
143 case RC_MOVE: $vals['type'] = 'move'; break;
144 case RC_LOG: $vals['type'] = 'log'; break;
145 case RC_MOVE_OVER_REDIRECT: $vals['type'] = 'move over redirect'; break;
146 default: $vals['type'] = $type;
147 }
148
149 if ($this->fld_title) {
150 ApiQueryBase :: addTitleInfo($vals, $title);
151 if ($movedToTitle)
152 ApiQueryBase :: addTitleInfo($vals, $movedToTitle, "new_");
153 }
154
155 if ($this->fld_ids) {
156 $vals['rcid'] = intval($row->rc_id);
157 $vals['pageid'] = intval($row->rc_cur_id);
158 $vals['revid'] = intval($row->rc_this_oldid);
159 $vals['old_revid'] = intval( $row->rc_last_oldid );
160 }
161
162 if ($this->fld_user) {
163 $vals['user'] = $row->rc_user_text;
164 if(!$row->rc_user)
165 $vals['anon'] = '';
166 }
167
168 if ($this->fld_flags) {
169 if ($row->rc_bot)
170 $vals['bot'] = '';
171 if ($row->rc_new)
172 $vals['new'] = '';
173 if ($row->rc_minor)
174 $vals['minor'] = '';
175 }
176
177 if ($this->fld_sizes) {
178 $vals['oldlen'] = intval($row->rc_old_len);
179 $vals['newlen'] = intval($row->rc_new_len);
180 }
181
182 if ($this->fld_timestamp)
183 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rc_timestamp);
184
185 if ($this->fld_comment && !empty ($row->rc_comment)) {
186 $vals['comment'] = $row->rc_comment;
187 }
188
189 return $vals;
190 }
191
192 protected function getAllowedParams() {
193 return array (
194 'start' => array (
195 ApiBase :: PARAM_TYPE => 'timestamp'
196 ),
197 'end' => array (
198 ApiBase :: PARAM_TYPE => 'timestamp'
199 ),
200 'dir' => array (
201 ApiBase :: PARAM_DFLT => 'older',
202 ApiBase :: PARAM_TYPE => array (
203 'newer',
204 'older'
205 )
206 ),
207 'namespace' => array (
208 ApiBase :: PARAM_ISMULTI => true,
209 ApiBase :: PARAM_TYPE => 'namespace'
210 ),
211 'prop' => array (
212 ApiBase :: PARAM_ISMULTI => true,
213 ApiBase :: PARAM_DFLT => 'title|timestamp|ids',
214 ApiBase :: PARAM_TYPE => array (
215 'user',
216 'comment',
217 'flags',
218 'timestamp',
219 'title',
220 'ids',
221 'sizes'
222 )
223 ),
224 'show' => array (
225 ApiBase :: PARAM_ISMULTI => true,
226 ApiBase :: PARAM_TYPE => array (
227 'minor',
228 '!minor',
229 'bot',
230 '!bot',
231 'anon',
232 '!anon'
233 )
234 ),
235 'limit' => array (
236 ApiBase :: PARAM_DFLT => 10,
237 ApiBase :: PARAM_TYPE => 'limit',
238 ApiBase :: PARAM_MIN => 1,
239 ApiBase :: PARAM_MAX => ApiBase :: LIMIT_BIG1,
240 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_BIG2
241 )
242 );
243 }
244
245 protected function getParamDescription() {
246 return array (
247 'start' => 'The timestamp to start enumerating from.',
248 'end' => 'The timestamp to end enumerating.',
249 'dir' => 'In which direction to enumerate.',
250 'namespace' => 'Filter log entries to only this namespace(s)',
251 'prop' => 'Include additional pieces of information',
252 'show' => array (
253 'Show only items that meet this criteria.',
254 'For example, to see only minor edits done by logged-in users, set show=minor|!anon'
255 ),
256 'limit' => 'How many total pages to return.'
257 );
258 }
259
260 protected function getDescription() {
261 return 'Enumerate recent changes';
262 }
263
264 protected function getExamples() {
265 return array (
266 'api.php?action=query&list=recentchanges'
267 );
268 }
269
270 public function getVersion() {
271 return __CLASS__ . ': $Id$';
272 }
273 }
274