3 * Copyright © 2011 Bryan Tong Minh <Bryan.TongMinh@Gmail.com>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * http://www.gnu.org/copyleft/gpl.html
26 class ApiFileRevert
extends ApiBase
{
31 protected $archiveName;
36 public function execute() {
37 $this->useTransactionalTimeLimit();
39 $this->params
= $this->extractRequestParams();
40 // Extract the file and archiveName from the request parameters
41 $this->validateParameters();
43 // Check whether we're allowed to revert this file
44 $this->checkTitleUserPermissions( $this->file
->getTitle(), [ 'edit', 'upload' ] );
46 $sourceUrl = $this->file
->getArchiveVirtualUrl( $this->archiveName
);
47 $status = $this->file
->upload(
49 $this->params
['comment'],
50 $this->params
['comment'],
57 if ( $status->isGood() ) {
58 $result = [ 'result' => 'Success' ];
61 'result' => 'Failure',
62 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
66 $this->getResult()->addValue( null, $this->getModuleName(), $result );
70 * Validate the user parameters and set $this->archiveName and $this->file.
71 * Throws an error if validation fails
73 protected function validateParameters() {
74 // Validate the input title
75 $title = Title
::makeTitleSafe( NS_FILE
, $this->params
['filename'] );
76 if ( is_null( $title ) ) {
78 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params
['filename'] ) ]
81 $localRepo = RepoGroup
::singleton()->getLocalRepo();
83 // Check if the file really exists
84 $this->file
= $localRepo->newFile( $title );
85 if ( !$this->file
->exists() ) {
86 $this->dieWithError( 'apierror-missingtitle' );
89 // Check if the archivename is valid for this file
90 $this->archiveName
= $this->params
['archivename'];
91 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName
);
92 if ( !$oldFile->exists() ) {
93 $this->dieWithError( 'filerevert-badversion' );
97 public function mustBePosted() {
101 public function isWriteMode() {
105 public function getAllowedParams() {
108 ApiBase
::PARAM_TYPE
=> 'string',
109 ApiBase
::PARAM_REQUIRED
=> true,
112 ApiBase
::PARAM_DFLT
=> '',
115 ApiBase
::PARAM_TYPE
=> 'string',
116 ApiBase
::PARAM_REQUIRED
=> true,
121 public function needsToken() {
125 protected function getExamplesMessages() {
127 'action=filerevert&filename=Wiki.png&comment=Revert&' .
128 'archivename=20110305152740!Wiki.png&token=123ABC'
129 => 'apihelp-filerevert-example-revert',