MediaWiki  master
RevertAction.php
Go to the documentation of this file.
1 <?php
27 
35 class RevertAction extends FormAction {
39  protected $oldFile;
40 
41  public function getName() {
42  return 'revert';
43  }
44 
45  public function getRestriction() {
46  return 'upload';
47  }
48 
49  protected function checkCanExecute( User $user ) {
50  if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
51  throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
52  }
53  parent::checkCanExecute( $user );
54 
55  $oldimage = $this->getRequest()->getText( 'oldimage' );
56  if ( strlen( $oldimage ) < 16
57  || strpos( $oldimage, '/' ) !== false
58  || strpos( $oldimage, '\\' ) !== false
59  ) {
60  throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
61  }
62 
63  $this->oldFile = MediaWikiServices::getInstance()->getRepoGroup()->getLocalRepo()
64  ->newFromArchiveName( $this->getTitle(), $oldimage );
65 
66  if ( !$this->oldFile->exists() ) {
67  throw new ErrorPageError( '', 'filerevert-badversion' );
68  }
69  }
70 
71  protected function usesOOUI() {
72  return true;
73  }
74 
75  protected function alterForm( HTMLForm $form ) {
76  $form->setWrapperLegendMsg( 'filerevert-legend' );
77  $form->setSubmitTextMsg( 'filerevert-submit' );
78  $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
79  $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
80  }
81 
82  protected function getFormFields() {
83  $timestamp = $this->oldFile->getTimestamp();
84 
85  $user = $this->getUser();
86  $lang = $this->getLanguage();
87  $userDate = $lang->userDate( $timestamp, $user );
88  $userTime = $lang->userTime( $timestamp, $user );
89  $siteTs = MWTimestamp::getLocalInstance( $timestamp );
90  $ts = $siteTs->format( 'YmdHis' );
91  $contLang = MediaWikiServices::getInstance()->getContentLanguage();
92  $siteDate = $contLang->date( $ts, false, false );
93  $siteTime = $contLang->time( $ts, false, false );
94  $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
95 
96  return [
97  'intro' => [
98  'type' => 'info',
99  'vertical-label' => true,
100  'raw' => true,
101  'default' => $this->msg( 'filerevert-intro',
102  $this->getTitle()->getText(), $userDate, $userTime,
103  wfExpandUrl(
104  $this->getFile()
105  ->getArchiveUrl(
106  $this->getRequest()->getText( 'oldimage' )
107  ),
109  ) )->parseAsBlock()
110  ],
111  'comment' => [
112  'type' => 'text',
113  'label-message' => 'filerevert-comment',
114  'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
115  $tzMsg )->inContentLanguage()->text()
116  ]
117  ];
118  }
119 
120  public function onSubmit( $data ) {
121  $this->useTransactionalTimeLimit();
122 
123  $old = $this->getRequest()->getText( 'oldimage' );
125  $localFile = $this->getFile();
126  '@phan-var LocalFile $localFile';
127  $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
128 
129  $source = $localFile->getArchiveVirtualUrl( $old );
130  $comment = $data['comment'];
131 
132  if ( $localFile->getSha1() === $oldFile->getSha1() ) {
133  return Status::newFatal( 'filerevert-identical' );
134  }
135 
136  // TODO: Preserve file properties from database instead of reloading from file
137  return $localFile->upload(
138  $source,
139  $comment,
140  $comment,
141  0,
142  false,
143  false,
144  $this->getUser(),
145  [],
146  true,
147  true
148  );
149  }
150 
151  public function onSuccess() {
152  $timestamp = $this->oldFile->getTimestamp();
153  $user = $this->getUser();
154  $lang = $this->getLanguage();
155  $userDate = $lang->userDate( $timestamp, $user );
156  $userTime = $lang->userTime( $timestamp, $user );
157 
158  $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
159  $userDate, $userTime,
160  wfExpandUrl(
161  $this->getFile()
162  ->getArchiveUrl(
163  $this->getRequest()->getText( 'oldimage' )
164  ),
166  ) );
167  $this->getOutput()->returnToMain( false, $this->getTitle() );
168  }
169 
170  protected function getPageTitle() {
171  return $this->msg( 'filerevert', $this->getTitle()->getText() );
172  }
173 
174  protected function getDescription() {
175  return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
176  }
177 
178  public function doesWrites() {
179  return true;
180  }
181 
186  private function getFile() : File {
188  $wikiPage = $this->getWikiPage();
189  // @phan-suppress-next-line PhanUndeclaredMethod
190  return $wikiPage->getFile();
191  }
192 }
LocalFile\getSha1
getSha1()
Definition: LocalFile.php:2245
RevertAction\getName
getName()
Return the name of the action this object responds to.
Definition: RevertAction.php:41
OutputPage\buildBacklinkSubtitle
static buildBacklinkSubtitle(Title $title, $query=[])
Build message object for a subtitle containing a backlink to a page.
Definition: OutputPage.php:1091
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
RevertAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: RevertAction.php:174
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:146
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:227
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
RevertAction\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: RevertAction.php:178
RevertAction\getFile
getFile()
Definition: RevertAction.php:186
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1037
NS_FILE
const NS_FILE
Definition: Defines.php:75
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:28
RevertAction
File reversion user interface WikiPage must contain getFile method: \WikiFilePage Article::getFile is...
Definition: RevertAction.php:35
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:62
RevertAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: RevertAction.php:82
HTMLForm\addHiddenField
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:947
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:211
RevertAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: RevertAction.php:151
Action\getWikiPage
getWikiPage()
Get a WikiPage object.
Definition: Action.php:276
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:247
RevertAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: RevertAction.php:49
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:297
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1388
Action\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:309
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1575
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:266
RevertAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: RevertAction.php:71
$source
$source
Definition: mwdoc-filter.php:34
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:237
Action\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:525
RevertAction\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: RevertAction.php:170
RevertAction\$oldFile
OldLocalFile $oldFile
Definition: RevertAction.php:39
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
RevertAction\getRestriction
getRestriction()
Get the permission required to perform this action.
Definition: RevertAction.php:45
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:55
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:32
MWTimestamp\getLocalInstance
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
Definition: MWTimestamp.php:203
OldLocalFile\newFromArchiveName
static newFromArchiveName( $title, $repo, $archiveName)
Definition: OldLocalFile.php:63
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:491
RevertAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: RevertAction.php:120
RevertAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: RevertAction.php:75
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:133