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