MediaWiki REL1_35
ApiFileRevert.php
Go to the documentation of this file.
1<?php
24
28class ApiFileRevert extends ApiBase {
30 protected $file;
31
33 protected $archiveName;
34
36 protected $params;
37
38 public function execute() {
40
41 $this->params = $this->extractRequestParams();
42 // Extract the file and archiveName from the request parameters
43 $this->validateParameters();
44
45 // Check whether we're allowed to revert this file
46 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
47
48 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
49 $status = $this->file->upload(
50 $sourceUrl,
51 $this->params['comment'],
52 $this->params['comment'],
53 0,
54 false,
55 false,
56 $this->getUser()
57 );
58
59 if ( $status->isGood() ) {
60 $result = [ 'result' => 'Success' ];
61 } else {
62 $result = [
63 'result' => 'Failure',
64 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
65 ];
66 }
67
68 $this->getResult()->addValue( null, $this->getModuleName(), $result );
69 }
70
75 protected function validateParameters() {
76 // Validate the input title
77 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
78 if ( $title === null ) {
79 $this->dieWithError(
80 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
81 );
82 }
83 $localRepo = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo();
84
85 // Check if the file really exists
86 $this->file = $localRepo->newFile( $title );
87 if ( !$this->file->exists() ) {
88 $this->dieWithError( 'apierror-missingtitle' );
89 }
90
91 // Check if the archivename is valid for this file
92 $this->archiveName = $this->params['archivename'];
93 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
94 if ( !$oldFile->exists() ) {
95 $this->dieWithError( 'filerevert-badversion' );
96 }
97 }
98
99 public function mustBePosted() {
100 return true;
101 }
102
103 public function isWriteMode() {
104 return true;
105 }
106
107 public function getAllowedParams() {
108 return [
109 'filename' => [
110 ApiBase::PARAM_TYPE => 'string',
112 ],
113 'comment' => [
115 ],
116 'archivename' => [
117 ApiBase::PARAM_TYPE => 'string',
119 ],
120 ];
121 }
122
123 public function needsToken() {
124 return 'csrf';
125 }
126
127 protected function getExamplesMessages() {
128 return [
129 'action=filerevert&filename=Wiki.png&comment=Revert&' .
130 'archivename=20110305152740!Wiki.png&token=123ABC'
131 => 'apihelp-filerevert-example-revert',
132 ];
133 }
134}
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:52
dieWithError( $msg, $code=null, $data=null, $httpCode=0)
Abort execution with an error.
Definition ApiBase.php:1437
const PARAM_REQUIRED
Definition ApiBase.php:102
const PARAM_TYPE
Definition ApiBase.php:78
getErrorFormatter()
Get the error formatter Stable to override.
Definition ApiBase.php:635
const PARAM_DFLT
Definition ApiBase.php:70
getResult()
Get the result object.
Definition ApiBase.php:620
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:772
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:499
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:1564
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1294
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.
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 Stable to override.
needsToken()
Returns the token type this module requires in order to execute.
getExamplesMessages()
Returns usage examples for this module.
getUser()
Stable to override.
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:59
MediaWikiServices is the service locator for the application scope of MediaWiki.
const NS_FILE
Definition Defines.php:76