613b91ff2464b6d1c5e12b8ba9b856efcdade55a
[lhc/web/wiklou.git] / includes / api / ApiQueryRevisions.php
1 <?php
2
3
4 /*
5 * Created on Sep 7, 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 ApiQueryRevisions extends ApiQueryBase {
33
34 public function __construct($query, $moduleName, $generator = false) {
35 parent :: __construct($query, $moduleName, $generator);
36 }
37
38 public function execute() {
39 $rvlimit = $rvstartid = $rvendid = $rvstart = $rvend = $rvdir = $rvprop = null;
40 extract($this->extractRequestParams());
41
42 $db = $this->getDB();
43
44 // true when ordered by timestamp from older to newer, false otherwise
45 $dirNewer = ($rvdir === 'newer');
46
47 // If any of those parameters are used, work in 'enumeration' mode.
48 // Enum mode can only be used when exactly one page is provided.
49 // Enumerating revisions on multiple pages make it extremelly
50 // difficult to manage continuations and require additional sql indexes
51 $enumRevMode = ($rvlimit !== 0 || $rvstartid !== 0 || $rvendid !== 0 || $dirNewer || isset ($rvstart) || isset ($rvend));
52
53 $pageSet = $this->getPageSet();
54 $pageCount = $pageSet->getGoodTitleCount();
55 $revCount = $pageSet->getRevisionCount();
56
57 // Optimization -- nothing to do
58 if ($revCount === 0 && $pageCount === 0)
59 return;
60
61 if ($revCount > 0 && $pageCount > 0)
62 $this->dieUsage('The revids= parameter may not be used with titles, pageids, or generator options.', 'rv_revids');
63
64 if ($revCount > 0 && $enumRevMode)
65 $this->dieUsage('The revids= parameter may not be used with the list options (rvlimit, rvstartid, rvendid, dirNewer, rvstart, rvend).', 'rv_revids');
66
67 if ($revCount === 0 && $pageCount > 1 && $enumRevMode)
68 $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the rvlimit, rvstartid, rvendid, dirNewer, rvstart, and rvend parameters may only be used on a single page.', 'rv_multpages');
69
70 $tables = array (
71 'revision'
72 );
73 $fields = array (
74 'rev_id',
75 'rev_page',
76 'rev_text_id',
77 'rev_minor_edit'
78 );
79 $conds = array (
80 'rev_deleted' => 0
81 );
82 $options = array ();
83
84 $showTimestamp = $showUser = $showComment = $showContent = false;
85 if (isset ($rvprop)) {
86 foreach ($rvprop as $prop) {
87 switch ($prop) {
88 case 'timestamp' :
89 $fields[] = 'rev_timestamp';
90 $showTimestamp = true;
91 break;
92 case 'user' :
93 $fields[] = 'rev_user';
94 $fields[] = 'rev_user_text';
95 $showUser = true;
96 break;
97 case 'comment' :
98 $fields[] = 'rev_comment';
99 $showComment = true;
100 break;
101 case 'content' :
102 // todo: check the page count/limit when requesting content
103 //$this->validateLimit( 'content: (rvlimit*pages)+revids',
104 //$rvlimit * count($this->existingPageIds) + count($this->revIdsArray), 50, 200 );
105 $tables[] = 'text';
106 $conds[] = 'rev_text_id=old_id';
107 $fields[] = 'old_id';
108 $fields[] = 'old_text';
109 $fields[] = 'old_flags';
110 $showContent = true;
111 break;
112 default :
113 ApiBase :: dieDebug(__METHOD__, "unknown rvprop $prop");
114 }
115 }
116 }
117
118 $userMax = ($showContent ? 50 : 500);
119 $botMax = ($showContent ? 200 : 10000);
120
121 if ($enumRevMode) {
122
123 // This is mostly to prevent parameter errors (and optimize sql?)
124 if ($rvstartid !== 0 && isset ($rvstart))
125 $this->dieUsage('rvstart and rvstartid cannot be used together', 'rv_badparams');
126
127 if ($rvendid !== 0 && isset ($rvend))
128 $this->dieUsage('rvend and rvend cannot be used together', 'rv_badparams');
129
130 $options['ORDER BY'] = 'rev_timestamp' . ($dirNewer ? '' : ' DESC');
131 $before = ($dirNewer ? '<=' : '>=');
132 $after = ($dirNewer ? '>=' : '<=');
133
134 if ($rvstartid !== 0)
135 $conds[] = 'rev_id' . $after . intval($rvstartid);
136 if ($rvendid !== 0)
137 $conds[] = 'rev_id' . $before . intval($rvendid);
138 if (isset ($rvstart))
139 $conds[] = 'rev_timestamp' . $after . $db->addQuotes($rvstart);
140 if (isset ($rvend))
141 $conds[] = 'rev_timestamp' . $before . $db->addQuotes($rvend);
142
143 // must manually initialize unset rvlimit
144 if (!isset ($rvlimit))
145 $rvlimit = 10;
146
147 $this->validateLimit('rvlimit', $rvlimit, 1, $userMax, $botMax);
148
149 // There is only one ID, use it
150 $conds['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles()));
151
152 }
153 elseif ($pageCount > 0) {
154 // When working in multi-page non-enumeration mode,
155 // limit to the latest revision only
156 $tables[] = 'page';
157 $conds[] = 'page_id=rev_page';
158 $conds[] = 'page_latest=rev_id';
159 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
160
161 // Get all page IDs
162 $conds['page_id'] = array_keys($pageSet->getGoodTitles());
163
164 $rvlimit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
165 }
166 elseif ($revCount > 0) {
167 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
168
169 // Get all revision IDs
170 $conds['rev_id'] = array_keys($pageSet->getRevisionIDs());
171
172 $rvlimit = $revCount; // assumption testing -- we should never get more then $revCount rows.
173 } else
174 ApiBase :: dieDebug(__METHOD__, 'param validation?');
175
176 $options['LIMIT'] = $rvlimit +1;
177
178 $this->profileDBIn();
179 $res = $db->select($tables, $fields, $conds, __METHOD__, $options);
180 $this->profileDBOut();
181
182 $data = array ();
183 $count = 0;
184 while ($row = $db->fetchObject($res)) {
185
186 if (++ $count > $rvlimit) {
187 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
188 if (!$enumRevMode)
189 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
190
191 $startStr = 'rvstartid=' . $row->rev_id;
192 $msg = array (
193 'continue' => $startStr
194 );
195 $this->getResult()->addValue('query-status', 'revisions', $msg);
196 break;
197 }
198
199 $vals = array (
200 'revid' => intval($row->rev_id
201 ), 'oldid' => intval($row->rev_text_id));
202
203 if ($row->rev_minor_edit) {
204 $vals['minor'] = '';
205 }
206
207 if ($showTimestamp)
208 $vals['timestamp'] = wfTimestamp(TS_ISO_8601, $row->rev_timestamp);
209
210 if ($showUser) {
211 $vals['user'] = $row->rev_user_text;
212 if (!$row->rev_user)
213 $vals['anon'] = '';
214 }
215
216 if ($showComment)
217 $vals['comment'] = $row->rev_comment;
218
219 if ($showContent) {
220 ApiResult :: setContent($vals, Revision :: getRevisionText($row));
221 }
222
223 $this->getResult()->addValue(array (
224 'query',
225 'pages',
226 intval($row->rev_page
227 ), 'revisions'), intval($row->rev_id), $vals);
228 }
229 $db->freeResult($res);
230
231 // Ensure that all revisions are shown as '<r>' elements
232 $data = & $this->getResultData();
233 foreach ($data['query']['pages'] as & $page) {
234 if (is_array($page) && array_key_exists('revisions', $page)) {
235 ApiResult :: setIndexedTagName($page['revisions'], 'rev');
236 }
237 }
238 }
239
240 protected function getAllowedParams() {
241 return array (
242 'rvprop' => array (
243 ApiBase :: PARAM_ISMULTI => true,
244 ApiBase :: PARAM_TYPE => array (
245 'timestamp',
246 'user',
247 'comment',
248 'content'
249 )
250 ),
251 'rvlimit' => array (
252 ApiBase :: PARAM_DFLT => 0,
253 ApiBase :: PARAM_TYPE => 'limit',
254 ApiBase :: PARAM_MIN => 0,
255 ApiBase :: PARAM_MAX1 => 50,
256 ApiBase :: PARAM_MAX2 => 500
257 ),
258 'rvstartid' => 0,
259 'rvendid' => 0,
260 'rvstart' => array (
261 ApiBase :: PARAM_TYPE => 'timestamp'
262 ),
263 'rvend' => array (
264 ApiBase :: PARAM_TYPE => 'timestamp'
265 ),
266 'rvdir' => array (
267 ApiBase :: PARAM_DFLT => 'older',
268 ApiBase :: PARAM_TYPE => array (
269 'newer',
270 'older'
271 )
272 )
273 );
274 }
275
276 protected function getParamDescription() {
277 return array (
278 'rvprop' => 'Which properties to get for each revision: user|timestamp|comment|content',
279 'rvlimit' => 'limit how many revisions will be returned (enum)',
280 'rvstartid' => 'from which revision id to start enumeration (enum)',
281 'rvendid' => 'stop revision enumeration on this revid (enum)',
282 'rvstart' => 'from which revision timestamp to start enumeration (enum)',
283 'rvend' => 'enumerate up to this timestamp (enum)',
284 'rvdir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
285 );
286 }
287
288 protected function getDescription() {
289 return array (
290 'Get revision information.',
291 'This module may be used in several ways:',
292 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
293 ' 2) Get revisions for one given page, by using titles/pageids with rvstart/rvend/rvlimit params.',
294 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
295 'All parameters marked as (enum) may only be used with a single page (#2).'
296 );
297 }
298
299 protected function getExamples() {
300 return array (
301 'Get data with content for the last revision of titles "API" and "Main Page":',
302 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
303 'Get last 5 revisions of the "Main Page":',
304 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
305 'Get first 5 revisions of the "Main Page":',
306 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
307 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
308 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000'
309 );
310 }
311
312 public function getVersion() {
313 return __CLASS__ . ': $Id$';
314 }
315 }
316 ?>