| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 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; |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
class RevertAction extends FormAction { |
| 43 |
|
| 44 |
private Language $contentLanguage; |
| 45 |
private RepoGroup $repoGroup; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public function __construct( |
| 54 |
Article $article, |
| 55 |
IContextSource $context, |
| 56 |
Language $contentLanguage, |
| 57 |
RepoGroup $repoGroup |
| 58 |
) { |
| 59 |
parent::__construct( $article, $context ); |
| 60 |
$this->contentLanguage = $contentLanguage; |
| 61 |
$this->repoGroup = $repoGroup; |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
protected $oldFile; |
| 68 |
|
| 69 |
public function getName() { |
| 70 |
return 'revert'; |
| 71 |
} |
| 72 |
|
| 73 |
public function getRestriction() { |
| 74 |
|
| 75 |
return 'upload'; |
| 76 |
} |
| 77 |
|
| 78 |
protected function checkCanExecute( User $user ) { |
| 79 |
if ( $this->getTitle()->getNamespace() !== NS_FILE ) { |
| 80 |
throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) ); |
| 81 |
} |
| 82 |
|
| 83 |
$rights = [ 'reupload' ]; |
| 84 |
if ( $user->equals( $this->getFile()->getUploader() ) ) { |
| 85 |
|
| 86 |
array_unshift( $rights, 'reupload-own' ); |
| 87 |
} |
| 88 |
if ( !$user->isAllowedAny( ...$rights ) ) { |
| 89 |
throw new PermissionsError( $rights[0] ); |
| 90 |
} |
| 91 |
|
| 92 |
parent::checkCanExecute( $user ); |
| 93 |
|
| 94 |
$oldimage = $this->getRequest()->getText( 'oldimage' ); |
| 95 |
if ( strlen( $oldimage ) < 16 |
| 96 |
|| strpos( $oldimage, '/' ) !== false |
| 97 |
|| strpos( $oldimage, '\\' ) !== false |
| 98 |
) { |
| 99 |
throw new ErrorPageError( 'internalerror', 'unexpected', [ 'oldimage', $oldimage ] ); |
| 100 |
} |
| 101 |
|
| 102 |
$this->oldFile = $this->repoGroup->getLocalRepo() |
| 103 |
->newFromArchiveName( $this->getTitle(), $oldimage ); |
| 104 |
|
| 105 |
if ( !$this->oldFile->exists() ) { |
| 106 |
throw new ErrorPageError( '', 'filerevert-badversion' ); |
| 107 |
} |
| 108 |
} |
| 109 |
|
| 110 |
protected function usesOOUI() { |
| 111 |
return true; |
| 112 |
} |
| 113 |
|
| 114 |
protected function alterForm( HTMLForm $form ) { |
| 115 |
$form->setWrapperLegendMsg( 'filerevert-legend' ); |
| 116 |
$form->setSubmitTextMsg( 'filerevert-submit' ); |
| 117 |
$form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) ); |
| 118 |
$form->setTokenSalt( [ 'revert', $this->getTitle()->getPrefixedDBkey() ] ); |
| 119 |
} |
| 120 |
|
| 121 |
protected function getFormFields() { |
| 122 |
$timestamp = $this->oldFile->getTimestamp(); |
| 123 |
|
| 124 |
$user = $this->getUser(); |
| 125 |
$lang = $this->getLanguage(); |
| 126 |
$userDate = $lang->userDate( $timestamp, $user ); |
| 127 |
$userTime = $lang->userTime( $timestamp, $user ); |
| 128 |
$siteTs = MWTimestamp::getLocalInstance( $timestamp ); |
| 129 |
$ts = $siteTs->format( 'YmdHis' ); |
| 130 |
$contLang = $this->contentLanguage; |
| 131 |
$siteDate = $contLang->date( $ts, false, false ); |
| 132 |
$siteTime = $contLang->time( $ts, false, false ); |
| 133 |
$tzMsg = $siteTs->getTimezoneMessage()->inContentLanguage()->text(); |
| 134 |
|
| 135 |
return [ |
| 136 |
'intro' => [ |
| 137 |
'type' => 'info', |
| 138 |
'raw' => true, |
| 139 |
'default' => $this->msg( 'filerevert-intro', |
| 140 |
$this->getTitle()->getText(), $userDate, $userTime, |
| 141 |
(string)MediaWikiServices::getInstance()->getUrlUtils()->expand( |
| 142 |
$this->getFile() |
| 143 |
->getArchiveUrl( |
| 144 |
$this->getRequest()->getText( 'oldimage' ) |
| 145 |
), |
| 146 |
PROTO_CURRENT |
| 147 |
) )->parseAsBlock() |
| 148 |
], |
| 149 |
'comment' => [ |
| 150 |
'type' => 'text', |
| 151 |
'label-message' => 'filerevert-comment', |
| 152 |
'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime, |
| 153 |
$tzMsg )->inContentLanguage()->text() |
| 154 |
] |
| 155 |
]; |
| 156 |
} |
| 157 |
|
| 158 |
public function onSubmit( $data ) { |
| 159 |
$this->useTransactionalTimeLimit(); |
| 160 |
|
| 161 |
$old = $this->getRequest()->getText( 'oldimage' ); |
| 162 |
|
| 163 |
$localFile = $this->getFile(); |
| 164 |
'@phan-var LocalFile $localFile'; |
| 165 |
$oldFile = OldLocalFile::newFromArchiveName( $this->getTitle(), $localFile->getRepo(), $old ); |
| 166 |
|
| 167 |
$source = $localFile->getArchiveVirtualUrl( $old ); |
| 168 |
$comment = $data['comment']; |
| 169 |
|
| 170 |
if ( $localFile->getSha1() === $oldFile->getSha1() ) { |
| 171 |
return Status::newFatal( 'filerevert-identical' ); |
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
return $localFile->upload( |
| 176 |
$source, |
| 177 |
$comment, |
| 178 |
$comment, |
| 179 |
0, |
| 180 |
false, |
| 181 |
false, |
| 182 |
$this->getAuthority(), |
| 183 |
[], |
| 184 |
true, |
| 185 |
true |
| 186 |
); |
| 187 |
} |
| 188 |
|
| 189 |
public function onSuccess() { |
| 190 |
$timestamp = $this->oldFile->getTimestamp(); |
| 191 |
$user = $this->getUser(); |
| 192 |
$lang = $this->getLanguage(); |
| 193 |
$userDate = $lang->userDate( $timestamp, $user ); |
| 194 |
$userTime = $lang->userTime( $timestamp, $user ); |
| 195 |
|
| 196 |
$this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(), |
| 197 |
$userDate, $userTime, |
| 198 |
(string)MediaWikiServices::getInstance()->getUrlUtils()->expand( |
| 199 |
$this->getFile() |
| 200 |
->getArchiveUrl( |
| 201 |
$this->getRequest()->getText( 'oldimage' ) |
| 202 |
), |
| 203 |
PROTO_CURRENT |
| 204 |
) ); |
| 205 |
$this->getOutput()->returnToMain( false, $this->getTitle() ); |
| 206 |
} |
| 207 |
|
| 208 |
protected function getPageTitle() { |
| 209 |
return $this->msg( 'filerevert' )->plaintextParams( $this->getTitle()->getText() ); |
| 210 |
} |
| 211 |
|
| 212 |
protected function getDescription() { |
| 213 |
return OutputPage::buildBacklinkSubtitle( $this->getTitle() )->escaped(); |
| 214 |
} |
| 215 |
|
| 216 |
public function doesWrites() { |
| 217 |
return true; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
private function getFile(): File { |
| 225 |
|
| 226 |
$wikiPage = $this->getWikiPage(); |
| 227 |
|
| 228 |
return $wikiPage->getFile(); |
| 229 |
} |
| 230 |
} |