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() {
52 $this->params
= $this->extractRequestParams();
53 // Extract the file and archiveName from the request parameters
54 $this->validateParameters();
56 // Check whether we're allowed to revert this file
57 $this->checkPermissions( $wgUser );
59 $sourceUrl = $this->file
->getArchiveVirtualUrl( $this->archiveName
);
60 $status = $this->file
->upload( $sourceUrl, $this->params
['comment'], $this->params
['comment'] );
62 if ( $status->isGood() ) {
63 $result = array( 'result' => 'Success' );
66 'result' => 'Failure',
67 'errors' => $this->getResult()->convertStatusToArray( $status ),
71 $this->getResult()->addValue( null, $this->getModuleName(), $result );
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.
80 protected function checkPermissions( $user ) {
81 $permissionErrors = array_merge(
82 $this->file
->getTitle()->getUserPermissionsErrors( 'edit' , $user ),
83 $this->file
->getTitle()->getUserPermissionsErrors( 'upload' , $user )
86 if ( $permissionErrors ) {
87 $this->dieUsageMsg( $permissionErrors[0] );
92 * Validate the user parameters and set $this->archiveName and $this->file.
93 * Throws an error if validation fails
95 protected function validateParameters() {
96 // Validate the input title
97 $title = Title
::makeTitleSafe( NS_FILE
, $this->params
['filename'] );
98 if ( is_null( $title ) ) {
99 $this->dieUsageMsg( array( 'invalidtitle', $this->params
['filename'] ) );
101 // Check if the file really exists
102 $this->file
= wfLocalFile( $title );
103 if ( !$this->file
->exists() ) {
104 $this->dieUsageMsg( array( 'notanarticle' ) );
107 // Check if the archivename is valid for this file
108 $this->archiveName
= $this->params
['archivename'];
109 $oldFile = RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $title, $this->archiveName
);
110 if ( !$oldFile->exists() ) {
111 $this->dieUsageMsg( array( 'filerevert-badversion' ) );
115 public function mustBePosted() {
119 public function isWriteMode() {
123 public function getAllowedParams() {
126 ApiBase
::PARAM_TYPE
=> 'string',
127 ApiBase
::PARAM_REQUIRED
=> true,
130 ApiBase
::PARAM_DFLT
=> '',
132 'archivename' => array(
133 ApiBase
::PARAM_TYPE
=> 'string',
134 ApiBase
::PARAM_REQUIRED
=> true,
141 public function getParamDescription() {
143 'filename' => 'Target filename',
144 'token' => 'Edit token. You can get one of these through prop=info',
145 'comment' => 'Upload comment',
146 'archivename' => 'Archive name of the revision to revert to',
153 public function getDescription() {
155 'Revert a file to an old version'
159 public function getPossibleErrors() {
160 return array_merge( parent
::getPossibleErrors(),
162 array( 'mustbeloggedin', 'upload' ),
163 array( 'badaccess-groups' ),
164 array( 'invalidtitle', 'title' ),
165 array( 'notanarticle' ),
166 array( 'filerevert-badversion' ),
171 public function needsToken() {
175 public function getTokenSalt() {
179 protected function getExamples() {
181 'Revert Wiki.png to the version of 20110305152740:',
182 ' api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=+\\',
186 public function getVersion() {
187 return __CLASS__
. ': $Id$';