MediaWiki master
ApiFileRevert.php
Go to the documentation of this file.
1<?php
23namespace MediaWiki\Api;
24
25use LocalFile;
27use RepoGroup;
29
33class ApiFileRevert extends ApiBase {
35 protected $file;
36
38 protected $archiveName;
39
41 protected $params;
42
44 private $repoGroup;
45
46 public function __construct(
47 ApiMain $main,
48 string $action,
49 RepoGroup $repoGroup
50 ) {
51 parent::__construct( $main, $action );
52 $this->repoGroup = $repoGroup;
53 }
54
55 public function execute() {
57
58 $this->params = $this->extractRequestParams();
59 // Extract the file and archiveName from the request parameters
60 $this->validateParameters();
61
62 // Check whether we're allowed to revert this file
63 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
64
65 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
66 $status = $this->file->upload(
67 $sourceUrl,
68 $this->params['comment'],
69 $this->params['comment'],
70 0,
71 false,
72 false,
73 $this->getAuthority()
74 );
75
76 if ( $status->isGood() ) {
77 $result = [ 'result' => 'Success' ];
78 } else {
79 $result = [
80 'result' => 'Failure',
81 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
82 ];
83 }
84
85 $this->getResult()->addValue( null, $this->getModuleName(), $result );
86 }
87
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'] ) ]
98 );
99 }
100 $localRepo = $this->repoGroup->getLocalRepo();
101
102 // Check if the file really exists
103 $this->file = $localRepo->newFile( $title );
104 if ( !$this->file->exists() ) {
105 $this->dieWithError( 'apierror-missingtitle' );
106 }
107
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' );
114 }
115 }
116
117 public function mustBePosted() {
118 return true;
119 }
120
121 public function isWriteMode() {
122 return true;
123 }
124
125 public function getAllowedParams() {
126 return [
127 'filename' => [
128 ParamValidator::PARAM_TYPE => 'string',
129 ParamValidator::PARAM_REQUIRED => true,
130 ],
131 'comment' => [
132 ParamValidator::PARAM_DEFAULT => '',
133 ],
134 'archivename' => [
135 ParamValidator::PARAM_TYPE => 'string',
136 ParamValidator::PARAM_REQUIRED => true,
137 ],
138 ];
139 }
140
141 public function needsToken() {
142 return 'csrf';
143 }
144
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',
150 ];
151 }
152
153 public function getHelpUrls() {
154 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerevert';
155 }
156}
157
159class_alias( ApiFileRevert::class, 'ApiFileRevert' );
const NS_FILE
Definition Defines.php:71
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Local file in the wiki's own database.
Definition LocalFile.php:75
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:76
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1577
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:571
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1425
getResult()
Get the result object.
Definition ApiBase.php:710
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:851
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1715
getExamplesMessages()
Returns usage examples for this module.
validateParameters()
Validate the user parameters and set $this->archiveName and $this->file.
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
needsToken()
Returns the token type this module requires in order to execute.
mustBePosted()
Indicates whether this module must be called with a POST request.
isWriteMode()
Indicates whether this module requires write access to the wiki.
getHelpUrls()
Return links to more detailed help pages about the module.
__construct(ApiMain $main, string $action, RepoGroup $repoGroup)
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
This is the main API class, used for both external and internal processing.
Definition ApiMain.php:78
Represents a title within MediaWiki.
Definition Title.php:78
Prioritized list of file repositories.
Definition RepoGroup.php:32
Service for formatting and validating API parameters.