5f238d6134ca13ba90b3dae56ff2fa1c04ba01bb
4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <Firstname><Lastname>@gmail.com
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.
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.
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
26 if (!defined('MEDIAWIKI')) {
27 // Eclipse helper - will be ignored in production
28 require_once ('ApiQueryBase.php');
32 * A query action to enumerate revisions of a given page, or show top revisions of multiple pages.
33 * Various pieces of information may be shown - flags, comments, and the actual wiki markup of the rev.
34 * In the enumeration mode, ranges of revisions may be requested and filtered.
38 class ApiQueryRevisions
extends ApiQueryBase
{
40 public function __construct($query, $moduleName) {
41 parent
:: __construct($query, $moduleName, 'rv');
44 private $fld_timestamp = false, $fld_comment = false, $fld_user = false, $fld_content = false;
46 public function execute() {
47 $limit = $startid = $endid = $start = $end = $dir = $prop = $user = $excludeuser = null;
48 extract($this->extractRequestParams());
50 // If any of those parameters are used, work in 'enumeration' mode.
51 // Enum mode can only be used when exactly one page is provided.
52 // Enumerating revisions on multiple pages make it extremelly
53 // difficult to manage continuations and require additional sql indexes
54 $enumRevMode = (!is_null($user) ||
!is_null($excludeuser) ||
!is_null($limit) ||
!is_null($startid) ||
!is_null($endid) ||
$dir === 'newer' ||
!is_null($start) ||
!is_null($end));
56 $pageSet = $this->getPageSet();
57 $pageCount = $pageSet->getGoodTitleCount();
58 $revCount = $pageSet->getRevisionCount();
60 // Optimization -- nothing to do
61 if ($revCount === 0 && $pageCount === 0)
64 if ($revCount > 0 && $enumRevMode)
65 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
67 if ($pageCount > 1 && $enumRevMode)
68 $this->dieUsage('titles, pageids or a generator was used to supply multiple pages, but the limit, startid, endid, dirNewer, user, excludeuser, start, and end parameters may only be used on a single page.', 'multpages');
70 $this->addTables('revision');
71 $this->addFields(array (
77 $this->addWhere('rev_deleted=0');
79 if (!is_null($prop)) {
80 $prop = array_flip($prop);
81 $this->fld_timestamp
= $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
82 $this->fld_comment
= $this->addFieldsIf('rev_comment', isset ($prop['comment']));
83 if (isset ($prop['user'])) {
84 $this->addFields('rev_user');
85 $this->addFields('rev_user_text');
86 $this->fld_user
= true;
88 if (isset ($prop['content'])) {
89 $this->addTables('text');
90 $this->addWhere('rev_text_id=old_id');
91 $this->addFields('old_id');
92 $this->addFields('old_text');
93 $this->addFields('old_flags');
94 $this->fld_content
= true;
98 $userMax = ($this->fld_content ?
50 : 500);
99 $botMax = ($this->fld_content ?
200 : 10000);
103 // This is mostly to prevent parameter errors (and optimize sql?)
104 if (!is_null($startid) && !is_null($start))
105 $this->dieUsage('start and startid cannot be used together', 'badparams');
107 if (!is_null($endid) && !is_null($end))
108 $this->dieUsage('end and endid cannot be used together', 'badparams');
110 if(!is_null($user) && !is_null( $excludeuser))
111 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
113 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
114 // the same result. This way users may request revisions starting at a given time,
115 // but to page through results use the rev_id returned after each page.
116 // Switching to rev_id removes the potential problem of having more than
117 // one row with the same timestamp for the same page.
118 // The order needs to be the same as start parameter to avoid SQL filesort.
120 if (is_null($startid))
121 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
123 $this->addWhereRange('rev_id', $dir, $startid, $endid);
125 // must manually initialize unset limit
128 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
130 // There is only one ID, use it
131 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
133 if(!is_null($user)) {
134 $this->addWhere('rev_user_text', $user);
135 } elseif (!is_null( $excludeuser)) {
136 $this->addWhere('rev_user_text != ' . $this->getDB()->addQuotes($excludeuser));
139 elseif ($revCount > 0) {
140 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
142 // Get all revision IDs
143 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
145 $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
147 elseif ($pageCount > 0) {
148 // When working in multi-page non-enumeration mode,
149 // limit to the latest revision only
150 $this->addTables('page');
151 $this->addWhere('page_id=rev_page');
152 $this->addWhere('page_latest=rev_id');
153 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
156 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
158 $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
160 ApiBase
:: dieDebug(__METHOD__
, 'param validation?');
162 $this->addOption('LIMIT', $limit +
1);
166 $res = $this->select(__METHOD__
);
168 $db = $this->getDB();
169 while ($row = $db->fetchObject($res)) {
171 if (++
$count > $limit) {
172 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
174 ApiBase
:: dieDebug(__METHOD__
, 'Got more rows then expected'); // bug report
175 $this->setContinueEnumParameter('startid', $row->rev_id
);
179 $this->getResult()->addValue(
183 intval($row->rev_page
),
186 $this->extractRowInfo($row));
188 $db->freeResult($res);
190 // Ensure that all revisions are shown as '<rev>' elements
191 $result = $this->getResult();
192 if ($result->getIsRawMode()) {
193 $data =& $result->getData();
194 foreach ($data['query']['pages'] as & $page) {
195 if (is_array($page) && array_key_exists('revisions', $page)) {
196 $result->setIndexedTagName($page['revisions'], 'rev');
202 private function extractRowInfo($row) {
206 $vals['revid'] = intval( $row->rev_id
);
207 $vals['pageid'] = intval($row->rev_page
);
208 $vals['oldid'] = intval($row->rev_text_id
);
210 if ($row->rev_minor_edit
)
213 if ($this->fld_user
) {
214 $vals['user'] = $row->rev_user_text
;
219 if ($this->fld_timestamp
) {
220 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
223 if ($this->fld_comment
&& !empty ($row->rev_comment
)) {
224 $vals['comment'] = $row->rev_comment
;
227 if ($this->fld_content
) {
228 ApiResult
:: setContent($vals, Revision
:: getRevisionText($row));
234 protected function getAllowedParams() {
237 ApiBase
:: PARAM_ISMULTI
=> true,
238 ApiBase
:: PARAM_TYPE
=> array (
246 ApiBase
:: PARAM_TYPE
=> 'limit',
247 ApiBase
:: PARAM_MIN
=> 1,
248 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_SML1
,
249 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_SML2
252 ApiBase
:: PARAM_TYPE
=> 'integer'
255 ApiBase
:: PARAM_TYPE
=> 'integer'
258 ApiBase
:: PARAM_TYPE
=> 'timestamp'
261 ApiBase
:: PARAM_TYPE
=> 'timestamp'
264 ApiBase
:: PARAM_DFLT
=> 'older',
265 ApiBase
:: PARAM_TYPE
=> array (
271 ApiBase
:: PARAM_TYPE
=> 'user'
273 'excludeuser' => array(
274 ApiBase
:: PARAM_TYPE
=> 'user'
279 protected function getParamDescription() {
281 'prop' => 'Which properties to get for each revision.',
282 'limit' => 'limit how many revisions will be returned (enum)',
283 'startid' => 'from which revision id to start enumeration (enum)',
284 'endid' => 'stop revision enumeration on this revid (enum)',
285 'start' => 'from which revision timestamp to start enumeration (enum)',
286 'end' => 'enumerate up to this timestamp (enum)',
287 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)',
288 'user' => 'only include revisions made by user',
289 'excludeuser' => 'exclude revisions made by user',
293 protected function getDescription() {
295 'Get revision information.',
296 'This module may be used in several ways:',
297 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
298 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
299 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
300 'All parameters marked as (enum) may only be used with a single page (#2).'
304 protected function getExamples() {
306 'Get data with content for the last revision of titles "API" and "Main Page":',
307 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
308 'Get last 5 revisions of the "Main Page":',
309 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
310 'Get first 5 revisions of the "Main Page":',
311 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
312 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
313 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000',
314 'Get first 5 revisions of the "Main Page" that were not made made by anonymous user "127.0.0.1"',
315 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvexcludeuser=127.0.0.1',
316 'Get first 5 revisions of the "Main Page" that were made by the user "MediaWiki default"',
317 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvuser=MediaWiki%20default',
321 public function getVersion() {
322 return __CLASS__
. ': $Id$';