MediaWiki  master
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 }
LocalFile $file
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
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking, formatting, etc.
getResult()
Get the result object.
Definition: ApiBase.php:640
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1862
const PARAM_DFLT
(null|boolean|integer|string) Default value of the parameter.
Definition: ApiBase.php:55
const PARAM_REQUIRED
(boolean) Is the parameter required?
Definition: ApiBase.php:118
validateParameters()
Validate the user parameters and set $this->archiveName and $this->file.
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:2005
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user...
Definition: ApiBase.php:761
static singleton()
Definition: RepoGroup.php:60
string $archiveName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:520
const NS_FILE
Definition: Defines.php:66
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:2147
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:612
This abstract class implements many basic API functions, and is the base of all API classes...
Definition: ApiBase.php:42