MediaWiki REL1_34
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
36 public function execute() {
38
39 $this->params = $this->extractRequestParams();
40 // Extract the file and archiveName from the request parameters
41 $this->validateParameters();
42
43 // Check whether we're allowed to revert this file
44 $this->checkTitleUserPermissions( $this->file->getTitle(), [ 'edit', 'upload' ] );
45
46 $sourceUrl = $this->file->getArchiveVirtualUrl( $this->archiveName );
47 $status = $this->file->upload(
48 $sourceUrl,
49 $this->params['comment'],
50 $this->params['comment'],
51 0,
52 false,
53 false,
54 $this->getUser()
55 );
56
57 if ( $status->isGood() ) {
58 $result = [ 'result' => 'Success' ];
59 } else {
60 $result = [
61 'result' => 'Failure',
62 'errors' => $this->getErrorFormatter()->arrayFromStatus( $status ),
63 ];
64 }
65
66 $this->getResult()->addValue( null, $this->getModuleName(), $result );
67 }
68
73 protected function validateParameters() {
74 // Validate the input title
75 $title = Title::makeTitleSafe( NS_FILE, $this->params['filename'] );
76 if ( is_null( $title ) ) {
77 $this->dieWithError(
78 [ 'apierror-invalidtitle', wfEscapeWikiText( $this->params['filename'] ) ]
79 );
80 }
81 $localRepo = RepoGroup::singleton()->getLocalRepo();
82
83 // Check if the file really exists
84 $this->file = $localRepo->newFile( $title );
85 if ( !$this->file->exists() ) {
86 $this->dieWithError( 'apierror-missingtitle' );
87 }
88
89 // Check if the archivename is valid for this file
90 $this->archiveName = $this->params['archivename'];
91 $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
92 if ( !$oldFile->exists() ) {
93 $this->dieWithError( 'filerevert-badversion' );
94 }
95 }
96
97 public function mustBePosted() {
98 return true;
99 }
100
101 public function isWriteMode() {
102 return true;
103 }
104
105 public function getAllowedParams() {
106 return [
107 'filename' => [
108 ApiBase::PARAM_TYPE => 'string',
110 ],
111 'comment' => [
113 ],
114 'archivename' => [
115 ApiBase::PARAM_TYPE => 'string',
117 ],
118 ];
119 }
120
121 public function needsToken() {
122 return 'csrf';
123 }
124
125 protected function getExamplesMessages() {
126 return [
127 'action=filerevert&filename=Wiki.png&comment=Revert&' .
128 'archivename=20110305152740!Wiki.png&token=123ABC'
129 => 'apihelp-filerevert-example-revert',
130 ];
131 }
132}
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:42
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition ApiBase.php:118
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition ApiBase.php:2014
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition ApiBase.php:2156
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition ApiBase.php:94
getErrorFormatter()
Get the error formatter.
Definition ApiBase.php:654
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition ApiBase.php:55
getResult()
Get the result object.
Definition ApiBase.php:640
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition ApiBase.php:761
getModuleName()
Get the name of the module being executed by this instance.
Definition ApiBase.php:520
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition ApiBase.php:1871
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.
needsToken()
Returns the token type this module requires in order to execute.
getExamplesMessages()
Returns usage examples for this module.
Class to represent a local file in the wiki's own database.
Definition LocalFile.php:56
const NS_FILE
Definition Defines.php:75