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