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
{
35 protected $archiveName;
40 public function execute() {
41 $this->useTransactionalTimeLimit();
43 $this->params
= $this->extractRequestParams();
44 // Extract the file and archiveName from the request parameters
45 $this->validateParameters();
47 // Check whether we're allowed to revert this file
48 $this->checkTitleUserPermissions( $this->file
->getTitle(), [ 'edit', 'upload' ] );
50 $sourceUrl = $this->file
->getArchiveVirtualUrl( $this->archiveName
);
51 $status = $this->file
->upload(
53 $this->params
['comment'],
54 $this->params
['comment'],
61 if ( $status->isGood() ) {
62 $result = [ 'result' => 'Success' ];
65 'result' => 'Failure',
66 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
70 $this->getResult()->addValue( null, $this->getModuleName(), $result );
74 * Validate the user parameters and set $this->archiveName and $this->file.
75 * Throws an error if validation fails
77 protected function validateParameters() {
78 // Validate the input title
79 $title = Title
::makeTitleSafe( NS_FILE
, $this->params
['filename'] );
80 if ( is_null( $title ) ) {
82 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params
['filename'] ) ]
85 $localRepo = RepoGroup
::singleton()->getLocalRepo();
87 // Check if the file really exists
88 $this->file
= $localRepo->newFile( $title );
89 if ( !$this->file
->exists() ) {
90 $this->dieWithError( 'apierror-missingtitle' );
93 // Check if the archivename is valid for this file
94 $this->archiveName
= $this->params
['archivename'];
95 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName
);
96 if ( !$oldFile->exists() ) {
97 $this->dieWithError( 'filerevert-badversion' );
101 public function mustBePosted() {
105 public function isWriteMode() {
109 public function getAllowedParams() {
112 ApiBase
::PARAM_TYPE
=> 'string',
113 ApiBase
::PARAM_REQUIRED
=> true,
116 ApiBase
::PARAM_DFLT
=> '',
119 ApiBase
::PARAM_TYPE
=> 'string',
120 ApiBase
::PARAM_REQUIRED
=> true,
125 public function needsToken() {
129 protected function getExamplesMessages() {
131 'action=filerevert&filename=Wiki.png&comment=Revert&' .
132 'archivename=20110305152740!Wiki.png&token=123ABC'
133 => 'apihelp-filerevert-example-revert',