f35075fd7a5b04ee14f640bf29c5b997a27eb781
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());
42 // true when ordered by timestamp from older to newer, false otherwise
43 $dirNewer = ($rvdir === 'newer');
45 // If any of those parameters are used, work in 'enumeration' mode.
46 // Enum mode can only be used when exactly one page is provided.
47 // Enumerating revisions on multiple pages make it extremelly
48 // difficult to manage continuations and require additional sql indexes
49 $enumRevMode = ($rvlimit !== 0 ||
$rvstartid !== 0 ||
$rvendid !== 0 ||
$dirNewer ||
isset ($rvstart) ||
isset ($rvend));
51 $data = $this->getData();
52 $pageCount = $data->getGoodTitleCount();
53 $revCount = $data->getRevisionCount();
55 // Optimization -- nothing to do
56 if ($revCount === 0 && $pageCount === 0)
59 if ($revCount > 0 && $pageCount > 0)
60 $this->dieUsage('The revids= parameter may not be used with titles, pageids, or generator options.', 'rv_revids');
62 if ($revCount > 0 && $enumRevMode)
63 $this->dieUsage('The revids= parameter may not be used with the list options (rvlimit, rvstartid, rvendid, dirNewer, rvstart, rvend).', 'rv_revids');
65 if ($revCount === 0 && $pageCount > 1 && $enumRevMode)
66 $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');
82 $showTimestamp = $showUser = $showComment = $showContent = false;
83 if (isset ($rvprop)) {
84 foreach ($rvprop as $prop) {
87 $fields[] = 'rev_timestamp';
88 $showTimestamp = true;
91 $fields[] = 'rev_user';
92 $fields[] = 'rev_user_text';
96 $fields[] = 'rev_comment';
100 // todo: check the page count/limit when requesting content
101 //$this->validateLimit( 'content: (rvlimit*pages)+revids',
102 //$rvlimit * count($this->existingPageIds) + count($this->revIdsArray), 50, 200 );
104 $conds[] = 'rev_text_id=old_id';
105 $fields[] = 'old_id';
106 $fields[] = 'old_text';
107 $fields[] = 'old_flags';
111 ApiBase
:: dieDebug("unknown rvprop $prop");
116 $userMax = ($showContent ?
50 : 500);
117 $botMax = ($showContent ?
200 : 10000);
121 // This is mostly to prevent parameter errors (and optimize sql?)
122 if ($rvstartid !== 0 && isset ($rvstart))
123 $this->dieUsage('rvstart and rvstartid cannot be used together', 'rv_badparams');
125 if ($rvendid !== 0 && isset ($rvend))
126 $this->dieUsage('rvend and rvend cannot be used together', 'rv_badparams');
128 $options['ORDER BY'] = 'rev_timestamp' . ($dirNewer ?
'' : ' DESC');
129 $before = ($dirNewer ?
'<=' : '>=');
130 $after = ($dirNewer ?
'>=' : '<=');
132 if ($rvstartid !== 0)
133 $conds[] = 'rev_id' . $after . intval($rvstartid);
135 $conds[] = 'rev_id' . $before . intval($rvendid);
136 if (isset ($rvstart))
137 $conds[] = 'rev_timestamp' . $after . $this->prepareTimestamp($rvstart);
139 $conds[] = 'rev_timestamp' . $before . $this->prepareTimestamp($rvend);
141 // must manually initialize unset rvlimit
142 if (!isset ($rvlimit))
145 $this->validateLimit('rvlimit', $rvlimit, 1, $userMax, $botMax);
147 // There is only one ID, use it
148 $conds['rev_page'] = array_pop(array_keys($data->getGoodTitles()));
151 if ($pageCount > 0) {
152 // When working in multi-page non-enumeration mode,
153 // limit to the latest revision only
155 $conds[] = 'page_id=rev_page';
156 $conds[] = 'page_latest=rev_id';
157 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
160 $conds['page_id'] = array_keys($data->getGoodTitles());
162 $rvlimit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
165 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
167 // Get all revision IDs
168 $conds['rev_id'] = array_keys($data->getRevisionIDs());
170 $rvlimit = $revCount; // assumption testing -- we should never get more then $revCount rows.
172 ApiBase
:: dieDebug('param validation?');
174 $options['LIMIT'] = $rvlimit +
1;
176 $db = $this->getDB();
177 $this->profileDBIn();
178 $res = $db->select($tables, $fields, $conds, __CLASS__
. '::' . __FUNCTION__
, $options);
179 $this->profileDBOut();
183 while ($row = $db->fetchObject($res)) {
185 if (++
$count > $rvlimit) {
186 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
188 ApiBase
:: dieDebug('Got more rows then expected'); // bug report
190 $startStr = 'rvstartid=' . $row->rev_id
;
192 'continue' => $startStr
194 $this->getResult()->addValue('query-status', 'revisions', $msg);
199 'revid' => intval($row->rev_id
200 ), 'oldid' => intval($row->rev_text_id
));
202 if ($row->rev_minor_edit
) {
207 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
210 $vals['user'] = $row->rev_user_text
;
216 $vals['comment'] = $row->rev_comment
;
219 ApiResult
:: addContent($vals, Revision
:: getRevisionText($row));
222 $this->getResult()->addValue(array (
225 intval($row->rev_page
226 ), 'revisions'), intval($row->rev_id
), $vals);
228 $db->freeResult($res);
230 // Ensure that all revisions are shown as '<r>' elements
231 $data = & $this->getResultData();
232 foreach ($data['query']['pages'] as & $page) {
233 if (isset ($page['revisions'])) {
234 ApiResult
:: setIndexedTagName($page['revisions'], 'rev');
239 protected function getAllowedParams() {
243 GN_ENUM_TYPE
=> 'limit',
251 GN_ENUM_TYPE
=> 'timestamp'
254 GN_ENUM_TYPE
=> 'timestamp'
257 GN_ENUM_DFLT
=> 'older',
258 GN_ENUM_TYPE
=> array (
264 GN_ENUM_ISMULTI
=> true,
265 GN_ENUM_TYPE
=> array (
275 protected function getDescription() {
277 'Get revision information.',
278 'This module may be used in several ways:',
279 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
280 ' 2) Get revisions for one given page, by using titles/pageids with rvstart/rvend/rvlimit params.',
281 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.'
285 protected function getExamples() {
287 'api.php?action=query&prop=revisions&titles=Main%20Page&rvprop=timestamp|user|comment|content'