MediaWiki REL1_37
ApiFileRevert.php
Go to the documentation of this file.
1<?php
26class ApiFileRevert extends ApiBase {
28 protected $file;
29
31 protected $archiveName;
32
34 protected $params;
35
37 private $repoGroup;
38
44 public function __construct(
45 ApiMain $main,
46 $action,
48 ) {
49 parent::__construct( $main, $action );
50 $this->repoGroup = $repoGroup;
51 }
52
53 public function execute() {
55
56 $this->params = $this->extractRequestParams();
57 // Extract the file and archiveName from the request parameters
58 $this->validateParameters();
59
60 // Check whether we're allowed to revert this file
61 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
62
63 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
64 $status = $this->file->upload(
65 $sourceUrl,
66 $this->params['comment'],
67 $this->params['comment'],
68 0,
69 false,
70 false,
71 $this->getAuthority()
72 );
73
74 if ( $status->isGood() ) {
75 $result = [ 'result' => 'Success' ];
76 } else {
77 $result = [
78 'result' => 'Failure',
79 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
80 ];
81 }
82
83 $this->getResult()->addValue( null, $this->getModuleName(), $result );
84 }
85
90 protected function validateParameters() {
91 // Validate the input title
92 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
93 if ( $title === null ) {
94 $this->dieWithError(
95 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
96 );
97 }
98 $localRepo = $this->repoGroup->getLocalRepo();
99
100 // Check if the file really exists
101 $this->file = $localRepo->newFile( $title );
102 if ( !$this->file->exists() ) {
103 $this->dieWithError( 'apierror-missingtitle' );
104 }
105
106 // Check if the archivename is valid for this file
107 $this->archiveName = $this->params['archivename'];
108 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
109 if ( !$oldFile->exists() ) {
110 $this->dieWithError( 'filerevert-badversion' );
111 }
112 }
113
114 public function mustBePosted() {
115 return true;
116 }
117
118 public function isWriteMode() {
119 return true;
120 }
121
122 public function getAllowedParams() {
123 return [
124 'filename' => [
125 ApiBase::PARAM_TYPE => 'string',
127 ],
128 'comment' => [
130 ],
131 'archivename' => [
132 ApiBase::PARAM_TYPE => 'string',
134 ],
135 ];
136 }
137
138 public function needsToken() {
139 return 'csrf';
140 }
141
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}
const NS_FILE
Definition Defines.php:70
wfEscapeWikiText( $text)
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:55
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1436
const PARAM_REQUIRED
Definition ApiBase.php:105
const PARAM_TYPE
Definition ApiBase.php:81
getErrorFormatter()
Definition ApiBase.php:639
const PARAM_DFLT
Definition ApiBase.php:73
getResult()
Get the result object.
Definition ApiBase.php:628
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:764
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1565
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:497
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1293
RepoGroup $repoGroup
LocalFile $file
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
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:49
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:63
Prioritized list of file repositories.
Definition RepoGroup.php:33