5 * Created on Sep 7, 2006
7 * API for MediaWiki 1.8+
9 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@gmail.com>
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.
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.
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
27 if (!defined('MEDIAWIKI')) {
28 // Eclipse helper - will be ignored in production
29 require_once ('ApiQueryBase.php');
32 class ApiQueryRevisions
extends ApiQueryBase
{
34 public function __construct($query, $moduleName, $generator = false) {
35 parent
:: __construct($query, $moduleName, $generator);
38 public function execute() {
39 $rvlimit = $rvstartid = $rvendid = $rvstart = $rvend = $rvdir = $rvprop = null;
40 extract($this->extractRequestParams());
44 // true when ordered by timestamp from older to newer, false otherwise
45 $dirNewer = ($rvdir === 'newer');
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));
53 $pageSet = $this->getPageSet();
54 $pageCount = $pageSet->getGoodTitleCount();
55 $revCount = $pageSet->getRevisionCount();
57 // Optimization -- nothing to do
58 if ($revCount === 0 && $pageCount === 0)
61 if ($revCount > 0 && $pageCount > 0)
62 $this->dieUsage('The revids= parameter may not be used with titles, pageids, or generator options.', 'rv_revids');
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');
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');
84 $showTimestamp = $showUser = $showComment = $showContent = false;
85 if (isset ($rvprop)) {
86 foreach ($rvprop as $prop) {
89 $fields[] = 'rev_timestamp';
90 $showTimestamp = true;
93 $fields[] = 'rev_user';
94 $fields[] = 'rev_user_text';
98 $fields[] = 'rev_comment';
103 $conds[] = 'rev_text_id=old_id';
104 $fields[] = 'old_id';
105 $fields[] = 'old_text';
106 $fields[] = 'old_flags';
110 ApiBase
:: dieDebug(__METHOD__
, "unknown rvprop $prop");
115 $userMax = ($showContent ?
50 : 500);
116 $botMax = ($showContent ?
200 : 10000);
120 // This is mostly to prevent parameter errors (and optimize sql?)
121 if ($rvstartid !== 0 && isset ($rvstart))
122 $this->dieUsage('rvstart and rvstartid cannot be used together', 'rv_badparams');
124 if ($rvendid !== 0 && isset ($rvend))
125 $this->dieUsage('rvend and rvend cannot be used together', 'rv_badparams');
127 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
128 // the same result. This way users may request revisions starting at a given time,
129 // but to page through results use the rev_id returned after each page.
130 // Switching to rev_id removes the potential problem of having more than
131 // one row with the same timestamp for the same page.
132 // The order needs to be the same as start parameter to avoid SQL filesort.
133 $options['ORDER BY'] = ($rvstartid !== 0 ?
'rev_id' : 'rev_timestamp') . ($dirNewer ?
'' : ' DESC');
135 $before = ($dirNewer ?
'<=' : '>=');
136 $after = ($dirNewer ?
'>=' : '<=');
138 if ($rvstartid !== 0)
139 $conds[] = 'rev_id' . $after . intval($rvstartid);
141 $conds[] = 'rev_id' . $before . intval($rvendid);
142 if (isset ($rvstart))
143 $conds[] = 'rev_timestamp' . $after . $db->addQuotes($rvstart);
145 $conds[] = 'rev_timestamp' . $before . $db->addQuotes($rvend);
147 // must manually initialize unset rvlimit
148 if (!isset ($rvlimit))
151 $this->validateLimit('rvlimit', $rvlimit, 1, $userMax, $botMax);
153 // There is only one ID, use it
154 $conds['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles()));
157 elseif ($pageCount > 0) {
158 // When working in multi-page non-enumeration mode,
159 // limit to the latest revision only
161 $conds[] = 'page_id=rev_page';
162 $conds[] = 'page_latest=rev_id';
163 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
166 $conds['page_id'] = array_keys($pageSet->getGoodTitles());
168 $rvlimit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
170 elseif ($revCount > 0) {
171 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
173 // Get all revision IDs
174 $conds['rev_id'] = array_keys($pageSet->getRevisionIDs());
176 $rvlimit = $revCount; // assumption testing -- we should never get more then $revCount rows.
178 ApiBase
:: dieDebug(__METHOD__
, 'param validation?');
180 $options['LIMIT'] = $rvlimit +
1;
182 $this->profileDBIn();
183 $res = $db->select($tables, $fields, $conds, __METHOD__
, $options);
184 $this->profileDBOut();
188 while ($row = $db->fetchObject($res)) {
190 if (++
$count > $rvlimit) {
191 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
193 ApiBase
:: dieDebug(__METHOD__
, 'Got more rows then expected'); // bug report
195 $startStr = 'rvstartid=' . $row->rev_id
;
197 'continue' => $startStr
199 $this->getResult()->addValue('query-status', 'revisions', $msg);
204 'revid' => intval($row->rev_id
205 ), 'oldid' => intval($row->rev_text_id
));
207 if ($row->rev_minor_edit
) {
212 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
215 $vals['user'] = $row->rev_user_text
;
221 $vals['comment'] = $row->rev_comment
;
224 ApiResult
:: setContent($vals, Revision
:: getRevisionText($row));
227 $this->getResult()->addValue(array (
230 intval($row->rev_page
231 ), 'revisions'), intval($row->rev_id
), $vals);
233 $db->freeResult($res);
235 // Ensure that all revisions are shown as '<r>' elements
236 $data = & $this->getResultData();
237 foreach ($data['query']['pages'] as & $page) {
238 if (is_array($page) && array_key_exists('revisions', $page)) {
239 ApiResult
:: setIndexedTagName($page['revisions'], 'rev');
244 protected function getAllowedParams() {
247 ApiBase
:: PARAM_ISMULTI
=> true,
248 ApiBase
:: PARAM_TYPE
=> array (
256 ApiBase
:: PARAM_DFLT
=> 0,
257 ApiBase
:: PARAM_TYPE
=> 'limit',
258 ApiBase
:: PARAM_MIN
=> 0,
259 ApiBase
:: PARAM_MAX1
=> 50,
260 ApiBase
:: PARAM_MAX2
=> 500
265 ApiBase
:: PARAM_TYPE
=> 'timestamp'
268 ApiBase
:: PARAM_TYPE
=> 'timestamp'
271 ApiBase
:: PARAM_DFLT
=> 'older',
272 ApiBase
:: PARAM_TYPE
=> array (
280 protected function getParamDescription() {
282 'rvprop' => 'Which properties to get for each revision: user|timestamp|comment|content',
283 'rvlimit' => 'limit how many revisions will be returned (enum)',
284 'rvstartid' => 'from which revision id to start enumeration (enum)',
285 'rvendid' => 'stop revision enumeration on this revid (enum)',
286 'rvstart' => 'from which revision timestamp to start enumeration (enum)',
287 'rvend' => 'enumerate up to this timestamp (enum)',
288 'rvdir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
292 protected function getDescription() {
294 'Get revision information.',
295 'This module may be used in several ways:',
296 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
297 ' 2) Get revisions for one given page, by using titles/pageids with rvstart/rvend/rvlimit params.',
298 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
299 'All parameters marked as (enum) may only be used with a single page (#2).'
303 protected function getExamples() {
305 'Get data with content for the last revision of titles "API" and "Main Page":',
306 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
307 'Get last 5 revisions of the "Main Page":',
308 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
309 'Get first 5 revisions of the "Main Page":',
310 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
311 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
312 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000'
316 public function getVersion() {
317 return __CLASS__
. ': $Id$';