Merge "Typo fix"
[mediawiki.git] / includes / api / ApiFileRevert.php
blobcbb2ba6a354305c72970c8461fdcb0fcd951023c
1 <?php
2 /**
5 * Created on March 5, 2011
7 * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
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.
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.
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
24 * @file
27 /**
28 * @ingroup API
30 class ApiFileRevert extends ApiBase {
32 /**
33 * @var File
35 protected $file;
36 protected $archiveName;
38 protected $params;
40 public function execute() {
41 $this->params = $this->extractRequestParams();
42 // Extract the file and archiveName from the request parameters
43 $this->validateParameters();
45 // Check whether we're allowed to revert this file
46 $this->checkPermissions( $this->getUser() );
48 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
49 $status = $this->file->upload( $sourceUrl, $this->params['comment'], $this->params['comment'], 0, false, false, $this->getUser() );
51 if ( $status->isGood() ) {
52 $result = array( 'result' => 'Success' );
53 } else {
54 $result = array(
55 'result' => 'Failure',
56 'errors' => $this->getResult()->convertStatusToArray( $status ),
60 $this->getResult()->addValue( null, $this->getModuleName(), $result );
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.
69 protected function checkPermissions( $user ) {
70 $title = $this->file->getTitle();
71 $permissionErrors = array_merge(
72 $title->getUserPermissionsErrors( 'edit', $user ),
73 $title->getUserPermissionsErrors( 'upload', $user )
76 if ( $permissionErrors ) {
77 $this->dieUsageMsg( $permissionErrors[0] );
81 /**
82 * Validate the user parameters and set $this->archiveName and $this->file.
83 * Throws an error if validation fails
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'] ) );
91 $localRepo = RepoGroup::singleton()->getLocalRepo();
93 // Check if the file really exists
94 $this->file = $localRepo->newFile( $title );
95 if ( !$this->file->exists() ) {
96 $this->dieUsageMsg( 'notanarticle' );
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' );
107 public function mustBePosted() {
108 return true;
111 public function isWriteMode() {
112 return true;
115 public function getAllowedParams() {
116 return array(
117 'filename' => array(
118 ApiBase::PARAM_TYPE => 'string',
119 ApiBase::PARAM_REQUIRED => true,
121 'comment' => array(
122 ApiBase::PARAM_DFLT => '',
124 'archivename' => array(
125 ApiBase::PARAM_TYPE => 'string',
126 ApiBase::PARAM_REQUIRED => true,
128 'token' => array(
129 ApiBase::PARAM_TYPE => 'string',
130 ApiBase::PARAM_REQUIRED => true
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',
145 public function getResultProperties() {
146 return array(
147 '' => array(
148 'result' => array(
149 ApiBase::PROP_TYPE => array(
150 'Success',
151 'Failure'
154 'errors' => array(
155 ApiBase::PROP_TYPE => 'string',
156 ApiBase::PROP_NULLABLE => true
162 public function getDescription() {
163 return array(
164 'Revert a file to an old version'
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' ),
180 public function needsToken() {
181 return true;
184 public function getTokenSalt() {
185 return '';
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',