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