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 
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  // @phan-suppress-next-line PhanTypeMismatchArgumentNullable T240141
109  $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
110  if ( !$oldFile->exists() ) {
111  $this->dieWithError( 'filerevert-badversion' );
112  }
113  }
114 
115  public function mustBePosted() {
116  return true;
117  }
118 
119  public function isWriteMode() {
120  return true;
121  }
122 
123  public function getAllowedParams() {
124  return [
125  'filename' => [
126  ApiBase::PARAM_TYPE => 'string',
127  ApiBase::PARAM_REQUIRED => true,
128  ],
129  'comment' => [
130  ApiBase::PARAM_DFLT => '',
131  ],
132  'archivename' => [
133  ApiBase::PARAM_TYPE => 'string',
134  ApiBase::PARAM_REQUIRED => true,
135  ],
136  ];
137  }
138 
139  public function needsToken() {
140  return 'csrf';
141  }
142 
143  protected function getExamplesMessages() {
144  return [
145  'action=filerevert&filename=Wiki.png&comment=Revert&' .
146  'archivename=20110305152740!Wiki.png&token=123ABC'
147  => 'apihelp-filerevert-example-revert',
148  ];
149  }
150 }
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:56
const PARAM_REQUIRED
Definition: ApiBase.php:106
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1446
const PARAM_TYPE
Definition: ApiBase.php:82
getErrorFormatter()
Definition: ApiBase.php:640
const PARAM_DFLT
Definition: ApiBase.php:74
getResult()
Get the result object.
Definition: ApiBase.php:629
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:765
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1581
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:498
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1303
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.
string $archiveName
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:51
Prioritized list of file repositories.
Definition: RepoGroup.php:29
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:663