4 * Created on Jul 2, 2007
6 * API for MediaWiki 1.8+
8 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
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 * Query module to enumerate all available pages.
36 class ApiQueryDeletedrevs
extends ApiQueryBase
{
38 public function __construct($query, $moduleName) {
39 parent
:: __construct($query, $moduleName, 'dr');
42 public function execute() {
45 // Before doing anything at all, let's check permissions
46 if(!$wgUser->isAllowed('deletedhistory'))
47 $this->dieUsage('You don\'t have permission to view deleted revision information', 'permissiondenied');
50 $params = $this->extractRequestParams(false);
51 $prop = array_flip($params['prop']);
52 $fld_revid = isset($prop['revid']);
53 $fld_user = isset($prop['user']);
54 $fld_comment = isset($prop['comment']);
55 $fld_minor = isset($prop['minor']);
56 $fld_len = isset($prop['len']);
57 $fld_content = isset($prop['content']);
58 $fld_token = isset($prop['token']);
60 $result = $this->getResult();
61 $pageSet = $this->getPageSet();
62 $titles = $pageSet->getTitles();
65 // This module operates in three modes:
66 // 'revs': List deleted revs for certain titles
67 // 'user': List deleted revs by a certain user
68 // 'all': List all deleted revs
70 if(count($titles) > 0)
72 else if(!is_null($params['user']))
75 if(!is_null($params['user']) && !is_null($params['excludeuser']))
76 $this->dieUsage('user and excludeuser cannot be used together', 'badparams');
78 $this->addTables('archive');
79 $this->addFields(array('ar_title', 'ar_namespace', 'ar_timestamp'));
81 $this->addFields('ar_rev_id');
83 $this->addFields('ar_user_text');
85 $this->addFields('ar_comment');
87 $this->addFields('ar_minor_edit');
89 $this->addFields('ar_len');
92 $this->addTables('text');
93 $this->addFields(array('ar_text', 'ar_text_id', 'old_text', 'old_flags'));
94 $this->addWhere('ar_text_id = old_id');
96 // This also means stricter restrictions
97 if(!$wgUser->isAllowed('undelete'))
98 $this->dieUsage('You don\'t have permission to view deleted revision content', 'permissiondenied');
101 $userMax = $fld_content ? ApiBase
:: LIMIT_SML1
: ApiBase
:: LIMIT_BIG1
;
102 $botMax = $fld_content ? ApiBase
:: LIMIT_SML2
: ApiBase
:: LIMIT_BIG2
;
104 $limit = $params['limit'];
106 if( $limit == 'max' ) {
107 $limit = $this->getMain()->canApiHighLimits() ?
$botMax : $userMax;
108 $this->getResult()->addValue( 'limits', $this->getModuleName(), $limit );
111 $this->validateLimit('limit', $limit, 1, $userMax, $botMax);
114 // Undelete tokens are identical for all pages, so we cache one here
115 $token = $wgUser->editToken();
117 // We need a custom WHERE clause that matches all titles.
120 $lb = new LinkBatch($titles);
121 $where = $lb->constructSet('ar', $db);
122 $this->addWhere($where);
124 elseif($mode == 'all')
126 $this->addWhereFld('ar_namespace', $params['namespace']);
127 if(!is_null($params['from']))
129 $from = $this->getDB()->strencode($this->titleToKey($params['from']));
130 $this->addWhere("ar_title >= '$from'");
134 if(!is_null($params['user'])) {
135 $this->addWhereFld('ar_user_text', $params['user']);
136 } elseif(!is_null($params['excludeuser'])) {
137 $this->addWhere('ar_user_text != ' .
138 $this->getDB()->addQuotes($params['excludeuser']));
141 if(!is_null($params['continue']) && $mode == 'all')
143 $cont = explode('|', $params['continue']);
144 if(count($cont) != 2)
145 $this->dieUsage("Invalid continue param. You should pass the original value returned by the previous query", "badcontinue");
146 $title = $this->getDB()->strencode($this->titleToKey($cont[0]));
147 $ts = $this->getDB()->strencode($cont[1]);
148 $this->addWhere("ar_title > '$title' OR " .
149 "(ar_title = '$title' AND " .
150 "ar_timestamp >= '$ts')");
153 $this->addOption('LIMIT', $limit +
1);
154 $this->addOption('USE INDEX', array('archive' => ($mode == 'user' ?
'ar_usertext_timestamp' : 'name_title_timestamp')));
157 if($params['unique'])
159 $this->addOption('GROUP BY', 'ar_title');
160 $this->addOption('ORDER BY', 'ar_title');
163 $this->addOption('ORDER BY', 'ar_title, ar_timestamp');
169 // Sort by ns and title in the same order as timestamp for efficiency
170 $this->addWhereRange('ar_namespace', $params['dir'], null, null);
171 $this->addWhereRange('ar_title', $params['dir'], null, null);
173 $this->addWhereRange('ar_timestamp', $params['dir'], $params['start'], $params['end']);
175 $res = $this->select(__METHOD__
);
178 // First populate the $pages array
179 while($row = $db->fetchObject($res))
181 if(++
$count > $limit)
185 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->ar_title
) . '|' .
188 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->ar_timestamp
));
193 $rev['timestamp'] = wfTimestamp(TS_ISO_8601
, $row->ar_timestamp
);
195 $rev['revid'] = $row->ar_rev_id
;
197 $rev['user'] = $row->ar_user_text
;
199 $rev['comment'] = $row->ar_comment
;
201 if($row->ar_minor_edit
== 1)
204 $rev['len'] = $row->ar_len
;
206 ApiResult
::setContent($rev, Revision
::getRevisionText($row));
208 $t = Title
::makeTitle($row->ar_namespace
, $row->ar_title
);
209 if(!isset($pages[$t->getPrefixedText()]))
211 $pages[$t->getPrefixedText()] = array(
212 'title' => $t->getPrefixedText(),
213 'ns' => intval($row->ar_namespace
),
214 'revisions' => array($rev)
217 $pages[$t->getPrefixedText()]['token'] = $token;
220 $pages[$t->getPrefixedText()]['revisions'][] = $rev;
222 $db->freeResult($res);
224 // We don't want entire pagenames as keys, so let's make this array indexed
225 foreach($pages as $page)
227 $result->setIndexedTagName($page['revisions'], 'rev');
230 $result->setIndexedTagName($data, 'page');
231 $result->addValue('query', $this->getModuleName(), $data);
234 public function getAllowedParams() {
237 ApiBase
:: PARAM_TYPE
=> 'timestamp'
240 ApiBase
:: PARAM_TYPE
=> 'timestamp',
243 ApiBase
:: PARAM_TYPE
=> array(
247 ApiBase
:: PARAM_DFLT
=> 'older'
253 ApiBase
:: PARAM_TYPE
=> 'user'
255 'excludeuser' => array(
256 ApiBase
:: PARAM_TYPE
=> 'user'
258 'namespace' => array(
259 ApiBase
:: PARAM_TYPE
=> 'namespace',
260 ApiBase
:: PARAM_DFLT
=> 0,
263 ApiBase
:: PARAM_DFLT
=> 10,
264 ApiBase
:: PARAM_TYPE
=> 'limit',
265 ApiBase
:: PARAM_MIN
=> 1,
266 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
267 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
270 ApiBase
:: PARAM_DFLT
=> 'user|comment',
271 ApiBase
:: PARAM_TYPE
=> array(
280 ApiBase
:: PARAM_ISMULTI
=> true
285 public function getParamDescription() {
287 'start' => 'The timestamp to start enumerating from. (1,2)',
288 'end' => 'The timestamp to stop enumerating at. (1,2)',
289 'dir' => 'The direction in which to enumerate. (1,2)',
290 'limit' => 'The maximum amount of revisions to list',
291 'prop' => 'Which properties to get',
292 'namespace' => 'Only list pages in this namespace (3)',
293 'user' => 'Only list revisions by this user',
294 'excludeuser' => 'Don\'t list revisions by this user',
295 'from' => 'Start listing at this title (3)',
296 'continue' => 'When more results are available, use this to continue (3)',
297 'unique' => 'List only one revision for each page (3)',
301 public function getDescription() {
302 return array( 'List deleted revisions.',
303 'This module operates in three modes:',
304 '1) List deleted revisions for the given title(s), sorted by timestamp',
305 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
306 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
307 'Certain parameters only apply to some modes and are ignored in others.',
308 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
312 protected function getExamples() {
314 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
315 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
316 'List the last 50 deleted contributions by Bob (mode 2):',
317 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
318 'List the first 50 deleted revisions in the main namespace (mode 3):',
319 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
320 'List the first 50 deleted pages in the Talk namespace (mode 3):',
321 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
325 public function getVersion() {
326 return __CLASS__
. ': $Id$';