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
23 namespace MediaWiki\Api
;
26 use MediaWiki\Title\Title
;
28 use Wikimedia\ParamValidator\ParamValidator
;
33 class ApiFileRevert
extends ApiBase
{
38 protected $archiveName;
46 public function __construct(
51 parent
::__construct( $main, $action );
52 $this->repoGroup
= $repoGroup;
55 public function execute() {
56 $this->useTransactionalTimeLimit();
58 $this->params
= $this->extractRequestParams();
59 // Extract the file and archiveName from the request parameters
60 $this->validateParameters();
62 // Check whether we're allowed to revert this file
63 $this->checkTitleUserPermissions( $this->file
->getTitle(), [ 'edit', 'upload' ] );
65 $sourceUrl = $this->file
->getArchiveVirtualUrl( $this->archiveName
);
66 $status = $this->file
->upload(
68 $this->params
['comment'],
69 $this->params
['comment'],
76 if ( $status->isGood() ) {
77 $result = [ 'result' => 'Success' ];
80 'result' => 'Failure',
81 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
85 $this->getResult()->addValue( null, $this->getModuleName(), $result );
89 * Validate the user parameters and set $this->archiveName and $this->file.
90 * Throws an error if validation fails
92 protected function validateParameters() {
93 // Validate the input title
94 $title = Title
::makeTitleSafe( NS_FILE
, $this->params
['filename'] );
95 if ( $title === null ) {
97 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params
['filename'] ) ]
100 $localRepo = $this->repoGroup
->getLocalRepo();
102 // Check if the file really exists
103 $this->file
= $localRepo->newFile( $title );
104 if ( !$this->file
->exists() ) {
105 $this->dieWithError( 'apierror-missingtitle' );
108 // Check if the archivename is valid for this file
109 $this->archiveName
= $this->params
['archivename'];
110 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
111 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName
);
112 if ( !$oldFile->exists() ) {
113 $this->dieWithError( 'filerevert-badversion' );
117 public function mustBePosted() {
121 public function isWriteMode() {
125 public function getAllowedParams() {
128 ParamValidator
::PARAM_TYPE
=> 'string',
129 ParamValidator
::PARAM_REQUIRED
=> true,
132 ParamValidator
::PARAM_DEFAULT
=> '',
135 ParamValidator
::PARAM_TYPE
=> 'string',
136 ParamValidator
::PARAM_REQUIRED
=> true,
141 public function needsToken() {
145 protected function getExamplesMessages() {
147 'action=filerevert&filename=Wiki.png&comment=Revert&' .
148 'archivename=20110305152740!Wiki.png&token=123ABC'
149 => 'apihelp-filerevert-example-revert',
153 public function getHelpUrls() {
154 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerevert';
158 /** @deprecated class alias since 1.43 */
159 class_alias( ApiFileRevert
::class, 'ApiFileRevert' );