(bug 43852) Fix converttitles param in api.php?action=query
[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'], 0, false, false, $this->getUser() );
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 $title = $this->file->getTitle();
75 $permissionErrors = array_merge(
76 $title->getUserPermissionsErrors( 'edit' , $user ),
77 $title->getUserPermissionsErrors( 'upload' , $user )
78 );
79
80 if ( $permissionErrors ) {
81 $this->dieUsageMsg( $permissionErrors[0] );
82 }
83 }
84
85 /**
86 * Validate the user parameters and set $this->archiveName and $this->file.
87 * Throws an error if validation fails
88 */
89 protected function validateParameters() {
90 // Validate the input title
91 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
92 if ( is_null( $title ) ) {
93 $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
94 }
95 $localRepo = RepoGroup::singleton()->getLocalRepo();
96
97 // Check if the file really exists
98 $this->file = $localRepo->newFile( $title );
99 if ( !$this->file->exists() ) {
100 $this->dieUsageMsg( 'notanarticle' );
101 }
102
103 // Check if the archivename is valid for this file
104 $this->archiveName = $this->params['archivename'];
105 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
106 if ( !$oldFile->exists() ) {
107 $this->dieUsageMsg( 'filerevert-badversion' );
108 }
109 }
110
111 public function mustBePosted() {
112 return true;
113 }
114
115 public function isWriteMode() {
116 return true;
117 }
118
119 public function getAllowedParams() {
120 return array(
121 'filename' => array(
122 ApiBase::PARAM_TYPE => 'string',
123 ApiBase::PARAM_REQUIRED => true,
124 ),
125 'comment' => array(
126 ApiBase::PARAM_DFLT => '',
127 ),
128 'archivename' => array(
129 ApiBase::PARAM_TYPE => 'string',
130 ApiBase::PARAM_REQUIRED => true,
131 ),
132 'token' => array(
133 ApiBase::PARAM_TYPE => 'string',
134 ApiBase::PARAM_REQUIRED => true
135 ),
136 );
137
138 }
139
140 public function getParamDescription() {
141 return array(
142 'filename' => 'Target filename without the File: prefix',
143 'token' => 'Edit token. You can get one of these through prop=info',
144 'comment' => 'Upload comment',
145 'archivename' => 'Archive name of the revision to revert to',
146 );
147 }
148
149 public function getResultProperties() {
150 return array(
151 '' => array(
152 'result' => array(
153 ApiBase::PROP_TYPE => array(
154 'Success',
155 'Failure'
156 )
157 ),
158 'errors' => array(
159 ApiBase::PROP_TYPE => 'string',
160 ApiBase::PROP_NULLABLE => true
161 )
162 )
163 );
164 }
165
166 public function getDescription() {
167 return array(
168 'Revert a file to an old version'
169 );
170 }
171
172 public function getPossibleErrors() {
173 return array_merge( parent::getPossibleErrors(),
174 array(
175 array( 'mustbeloggedin', 'upload' ),
176 array( 'badaccess-groups' ),
177 array( 'invalidtitle', 'title' ),
178 array( 'notanarticle' ),
179 array( 'filerevert-badversion' ),
180 )
181 );
182 }
183
184 public function needsToken() {
185 return true;
186 }
187
188 public function getTokenSalt() {
189 return '';
190 }
191
192 public function getExamples() {
193 return array(
194 'api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\'
195 => 'Revert Wiki.png to the version of 20110305152740',
196 );
197 }
198
199 public function getVersion() {
200 return __CLASS__ . ': $Id$';
201 }
202 }