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 use MediaWiki\Title\Title
;
24 use Wikimedia\ParamValidator\ParamValidator
;
29 class ApiFileRevert
extends ApiBase
{
34 protected $archiveName;
43 * @param ApiMain $main
44 * @param string $action
45 * @param RepoGroup $repoGroup
47 public function __construct(
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(
69 $this->params
['comment'],
70 $this->params
['comment'],
77 if ( $status->isGood() ) {
78 $result = [ 'result' => 'Success' ];
81 'result' => 'Failure',
82 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
86 $this->getResult()->addValue( null, $this->getModuleName(), $result );
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 ) {
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() {
122 public function isWriteMode() {
126 public function getAllowedParams() {
129 ParamValidator
::PARAM_TYPE
=> 'string',
130 ParamValidator
::PARAM_REQUIRED
=> true,
133 ParamValidator
::PARAM_DEFAULT
=> '',
136 ParamValidator
::PARAM_TYPE
=> 'string',
137 ParamValidator
::PARAM_REQUIRED
=> true,
142 public function needsToken() {
146 protected function getExamplesMessages() {
148 'action=filerevert&filename=Wiki.png&comment=Revert&' .
149 'archivename=20110305152740!Wiki.png&token=123ABC'
150 => 'apihelp-filerevert-example-revert',