MediaWiki  master
ApiFileRevert.php
Go to the documentation of this file.
1 <?php
24 
28 class 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',
111  ApiBase::PARAM_REQUIRED => true,
112  ],
113  'comment' => [
114  ApiBase::PARAM_DFLT => '',
115  ],
116  'archivename' => [
117  ApiBase::PARAM_TYPE => 'string',
118  ApiBase::PARAM_REQUIRED => true,
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 }
ApiFileRevert\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiFileRevert.php:103
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:77
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:154
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1382
ApiFileRevert\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiFileRevert.php:38
ApiFileRevert\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiFileRevert.php:123
ApiBase\PARAM_TYPE
const PARAM_TYPE
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:71
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:565
NS_FILE
const NS_FILE
Definition: Defines.php:75
ApiFileRevert
Definition: ApiFileRevert.php:28
ContextSource\getUser
getUser()
Stable to override.
Definition: ContextSource.php:131
ApiFileRevert\$params
array $params
Definition: ApiFileRevert.php:36
ApiBase
This abstract class implements many basic API functions, and is the base of all API classes.
Definition: ApiBase.php:52
ApiFileRevert\$file
LocalFile $file
Definition: ApiFileRevert.php:30
ApiFileRevert\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request Stable to override.
Definition: ApiFileRevert.php:99
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:717
$title
$title
Definition: testCompression.php:38
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:59
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:618
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1239
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1487
ApiFileRevert\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiFileRevert.php:107
ApiBase\PARAM_DFLT
const PARAM_DFLT
(boolean) Inverse of IntegerDef::PARAM_IGNORE_RANGE
Definition: ApiBase.php:69
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:444
ApiFileRevert\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiFileRevert.php:127
ApiFileRevert\$archiveName
string $archiveName
Definition: ApiFileRevert.php:33
ApiFileRevert\validateParameters
validateParameters()
Validate the user parameters and set $this->archiveName and $this->file.
Definition: ApiFileRevert.php:75
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions(LinkTarget $linkTarget, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1509
ApiBase\getErrorFormatter
getErrorFormatter()
Get the error formatter Stable to override.
Definition: ApiBase.php:580