MediaWiki 1.44.0
RevertAction.php
Go to the documentation of this file.
1<?php
26namespace MediaWiki\Actions;
27
44
52class RevertAction extends FormAction {
53
54 private Language $contentLanguage;
55 private RepoGroup $repoGroup;
56
63 public function __construct(
64 Article $article,
66 Language $contentLanguage,
67 RepoGroup $repoGroup
68 ) {
69 parent::__construct( $article, $context );
70 $this->contentLanguage = $contentLanguage;
71 $this->repoGroup = $repoGroup;
72 }
73
77 protected $oldFile;
78
79 public function getName() {
80 return 'revert';
81 }
82
83 public function getRestriction() {
84 // Required permissions of revert are complicated, will be checked below.
85 return 'upload';
86 }
87
88 protected function checkCanExecute( User $user ) {
89 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
90 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
91 }
92
93 $rights = [ 'reupload' ];
94 if ( $user->equals( $this->getFile()->getUploader() ) ) {
95 // reupload-own is more basic, put it in the front for error messages.
96 array_unshift( $rights, 'reupload-own' );
97 }
98 if ( !$user->isAllowedAny( ...$rights ) ) {
99 throw new PermissionsError( $rights[0] );
100 }
101
102 parent::checkCanExecute( $user );
103
104 $oldimage = $this->getRequest()->getText( 'oldimage' );
105 if ( strlen( $oldimage ) < 16
106 || strpos( $oldimage, '/' ) !== false
107 || strpos( $oldimage, '\\' ) !== false
108 ) {
109 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
110 }
111
112 $this->oldFile = $this->repoGroup->getLocalRepo()
113 ->newFromArchiveName( $this->getTitle(), $oldimage );
114
115 if ( !$this->oldFile->exists() ) {
116 throw new ErrorPageError( '', 'filerevert-badversion' );
117 }
118 }
119
120 protected function usesOOUI() {
121 return true;
122 }
123
124 protected function alterForm( HTMLForm $form ) {
125 $form->setWrapperLegendMsg( 'filerevert-legend' );
126 $form->setSubmitTextMsg( 'filerevert-submit' );
127 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
128 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
129 }
130
131 protected function getFormFields() {
132 $timestamp = $this->oldFile->getTimestamp();
133
134 $user = $this->getUser();
135 $lang = $this->getLanguage();
136 $userDate = $lang->userDate( $timestamp, $user );
137 $userTime = $lang->userTime( $timestamp, $user );
138 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
139 $ts = $siteTs->format( 'YmdHis' );
140 $contLang = $this->contentLanguage;
141 $siteDate = $contLang->date( $ts, false, false );
142 $siteTime = $contLang->time( $ts, false, false );
143 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
144
145 return [
146 'intro' => [
147 'type' => 'info',
148 'raw' => true,
149 'default' => $this->msg( 'filerevert-intro',
150 $this->getTitle()->getText(), $userDate, $userTime,
151 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
152 $this->getFile()
153 ->getArchiveUrl(
154 $this->getRequest()->getText( 'oldimage' )
155 ),
157 ) )->parseAsBlock()
158 ],
159 'comment' => [
160 'type' => 'text',
161 'label-message' => 'filerevert-comment',
162 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
163 $tzMsg )->inContentLanguage()->text()
164 ]
165 ];
166 }
167
168 public function onSubmit( $data ) {
170
171 $old = $this->getRequest()->getText( 'oldimage' );
173 $localFile = $this->getFile();
174 '@phan-var LocalFile $localFile';
175 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
176
177 $source = $localFile->getArchiveVirtualUrl( $old );
178 $comment = $data['comment'];
179
180 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
181 return Status::newFatal( 'filerevert-identical' );
182 }
183
184 // TODO: Preserve file properties from database instead of reloading from file
185 return $localFile->upload(
186 $source,
187 $comment,
188 $comment,
189 0,
190 false,
191 false,
192 $this->getAuthority(),
193 [],
194 true,
195 true
196 );
197 }
198
199 public function onSuccess() {
200 $timestamp = $this->oldFile->getTimestamp();
201 $user = $this->getUser();
202 $lang = $this->getLanguage();
203 $userDate = $lang->userDate( $timestamp, $user );
204 $userTime = $lang->userTime( $timestamp, $user );
205
206 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
207 $userDate, $userTime,
208 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
209 $this->getFile()
210 ->getArchiveUrl(
211 $this->getRequest()->getText( 'oldimage' )
212 ),
214 ) );
215 $this->getOutput()->returnToMain( false, $this->getTitle() );
216 }
217
218 protected function getPageTitle() {
219 return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() );
220 }
221
222 protected function getDescription() {
223 return OutputPage::buildBacklinkSubtitle( $this->getTitle() )->escaped();
224 }
225
226 public function doesWrites() {
227 return true;
228 }
229
234 private function getFile(): File {
236 $wikiPage = $this->getWikiPage();
237 // @phan-suppress-next-line PhanUndeclaredMethod
238 return $wikiPage->getFile();
239 }
240}
241
243class_alias( RevertAction::class, 'RevertAction' );
const NS_FILE
Definition Defines.php:71
const PROTO_CURRENT
Definition Defines.php:236
getWikiPage()
Get a WikiPage object.
Definition Action.php:205
IContextSource null $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition Action.php:79
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:166
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:240
getTitle()
Shortcut to get the Title object from the page.
Definition Action.php:226
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:146
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition Action.php:494
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:195
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:156
getAuthority()
Shortcut to get the Authority executing this instance.
Definition Action.php:176
An action which shows a form and does something based on the input from the form.
File reversion user interface WikiPage must contain getFile method: \WikiFilePage Article::getFile is...
onSuccess()
Do something exciting on successful processing of the form.
getFormFields()
Get an HTMLForm descriptor array.
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
__construct(Article $article, IContextSource $context, Language $contentLanguage, RepoGroup $repoGroup)
getPageTitle()
Returns the name that goes in the <h1> page title.
getRestriction()
Get the permission required to perform this action.
usesOOUI()
Whether the form should use OOUI.
onSubmit( $data)
Process the form on POST submission.
getDescription()
Returns the description that goes below the <h1> element.
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
getName()
Return the name of the action this object responds to.
An error page which can definitely be safely rendered using the OutputPage.
Show an error when a user tries to do something they do not have the necessary permissions for.
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition File.php:93
Local file in the wiki's own database.
Definition LocalFile.php:93
Old file in the oldimage table.
Prioritized list of file repositories.
Definition RepoGroup.php:38
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:209
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
setTokenSalt( $salt)
Set the salt for the edit token.
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output Array values are discarded for security reasons (per WebRequest::get...
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Base class for language-specific code.
Definition Language.php:81
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
This is one of the Core classes and should be read at least once by any new developers.
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:76
Special handling for representing file pages.
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
User class for the MediaWiki software.
Definition User.php:121
isAllowedAny(... $permissions)
Checks whether this authority has any of the given permissions in general.
Definition User.php:2209
equals(?UserIdentity $user)
Checks if two user objects point to the same user.
Definition User.php:3272
Library for creating and parsing MW-style timestamps.
Interface for objects which can provide a MediaWiki context on request.
$source