MediaWiki  master
RevertAction.php
Go to the documentation of this file.
1 <?php
27 
33 class RevertAction extends FormAction {
37  protected $oldFile;
38 
39  public function getName() {
40  return 'revert';
41  }
42 
43  public function getRestriction() {
44  return 'upload';
45  }
46 
47  protected function checkCanExecute( User $user ) {
48  if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
49  throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
50  }
51  parent::checkCanExecute( $user );
52 
53  $oldimage = $this->getRequest()->getText( 'oldimage' );
54  if ( strlen( $oldimage ) < 16
55  || strpos( $oldimage, '/' ) !== false
56  || strpos( $oldimage, '\\' ) !== false
57  ) {
58  throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
59  }
60 
61  $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
62  $this->getTitle(),
63  $oldimage
64  );
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->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
106  ) )->parseAsBlock()
107  ],
108  'comment' => [
109  'type' => 'text',
110  'label-message' => 'filerevert-comment',
111  'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
112  $tzMsg )->inContentLanguage()->text()
113  ]
114  ];
115  }
116 
117  public function onSubmit( $data ) {
118  $this->useTransactionalTimeLimit();
119 
120  $old = $this->getRequest()->getText( 'oldimage' );
122  $localFile = $this->page->getFile();
123  '@phan-var LocalFile $localFile';
124  $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
125 
126  $source = $localFile->getArchiveVirtualUrl( $old );
127  $comment = $data['comment'];
128 
129  if ( $localFile->getSha1() === $oldFile->getSha1() ) {
130  return Status::newFatal( 'filerevert-identical' );
131  }
132 
133  // TODO: Preserve file properties from database instead of reloading from file
134  return $localFile->upload(
135  $source,
136  $comment,
137  $comment,
138  0,
139  false,
140  false,
141  $this->getUser(),
142  [],
143  true,
144  true
145  );
146  }
147 
148  public function onSuccess() {
149  $timestamp = $this->oldFile->getTimestamp();
150  $user = $this->getUser();
151  $lang = $this->getLanguage();
152  $userDate = $lang->userDate( $timestamp, $user );
153  $userTime = $lang->userTime( $timestamp, $user );
154 
155  $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
156  $userDate, $userTime,
157  wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
159  ) );
160  $this->getOutput()->returnToMain( false, $this->getTitle() );
161  }
162 
163  protected function getPageTitle() {
164  return $this->msg( 'filerevert', $this->getTitle()->getText() );
165  }
166 
167  protected function getDescription() {
168  return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
169  }
170 
171  public function doesWrites() {
172  return true;
173  }
174 }
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:69
OldLocalFile $oldFile
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:208
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:247
onSubmit( $data)
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1036
static newFromArchiveName( $title, $repo, $archiveName)
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
if(!isset( $args[0])) $lang
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:218
File reversion user interface.
$source
const PROTO_CURRENT
Definition: Defines.php:202
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:237
The User object encapsulates all of the user-specific settings (user_id, name, rights, email address, options, last login time).
Definition: User.php:51
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:259
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:422
An action which shows a form and does something based on the input from the form. ...
Definition: FormAction.php:28
static buildBacklinkSubtitle(Title $title, $query=[])
Build message object for a subtitle containing a backlink to a page.
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:943
An error page which can definitely be safely rendered using the OutputPage.
static singleton()
Definition: RepoGroup.php:60
const NS_FILE
Definition: Defines.php:66
alterForm(HTMLForm $form)
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1392
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element...
Definition: HTMLForm.php:1579
checkCanExecute(User $user)
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:198