MediaWiki REL1_41
RevertAction.php
Go to the documentation of this file.
1<?php
31
39class RevertAction extends FormAction {
40
41 private Language $contentLanguage;
42 private RepoGroup $repoGroup;
43
50 public function __construct(
51 Article $article,
52 IContextSource $context,
53 Language $contentLanguage,
54 RepoGroup $repoGroup
55 ) {
56 parent::__construct( $article, $context );
57 $this->contentLanguage = $contentLanguage;
58 $this->repoGroup = $repoGroup;
59 }
60
64 protected $oldFile;
65
66 public function getName() {
67 return 'revert';
68 }
69
70 public function getRestriction() {
71 return 'upload';
72 }
73
74 protected function checkCanExecute( User $user ) {
75 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
76 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
77 }
78 parent::checkCanExecute( $user );
79
80 $oldimage = $this->getRequest()->getText( 'oldimage' );
81 if ( strlen( $oldimage ) < 16
82 || strpos( $oldimage, '/' ) !== false
83 || strpos( $oldimage, '\\' ) !== false
84 ) {
85 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
86 }
87
88 $this->oldFile = $this->repoGroup->getLocalRepo()
89 ->newFromArchiveName( $this->getTitle(), $oldimage );
90
91 if ( !$this->oldFile->exists() ) {
92 throw new ErrorPageError( '', 'filerevert-badversion' );
93 }
94 }
95
96 protected function usesOOUI() {
97 return true;
98 }
99
100 protected function alterForm( HTMLForm $form ) {
101 $form->setWrapperLegendMsg( 'filerevert-legend' );
102 $form->setSubmitTextMsg( 'filerevert-submit' );
103 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
104 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
105 }
106
107 protected function getFormFields() {
108 $timestamp = $this->oldFile->getTimestamp();
109
110 $user = $this->getUser();
111 $lang = $this->getLanguage();
112 $userDate = $lang->userDate( $timestamp, $user );
113 $userTime = $lang->userTime( $timestamp, $user );
114 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
115 $ts = $siteTs->format( 'YmdHis' );
116 $contLang = $this->contentLanguage;
117 $siteDate = $contLang->date( $ts, false, false );
118 $siteTime = $contLang->time( $ts, false, false );
119 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
120
121 return [
122 'intro' => [
123 'type' => 'info',
124 'raw' => true,
125 'default' => $this->msg( 'filerevert-intro',
126 $this->getTitle()->getText(), $userDate, $userTime,
127 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
128 $this->getFile()
129 ->getArchiveUrl(
130 $this->getRequest()->getText( 'oldimage' )
131 ),
133 ) )->parseAsBlock()
134 ],
135 'comment' => [
136 'type' => 'text',
137 'label-message' => 'filerevert-comment',
138 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
139 $tzMsg )->inContentLanguage()->text()
140 ]
141 ];
142 }
143
144 public function onSubmit( $data ) {
146
147 $old = $this->getRequest()->getText( 'oldimage' );
149 $localFile = $this->getFile();
150 '@phan-var LocalFile $localFile';
151 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
152
153 $source = $localFile->getArchiveVirtualUrl( $old );
154 $comment = $data['comment'];
155
156 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
157 return Status::newFatal( 'filerevert-identical' );
158 }
159
160 // TODO: Preserve file properties from database instead of reloading from file
161 return $localFile->upload(
162 $source,
163 $comment,
164 $comment,
165 0,
166 false,
167 false,
168 $this->getAuthority(),
169 [],
170 true,
171 true
172 );
173 }
174
175 public function onSuccess() {
176 $timestamp = $this->oldFile->getTimestamp();
177 $user = $this->getUser();
178 $lang = $this->getLanguage();
179 $userDate = $lang->userDate( $timestamp, $user );
180 $userTime = $lang->userTime( $timestamp, $user );
181
182 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
183 $userDate, $userTime,
184 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
185 $this->getFile()
186 ->getArchiveUrl(
187 $this->getRequest()->getText( 'oldimage' )
188 ),
190 ) );
191 $this->getOutput()->returnToMain( false, $this->getTitle() );
192 }
193
194 protected function getPageTitle() {
195 return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() );
196 }
197
198 protected function getDescription() {
199 return OutputPage::buildBacklinkSubtitle( $this->getTitle() )->escaped();
200 }
201
202 public function doesWrites() {
203 return true;
204 }
205
210 private function getFile(): File {
212 $wikiPage = $this->getWikiPage();
213 // @phan-suppress-next-line PhanUndeclaredMethod
214 return $wikiPage->getFile();
215 }
216}
getUser()
getAuthority()
const NS_FILE
Definition Defines.php:70
const PROTO_CURRENT
Definition Defines.php:196
getFile()
Get the file for this page, if one exists.
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:139
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition Action.php:469
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:221
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:178
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:129
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:61
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:70
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:158
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output Array values are discarded for security reasons (per WebRequest::get...
setTokenSalt( $salt)
Set the salt for the edit token.
Base class for language-specific code.
Definition Language.php:63
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:58
internal since 1.36
Definition User.php:98
Library for creating and parsing MW-style timestamps.
Old file in the oldimage table.
Prioritized list of file repositories.
Definition RepoGroup.php:30
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