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