Update git submodules
[mediawiki.git] / includes / api / ApiFileRevert.php
blobf5417cae4945e5cdaea042d831e6576c4f518a60
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 use MediaWiki\Title\Title;
24 use Wikimedia\ParamValidator\ParamValidator;
26 /**
27 * @ingroup API
29 class ApiFileRevert extends ApiBase {
30 /** @var LocalFile */
31 protected $file;
33 /** @var string */
34 protected $archiveName;
36 /** @var array */
37 protected $params;
39 /** @var RepoGroup */
40 private $repoGroup;
42 /**
43 * @param ApiMain $main
44 * @param string $action
45 * @param RepoGroup $repoGroup
47 public function __construct(
48 ApiMain $main,
49 $action,
50 RepoGroup $repoGroup
51 ) {
52 parent::__construct( $main, $action );
53 $this->repoGroup = $repoGroup;
56 public function execute() {
57 $this->useTransactionalTimeLimit();
59 $this->params = $this->extractRequestParams();
60 // Extract the file and archiveName from the request parameters
61 $this->validateParameters();
63 // Check whether we're allowed to revert this file
64 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
66 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
67 $status = $this->file->upload(
68 $sourceUrl,
69 $this->params['comment'],
70 $this->params['comment'],
72 false,
73 false,
74 $this->getAuthority()
77 if ( $status->isGood() ) {
78 $result = [ 'result' => 'Success' ];
79 } else {
80 $result = [
81 'result' => 'Failure',
82 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
86 $this->getResult()->addValue( null, $this->getModuleName(), $result );
89 /**
90 * Validate the user parameters and set $this->archiveName and $this->file.
91 * Throws an error if validation fails
93 protected function validateParameters() {
94 // Validate the input title
95 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
96 if ( $title === null ) {
97 $this->dieWithError(
98 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
101 $localRepo = $this->repoGroup->getLocalRepo();
103 // Check if the file really exists
104 $this->file = $localRepo->newFile( $title );
105 if ( !$this->file->exists() ) {
106 $this->dieWithError( 'apierror-missingtitle' );
109 // Check if the archivename is valid for this file
110 $this->archiveName = $this->params['archivename'];
111 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
112 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
113 if ( !$oldFile->exists() ) {
114 $this->dieWithError( 'filerevert-badversion' );
118 public function mustBePosted() {
119 return true;
122 public function isWriteMode() {
123 return true;
126 public function getAllowedParams() {
127 return [
128 'filename' => [
129 ParamValidator::PARAM_TYPE => 'string',
130 ParamValidator::PARAM_REQUIRED => true,
132 'comment' => [
133 ParamValidator::PARAM_DEFAULT => '',
135 'archivename' => [
136 ParamValidator::PARAM_TYPE => 'string',
137 ParamValidator::PARAM_REQUIRED => true,
142 public function needsToken() {
143 return 'csrf';
146 protected function getExamplesMessages() {
147 return [
148 'action=filerevert&filename=Wiki.png&comment=Revert&' .
149 'archivename=20110305152740!Wiki.png&token=123ABC'
150 => 'apihelp-filerevert-example-revert',