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
27 if ( !defined( 'MEDIAWIKI' ) ) {
28 // Eclipse helper - will be ignored in production
29 require_once( "ApiBase.php" );
35 class ApiFileRevert
extends ApiBase
{
41 protected $archiveName;
45 public function __construct( $main, $action ) {
46 parent
::__construct( $main, $action );
49 public function execute() {
50 $this->params
= $this->extractRequestParams();
51 // Extract the file and archiveName from the request parameters
52 $this->validateParameters();
54 // Check whether we're allowed to revert this file
55 $this->checkPermissions( $this->getUser() );
57 $sourceUrl = $this->file
->getArchiveVirtualUrl( $this->archiveName
);
58 $status = $this->file
->upload( $sourceUrl, $this->params
['comment'], $this->params
['comment'] );
60 if ( $status->isGood() ) {
61 $result = array( 'result' => 'Success' );
64 'result' => 'Failure',
65 'errors' => $this->getResult()->convertStatusToArray( $status ),
69 $this->getResult()->addValue( null, $this->getModuleName(), $result );
74 * Checks that the user has permissions to perform this revert.
75 * Dies with usage message on inadequate permissions.
76 * @param $user User The user to check.
78 protected function checkPermissions( $user ) {
79 $permissionErrors = array_merge(
80 $this->file
->getTitle()->getUserPermissionsErrors( 'edit' , $user ),
81 $this->file
->getTitle()->getUserPermissionsErrors( 'upload' , $user )
84 if ( $permissionErrors ) {
85 $this->dieUsageMsg( $permissionErrors[0] );
90 * Validate the user parameters and set $this->archiveName and $this->file.
91 * Throws an error if validation fails
93 protected function validateParameters() {
94 // Validate the input title
95 $title = Title
::makeTitleSafe( NS_FILE
, $this->params
['filename'] );
96 if ( is_null( $title ) ) {
97 $this->dieUsageMsg( array( 'invalidtitle', $this->params
['filename'] ) );
99 // Check if the file really exists
100 $this->file
= wfLocalFile( $title );
101 if ( !$this->file
->exists() ) {
102 $this->dieUsageMsg( 'notanarticle' );
105 // Check if the archivename is valid for this file
106 $this->archiveName
= $this->params
['archivename'];
107 $oldFile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName
);
108 if ( !$oldFile->exists() ) {
109 $this->dieUsageMsg( 'filerevert-badversion' );
113 public function mustBePosted() {
117 public function isWriteMode() {
121 public function getAllowedParams() {
124 ApiBase
::PARAM_TYPE
=> 'string',
125 ApiBase
::PARAM_REQUIRED
=> true,
128 ApiBase
::PARAM_DFLT
=> '',
130 'archivename' => array(
131 ApiBase
::PARAM_TYPE
=> 'string',
132 ApiBase
::PARAM_REQUIRED
=> true,
139 public function getParamDescription() {
141 'filename' => 'Target filename',
142 'token' => 'Edit token. You can get one of these through prop=info',
143 'comment' => 'Upload comment',
144 'archivename' => 'Archive name of the revision to revert to',
151 public function getDescription() {
153 'Revert a file to an old version'
157 public function getPossibleErrors() {
158 return array_merge( parent
::getPossibleErrors(),
160 array( 'mustbeloggedin', 'upload' ),
161 array( 'badaccess-groups' ),
162 array( 'invalidtitle', 'title' ),
163 array( 'notanarticle' ),
164 array( 'filerevert-badversion' ),
169 public function needsToken() {
173 public function getTokenSalt() {
177 public function getExamples() {
179 'Revert Wiki.png to the version of 20110305152740:',
180 ' api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\',
184 public function getVersion() {
185 return __CLASS__
. ': $Id$';