Localisation updates from https://translatewiki.net.
[mediawiki.git] / includes / api / ApiFileRevert.php
blobc33cb8c2180fd9d6bebf4a13e32c86412fe89974
1 <?php
2 /**
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
20 * @file
23 namespace MediaWiki\Api;
25 use LocalFile;
26 use MediaWiki\Title\Title;
27 use RepoGroup;
28 use Wikimedia\ParamValidator\ParamValidator;
30 /**
31 * @ingroup API
33 class ApiFileRevert extends ApiBase {
34 /** @var LocalFile */
35 protected $file;
37 /** @var string */
38 protected $archiveName;
40 /** @var array */
41 protected $params;
43 /** @var RepoGroup */
44 private $repoGroup;
46 public function __construct(
47 ApiMain $main,
48 string $action,
49 RepoGroup $repoGroup
50 ) {
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(
67 $sourceUrl,
68 $this->params['comment'],
69 $this->params['comment'],
71 false,
72 false,
73 $this->getAuthority()
76 if ( $status->isGood() ) {
77 $result = [ 'result' => 'Success' ];
78 } else {
79 $result = [
80 'result' => 'Failure',
81 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
85 $this->getResult()->addValue( null, $this->getModuleName(), $result );
88 /**
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 ) {
96 $this->dieWithError(
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() {
118 return true;
121 public function isWriteMode() {
122 return true;
125 public function getAllowedParams() {
126 return [
127 'filename' => [
128 ParamValidator::PARAM_TYPE => 'string',
129 ParamValidator::PARAM_REQUIRED => true,
131 'comment' => [
132 ParamValidator::PARAM_DEFAULT => '',
134 'archivename' => [
135 ParamValidator::PARAM_TYPE => 'string',
136 ParamValidator::PARAM_REQUIRED => true,
141 public function needsToken() {
142 return 'csrf';
145 protected function getExamplesMessages() {
146 return [
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' );