Checking permissions for $wgUser while doing an edit with another user is not a good...
[lhc/web/wiklou.git] / includes / api / ApiUndelete.php
1 <?php
2 /**
3 * API for MediaWiki 1.8+
4 *
5 * Created on Jul 3, 2007
6 *
7 * Copyright © 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 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * http://www.gnu.org/copyleft/gpl.html
23 *
24 * @file
25 */
26
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
30 }
31
32 /**
33 * @ingroup API
34 */
35 class ApiUndelete extends ApiBase {
36
37 public function __construct( $main, $action ) {
38 parent::__construct( $main, $action );
39 }
40
41 public function execute() {
42 global $wgUser;
43 $params = $this->extractRequestParams();
44
45 if ( !$wgUser->isAllowed( 'undelete' ) ) {
46 $this->dieUsageMsg( array( 'permdenied-undelete' ) );
47 }
48
49 if ( $wgUser->isBlocked() ) {
50 $this->dieUsageMsg( array( 'blockedtext' ) );
51 }
52
53 $titleObj = Title::newFromText( $params['title'] );
54 if ( !$titleObj ) {
55 $this->dieUsageMsg( array( 'invalidtitle', $params['title'] ) );
56 }
57
58 // Convert timestamps
59 if ( !isset( $params['timestamps'] ) ) {
60 $params['timestamps'] = array();
61 }
62 if ( !is_array( $params['timestamps'] ) ) {
63 $params['timestamps'] = array( $params['timestamps'] );
64 }
65 foreach ( $params['timestamps'] as $i => $ts ) {
66 $params['timestamps'][$i] = wfTimestamp( TS_MW, $ts );
67 }
68
69 $pa = new PageArchive( $titleObj );
70 $retval = $pa->undelete( ( isset( $params['timestamps'] ) ? $params['timestamps'] : array() ), $params['reason'] );
71 if ( !is_array( $retval ) ) {
72 $this->dieUsageMsg( array( 'cannotundelete' ) );
73 }
74
75 if ( $retval[1] ) {
76 wfRunHooks( 'FileUndeleteComplete',
77 array( $titleObj, array(), $wgUser, $params['reason'] ) );
78 }
79
80 $this->setWatch( $params['watchlist'], $titleObj );
81
82 $info['title'] = $titleObj->getPrefixedText();
83 $info['revisions'] = intval( $retval[0] );
84 $info['fileversions'] = intval( $retval[1] );
85 $info['reason'] = intval( $retval[2] );
86 $this->getResult()->addValue( null, $this->getModuleName(), $info );
87 }
88
89 public function mustBePosted() {
90 return true;
91 }
92
93 public function isWriteMode() {
94 return true;
95 }
96
97 public function getAllowedParams() {
98 return array(
99 'title' => array(
100 ApiBase::PARAM_TYPE => 'string',
101 ApiBase::PARAM_REQUIRED => true
102 ),
103 'token' => null,
104 'reason' => '',
105 'timestamps' => array(
106 ApiBase::PARAM_ISMULTI => true
107 ),
108 'watchlist' => array(
109 ApiBase::PARAM_DFLT => 'preferences',
110 ApiBase::PARAM_TYPE => array(
111 'watch',
112 'unwatch',
113 'preferences',
114 'nochange'
115 ),
116 ),
117 );
118 }
119
120 public function getParamDescription() {
121 return array(
122 'title' => 'Title of the page you want to restore',
123 'token' => 'An undelete token previously retrieved through list=deletedrevs',
124 'reason' => 'Reason for restoring (optional)',
125 'timestamps' => 'Timestamps of the revisions to restore. If not set, all revisions will be restored.',
126 'watchlist' => 'Unconditionally add or remove the page from your watchlist, use preferences or do not change watch',
127 );
128 }
129
130 public function getDescription() {
131 return array(
132 'Restore certain revisions of a deleted page. A list of deleted revisions (including timestamps) can be',
133 'retrieved through list=deletedrevs'
134 );
135 }
136
137 public function getPossibleErrors() {
138 return array_merge( parent::getPossibleErrors(), array(
139 array( 'permdenied-undelete' ),
140 array( 'blockedtext' ),
141 array( 'invalidtitle', 'title' ),
142 array( 'cannotundelete' ),
143 ) );
144 }
145
146 public function needsToken() {
147 return true;
148 }
149
150 public function getTokenSalt() {
151 return '';
152 }
153
154 protected function getExamples() {
155 return array(
156 'api.php?action=undelete&title=Main%20Page&token=123ABC&reason=Restoring%20main%20page',
157 'api.php?action=undelete&title=Main%20Page&token=123ABC&timestamps=20070703220045|20070702194856'
158 );
159 }
160
161 public function getVersion() {
162 return __CLASS__ . ': $Id$';
163 }
164 }