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
30 class ApiFileRevert
extends ApiBase
{
36 protected $archiveName;
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' );
55 'result' => 'Failure',
56 'errors' => $this->getResult()->convertStatusToArray( $status ),
60 $this->getResult()->addValue( null, $this->getModuleName(), $result );
64 * Checks that the user has permissions to perform this revert.
65 * Dies with usage message on inadequate permissions.
66 * @param $user User The user to check.
68 protected function checkPermissions( $user ) {
69 $title = $this->file
->getTitle();
70 $permissionErrors = array_merge(
71 $title->getUserPermissionsErrors( 'edit', $user ),
72 $title->getUserPermissionsErrors( 'upload', $user )
75 if ( $permissionErrors ) {
76 $this->dieUsageMsg( $permissionErrors[0] );
81 * Validate the user parameters and set $this->archiveName and $this->file.
82 * Throws an error if validation fails
84 protected function validateParameters() {
85 // Validate the input title
86 $title = Title
::makeTitleSafe( NS_FILE
, $this->params
['filename'] );
87 if ( is_null( $title ) ) {
88 $this->dieUsageMsg( array( 'invalidtitle', $this->params
['filename'] ) );
90 $localRepo = RepoGroup
::singleton()->getLocalRepo();
92 // Check if the file really exists
93 $this->file
= $localRepo->newFile( $title );
94 if ( !$this->file
->exists() ) {
95 $this->dieUsageMsg( 'notanarticle' );
98 // Check if the archivename is valid for this file
99 $this->archiveName
= $this->params
['archivename'];
100 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName
);
101 if ( !$oldFile->exists() ) {
102 $this->dieUsageMsg( 'filerevert-badversion' );
106 public function mustBePosted() {
110 public function isWriteMode() {
114 public function getAllowedParams() {
117 ApiBase
::PARAM_TYPE
=> 'string',
118 ApiBase
::PARAM_REQUIRED
=> true,
121 ApiBase
::PARAM_DFLT
=> '',
123 'archivename' => array(
124 ApiBase
::PARAM_TYPE
=> 'string',
125 ApiBase
::PARAM_REQUIRED
=> true,
128 ApiBase
::PARAM_TYPE
=> 'string',
129 ApiBase
::PARAM_REQUIRED
=> true
134 public function getParamDescription() {
136 'filename' => 'Target filename without the File: prefix',
137 'token' => 'Edit token. You can get one of these through prop=info',
138 'comment' => 'Upload comment',
139 'archivename' => 'Archive name of the revision to revert to',
143 public function getResultProperties() {
147 ApiBase
::PROP_TYPE
=> array(
153 ApiBase
::PROP_TYPE
=> 'string',
154 ApiBase
::PROP_NULLABLE
=> true
160 public function getDescription() {
162 'Revert a file to an old version'
166 public function getPossibleErrors() {
167 return array_merge( parent
::getPossibleErrors(),
169 array( 'mustbeloggedin', 'upload' ),
170 array( 'badaccess-groups' ),
171 array( 'invalidtitle', 'title' ),
172 array( 'notanarticle' ),
173 array( 'filerevert-badversion' ),
178 public function needsToken() {
182 public function getTokenSalt() {
186 public function getExamples() {
188 'api.php?action=filerevert&filename=Wiki.png&comment=Revert&archivename=20110305152740!Wiki.png&token=123ABC'
189 => 'Revert Wiki.png to the version of 20110305152740',