9ae309a59b5a6ea824f91b3515ab6f1aa510755c
[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 // First check permission to upload/revert
53 $this->checkPermissions( $wgUser );
54
55 $this->params = $this->extractRequestParams();
56 $this->validateParameters();
57
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 $permission = $user->isAllowed( 'edit' ) && $user->isAllowed( 'upload' );
82
83 if ( $permission !== true ) {
84 if ( !$user->isLoggedIn() ) {
85 $this->dieUsageMsg( array( 'mustbeloggedin', 'upload' ) );
86 } else {
87 $this->dieUsageMsg( array( 'badaccess-groups' ) );
88 }
89 }
90 }
91
92 /**
93 * Validate the user parameters and set $this->archiveName and $this->file.
94 * Throws an error if validation fails
95 */
96 protected function validateParameters() {
97 // Validate the input title
98 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
99 if ( is_null( $title ) ) {
100 $this->dieUsageMsg( array( 'invalidtitle', $this->params['filename'] ) );
101 }
102 // Check if the file really exists
103 $this->file = wfLocalFile( $title );
104 if ( !$this->file->exists() ) {
105 $this->dieUsageMsg( array( 'notanarticle' ) );
106 }
107
108 // Check if the archivename is valid for this file
109 $this->archiveName = $this->params['archivename'];
110 $oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName );
111 if ( !$oldFile->exists() ) {
112 $this->dieUsageMsg( array( 'filerevert-badversion' ) );
113 }
114 }
115
116
117
118 public function mustBePosted() {
119 return true;
120 }
121
122 public function isWriteMode() {
123 return true;
124 }
125
126 public function getAllowedParams() {
127 return array(
128 'filename' => array(
129 ApiBase::PARAM_TYPE => 'string',
130 ApiBase::PARAM_REQUIRED => true,
131 ),
132 'comment' => array(
133 ApiBase::PARAM_DFLT => '',
134 ),
135 'archivename' => array(
136 ApiBase::PARAM_TYPE => 'string',
137 ApiBase::PARAM_REQUIRED => true,
138 ),
139 'token' => null,
140 );
141
142 }
143
144 public function getParamDescription() {
145 $params = array(
146 'filename' => 'Target filename',
147 'token' => 'Edit token. You can get one of these through prop=info',
148 'comment' => 'Upload comment',
149 'archivename' => 'Archive name of the revision to revert to',
150 );
151
152 return $params;
153
154 }
155
156 public function getDescription() {
157 return array(
158 'Revert a file to an old version'
159 );
160 }
161
162 public function getPossibleErrors() {
163 return array_merge( parent::getPossibleErrors(),
164 array(
165 array( 'mustbeloggedin', 'upload' ),
166 array( 'badaccess-groups' ),
167 array( 'invalidtitle', 'title' ),
168 array( 'notanarticle' ),
169 array( 'filerevert-badversion' ),
170 )
171 );
172 }
173
174 public function needsToken() {
175 return true;
176 }
177
178 public function getTokenSalt() {
179 return '';
180 }
181
182 protected function getExamples() {
183 return array(
184 'Revert Wiki.png to the version of 20110305152740:',
185 ' api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\',
186 );
187 }
188
189 public function getVersion() {
190 return __CLASS__ . ': $Id$';
191 }
192 }