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());
42 // If any of those parameters are used, work in 'enumeration' mode.
43 // Enum mode can only be used when exactly one page is provided.
44 // Enumerating revisions on multiple pages make it extremelly
45 // difficult to manage continuations and require additional sql indexes
46 $enumRevMode = (!is_null($limit) ||
!is_null($startid) ||
!is_null($endid) ||
$dir === 'newer' ||
!is_null($start) ||
!is_null($end));
48 $pageSet = $this->getPageSet();
49 $pageCount = $pageSet->getGoodTitleCount();
50 $revCount = $pageSet->getRevisionCount();
52 // Optimization -- nothing to do
53 if ($revCount === 0 && $pageCount === 0)
56 if ($revCount > 0 && $enumRevMode)
57 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
59 if ($pageCount > 1 && $enumRevMode)
60 $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');
62 $this->addTables('revision');
63 $this->addFields(array (
69 $this->addWhere('rev_deleted=0');
73 if (!is_null($prop)) {
74 $prop = array_flip($prop);
75 $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
76 $this->addFieldsIf('rev_comment', isset ($prop['comment']));
77 if (isset ($prop['user'])) {
78 $this->addFields('rev_user');
79 $this->addFields('rev_user_text');
81 if (isset ($prop['content'])) {
82 $this->addTables('text');
83 $this->addWhere('rev_text_id=old_id');
84 $this->addFields('old_id');
85 $this->addFields('old_text');
86 $this->addFields('old_flags');
91 $userMax = ($showContent ?
50 : 500);
92 $botMax = ($showContent ?
200 : 10000);
96 // This is mostly to prevent parameter errors (and optimize sql?)
97 if (!is_null($startid) && !is_null($start))
98 $this->dieUsage('start and startid cannot be used together', 'badparams');
100 if (!is_null($endid) && !is_null($end))
101 $this->dieUsage('end and endid cannot be used together', 'badparams');
103 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
104 // the same result. This way users may request revisions starting at a given time,
105 // but to page through results use the rev_id returned after each page.
106 // Switching to rev_id removes the potential problem of having more than
107 // one row with the same timestamp for the same page.
108 // The order needs to be the same as start parameter to avoid SQL filesort.
110 if (is_null($startid))
111 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
113 $this->addWhereRange('rev_id', $dir, $startid, $endid);
115 // must manually initialize unset limit
118 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
120 // There is only one ID, use it
121 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
123 elseif ($pageCount > 0) {
124 // When working in multi-page non-enumeration mode,
125 // limit to the latest revision only
126 $this->addTables('page');
127 $this->addWhere('page_id=rev_page');
128 $this->addWhere('page_latest=rev_id');
129 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
132 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
134 $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
136 elseif ($revCount > 0) {
137 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
139 // Get all revision IDs
140 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
142 $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
144 ApiBase
:: dieDebug(__METHOD__
, 'param validation?');
146 $this->addOption('LIMIT', $limit +
1);
150 $res = $this->select(__METHOD__
);
152 $db = & $this->getDB();
153 while ($row = $db->fetchObject($res)) {
155 if (++
$count > $limit) {
156 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
158 ApiBase
:: dieDebug(__METHOD__
, 'Got more rows then expected'); // bug report
159 $this->setContinueEnumParameter('startid', $row->rev_id
);
163 $vals = $this->addRowInfo('rev', $row);
166 ApiResult
:: setContent($vals, Revision
:: getRevisionText($row));
168 $this->getResult()->addValue(array (
171 intval($row->rev_page
172 ), 'revisions'), intval($row->rev_id
), $vals);
175 $db->freeResult($res);
177 // Ensure that all revisions are shown as '<rev>' elements
178 $result = $this->getResult();
179 if ($result->getIsRawMode()) {
180 $data = $result->getData();
181 foreach ($data['query']['pages'] as & $page) {
182 if (is_array($page) && array_key_exists('revisions', $page)) {
183 $result->setIndexedTagName($page['revisions'], 'rev');
189 protected function getAllowedParams() {
192 ApiBase
:: PARAM_ISMULTI
=> true,
193 ApiBase
:: PARAM_TYPE
=> array (
201 ApiBase
:: PARAM_TYPE
=> 'limit',
202 ApiBase
:: PARAM_MIN
=> 1,
203 ApiBase
:: PARAM_MAX1
=> ApiBase
:: LIMIT_SML1
,
204 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_SML2
207 ApiBase
:: PARAM_TYPE
=> 'integer'
210 ApiBase
:: PARAM_TYPE
=> 'integer'
213 ApiBase
:: PARAM_TYPE
=> 'timestamp'
216 ApiBase
:: PARAM_TYPE
=> 'timestamp'
219 ApiBase
:: PARAM_DFLT
=> 'older',
220 ApiBase
:: PARAM_TYPE
=> array (
228 protected function getParamDescription() {
230 'prop' => 'Which properties to get for each revision.',
231 'limit' => 'limit how many revisions will be returned (enum)',
232 'startid' => 'from which revision id to start enumeration (enum)',
233 'endid' => 'stop revision enumeration on this revid (enum)',
234 'start' => 'from which revision timestamp to start enumeration (enum)',
235 'end' => 'enumerate up to this timestamp (enum)',
236 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
240 protected function getDescription() {
242 'Get revision information.',
243 'This module may be used in several ways:',
244 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
245 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
246 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
247 'All parameters marked as (enum) may only be used with a single page (#2).'
251 protected function getExamples() {
253 'Get data with content for the last revision of titles "API" and "Main Page":',
254 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
255 'Get last 5 revisions of the "Main Page":',
256 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
257 'Get first 5 revisions of the "Main Page":',
258 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
259 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
260 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000'
264 public function getVersion() {
265 return __CLASS__
. ': $Id$';