MediaWiki master
ApiFileRevert.php
Go to the documentation of this file.
1<?php
9namespace MediaWiki\Api;
10
15
19class ApiFileRevert extends ApiBase {
21 protected $file;
22
24 protected $archiveName;
25
27 protected $params;
28
30 private $repoGroup;
31
32 public function __construct(
33 ApiMain $main,
34 string $action,
35 RepoGroup $repoGroup
36 ) {
37 parent::__construct( $main, $action );
38 $this->repoGroup = $repoGroup;
39 }
40
41 public function execute() {
43
44 $this->params = $this->extractRequestParams();
45 // Extract the file and archiveName from the request parameters
46 $this->validateParameters();
47
48 // Check whether we're allowed to revert this file
49 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
50 $rights = [ 'reupload' ];
51 if ( $this->getUser()->equals( $this->file->getUploader() ) ) {
52 // reupload-own is more basic, put it in the front for error messages.
53 array_unshift( $rights, 'reupload-own' );
54 }
55 $this->checkUserRightsAny( $rights );
56
57 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
58 $status = $this->file->upload(
59 $sourceUrl,
60 $this->params['comment'],
61 $this->params['comment'],
62 0,
63 false,
64 false,
65 $this->getAuthority()
66 );
67
68 if ( $status->isGood() ) {
69 $result = [ 'result' => 'Success' ];
70 } else {
71 $result = [
72 'result' => 'Failure',
73 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
74 ];
75 }
76
77 $this->getResult()->addValue( null, $this->getModuleName(), $result );
78 }
79
84 protected function validateParameters() {
85 // Validate the input title
86 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
87 if ( $title === null ) {
88 $this->dieWithError(
89 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
90 );
91 }
92 $localRepo = $this->repoGroup->getLocalRepo();
93
94 // Check if the file really exists
95 $this->file = $localRepo->newFile( $title );
96 if ( !$this->file->exists() ) {
97 $this->dieWithError( 'apierror-missingtitle' );
98 }
99
100 // Check if the archivename is valid for this file
101 $this->archiveName = $this->params['archivename'];
102 // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
103 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
104 if ( !$oldFile->exists() ) {
105 $this->dieWithError( 'filerevert-badversion' );
106 }
107 }
108
110 public function mustBePosted() {
111 return true;
112 }
113
115 public function isWriteMode() {
116 return true;
117 }
118
120 public function getAllowedParams() {
121 return [
122 'filename' => [
123 ParamValidator::PARAM_TYPE => 'string',
124 ParamValidator::PARAM_REQUIRED => true,
125 ],
126 'comment' => [
127 ParamValidator::PARAM_DEFAULT => '',
128 ],
129 'archivename' => [
130 ParamValidator::PARAM_TYPE => 'string',
131 ParamValidator::PARAM_REQUIRED => true,
132 ],
133 ];
134 }
135
137 public function needsToken() {
138 return 'csrf';
139 }
140
142 protected function getExamplesMessages() {
143 return [
144 'action=filerevert&filename=Wiki.png&comment=Revert&' .
145 'archivename=20110305152740!Wiki.png&token=123ABC'
146 => 'apihelp-filerevert-example-revert',
147 ];
148 }
149
151 public function getHelpUrls() {
152 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerevert';
153 }
154}
155
157class_alias( ApiFileRevert::class, 'ApiFileRevert' );
const NS_FILE
Definition Defines.php:57
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
This abstract class implements many basic API functions, and is the base of all API classes.
Definition ApiBase.php:61
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1511
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1620
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:543
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1359
getResult()
Get the result object.
Definition ApiBase.php:682
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:823
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1643
getExamplesMessages()
Returns usage examples for this module.Return value has query strings as keys, with values being eith...
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.Modules are strongly encouraged to us...
mustBePosted()
Indicates whether this module must be called with a POST request.Implementations of this method must ...
isWriteMode()
Indicates whether this module requires write access to the wiki.API modules must override this method...
getHelpUrls()
Return links to more detailed help pages about the module.1.25, returning boolean false is deprecated...
__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:66
Local file in the wiki's own database.
Definition LocalFile.php:81
Prioritized list of file repositories.
Definition RepoGroup.php:30
Represents a title within MediaWiki.
Definition Title.php:69
Service for formatting and validating API parameters.