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  $oldFile = $localRepo->newFromArchiveName( $title, $this->archiveName );
109  if ( !$oldFile->exists() ) {
110  $this->dieWithError( 'filerevert-badversion' );
111  }
112  }
113 
114  public function mustBePosted() {
115  return true;
116  }
117 
118  public function isWriteMode() {
119  return true;
120  }
121 
122  public function getAllowedParams() {
123  return [
124  'filename' => [
125  ApiBase::PARAM_TYPE => 'string',
126  ApiBase::PARAM_REQUIRED => true,
127  ],
128  'comment' => [
129  ApiBase::PARAM_DFLT => '',
130  ],
131  'archivename' => [
132  ApiBase::PARAM_TYPE => 'string',
133  ApiBase::PARAM_REQUIRED => true,
134  ],
135  ];
136  }
137 
138  public function needsToken() {
139  return 'csrf';
140  }
141 
142  protected function getExamplesMessages() {
143  return [
144  'action=filerevert&filename=Wiki.png&comment=Revert&' .
145  'archivename=20110305152740!Wiki.png&token=123ABC'
146  => 'apihelp-filerevert-example-revert',
147  ];
148  }
149 }
ApiBase\checkTitleUserPermissions
checkTitleUserPermissions( $pageIdentity, $actions, array $options=[])
Helper function for permission-denied errors.
Definition: ApiBase.php:1571
ApiMain
This is the main API class, used for both external and internal processing.
Definition: ApiMain.php:49
ApiFileRevert\isWriteMode
isWriteMode()
Indicates whether this module requires write mode.
Definition: ApiFileRevert.php:118
ApiBase\PARAM_REQUIRED
const PARAM_REQUIRED
Definition: ApiBase.php:105
ApiBase\dieWithError
dieWithError( $msg, $code=null, $data=null, $httpCode=null)
Abort execution with an error.
Definition: ApiBase.php:1436
ApiFileRevert\execute
execute()
Evaluates the parameters, performs the requested query, and sets up the result.
Definition: ApiFileRevert.php:53
ApiFileRevert\needsToken
needsToken()
Returns the token type this module requires in order to execute.
Definition: ApiFileRevert.php:138
ApiBase\PARAM_TYPE
const PARAM_TYPE
Definition: ApiBase.php:81
ApiBase\getResult
getResult()
Get the result object.
Definition: ApiBase.php:628
ApiFileRevert\$repoGroup
RepoGroup $repoGroup
Definition: ApiFileRevert.php:37
ApiFileRevert
Definition: ApiFileRevert.php:26
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:55
ApiFileRevert\$file
LocalFile $file
Definition: ApiFileRevert.php:28
ApiFileRevert\__construct
__construct(ApiMain $main, $action, RepoGroup $repoGroup)
Definition: ApiFileRevert.php:44
ApiFileRevert\mustBePosted
mustBePosted()
Indicates whether this module must be called with a POST request.
Definition: ApiFileRevert.php:114
ApiBase\extractRequestParams
extractRequestParams( $options=[])
Using getAllowedParams(), this function makes an array of the values provided by the user,...
Definition: ApiBase.php:764
$title
$title
Definition: testCompression.php:38
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:62
Title\makeTitleSafe
static makeTitleSafe( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:692
ContextSource\getAuthority
getAuthority()
Definition: ContextSource.php:144
ApiBase\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: ApiBase.php:1293
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1456
ApiFileRevert\getAllowedParams
getAllowedParams()
Returns an array of allowed parameters (parameter name) => (default value) or (parameter name) => (ar...
Definition: ApiFileRevert.php:122
ApiBase\PARAM_DFLT
const PARAM_DFLT
Definition: ApiBase.php:73
ApiBase\getModuleName
getModuleName()
Get the name of the module being executed by this instance.
Definition: ApiBase.php:497
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
ApiFileRevert\getExamplesMessages
getExamplesMessages()
Returns usage examples for this module.
Definition: ApiFileRevert.php:142
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:90
NS_FILE
const NS_FILE
Definition: Defines.php:70
ApiBase\getErrorFormatter
getErrorFormatter()
Definition: ApiBase.php:639