MediaWiki  REL1_31
RevertAction.php
Go to the documentation of this file.
1 <?php
31 class RevertAction extends FormAction {
35  protected $oldFile;
36 
37  public function getName() {
38  return 'revert';
39  }
40 
41  public function getRestriction() {
42  return 'upload';
43  }
44 
45  protected function checkCanExecute( User $user ) {
46  if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
47  throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
48  }
49  parent::checkCanExecute( $user );
50 
51  $oldimage = $this->getRequest()->getText( 'oldimage' );
52  if ( strlen( $oldimage ) < 16
53  || strpos( $oldimage, '/' ) !== false
54  || strpos( $oldimage, '\\' ) !== false
55  ) {
56  throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
57  }
58 
59  $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
60  $this->getTitle(),
61  $oldimage
62  );
63 
64  if ( !$this->oldFile->exists() ) {
65  throw new ErrorPageError( '', 'filerevert-badversion' );
66  }
67  }
68 
69  protected function usesOOUI() {
70  return true;
71  }
72 
73  protected function alterForm( HTMLForm $form ) {
74  $form->setWrapperLegendMsg( 'filerevert-legend' );
75  $form->setSubmitTextMsg( 'filerevert-submit' );
76  $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
77  $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
78  }
79 
80  protected function getFormFields() {
82 
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  $siteDate = $wgContLang->date( $ts, false, false );
92  $siteTime = $wgContLang->time( $ts, false, false );
93  $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
94 
95  return [
96  'intro' => [
97  'type' => 'info',
98  'vertical-label' => true,
99  'raw' => true,
100  'default' => $this->msg( 'filerevert-intro',
101  $this->getTitle()->getText(), $userDate, $userTime,
102  wfExpandUrl(
103  $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
105  ) )->parseAsBlock()
106  ],
107  'comment' => [
108  'type' => 'text',
109  'label-message' => 'filerevert-comment',
110  'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
111  $tzMsg )->inContentLanguage()->text()
112  ]
113  ];
114  }
115 
116  public function onSubmit( $data ) {
117  $this->useTransactionalTimeLimit();
118 
119  $old = $this->getRequest()->getText( 'oldimage' );
120  $localFile = $this->page->getFile();
121  $oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
122 
123  $source = $localFile->getArchiveVirtualUrl( $old );
124  $comment = $data['comment'];
125 
126  if ( $localFile->getSha1() === $oldFile->getSha1() ) {
127  return Status::newFatal( 'filerevert-identical' );
128  }
129 
130  // TODO: Preserve file properties from database instead of reloading from file
131  return $localFile->upload(
132  $source,
133  $comment,
134  $comment,
135  0,
136  false,
137  false,
138  $this->getUser()
139  );
140  }
141 
142  public function onSuccess() {
143  $timestamp = $this->oldFile->getTimestamp();
144  $user = $this->getUser();
145  $lang = $this->getLanguage();
146  $userDate = $lang->userDate( $timestamp, $user );
147  $userTime = $lang->userTime( $timestamp, $user );
148 
149  $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
150  $userDate, $userTime,
151  wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
153  ) );
154  $this->getOutput()->returnToMain( false, $this->getTitle() );
155  }
156 
157  protected function getPageTitle() {
158  return $this->msg( 'filerevert', $this->getTitle()->getText() );
159  }
160 
161  protected function getDescription() {
162  return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
163  }
164 
165  public function doesWrites() {
166  return true;
167  }
168 }
LocalFile\getSha1
getSha1()
Definition: LocalFile.php:2174
$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:247
RevertAction\getName
getName()
Return the name of the action this object responds to.
Definition: RevertAction.php:37
OutputPage\buildBacklinkSubtitle
static buildBacklinkSubtitle(Title $title, $query=[])
Build message object for a subtitle containing a backlink to a page.
Definition: OutputPage.php:1002
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:161
Action\getRequest
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:197
$lang
if(!isset( $args[0])) $lang
Definition: testCompression.php:33
RevertAction\doesWrites
doesWrites()
Indicates whether this action may perform database writes.
Definition: RevertAction.php:165
HTMLForm\setTokenSalt
setTokenSalt( $salt)
Set the salt for the edit token.
Definition: HTMLForm.php:988
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:68
NS_FILE
const NS_FILE
Definition: Defines.php:80
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:37
RevertAction
File reversion user interface.
Definition: RevertAction.php:31
RevertAction\getFormFields
getFormFields()
Get an HTMLForm descriptor array.
Definition: RevertAction.php:80
Action\msg
msg( $key)
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:256
HTMLForm\addHiddenField
addHiddenField( $name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:898
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:232
RevertAction\onSuccess
onSuccess()
Do something exciting on successful processing of the form.
Definition: RevertAction.php:142
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:95
Action\getUser
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:217
RevertAction\checkCanExecute
checkCanExecute(User $user)
Checks if the given user (identified by an object) can perform this action.
Definition: RevertAction.php:45
Action\getTitle
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:246
HTMLForm\setSubmitTextMsg
setSubmitTextMsg( $msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1356
HTMLForm\setWrapperLegendMsg
setWrapperLegendMsg( $msg)
Prompt the whole form to be wrapped in a "<fieldset>", with this message as its "<legend>" element.
Definition: HTMLForm.php:1530
Action\getLanguage
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:236
RevertAction\usesOOUI
usesOOUI()
Whether the form should use OOUI.
Definition: RevertAction.php:69
$source
$source
Definition: mwdoc-filter.php:46
Action\getOutput
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:207
Action\useTransactionalTimeLimit
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:416
RevertAction\getPageTitle
getPageTitle()
Returns the name that goes in the <h1> page title.
Definition: RevertAction.php:157
RevertAction\$oldFile
OldLocalFile $oldFile
Definition: RevertAction.php:35
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:41
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:53
OldLocalFile
Class to represent a file in the oldimage table.
Definition: OldLocalFile.php:29
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:61
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:521
RevertAction\onSubmit
onSubmit( $data)
Process the form on POST submission.
Definition: RevertAction.php:116
RevertAction\alterForm
alterForm(HTMLForm $form)
Play with the HTMLForm if you need to more substantially.
Definition: RevertAction.php:73
$wgContLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as and the content language as $wgContLang
Definition: design.txt:57
HTMLForm
Object handling generic submission, CSRF protection, layout and other logic for UI forms.
Definition: HTMLForm.php:130