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' ?
'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__
);
176 $pageMap = array(); // Maps ns&title to (fake) pageid
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 if(!isset($pageMap[$row->ar_namespace
][$row->ar_title
]))
210 $pageID = $newPageID++
;
211 $pageMap[$row->ar_namespace
][$row->ar_title
] = $pageID;
212 $t = Title
::makeTitle($row->ar_namespace
, $row->ar_title
);
214 'title' => $t->getPrefixedText(),
215 'ns' => intval($row->ar_namespace
),
216 'revisions' => array($rev)
218 $result->setIndexedTagName($a['revisions'], 'rev');
220 $a['token'] = $token;
221 $fit = $result->addValue(array('query', $this->getModuleName()), $pageID, $a);
225 $pageID = $pageMap[$row->ar_namespace
][$row->ar_title
];
226 $fit = $result->addValue(
227 array('query', $this->getModuleName(), $pageID, 'revisions'),
233 $this->setContinueEnumParameter('continue', $this->keyToTitle($row->ar_title
) . '|' .
236 $this->setContinueEnumParameter('start', wfTimestamp(TS_ISO_8601
, $row->ar_timestamp
));
240 $db->freeResult($res);
241 $result->setIndexedTagName_internal(array('query', $this->getModuleName()), 'page');
244 public function getAllowedParams() {
247 ApiBase
:: PARAM_TYPE
=> 'timestamp'
250 ApiBase
:: PARAM_TYPE
=> 'timestamp',
253 ApiBase
:: PARAM_TYPE
=> array(
257 ApiBase
:: PARAM_DFLT
=> 'older'
263 ApiBase
:: PARAM_TYPE
=> 'user'
265 'excludeuser' => array(
266 ApiBase
:: PARAM_TYPE
=> 'user'
268 'namespace' => array(
269 ApiBase
:: PARAM_TYPE
=> 'namespace',
270 ApiBase
:: PARAM_DFLT
=> 0,
273 ApiBase
:: PARAM_DFLT
=> 10,
274 ApiBase
:: PARAM_TYPE
=> 'limit',
275 ApiBase
:: PARAM_MIN
=> 1,
276 ApiBase
:: PARAM_MAX
=> ApiBase
:: LIMIT_BIG1
,
277 ApiBase
:: PARAM_MAX2
=> ApiBase
:: LIMIT_BIG2
280 ApiBase
:: PARAM_DFLT
=> 'user|comment',
281 ApiBase
:: PARAM_TYPE
=> array(
290 ApiBase
:: PARAM_ISMULTI
=> true
295 public function getParamDescription() {
297 'start' => 'The timestamp to start enumerating from. (1,2)',
298 'end' => 'The timestamp to stop enumerating at. (1,2)',
299 'dir' => 'The direction in which to enumerate. (1,2)',
300 'limit' => 'The maximum amount of revisions to list',
301 'prop' => 'Which properties to get',
302 'namespace' => 'Only list pages in this namespace (3)',
303 'user' => 'Only list revisions by this user',
304 'excludeuser' => 'Don\'t list revisions by this user',
305 'from' => 'Start listing at this title (3)',
306 'continue' => 'When more results are available, use this to continue (3)',
307 'unique' => 'List only one revision for each page (3)',
311 public function getDescription() {
312 return array( 'List deleted revisions.',
313 'This module operates in three modes:',
314 '1) List deleted revisions for the given title(s), sorted by timestamp',
315 '2) List deleted contributions for the given user, sorted by timestamp (no titles specified)',
316 '3) List all deleted revisions in the given namespace, sorted by title and timestamp (no titles specified, druser not set)',
317 'Certain parameters only apply to some modes and are ignored in others.',
318 'For instance, a parameter marked (1) only applies to mode 1 and is ignored in modes 2 and 3.',
322 protected function getExamples() {
324 'List the last deleted revisions of Main Page and Talk:Main Page, with content (mode 1):',
325 ' api.php?action=query&list=deletedrevs&titles=Main%20Page|Talk:Main%20Page&drprop=user|comment|content',
326 'List the last 50 deleted contributions by Bob (mode 2):',
327 ' api.php?action=query&list=deletedrevs&druser=Bob&drlimit=50',
328 'List the first 50 deleted revisions in the main namespace (mode 3):',
329 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50',
330 'List the first 50 deleted pages in the Talk namespace (mode 3):',
331 ' api.php?action=query&list=deletedrevs&drdir=newer&drlimit=50&drnamespace=1&drunique',
335 public function getVersion() {
336 return __CLASS__
. ': $Id$';