MediaWiki REL1_37
RevertAction.php
Go to the documentation of this file.
1<?php
33class RevertAction extends FormAction {
34
37
39 private $repoGroup;
40
47 public function __construct(
48 Page $page,
49 IContextSource $context,
50 Language $contentLanguage,
51 RepoGroup $repoGroup
52 ) {
53 parent::__construct( $page, $context );
54 $this->contentLanguage = $contentLanguage;
55 $this->repoGroup = $repoGroup;
56 }
57
61 protected $oldFile;
62
63 public function getName() {
64 return 'revert';
65 }
66
67 public function getRestriction() {
68 return 'upload';
69 }
70
71 protected function checkCanExecute( User $user ) {
72 if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
73 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
74 }
75 parent::checkCanExecute( $user );
76
77 $oldimage = $this->getRequest()->getText( 'oldimage' );
78 if ( strlen( $oldimage ) < 16
79 || strpos( $oldimage, '/' ) !== false
80 || strpos( $oldimage, '\\' ) !== false
81 ) {
82 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
83 }
84
85 $this->oldFile = $this->repoGroup->getLocalRepo()
86 ->newFromArchiveName( $this->getTitle(), $oldimage );
87
88 if ( !$this->oldFile->exists() ) {
89 throw new ErrorPageError( '', 'filerevert-badversion' );
90 }
91 }
92
93 protected function usesOOUI() {
94 return true;
95 }
96
97 protected function alterForm( HTMLForm $form ) {
98 $form->setWrapperLegendMsg( 'filerevert-legend' );
99 $form->setSubmitTextMsg( 'filerevert-submit' );
100 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
101 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
102 }
103
104 protected function getFormFields() {
105 $timestamp = $this->oldFile->getTimestamp();
106
107 $user = $this->getUser();
108 $lang = $this->getLanguage();
109 $userDate = $lang->userDate( $timestamp, $user );
110 $userTime = $lang->userTime( $timestamp, $user );
111 $siteTs = MWTimestamp::getLocalInstance( $timestamp );
112 $ts = $siteTs->format( 'YmdHis' );
113 $contLang = $this->contentLanguage;
114 $siteDate = $contLang->date( $ts, false, false );
115 $siteTime = $contLang->time( $ts, false, false );
116 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
117
118 return [
119 'intro' => [
120 'type' => 'info',
121 'raw' => true,
122 'default' => $this->msg( 'filerevert-intro',
123 $this->getTitle()->getText(), $userDate, $userTime,
125 $this->getFile()
126 ->getArchiveUrl(
127 $this->getRequest()->getText( 'oldimage' )
128 ),
130 ) )->parseAsBlock()
131 ],
132 'comment' => [
133 'type' => 'text',
134 'label-message' => 'filerevert-comment',
135 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
136 $tzMsg )->inContentLanguage()->text()
137 ]
138 ];
139 }
140
141 public function onSubmit( $data ) {
143
144 $old = $this->getRequest()->getText( 'oldimage' );
146 $localFile = $this->getFile();
147 '@phan-var LocalFile $localFile';
148 $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
149
150 $source = $localFile->getArchiveVirtualUrl( $old );
151 $comment = $data['comment'];
152
153 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
154 return Status::newFatal( 'filerevert-identical' );
155 }
156
157 // TODO: Preserve file properties from database instead of reloading from file
158 return $localFile->upload(
159 $source,
160 $comment,
161 $comment,
162 0,
163 false,
164 false,
165 $this->getUser(),
166 [],
167 true,
168 true
169 );
170 }
171
172 public function onSuccess() {
173 $timestamp = $this->oldFile->getTimestamp();
174 $user = $this->getUser();
175 $lang = $this->getLanguage();
176 $userDate = $lang->userDate( $timestamp, $user );
177 $userTime = $lang->userTime( $timestamp, $user );
178
179 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
180 $userDate, $userTime,
182 $this->getFile()
183 ->getArchiveUrl(
184 $this->getRequest()->getText( 'oldimage' )
185 ),
187 ) );
188 $this->getOutput()->returnToMain( false, $this->getTitle() );
189 }
190
191 protected function getPageTitle() {
192 return $this->msg( 'filerevert', $this->getTitle()->getText() );
193 }
194
195 protected function getDescription() {
196 return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
197 }
198
199 public function doesWrites() {
200 return true;
201 }
202
207 private function getFile(): File {
209 $wikiPage = $this->getWikiPage();
210 // @phan-suppress-next-line PhanUndeclaredMethod
211 return $wikiPage->getFile();
212 }
213}
const NS_FILE
Definition Defines.php:70
const PROTO_CURRENT
Definition Defines.php:195
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
getFile()
Get the file for this page, if one exists.
getWikiPage()
Get a WikiPage object.
Definition Action.php:195
getOutput()
Get the OutputPage being used for this instance.
Definition Action.php:156
getUser()
Shortcut to get the User being used for this instance.
Definition Action.php:166
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition Action.php:470
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition Action.php:228
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition Action.php:185
getRequest()
Get the WebRequest being used for this instance.
Definition Action.php:146
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:66
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:143
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.
Definition HTMLForm.php:968
setTokenSalt( $salt)
Set the salt for the edit token.
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition Language.php:42
Class to represent a file in the oldimage table.
static newFromArchiveName( $title, $repo, $archiveName)
Prioritized list of file repositories.
Definition RepoGroup.php:33
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(Page $page, IContextSource $context, Language $contentLanguage, RepoGroup $repoGroup)
getDescription()
Returns the description that goes below the <h1> tag.
onSuccess()
Do something exciting on successful processing of the form.
getName()
Return the name of the action this object responds to.
Language $contentLanguage
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.
RepoGroup $repoGroup
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition User.php:69
Interface for objects which can provide a MediaWiki context on request.
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition Page.php:29
$source
if(!isset( $args[0])) $lang