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 if ($revCount > 0 && $pageCount > 0)
56 $this->dieUsage('The rvrevids= parameter may not be used with titles, pageids, and generator options.', 'rv_rvrevids');
58 if ($revCount > 0 && $enumRevMode)
59 $this->dieUsage('The rvrevids= parameter may not be used with the list options (rvlimit, rvstartid, rvendid, dirNewer, rvstart, rvend).', 'rv_rvrevids');
61 if ($revCount === 0 && $pageCount === 0)
62 $this->dieUsage('No pages were given. Please use titles, pageids or a generator to provide page(s) to work on.', 'rv_no_pages');
64 if ($revCount === 0 && $pageCount > 1 && $enumRevMode)
65 $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');
81 $showTimestamp = $showUser = $showComment = $showContent = false;
82 if (isset ($rvprop)) {
83 foreach ($rvprop as $prop) {
86 $fields[] = 'rev_timestamp';
87 $showTimestamp = true;
90 $fields[] = 'rev_user';
91 $fields[] = 'rev_user_text';
95 $fields[] = 'rev_comment';
99 // todo: check the page count/limit when requesting content
100 //$this->validateLimit( 'content: (rvlimit*pages)+revids',
101 //$rvlimit * count($this->existingPageIds) + count($this->revIdsArray), 50, 200 );
103 $conds[] = 'rev_text_id=old_id';
104 $fields[] = 'old_id';
105 $fields[] = 'old_text';
106 $fields[] = 'old_flags';
110 $this->dieDebug("unknown rvprop $prop");
115 $userMax = ($showContent ?
50 : 500);
116 $botMax = ($showContent ?
200 : 10000);
120 // This is mostly to prevent parameter errors (and optimize sql?)
121 if ($rvstartid !== 0 && isset ($rvstart))
122 $this->dieUsage('rvstart and rvstartid cannot be used together', 'rv_badparams');
124 if ($rvendid !== 0 && isset ($rvend))
125 $this->dieUsage('rvend and rvend cannot be used together', 'rv_badparams');
127 $options['ORDER BY'] = 'rev_timestamp' . ($dirNewer ?
'' : ' DESC');
128 $before = ($dirNewer ?
'<=' : '>=');
129 $after = ($dirNewer ?
'>=' : '<=');
131 if ($rvstartid !== 0)
132 $conds[] = 'rev_id' . $after . intval($rvstartid);
134 $conds[] = 'rev_id' . $before . intval($rvendid);
135 if (isset ($rvstart))
136 $conds[] = 'rev_timestamp' . $after . $this->prepareTimestamp($rvstart);
138 $conds[] = 'rev_timestamp' . $before . $this->prepareTimestamp($rvend);
140 // must manually initialize unset rvlimit
141 if (!isset ($rvlimit))
144 $this->validateLimit('rvlimit', $rvlimit, 1, $userMax, $botMax);
146 // There is only one ID, use it
147 $conds['rev_page'] = array_pop(array_keys($data->getGoodTitles()));
150 if ($pageCount > 0) {
151 // When working in multi-page non-enumeration mode,
152 // limit to the latest revision only
154 $conds[] = 'page_id=rev_page';
155 $conds[] = 'page_latest=rev_id';
156 $this->validateLimit('page_count', $pageCount, 1, $userMax, $botMax);
159 $conds['page_id'] = array_keys($data->getGoodTitles());
161 $rvlimit = $pageCount; // assumption testing -- we should never get more then $pageCount rows.
164 $this->validateLimit('rev_count', $revCount, 1, $userMax, $botMax);
166 // Get all revision IDs
167 $conds['rev_id'] = array_keys($data->getRevisionIDs());
169 $rvlimit = $revCount; // assumption testing -- we should never get more then $revCount rows.
171 $this->dieDebug('param validation?');
173 $options['LIMIT'] = $rvlimit +
1;
175 $db = $this->getDB();
176 $this->profileDBIn();
177 $res = $db->select($tables, $fields, $conds, __CLASS__
. '::' . __FUNCTION__
, $options);
178 $this->profileDBOut();
182 while ($row = $db->fetchObject($res)) {
184 if (++
$count > $rvlimit) {
185 // We've reached the one extra which shows that there are additional pages to be had. Stop here...
187 $this->dieDebug('Got more rows then expected'); // bug report
189 $startStr = 'rvstartid=' . $row->rev_id
;
191 'continue' => $startStr
193 $this->getResult()->addMessage('query-status', 'revisions', $msg);
198 'revid' => intval($row->rev_id
199 ), 'oldid' => intval($row->rev_text_id
));
201 if ($row->rev_minor_edit
) {
206 $vals['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->rev_timestamp
);
209 $vals['user'] = $row->rev_user_text
;
215 $vals['comment'] = $row->rev_comment
;
218 $vals['xml:space'] = 'preserve';
219 $vals['*'] = Revision
:: getRevisionText($row);
221 $vals['*'] = ''; // Force all elements to be attributes
224 $data[$row->rev_page
]['revisions']['_element'] = 'rv';
225 $data[$row->rev_page
]['revisions'][$row->rev_id
] = $vals;
227 $db->freeResult($res);
229 $this->getResult()->addMessage('query', 'allpages', $data);
232 protected function getAllowedParams() {
234 'rvrevids' => array (
235 GN_ENUM_ISMULTI
=> true,
236 GN_ENUM_TYPE
=> 'integer'
240 GN_ENUM_TYPE
=> 'limit',
248 GN_ENUM_TYPE
=> 'timestamp'
251 GN_ENUM_TYPE
=> 'timestamp'
254 GN_ENUM_DFLT
=> 'older',
255 GN_ENUM_TYPE
=> array (
261 GN_ENUM_ISMULTI
=> true,
262 GN_ENUM_TYPE
=> array (
272 protected function getDescription() {
274 'Get revision information.',
275 'This module may be used in several ways:',
276 ' 1) Get data about a set of pages (last revision), by setting titles or pageids parameter.',
277 ' 2) Get revisions for one given page, by using titles/pageids with rvstart*/rvend*/rvlimit params.',
278 ' 3) Get data about a set of revisions by setting their IDs with revids parameter.'
282 protected function getExamples() {
284 'api.php?action=query&prop=revisions&titles=ArticleA&rvprop=timestamp|user|comment|content'