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) {
35 parent
:: __construct($query, $moduleName, 'rv');
38 public function execute() {
39 $limit = $startid = $endid = $start = $end = $dir = $prop = null;
40 extract($this->extractRequestParams());
44 // true when ordered by timestamp from older to newer, false otherwise
45 $dirNewer = ($dir === '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 = (!is_null($limit) ||
!is_null($startid) ||
!is_null($endid) ||
$dirNewer ||
!is_null($start) ||
!is_null($end));
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 && $enumRevMode)
62 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
64 if ($pageCount > 1 && $enumRevMode)
65 $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, start, and end parameters may only be used on a single page.', 'multpages');
81 $showTimestamp = $showUser = $showComment = $showContent = false;
82 if (!is_null($prop)) {
83 foreach ($prop as $p) {
86 $fields[] = 'rev_timestamp';
87 $showTimestamp = true;
90 $fields[] = 'rev_user';
91 $fields[] = 'rev_user_text';
95 $fields[] = 'rev_comment';
100 $where[] = 'rev_text_id=old_id';
101 $fields[] = 'old_id';
102 $fields[] = 'old_text';
103 $fields[] = 'old_flags';
107 ApiBase
:: dieDebug(__METHOD__
, "unknown prop $p");
112 $userMax = ($showContent ?
50 : 500);
113 $botMax = ($showContent ?
200 : 10000);
117 // This is mostly to prevent parameter errors (and optimize sql?)
118 if (!is_null($startid) && !is_null($start))
119 $this->dieUsage('start and startid cannot be used together', 'badparams');
121 if (!is_null($endid) && !is_null($end))
122 $this->dieUsage('end and endid cannot be used together', 'badparams');
124 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
125 // the same result. This way users may request revisions starting at a given time,
126 // but to page through results use the rev_id returned after each page.
127 // Switching to rev_id removes the potential problem of having more than
128 // one row with the same timestamp for the same page.
129 // The order needs to be the same as start parameter to avoid SQL filesort.
130 $options['ORDER BY'] = (!is_null($startid) ?
'rev_id' : 'rev_timestamp') . ($dirNewer ?
'' : ' DESC');
132 $before = ($dirNewer ?
'<=' : '>=');
133 $after = ($dirNewer ?
'>=' : '<=');
135 if (!is_null($startid))
136 $where[] = 'rev_id' . $after . intval($startid);
137 if (!is_null($endid))
138 $where[] = 'rev_id' . $before . intval($endid);
139 if (!is_null($start))
140 $where[] = 'rev_timestamp' . $after . $db->addQuotes($start);
142 $where[] = 'rev_timestamp' . $before . $db->addQuotes($end);
144 // must manually initialize unset limit
145 if (!!is_null($limit))
148 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
150 // There is only one ID, use it
151 $where['rev_page'] = array_pop(array_keys($pageSet->getGoodTitles()));
154 elseif ($pageCount > 0) {
155 // When working in multi-page non-enumeration mode,
156 // limit to the latest revision only
158 $where[] = 'page_id=rev_page';
159 $where[] = 'page_latest=rev_id';
160 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
163 $where['page_id'] = array_keys($pageSet->getGoodTitles());
165 $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
167 elseif ($revCount > 0) {
168 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
170 // Get all revision IDs
171 $where['rev_id'] = array_keys($pageSet->getRevisionIDs());
173 $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
175 ApiBase
:: dieDebug(__METHOD__
, 'param validation?');
177 $options['LIMIT'] = $limit +
1;
179 $this->profileDBIn();
180 $res = $db->select($tables, $fields, $where, __METHOD__
, $options);
181 $this->profileDBOut();
185 while ($row = $db->fetchObject($res)) {
187 if (++
$count > $limit) {
188 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
190 ApiBase
:: dieDebug(__METHOD__
, 'Got more rows then expected'); // bug report
191 $this->setContinueEnumParameter('startid', $row->rev_id
);
196 'revid' => intval($row->rev_id
197 ), 'oldid' => intval($row->rev_text_id
));
199 if ($row->rev_minor_edit
) {
204 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
207 $vals['user'] = $row->rev_user_text
;
213 $vals['comment'] = $row->rev_comment
;
216 ApiResult
:: setContent($vals, Revision
:: getRevisionText($row));
219 $this->getResult()->addValue(array (
222 intval($row->rev_page
223 ), 'revisions'), intval($row->rev_id
), $vals);
225 $db->freeResult($res);
227 // Ensure that all revisions are shown as '<r>' elements
228 $result = $this->getResult();
229 $data = & $result->getData();
230 foreach ($data['query']['pages'] as & $page) {
231 if (is_array($page) && array_key_exists('revisions', $page)) {
232 $result->setIndexedTagName($page['revisions'], 'rev');
237 protected function getAllowedParams() {
240 ApiBase
:: PARAM_ISMULTI
=> true,
241 ApiBase
:: PARAM_TYPE
=> array (
249 ApiBase
:: PARAM_TYPE
=> 'limit',
250 ApiBase
:: PARAM_MIN
=> 1,
251 ApiBase
:: PARAM_MAX1
=> ApiBase
:: LIMIT_SML1
,
252 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_SML2
255 ApiBase
:: PARAM_TYPE
=> 'integer'
258 ApiBase
:: PARAM_TYPE
=> 'integer'
261 ApiBase
:: PARAM_TYPE
=> 'timestamp'
264 ApiBase
:: PARAM_TYPE
=> 'timestamp'
267 ApiBase
:: PARAM_DFLT
=> 'older',
268 ApiBase
:: PARAM_TYPE
=> array (
276 protected function getParamDescription() {
278 'prop' => 'Which properties to get for each revision.',
279 'limit' => 'limit how many revisions will be returned (enum)',
280 'startid' => 'from which revision id to start enumeration (enum)',
281 'endid' => 'stop revision enumeration on this revid (enum)',
282 'start' => 'from which revision timestamp to start enumeration (enum)',
283 'end' => 'enumerate up to this timestamp (enum)',
284 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
288 protected function getDescription() {
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 start/end/limit 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).'
299 protected function getExamples() {
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'
312 public function getVersion() {
313 return __CLASS__
. ': $Id$';