MediaWiki  1.27.2
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 alterForm( HTMLForm $form ) {
70  $form->setWrapperLegendMsg( 'filerevert-legend' );
71  $form->setSubmitTextMsg( 'filerevert-submit' );
72  $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
73  $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
74  }
75 
76  protected function getFormFields() {
78 
79  $timestamp = $this->oldFile->getTimestamp();
80 
81  $user = $this->getUser();
82  $lang = $this->getLanguage();
83  $userDate = $lang->userDate( $timestamp, $user );
84  $userTime = $lang->userTime( $timestamp, $user );
86  $ts = $siteTs->format( 'YmdHis' );
87  $siteDate = $wgContLang->date( $ts, false, false );
88  $siteTime = $wgContLang->time( $ts, false, false );
89  $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
90 
91  return [
92  'intro' => [
93  'type' => 'info',
94  'vertical-label' => true,
95  'raw' => true,
96  'default' => $this->msg( 'filerevert-intro',
97  $this->getTitle()->getText(), $userDate, $userTime,
99  $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
101  ) )->parseAsBlock()
102  ],
103  'comment' => [
104  'type' => 'text',
105  'label-message' => 'filerevert-comment',
106  'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
107  $tzMsg )->inContentLanguage()->text()
108  ]
109  ];
110  }
111 
112  public function onSubmit( $data ) {
113  $this->useTransactionalTimeLimit();
114 
115  $source = $this->page->getFile()->getArchiveVirtualUrl(
116  $this->getRequest()->getText( 'oldimage' )
117  );
118  $comment = $data['comment'];
119 
120  // TODO: Preserve file properties from database instead of reloading from file
121  return $this->page->getFile()->upload(
122  $source,
123  $comment,
124  $comment,
125  0,
126  false,
127  false,
128  $this->getUser()
129  );
130  }
131 
132  public function onSuccess() {
133  $timestamp = $this->oldFile->getTimestamp();
134  $user = $this->getUser();
135  $lang = $this->getLanguage();
136  $userDate = $lang->userDate( $timestamp, $user );
137  $userTime = $lang->userTime( $timestamp, $user );
138 
139  $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
140  $userDate, $userTime,
141  wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
143  ) );
144  $this->getOutput()->returnToMain( false, $this->getTitle() );
145  }
146 
147  protected function getPageTitle() {
148  return $this->msg( 'filerevert', $this->getTitle()->getText() );
149  }
150 
151  protected function getDescription() {
152  return OutputPage::buildBacklinkSubtitle( $this->getTitle() );
153  }
154 
155  public function doesWrites() {
156  return true;
157  }
158 }
setTokenSalt($salt)
Set the salt for the edit token.
Definition: HTMLForm.php:939
OldLocalFile $oldFile
getOutput()
Get the OutputPage being used for this instance.
Definition: Action.php:207
addHiddenField($name, $value, array $attribs=[])
Add a hidden field to the output.
Definition: HTMLForm.php:851
getTitle()
Shortcut to get the Title object from the page.
Definition: Action.php:246
if(!isset($args[0])) $lang
getUser()
Shortcut to get the User being used for this instance.
Definition: Action.php:217
$comment
setWrapperLegendMsg($msg)
Prompt the whole form to be wrapped in a "
", with this message as its "" element...
Definition: HTMLForm.php:1381
File reversion user interface.
$source
const PROTO_CURRENT
Definition: Defines.php:264
getLanguage()
Shortcut to get the user Language being used for this instance.
Definition: Action.php:236
when a variable name is used in a it is silently declared as a new local masking the global
Definition: design.txt:93
wfExpandUrl($url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
static getLocalInstance($ts=false)
Get a timestamp instance in the server local timezone ($wgLocaltimezone)
useTransactionalTimeLimit()
Call wfTransactionalTimeLimit() if this request was POSTed.
Definition: Action.php:418
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.
An error page which can definitely be safely rendered using the OutputPage.
if($limit) $timestamp
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
setSubmitTextMsg($msg)
Set the text for the submit button to a message.
Definition: HTMLForm.php:1250
Object handling generic submission, CSRF protection, layout and other logic for UI forms...
Definition: HTMLForm.php:123
const NS_FILE
Definition: Defines.php:75
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 local account $user
Definition: hooks.txt:242
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
alterForm(HTMLForm $form)
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 local content language as $wgContLang
Definition: design.txt:56
checkCanExecute(User $user)
msg()
Get a Message object with context set Parameters are the same as wfMessage()
Definition: Action.php:256
getRequest()
Get the WebRequest being used for this instance.
Definition: Action.php:197
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached one of or reset my talk page
Definition: hooks.txt:2338