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