MediaWiki  master
RevertAction.php
Go to the documentation of this file.
1 <?php
33 class RevertAction extends FormAction {
34 
37 
39  private $repoGroup;
40 
47  public function __construct(
48  Page $page,
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,
124  wfExpandUrl(
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 ) {
142  $this->useTransactionalTimeLimit();
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,
181  wfExpandUrl(
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 }
LocalFile\getSha1
getSha1()
Definition: LocalFile.php:2639
RevertAction\getName
getName()
Return the name of the action this object responds to.
Definition: RevertAction.php:63
Page
Interface for type hinting (accepts WikiPage, Article, ImagePage, CategoryPage)
Definition: Page.php:29
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
RevertAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: RevertAction.php:195
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:146
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:37
RevertAction\__construct
__construct(Page $page, IContextSource $context, Language $contentLanguage, RepoGroup $repoGroup)
Definition: RevertAction.php:47
RevertAction\doesWrites
doesWrites()
Definition: RevertAction.php:199
RevertAction\getFile
getFile()
Definition: RevertAction.php:207
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1057
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:30
RevertAction
File reversion user interface WikiPage must contain getFile method: \WikiFilePage Article::getFile is...
Definition: RevertAction.php:33
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:66
RevertAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: RevertAction.php:104
HTMLForm\addHiddenField
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:968
RevertAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: RevertAction.php:172
OutputPage\buildBacklinkSubtitle
static buildBacklinkSubtitle(PageReference $page, $query=[])
Build message object for a subtitle containing a backlink to a page.
Definition: OutputPage.php:1092
Action\getWikiPage
getWikiPage()
Get a WikiPage object.
Definition: Action.php:195
RevertAction\$repoGroup
RepoGroup $repoGroup
Definition: RevertAction.php:39
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:195
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:166
Action\$context
IContextSource $context
IContextSource if specified; otherwise we'll use the Context from the Page.
Definition: Action.php:66
RevertAction\$contentLanguage
Language $contentLanguage
Definition: RevertAction.php:36
RevertAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: RevertAction.php:71
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:216
IContextSource
Interface for objects which can provide a MediaWiki context on request.
Definition: IContextSource.php:58
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1420
Language\date
date( $ts, $adj=false, $format=true, $timecorrection=false)
Definition: Language.php:2254
Action\$page
WikiPage Article ImagePage CategoryPage Page $page
Page on which we're performing the action.
Definition: Action.php:53
Action\msg
msg( $key,... $params)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:228
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1625
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:185
RevertAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: RevertAction.php:93
RepoGroup
Prioritized list of file repositories.
Definition: RepoGroup.php:32
$source
$source
Definition: mwdoc-filter.php:34
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:156
Action\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:460
RevertAction\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: RevertAction.php:191
RevertAction\$oldFile
OldLocalFile $oldFile
Definition: RevertAction.php:61
NS_FILE
const NS_FILE
Definition: Defines.php:70
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:30
RevertAction\getRestriction
getRestriction()
Get the permission required to perform this action.
Definition: RevertAction.php:67
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:68
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:35
MWTimestamp\getLocalInstance
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
Definition: MWTimestamp.php:173
OldLocalFile\newFromArchiveName
static newFromArchiveName( $title, $repo, $archiveName)
Definition: OldLocalFile.php:69
Language
Internationalisation code See https://www.mediawiki.org/wiki/Special:MyLanguage/Localisation for more...
Definition: Language.php:42
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:474
RevertAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: RevertAction.php:141
RevertAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: RevertAction.php:97
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms in a reusabl...
Definition: HTMLForm.php:143