MediaWiki  1.32.0
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' );
121  $localFile = $this->page->getFile();
122  $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
123 
124  $source = $localFile->getArchiveVirtualUrl( $old );
125  $comment = $data['comment'];
126 
127  if ( $localFile->getSha1() === $oldFile->getSha1() ) {
128  return Status::newFatal( 'filerevert-identical' );
129  }
130 
131  // TODO: Preserve file properties from database instead of reloading from file
132  return $localFile->upload(
133  $source,
134  $comment,
135  $comment,
136  0,
137  false,
138  false,
139  $this->getUser()
140  );
141  }
142 
143  public function onSuccess() {
144  $timestamp = $this->oldFile->getTimestamp();
145  $user = $this->getUser();
146  $lang = $this->getLanguage();
147  $userDate = $lang->userDate( $timestamp, $user );
148  $userTime = $lang->userTime( $timestamp, $user );
149 
150  $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
151  $userDate, $userTime,
152  wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
154  ) );
155  $this->getOutput()->returnToMain( false, $this->getTitle() );
156  }
157 
158  protected function getPageTitle() {
159  return $this->msg( 'filerevert', $this->getTitle()->getText() );
160  }
161 
162  protected function getDescription() {
163  return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
164  }
165 
166  public function doesWrites() {
167  return true;
168  }
169 }
LocalFile\getSha1
getSha1()
Definition: LocalFile.php:2181
RevertAction\getName
getName()
Return the name of the action this object responds to.
Definition: RevertAction.php:39
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:244
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
RevertAction\getDescription
getDescription()
Returns the description that goes below the <h1> tag.
Definition: RevertAction.php:162
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:199
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
RevertAction\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: RevertAction.php:166
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:1004
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
NS_FILE
const NS_FILE
Definition: Defines.php:70
FormAction
An action which shows a form and does something based on the input from the form.
Definition: FormAction.php:28
page
target page
Definition: All_system_messages.txt:1267
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
RevertAction
File reversion user interface.
Definition: RevertAction.php:33
RevertAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: RevertAction.php:82
Action\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:258
HTMLForm\addHiddenField
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:914
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:222
RevertAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: RevertAction.php:143
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:219
RevertAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: RevertAction.php:47
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:248
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1376
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1550
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:238
RevertAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: RevertAction.php:71
$source
$source
Definition: mwdoc-filter.php:46
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:209
Action\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:417
RevertAction\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: RevertAction.php:158
RevertAction\$oldFile
OldLocalFile $oldFile
Definition: RevertAction.php:37
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
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:43
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:47
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:31
MWTimestamp\getLocalInstance
static getLocalInstance( $ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
Definition: MWTimestamp.php:204
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:512
RevertAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: RevertAction.php:117
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.
Definition: HTMLForm.php:136