MediaWiki master
RevertAction.php
Go to the documentation of this file.
1<?php
32
40class RevertAction extends FormAction {
41
42 private Language $contentLanguage;
43 private RepoGroup $repoGroup;
44
51 public function __construct(
52 Article $article,
53 IContextSource $context,
54 Language $contentLanguage,
55 RepoGroup $repoGroup
56 ) {
57 parent::__construct( $article, $context );
58 $this->contentLanguage = $contentLanguage;
59 $this->repoGroup = $repoGroup;
60 }
61
65 protected $oldFile;
66
67 public function getName() {
68 return 'revert';
69 }
70
71 public function getRestriction() {
72 return 'upload';
73 }
74
75 protected function checkCanExecute( User $user ) {
76 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
77 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
78 }
79 parent::checkCanExecute( $user );
80
81 $oldimage = $this->getRequest()->getText( 'oldimage' );
82 if ( strlen( $oldimage ) < 16
83 || strpos( $oldimage, '/' ) !== false
84 || strpos( $oldimage, '\\' ) !== false
85 ) {
86 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
87 }
88
89 $this->oldFile = $this->repoGroup->getLocalRepo()
90 ->newFromArchiveName( $this->getTitle(), $oldimage );
91
92 if ( !$this->oldFile->exists() ) {
93 throw new ErrorPageError( '', 'filerevert-badversion' );
94 }
95 }
96
97 protected function usesOOUI() {
98 return true;
99 }
100
101 protected function alterForm( HTMLForm $form ) {
102 $form->setWrapperLegendMsg( 'filerevert-legend' );
103 $form->setSubmitTextMsg( 'filerevert-submit' );
104 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
105 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
106 }
107
108 protected function getFormFields() {
109 $timestamp = $this->oldFile->getTimestamp();
110
111 $user = $this->getUser();
112 $lang = $this->getLanguage();
113 $userDate = $lang->userDate( $timestamp, $user );
114 $userTime = $lang->userTime( $timestamp, $user );
115 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
116 $ts = $siteTs->format( 'YmdHis' );
117 $contLang = $this->contentLanguage;
118 $siteDate = $contLang->date( $ts, false, false );
119 $siteTime = $contLang->time( $ts, false, false );
120 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
121
122 return [
123 'intro' => [
124 'type' => 'info',
125 'raw' => true,
126 'default' => $this->msg( 'filerevert-intro',
127 $this->getTitle()->getText(), $userDate, $userTime,
128 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
129 $this->getFile()
130 ->getArchiveUrl(
131 $this->getRequest()->getText( 'oldimage' )
132 ),
134 ) )->parseAsBlock()
135 ],
136 'comment' => [
137 'type' => 'text',
138 'label-message' => 'filerevert-comment',
139 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
140 $tzMsg )->inContentLanguage()->text()
141 ]
142 ];
143 }
144
145 public function onSubmit( $data ) {
147
148 $old = $this->getRequest()->getText( 'oldimage' );
150 $localFile = $this->getFile();
151 '@phan-var LocalFile $localFile';
152 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
153
154 $source = $localFile->getArchiveVirtualUrl( $old );
155 $comment = $data['comment'];
156
157 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
158 return Status::newFatal( 'filerevert-identical' );
159 }
160
161 // TODO: Preserve file properties from database instead of reloading from file
162 return $localFile->upload(
163 $source,
164 $comment,
165 $comment,
166 0,
167 false,
168 false,
169 $this->getAuthority(),
170 [],
171 true,
172 true
173 );
174 }
175
176 public function onSuccess() {
177 $timestamp = $this->oldFile->getTimestamp();
178 $user = $this->getUser();
179 $lang = $this->getLanguage();
180 $userDate = $lang->userDate( $timestamp, $user );
181 $userTime = $lang->userTime( $timestamp, $user );
182
183 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
184 $userDate, $userTime,
185 (string)MediaWikiServices::getInstance()->getUrlUtils()->expand(
186 $this->getFile()
187 ->getArchiveUrl(
188 $this->getRequest()->getText( 'oldimage' )
189 ),
191 ) );
192 $this->getOutput()->returnToMain( false, $this->getTitle() );
193 }
194
195 protected function getPageTitle() {
196 return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() );
197 }
198
199 protected function getDescription() {
200 return OutputPage::buildBacklinkSubtitle( $this->getTitle() )->escaped();
201 }
202
203 public function doesWrites() {
204 return true;
205 }
206
211 private function getFile(): File {
213 $wikiPage = $this->getWikiPage();
214 // @phan-suppress-next-line PhanUndeclaredMethod
215 return $wikiPage->getFile();
216 }
217}
getUser()
getRequest()
getAuthority()
const NS_FILE
Definition Defines.php:70
const PROTO_CURRENT
Definition Defines.php:207
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:471
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:67
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:73
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: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: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