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
29 public function __construct(
30 ApiMain $main,
31 string $action,
32 private readonly RepoGroup $repoGroup,
33 ) {
34 parent::__construct( $main, $action );
35 }
36
37 public function execute() {
39
40 $this->params = $this->extractRequestParams();
41 // Extract the file and archiveName from the request parameters
42 $this->validateParameters();
43
44 // Check whether we're allowed to revert this file
45 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
46 $rights = [ 'reupload' ];
47 if ( $this->getUser()->equals( $this->file->getUploader() ) ) {
48 // reupload-own is more basic, put it in the front for error messages.
49 array_unshift( $rights, 'reupload-own' );
50 }
51 $this->checkUserRightsAny( $rights );
52
53 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
54 $status = $this->file->upload(
55 $sourceUrl,
56 $this->params['comment'],
57 $this->params['comment'],
58 0,
59 false,
60 false,
61 $this->getAuthority()
62 );
63
64 if ( $status->isGood() ) {
65 $result = [ 'result' => 'Success' ];
66 } else {
67 $result = [
68 'result' => 'Failure',
69 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
70 ];
71 }
72
73 $this->getResult()->addValue( null, $this->getModuleName(), $result );
74 }
75
80 protected function validateParameters() {
81 // Validate the input title
82 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
83 if ( $title === null ) {
84 $this->dieWithError(
85 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
86 );
87 }
88 $localRepo = $this->repoGroup->getLocalRepo();
89
90 // Check if the file really exists
91 $this->file = $localRepo->newFile( $title );
92 if ( !$this->file->exists() ) {
93 $this->dieWithError( 'apierror-missingtitle' );
94 }
95
96 // Check if the archivename is valid for this file
97 $this->archiveName = $this->params['archivename'];
98 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
99 if ( !$oldFile->exists() ) {
100 $this->dieWithError( 'filerevert-badversion' );
101 }
102 }
103
105 public function mustBePosted() {
106 return true;
107 }
108
110 public function isWriteMode() {
111 return true;
112 }
113
115 public function getAllowedParams() {
116 return [
117 'filename' => [
118 ParamValidator::PARAM_TYPE => 'string',
119 ParamValidator::PARAM_REQUIRED => true,
120 ],
121 'comment' => [
122 ParamValidator::PARAM_DEFAULT => '',
123 ],
124 'archivename' => [
125 ParamValidator::PARAM_TYPE => 'string',
126 ParamValidator::PARAM_REQUIRED => true,
127 ],
128 ];
129 }
130
132 public function needsToken() {
133 return 'csrf';
134 }
135
137 protected function getExamplesMessages() {
138 return [
139 'action=filerevert&filename=Wiki.png&comment=Revert&' .
140 'archivename=20110305152740!Wiki.png&token=123ABC'
141 => 'apihelp-filerevert-example-revert',
142 ];
143 }
144
146 public function getHelpUrls() {
147 return 'https://www.mediawiki.org/wiki/Special:MyLanguage/API:Filerevert';
148 }
149}
150
152class_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:60
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1522
checkUserRightsAny( $rights)
Helper function for permission-denied errors.
Definition ApiBase.php:1631
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:557
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1369
getResult()
Get the result object.
Definition ApiBase.php:696
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:837
checkTitleUserPermissions(PageIdentity $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1654
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 ...
__construct(ApiMain $main, string $action, private readonly RepoGroup $repoGroup,)
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...
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.