MediaWiki  1.34.0
ApiFileRevert.php
Go to the documentation of this file.
1 <?php
26 class 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',
109  ApiBase::PARAM_REQUIRED => true,
110  ],
111  'comment' => [
112  ApiBase::PARAM_DFLT => '',
113  ],
114  'archivename' => [
115  ApiBase::PARAM_TYPE => 'string',
116  ApiBase::PARAM_REQUIRED => true,
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 }
ApiFileRevert\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiFileRevert.php:101
RepoGroup\singleton
static singleton()
Definition: RepoGroup.php:60
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2014
ApiFileRevert\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiFileRevert.php:36
ApiFileRevert\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiFileRevert.php:121
ApiBase\PARAM_TYPE
const PARAM_TYPE
(string|string[]) Either an array of allowed value strings, or a string type as described below.
Definition: ApiBase.php:94
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:640
NS_FILE
const NS_FILE
Definition: Defines.php:66
ApiFileRevert
Definition: ApiFileRevert.php:26
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:2156
ContextSource\getUser
getUser()
Definition: ContextSource.php:120
ApiFileRevert\$params
array $params
Definition: ApiFileRevert.php:34
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:42
ApiFileRevert\$file
LocalFile $file
Definition: ApiFileRevert.php:28
ApiFileRevert\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiFileRevert.php:97
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:761
$title
$title
Definition: testCompression.php:34
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:56
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:613
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1871
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1551
ApiFileRevert\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiFileRevert.php:105
$status
return $status
Definition: SyntaxHighlight.php:347
ApiBase\PARAM_DFLT
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
ApiFileRevert\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiFileRevert.php:125
ApiFileRevert\$archiveName
string $archiveName
Definition: ApiFileRevert.php:31
ApiFileRevert\validateParameters
validateParameters()
Validate the user parameters and set $this->archiveName and $this->file.
Definition: ApiFileRevert.php:73
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter.
Definition: ApiBase.php:654