3 * File reversion user interface
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
22 * @author Alexandre Emsenhuber
23 * @author Rob Church <robchur@gmail.com>
26 use MediaWiki\MediaWikiServices
;
29 * File reversion user interface
33 class RevertAction
extends FormAction
{
39 public function getName() {
43 public function getRestriction() {
47 protected function checkCanExecute( User
$user ) {
48 if ( $this->getTitle()->getNamespace() !== NS_FILE
) {
49 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
51 parent
::checkCanExecute( $user );
53 $oldimage = $this->getRequest()->getText( 'oldimage' );
54 if ( strlen( $oldimage ) < 16
55 ||
strpos( $oldimage, '/' ) !== false
56 ||
strpos( $oldimage, '\\' ) !== false
58 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
61 $this->oldFile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName(
66 if ( !$this->oldFile
->exists() ) {
67 throw new ErrorPageError( '', 'filerevert-badversion' );
71 protected function usesOOUI() {
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() ] );
82 protected function getFormFields() {
83 $timestamp = $this->oldFile
->getTimestamp();
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();
99 'vertical-label' => true,
101 'default' => $this->msg( 'filerevert-intro',
102 $this->getTitle()->getText(), $userDate, $userTime,
104 $this->page
->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
110 'label-message' => 'filerevert-comment',
111 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
112 $tzMsg )->inContentLanguage()->text()
117 public function onSubmit( $data ) {
118 $this->useTransactionalTimeLimit();
120 $old = $this->getRequest()->getText( 'oldimage' );
121 $localFile = $this->page
->getFile();
122 $oldFile = OldLocalFile
::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
124 $source = $localFile->getArchiveVirtualUrl( $old );
125 $comment = $data['comment'];
127 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
128 return Status
::newFatal( 'filerevert-identical' );
131 // TODO: Preserve file properties from database instead of reloading from file
132 return $localFile->upload(
146 public function onSuccess() {
147 $timestamp = $this->oldFile
->getTimestamp();
148 $user = $this->getUser();
149 $lang = $this->getLanguage();
150 $userDate = $lang->userDate( $timestamp, $user );
151 $userTime = $lang->userTime( $timestamp, $user );
153 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
154 $userDate, $userTime,
155 wfExpandUrl( $this->page
->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
158 $this->getOutput()->returnToMain( false, $this->getTitle() );
161 protected function getPageTitle() {
162 return $this->msg( 'filerevert', $this->getTitle()->getText() );
165 protected function getDescription() {
166 return OutputPage
::buildBacklinkSubtitle( $this->getTitle() );
169 public function doesWrites() {