Follow-up r83302: Check permissions
[lhc/web/wiklou.git] / includes / api / ApiFileRevert.php
1 <?php
2 /**
3 *
4 *
5 * Created on March 5, 2011
6 *
7 * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
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 ApiFileRevert extends ApiBase {
36
37 /**
38 * @var File
39 */
40 protected $file;
41 protected $archiveName;
42
43 protected $params;
44
45 public function __construct( $main, $action ) {
46 parent::__construct( $main, $action );
47 }
48
49 public function execute() {
50 global $wgUser;
51
52 $this->params = $this->extractRequestParams();
53 // Extract the file and archiveName from the request parameters
54 $this->validateParameters();
55
56 // Check whether we're allowed to revert this file
57 $this->checkPermissions( $wgUser );
58
59 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
60 $status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'] );
61
62 if ( $status->isGood() ) {
63 $result = array( 'result' => 'Success' );
64 } else {
65 $result = array(
66 'result' => 'Failure',
67 'errors' => $this->getResult()->convertStatusToArray( $status ),
68 );
69 }
70
71 $this->getResult()->addValue( null, $this->getModuleName(), $result );
72
73 }
74
75 /**
76 * Checks that the user has permissions to perform this revert.
77 * Dies with usage message on inadequate permissions.
78 * @param $user User The user to check.
79 */
80 protected function checkPermissions( $user ) {
81 $permissionErrors = array_merge(
82 $this->file->getTitle()->getUserPermissionsErrors( 'edit' , $user ),
83 $this->file->getTitle()->getUserPermissionsErrors( 'upload' , $user )
84 );
85
86 if ( $permissionErrors ) {
87 $this->dieUsageMsg( $permissionErrors[0] );
88 }
89
90
91 }
92
93 /**
94 * Validate the user parameters and set $this->archiveName and $this->file.
95 * Throws an error if validation fails
96 */
97 protected function validateParameters() {
98 // Validate the input title
99 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
100 if ( is_null( $title ) ) {
101 $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
102 }
103 // Check if the file really exists
104 $this->file = wfLocalFile( $title );
105 if ( !$this->file->exists() ) {
106 $this->dieUsageMsg( array( 'notanarticle' ) );
107 }
108
109 // Check if the archivename is valid for this file
110 $this->archiveName = $this->params['archivename'];
111 $oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName );
112 if ( !$oldFile->exists() ) {
113 $this->dieUsageMsg( array( 'filerevert-badversion' ) );
114 }
115 }
116
117 public function mustBePosted() {
118 return true;
119 }
120
121 public function isWriteMode() {
122 return true;
123 }
124
125 public function getAllowedParams() {
126 return array(
127 'filename' => array(
128 ApiBase::PARAM_TYPE => 'string',
129 ApiBase::PARAM_REQUIRED => true,
130 ),
131 'comment' => array(
132 ApiBase::PARAM_DFLT => '',
133 ),
134 'archivename' => array(
135 ApiBase::PARAM_TYPE => 'string',
136 ApiBase::PARAM_REQUIRED => true,
137 ),
138 'token' => null,
139 );
140
141 }
142
143 public function getParamDescription() {
144 $params = array(
145 'filename' => 'Target filename',
146 'token' => 'Edit token. You can get one of these through prop=info',
147 'comment' => 'Upload comment',
148 'archivename' => 'Archive name of the revision to revert to',
149 );
150
151 return $params;
152
153 }
154
155 public function getDescription() {
156 return array(
157 'Revert a file to an old version'
158 );
159 }
160
161 public function getPossibleErrors() {
162 return array_merge( parent::getPossibleErrors(),
163 array(
164 array( 'mustbeloggedin', 'upload' ),
165 array( 'badaccess-groups' ),
166 array( 'invalidtitle', 'title' ),
167 array( 'notanarticle' ),
168 array( 'filerevert-badversion' ),
169 )
170 );
171 }
172
173 public function needsToken() {
174 return true;
175 }
176
177 public function getTokenSalt() {
178 return '';
179 }
180
181 protected function getExamples() {
182 return array(
183 'Revert Wiki.png to the version of 20110305152740:',
184 ' api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\',
185 );
186 }
187
188 public function getVersion() {
189 return __CLASS__ . ': $Id$';
190 }
191 }