Oops, requestWriteMode() is in ApiMain, not ApiBase
[lhc/web/wiklou.git] / includes / api / ApiUndelete.php
1 <?php
2
3 /*
4 * Created on Jul 3, 2007
5 * API for MediaWiki 1.8+
6 *
7 * Copyright (C) 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write to the Free Software Foundation, Inc.,
21 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 */
24
25 if (!defined('MEDIAWIKI')) {
26 // Eclipse helper - will be ignored in production
27 require_once ("ApiBase.php");
28 }
29
30 /**
31 * @addtogroup API
32 */
33 class ApiUndelete extends ApiBase {
34
35 public function __construct($main, $action) {
36 parent :: __construct($main, $action);
37 }
38
39 public function execute() {
40 global $wgUser;
41 $this->getMain()->requestWriteMode();
42 $params = $this->extractRequestParams();
43
44 $titleObj = NULL;
45 if(!isset($params['title']))
46 $this->dieUsage('The title parameter must be set', 'notitle');
47 if(!isset($params['token']))
48 $this->dieUsage('The token parameter must be set', 'notoken');
49
50 if(!$wgUser->isAllowed('delete'))
51 $this->dieUsage('You don\'t have permission to restore deleted revisions', 'permissiondenied');
52 if($wgUser->isBlocked())
53 $this->dieUsage('You have been blocked from editing', 'blocked');
54 if(wfReadOnly())
55 $this->dieUsage('The wiki is in read-only mode', 'readonly');
56 if(!$wgUser->matchEditToken($params['token']))
57 $this->dieUsage('Invalid token', 'badtoken');
58
59 $titleObj = Title::newFromText($params['title']);
60 if(!$titleObj)
61 $this->dieUsage("Bad title ``{$params['title']}''", 'invalidtitle');
62
63 // Convert timestamps
64 if(!is_array($params['timestamps']))
65 $params['timestamps'] = array($params['timestamps']);
66 foreach($params['timestamps'] as $i => $ts)
67 $params['timestamps'][$i] = wfTimestamp(TS_MW, $ts);
68
69 $pa = new PageArchive($titleObj);
70 $dbw = wfGetDb(DB_MASTER);
71 $dbw->begin();
72 $retval = $pa->undelete((isset($params['timestamps']) ? $params['timestamps'] : array()), $params['reason']);
73 if(!is_array($retval))
74 switch($retval)
75 {
76 case PageArchive::UNDELETE_NOTHINGRESTORED:
77 $this->dieUsage('No revisions could be restored', 'norevs');
78 case PageArchive::UNDELETE_NOTAVAIL:
79 $this->dieUsage('Not all requested revisions could be found', 'revsnotfound');
80 case PageArchive::UNDELETE_UNKNOWNERR:
81 $this->dieUsage('Undeletion failed with unknown error', 'unknownerror');
82 }
83 $dbw->commit();
84
85 $info['title'] = $titleObj->getPrefixedText();
86 $info['revisions'] = $retval[0];
87 $info['fileversions'] = $retval[1];
88 $info['reason'] = $retval[2];
89 $this->getResult()->addValue(null, $this->getModuleName(), $info);
90 }
91
92 protected function getAllowedParams() {
93 return array (
94 'title' => null,
95 'token' => null,
96 'reason' => "",
97 'timestamps' => array(
98 ApiBase :: PARAM_ISMULTI => true
99 )
100 );
101 }
102
103 protected function getParamDescription() {
104 return array (
105 'title' => 'Title of the page you want to restore.',
106 'token' => 'An undelete token previously retrieved through list=deletedrevs',
107 'reason' => 'Reason for restoring (optional)',
108 'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.'
109 );
110 }
111
112 protected function getDescription() {
113 return array(
114 'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
115 'retrieved through list=deletedrevs'
116 );
117 }
118
119 protected function getExamples() {
120 return array (
121 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
122 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
123 );
124 }
125
126 public function getVersion() {
127 return __CLASS__ . ': $Id: ApiUndelete.php 22289 2007-05-20 23:31:44Z yurik $';
128 }
129 }