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\Context\IContextSource
;
27 use MediaWiki\HTMLForm\HTMLForm
;
28 use MediaWiki\Language\Language
;
29 use MediaWiki\MediaWikiServices
;
30 use MediaWiki\Output\OutputPage
;
31 use MediaWiki\Status\Status
;
32 use MediaWiki\User\User
;
33 use MediaWiki\Utils\MWTimestamp
;
36 * File reversion user interface
37 * WikiPage must contain getFile method: \WikiFilePage
38 * Article::getFile is only for b/c: \ImagePage
42 class RevertAction
extends FormAction
{
44 private Language
$contentLanguage;
45 private RepoGroup
$repoGroup;
48 * @param Article $article
49 * @param IContextSource $context
50 * @param Language $contentLanguage
51 * @param RepoGroup $repoGroup
53 public function __construct(
55 IContextSource
$context,
56 Language
$contentLanguage,
59 parent
::__construct( $article, $context );
60 $this->contentLanguage
= $contentLanguage;
61 $this->repoGroup
= $repoGroup;
69 public function getName() {
73 public function getRestriction() {
77 protected function checkCanExecute( User
$user ) {
78 if ( $this->getTitle()->getNamespace() !== NS_FILE
) {
79 throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
81 parent
::checkCanExecute( $user );
83 $oldimage = $this->getRequest()->getText( 'oldimage' );
84 if ( strlen( $oldimage ) < 16
85 ||
strpos( $oldimage, '/' ) !== false
86 ||
strpos( $oldimage, '\\' ) !== false
88 throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] );
91 $this->oldFile
= $this->repoGroup
->getLocalRepo()
92 ->newFromArchiveName( $this->getTitle(), $oldimage );
94 if ( !$this->oldFile
->exists() ) {
95 throw new ErrorPageError( '', 'filerevert-badversion' );
99 protected function usesOOUI() {
103 protected function alterForm( HTMLForm
$form ) {
104 $form->setWrapperLegendMsg( 'filerevert-legend' );
105 $form->setSubmitTextMsg( 'filerevert-submit' );
106 $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
107 $form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] );
110 protected function getFormFields() {
111 $timestamp = $this->oldFile
->getTimestamp();
113 $user = $this->getUser();
114 $lang = $this->getLanguage();
115 $userDate = $lang->userDate( $timestamp, $user );
116 $userTime = $lang->userTime( $timestamp, $user );
117 $siteTs = MWTimestamp
::getLocalInstance( $timestamp );
118 $ts = $siteTs->format( 'YmdHis' );
119 $contLang = $this->contentLanguage
;
120 $siteDate = $contLang->date( $ts, false, false );
121 $siteTime = $contLang->time( $ts, false, false );
122 $tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text();
128 'default' => $this->msg( 'filerevert-intro',
129 $this->getTitle()->getText(), $userDate, $userTime,
130 (string)MediaWikiServices
::getInstance()->getUrlUtils()->expand(
133 $this->getRequest()->getText( 'oldimage' )
140 'label-message' => 'filerevert-comment',
141 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime,
142 $tzMsg )->inContentLanguage()->text()
147 public function onSubmit( $data ) {
148 $this->useTransactionalTimeLimit();
150 $old = $this->getRequest()->getText( 'oldimage' );
151 /** @var LocalFile $localFile */
152 $localFile = $this->getFile();
153 '@phan-var LocalFile $localFile';
154 $oldFile = OldLocalFile
::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old );
156 $source = $localFile->getArchiveVirtualUrl( $old );
157 $comment = $data['comment'];
159 if ( $localFile->getSha1() === $oldFile->getSha1() ) {
160 return Status
::newFatal( 'filerevert-identical' );
163 // TODO: Preserve file properties from database instead of reloading from file
164 return $localFile->upload(
171 $this->getAuthority(),
178 public function onSuccess() {
179 $timestamp = $this->oldFile
->getTimestamp();
180 $user = $this->getUser();
181 $lang = $this->getLanguage();
182 $userDate = $lang->userDate( $timestamp, $user );
183 $userTime = $lang->userTime( $timestamp, $user );
185 $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
186 $userDate, $userTime,
187 (string)MediaWikiServices
::getInstance()->getUrlUtils()->expand(
190 $this->getRequest()->getText( 'oldimage' )
194 $this->getOutput()->returnToMain( false, $this->getTitle() );
197 protected function getPageTitle() {
198 return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() );
201 protected function getDescription() {
202 return OutputPage
::buildBacklinkSubtitle( $this->getTitle() )->escaped();
205 public function doesWrites() {
213 private function getFile(): File
{
214 /** @var \WikiFilePage $wikiPage */
215 $wikiPage = $this->getWikiPage();
216 // @phan-suppress-next-line PhanUndeclaredMethod
217 return $wikiPage->getFile();