MediaWiki master
RevertAction.php
Go to the documentation of this file.
1<?php
33
41class RevertAction extends FormAction {
42
43 private Language $contentLanguage;
44 private RepoGroup $repoGroup;
45
52 public function __construct(
53 Article $article,
54 IContextSource $context,
55 Language $contentLanguage,
56 RepoGroup $repoGroup
57 ) {
58 parent::__construct( $article, $context );
59 $this->contentLanguage = $contentLanguage;
60 $this->repoGroup = $repoGroup;
61 }
62
66 protected $oldFile;
67
68 public function getName() {
69 return 'revert';
70 }
71
72 public function getRestriction() {
73 return 'upload';
74 }
75
76 protected function checkCanExecute( User $user ) {
77 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
78 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
79 }
80 parent::checkCanExecute( $user );
81
82 $oldimage = $this->getRequest()->getText( 'oldimage' );
83 if ( strlen( $oldimage ) < 16
84 || strpos( $oldimage, '/' ) !== false
85 || strpos( $oldimage, '\\' ) !== false
86 ) {
87 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
88 }
89
90 $this->oldFile = $this->repoGroup->getLocalRepo()
91 ->newFromArchiveName( $this->getTitle(), $oldimage );
92
93 if ( !$this->oldFile->exists() ) {
94 throw new ErrorPageError( '', 'filerevert-badversion' );
95 }
96 }
97
98 protected function usesOOUI() {
99 return true;
100 }
101
102 protected function alterForm( HTMLForm $form ) {
103 $form->setWrapperLegendMsg( 'filerevert-legend' );
104 $form->setSubmitTextMsg( 'filerevert-submit' );
105 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
106 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
107 }
108
109 protected function getFormFields() {
110 $timestamp = $this->oldFile->getTimestamp();
111
112 $user = $this->getUser();
113 $lang = $this->getLanguage();
114 $userDate = $lang->userDate( $timestamp, $user );
115 $userTime = $lang->userTime( $timestamp, $user );
116 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
117 $ts = $siteTs->format( 'YmdHis' );
118 $contLang = $this->contentLanguage;
119 $siteDate = $contLang->date( $ts, false, false );
120 $siteTime = $contLang->time( $ts, false, false );
121 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
122
123 return [
124 'intro' => [
125 'type' => 'info',
126 'raw' => true,
127 'default' => $this->msg( 'filerevert-intro',
128 $this->getTitle()->getText(), $userDate, $userTime,
129 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
130 $this->getFile()
131 ->getArchiveUrl(
132 $this->getRequest()->getText( 'oldimage' )
133 ),
135 ) )->parseAsBlock()
136 ],
137 'comment' => [
138 'type' => 'text',
139 'label-message' => 'filerevert-comment',
140 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
141 $tzMsg )->inContentLanguage()->text()
142 ]
143 ];
144 }
145
146 public function onSubmit( $data ) {
148
149 $old = $this->getRequest()->getText( 'oldimage' );
151 $localFile = $this->getFile();
152 '@phan-var LocalFile $localFile';
153 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
154
155 $source = $localFile->getArchiveVirtualUrl( $old );
156 $comment = $data['comment'];
157
158 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
159 return Status::newFatal( 'filerevert-identical' );
160 }
161
162 // TODO: Preserve file properties from database instead of reloading from file
163 return $localFile->upload(
164 $source,
165 $comment,
166 $comment,
167 0,
168 false,
169 false,
170 $this->getAuthority(),
171 [],
172 true,
173 true
174 );
175 }
176
177 public function onSuccess() {
178 $timestamp = $this->oldFile->getTimestamp();
179 $user = $this->getUser();
180 $lang = $this->getLanguage();
181 $userDate = $lang->userDate( $timestamp, $user );
182 $userTime = $lang->userTime( $timestamp, $user );
183
184 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
185 $userDate, $userTime,
186 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
187 $this->getFile()
188 ->getArchiveUrl(
189 $this->getRequest()->getText( 'oldimage' )
190 ),
192 ) );
193 $this->getOutput()->returnToMain( false, $this->getTitle() );
194 }
195
196 protected function getPageTitle() {
197 return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() );
198 }
199
200 protected function getDescription() {
201 return OutputPage::buildBacklinkSubtitle( $this->getTitle() )->escaped();
202 }
203
204 public function doesWrites() {
205 return true;
206 }
207
212 private function getFile(): File {
214 $wikiPage = $this->getWikiPage();
215 // @phan-suppress-next-line PhanUndeclaredMethod
216 return $wikiPage->getFile();
217 }
218}
getUser()
getRequest()
getAuthority()
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:141
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition Action.php:468
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:223
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:180
Legacy class representing an editable page and handling UI for some page actions.
Definition Article.php:70
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:74
An action which shows a form and does something based on the input from the form.
Base class for language-specific code.
Definition Language.php:66
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition HTMLForm.php:208
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.
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:93
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