* Removed inconsistent hard coding of colons
[mediawiki.git] / includes / api / ApiQueryRevisions.php
blobc07b938cf607a773f0454a5a7b6e7957542f0a66
1 <?php
3 /*
4 * Created on Sep 7, 2006
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2006 Yuri Astrakhan <FirstnameLastname@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');
31 /**
32 * @addtogroup API
34 class ApiQueryRevisions extends ApiQueryBase {
36 public function __construct($query, $moduleName) {
37 parent :: __construct($query, $moduleName, 'rv');
40 public function execute() {
41 $limit = $startid = $endid = $start = $end = $dir = $prop = null;
42 extract($this->extractRequestParams());
44 // If any of those parameters are used, work in 'enumeration' mode.
45 // Enum mode can only be used when exactly one page is provided.
46 // Enumerating revisions on multiple pages make it extremelly
47 // difficult to manage continuations and require additional sql indexes
48 $enumRevMode = (!is_null($limit) || !is_null($startid) || !is_null($endid) || $dir === 'newer' || !is_null($start) || !is_null($end));
50 $pageSet = $this->getPageSet();
51 $pageCount = $pageSet->getGoodTitleCount();
52 $revCount = $pageSet->getRevisionCount();
54 // Optimization -- nothing to do
55 if ($revCount === 0 && $pageCount === 0)
56 return;
58 if ($revCount > 0 && $enumRevMode)
59 $this->dieUsage('The revids= parameter may not be used with the list options (limit, startid, endid, dirNewer, start, end).', 'revids');
61 if ($pageCount > 1 && $enumRevMode)
62 $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');
64 $this->addTables('revision');
65 $this->addFields(array (
66 'rev_id',
67 'rev_page',
68 'rev_text_id',
69 'rev_minor_edit'
70 ));
71 $this->addWhere('rev_deleted=0');
73 $showContent = false;
75 if (!is_null($prop)) {
76 $prop = array_flip($prop);
77 $this->addFieldsIf('rev_timestamp', isset ($prop['timestamp']));
78 $this->addFieldsIf('rev_comment', isset ($prop['comment']));
79 if (isset ($prop['user'])) {
80 $this->addFields('rev_user');
81 $this->addFields('rev_user_text');
83 if (isset ($prop['content'])) {
84 $this->addTables('text');
85 $this->addWhere('rev_text_id=old_id');
86 $this->addFields('old_id');
87 $this->addFields('old_text');
88 $this->addFields('old_flags');
89 $showContent = true;
93 $userMax = ($showContent ? 50 : 500);
94 $botMax = ($showContent ? 200 : 10000);
96 if ($enumRevMode) {
98 // This is mostly to prevent parameter errors (and optimize sql?)
99 if (!is_null($startid) && !is_null($start))
100 $this->dieUsage('start and startid cannot be used together', 'badparams');
102 if (!is_null($endid) && !is_null($end))
103 $this->dieUsage('end and endid cannot be used together', 'badparams');
105 // This code makes an assumption that sorting by rev_id and rev_timestamp produces
106 // the same result. This way users may request revisions starting at a given time,
107 // but to page through results use the rev_id returned after each page.
108 // Switching to rev_id removes the potential problem of having more than
109 // one row with the same timestamp for the same page.
110 // The order needs to be the same as start parameter to avoid SQL filesort.
112 if (is_null($startid))
113 $this->addWhereRange('rev_timestamp', $dir, $start, $end);
114 else
115 $this->addWhereRange('rev_id', $dir, $startid, $endid);
117 // must manually initialize unset limit
118 if (is_null($limit))
119 $limit = 10;
120 $this->validateLimit($this->encodeParamName('limit'), $limit, 1, $userMax, $botMax);
122 // There is only one ID, use it
123 $this->addWhereFld('rev_page', current(array_keys($pageSet->getGoodTitles())));
125 elseif ($pageCount > 0) {
126 // When working in multi-page non-enumeration mode,
127 // limit to the latest revision only
128 $this->addTables('page');
129 $this->addWhere('page_id=rev_page');
130 $this->addWhere('page_latest=rev_id');
131 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
133 // Get all page IDs
134 $this->addWhereFld('page_id', array_keys($pageSet->getGoodTitles()));
136 $limit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
138 elseif ($revCount > 0) {
139 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
141 // Get all revision IDs
142 $this->addWhereFld('rev_id', array_keys($pageSet->getRevisionIDs()));
144 $limit = $revCount; // assumption testing -- we should never get more then $revCount rows.
145 } else
146 ApiBase :: dieDebug(__METHOD__, 'param validation?');
148 $this->addOption('LIMIT', $limit +1);
150 $data = array ();
151 $count = 0;
152 $res = $this->select(__METHOD__);
154 $db = $this->getDB();
155 while ($row = $db->fetchObject($res)) {
157 if (++ $count > $limit) {
158 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
159 if (!$enumRevMode)
160 ApiBase :: dieDebug(__METHOD__, 'Got more rows then expected'); // bug report
161 $this->setContinueEnumParameter('startid', $row->rev_id);
162 break;
165 $vals = $this->addRowInfo('rev', $row);
166 if ($vals) {
167 if ($showContent)
168 ApiResult :: setContent($vals, Revision :: getRevisionText($row));
170 $this->getResult()->addValue(array (
171 'query',
172 'pages',
173 intval($row->rev_page
174 ), 'revisions'), intval($row->rev_id), $vals);
177 $db->freeResult($res);
179 // Ensure that all revisions are shown as '<rev>' elements
180 $result = $this->getResult();
181 if ($result->getIsRawMode()) {
182 $data =& $result->getData();
183 foreach ($data['query']['pages'] as & $page) {
184 if (is_array($page) && array_key_exists('revisions', $page)) {
185 $result->setIndexedTagName($page['revisions'], 'rev');
191 protected function getAllowedParams() {
192 return array (
193 'prop' => array (
194 ApiBase :: PARAM_ISMULTI => true,
195 ApiBase :: PARAM_TYPE => array (
196 'timestamp',
197 'user',
198 'comment',
199 'content'
202 'limit' => array (
203 ApiBase :: PARAM_TYPE => 'limit',
204 ApiBase :: PARAM_MIN => 1,
205 ApiBase :: PARAM_MAX1 => ApiBase :: LIMIT_SML1,
206 ApiBase :: PARAM_MAX2 => ApiBase :: LIMIT_SML2
208 'startid' => array (
209 ApiBase :: PARAM_TYPE => 'integer'
211 'endid' => array (
212 ApiBase :: PARAM_TYPE => 'integer'
214 'start' => array (
215 ApiBase :: PARAM_TYPE => 'timestamp'
217 'end' => array (
218 ApiBase :: PARAM_TYPE => 'timestamp'
220 'dir' => array (
221 ApiBase :: PARAM_DFLT => 'older',
222 ApiBase :: PARAM_TYPE => array (
223 'newer',
224 'older'
230 protected function getParamDescription() {
231 return array (
232 'prop' => 'Which properties to get for each revision.',
233 'limit' => 'limit how many revisions will be returned (enum)',
234 'startid' => 'from which revision id to start enumeration (enum)',
235 'endid' => 'stop revision enumeration on this revid (enum)',
236 'start' => 'from which revision timestamp to start enumeration (enum)',
237 'end' => 'enumerate up to this timestamp (enum)',
238 'dir' => 'direction of enumeration - towards "newer" or "older" revisions (enum)'
242 protected function getDescription() {
243 return array (
244 'Get revision information.',
245 'This module may be used in several ways:',
246 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
247 ' 2) Get revisions for one given page, by using titles/pageids with start/end/limit params.',
248 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.',
249 'All parameters marked as (enum) may only be used with a single page (#2).'
253 protected function getExamples() {
254 return array (
255 'Get data with content for the last revision of titles "API" and "Main Page":',
256 ' api.php?action=query&prop=revisions&titles=API|Main%20Page&rvprop=timestamp|user|comment|content',
257 'Get last 5 revisions of the "Main Page":',
258 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment',
259 'Get first 5 revisions of the "Main Page":',
260 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer',
261 'Get first 5 revisions of the "Main Page" made after 2006-05-01:',
262 ' api.php?action=query&prop=revisions&titles=Main%20Page&rvlimit=5&rvprop=timestamp|user|comment&rvdir=newer&rvstart=20060501000000'
266 public function getVersion() {
267 return __CLASS__ . ': $Id$';