| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Actions; |
| 22 |
|
| 23 |
use Article; |
| 24 |
use DeleteAction; |
| 25 |
use ErrorPageError; |
| 26 |
use File; |
| 27 |
use LocalFile; |
| 28 |
use MediaWiki\Context\IContextSource; |
| 29 |
use MediaWiki\MainConfigNames; |
| 30 |
use MediaWiki\MediaWikiServices; |
| 31 |
use MediaWiki\Page\File\FileDeleteForm; |
| 32 |
use MediaWiki\Permissions\PermissionStatus; |
| 33 |
use MediaWiki\Title\Title; |
| 34 |
use MediaWiki\User\User; |
| 35 |
use OldLocalFile; |
| 36 |
use PermissionsError; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
class FileDeleteAction extends DeleteAction { |
| 44 |
|
| 45 |
private $file; |
| 46 |
|
| 47 |
private $oldImage; |
| 48 |
|
| 49 |
private $oldFile; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct( Article $article, IContextSource $context ) { |
| 55 |
parent::__construct( $article, $context ); |
| 56 |
$services = MediaWikiServices::getInstance(); |
| 57 |
$this->file = $this->getArticle()->getFile(); |
| 58 |
$this->oldImage = $this->getRequest()->getText( 'oldimage', '' ); |
| 59 |
if ( $this->oldImage !== '' ) { |
| 60 |
$this->oldFile = $services->getRepoGroup()->getLocalRepo()->newFromArchiveName( |
| 61 |
$this->getTitle(), |
| 62 |
$this->oldImage |
| 63 |
); |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
protected function getPageTitle() { |
| 68 |
$title = $this->getTitle(); |
| 69 |
return $this->msg( 'filedelete' )->plaintextParams( $title->getText() ); |
| 70 |
} |
| 71 |
|
| 72 |
protected function tempDelete() { |
| 73 |
$file = $this->file; |
| 74 |
'@phan-var LocalFile $file'; |
| 75 |
$this->tempExecute( $file ); |
| 76 |
} |
| 77 |
|
| 78 |
private function tempExecute( LocalFile $file ): void { |
| 79 |
$context = $this->getContext(); |
| 80 |
$title = $this->getTitle(); |
| 81 |
$article = $this->getArticle(); |
| 82 |
$outputPage = $context->getOutput(); |
| 83 |
$request = $context->getRequest(); |
| 84 |
|
| 85 |
$checkFile = $this->oldFile ?: $file; |
| 86 |
if ( !$checkFile->exists() || !$checkFile->isLocal() ) { |
| 87 |
$outputPage->addHTML( $this->prepareMessage( 'filedelete-nofile' ) ); |
| 88 |
$outputPage->addReturnTo( $title ); |
| 89 |
return; |
| 90 |
} |
| 91 |
|
| 92 |
|
| 93 |
$token = $request->getVal( 'wpEditToken' ); |
| 94 |
if ( |
| 95 |
!$request->wasPosted() || |
| 96 |
!$context->getUser()->matchEditToken( $token, [ 'delete', $title->getPrefixedText() ] ) |
| 97 |
) { |
| 98 |
$this->showConfirm(); |
| 99 |
return; |
| 100 |
} |
| 101 |
|
| 102 |
|
| 103 |
if ( $article->getRevIdFetched() !== $request->getIntOrNull( 'wpConfirmationRevId' ) ) { |
| 104 |
$this->showEditedWarning(); |
| 105 |
$this->showConfirm(); |
| 106 |
return; |
| 107 |
} |
| 108 |
|
| 109 |
$permissionStatus = PermissionStatus::newEmpty(); |
| 110 |
if ( !$context->getAuthority()->authorizeWrite( |
| 111 |
'delete', $title, $permissionStatus |
| 112 |
) ) { |
| 113 |
throw new PermissionsError( 'delete', $permissionStatus ); |
| 114 |
} |
| 115 |
|
| 116 |
$reason = $this->getDeleteReason(); |
| 117 |
|
| 118 |
|
| 119 |
$suppress = $request->getCheck( 'wpSuppress' ) && |
| 120 |
$context->getAuthority()->isAllowed( 'suppressrevision' ); |
| 121 |
|
| 122 |
$status = FileDeleteForm::doDelete( |
| 123 |
$title, |
| 124 |
$file, |
| 125 |
$this->oldImage, |
| 126 |
$reason, |
| 127 |
$suppress, |
| 128 |
$context->getUser(), |
| 129 |
[], |
| 130 |
$request->getCheck( 'wpDeleteTalk' ) |
| 131 |
); |
| 132 |
|
| 133 |
if ( !$status->isGood() ) { |
| 134 |
$outputPage->wrapWikiTextAsInterface( |
| 135 |
'error', |
| 136 |
$status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) |
| 137 |
); |
| 138 |
} |
| 139 |
if ( $status->isOK() ) { |
| 140 |
$outputPage->setPageTitleMsg( $context->msg( 'actioncomplete' ) ); |
| 141 |
$outputPage->addHTML( $this->prepareMessage( 'filedelete-success' ) ); |
| 142 |
|
| 143 |
|
| 144 |
$outputPage->addReturnTo( $this->oldImage ? $title : Title::newMainPage() ); |
| 145 |
|
| 146 |
$this->watchlistManager->setWatch( |
| 147 |
$request->getCheck( 'wpWatch' ), |
| 148 |
$context->getAuthority(), |
| 149 |
$title |
| 150 |
); |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
protected function showFormWarnings(): void { |
| 155 |
$this->getOutput()->addHTML( $this->prepareMessage( 'filedelete-intro' ) ); |
| 156 |
$this->showSubpagesWarnings(); |
| 157 |
} |
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
private function showConfirm() { |
| 163 |
$this->prepareOutputForForm(); |
| 164 |
$context = $this->getContext(); |
| 165 |
$article = $this->getArticle(); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
$oldid = $context->getRequest()->getIntOrNull( 'oldid' ); |
| 171 |
if ( $oldid !== null && $oldid !== $article->getRevIdFetched() ) { |
| 172 |
$this->showEditedWarning(); |
| 173 |
} |
| 174 |
|
| 175 |
$this->showFormWarnings(); |
| 176 |
$form = $this->getForm(); |
| 177 |
if ( $form->show() ) { |
| 178 |
$this->onSuccess(); |
| 179 |
} |
| 180 |
$this->showEditReasonsLinks(); |
| 181 |
$this->showLogEntries(); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
private function prepareMessage( string $message ) { |
| 193 |
if ( $this->oldFile ) { |
| 194 |
$lang = $this->getContext()->getLanguage(); |
| 195 |
|
| 196 |
|
| 197 |
return $this->getContext()->msg( |
| 198 |
"{$message}-old", |
| 199 |
wfEscapeWikiText( $this->getTitle()->getText() ), |
| 200 |
$lang->date( $this->oldFile->getTimestamp(), true ), |
| 201 |
$lang->time( $this->oldFile->getTimestamp(), true ), |
| 202 |
(string)MediaWikiServices::getInstance()->getUrlUtils()->expand( |
| 203 |
$this->file->getArchiveUrl( $this->oldImage ), |
| 204 |
PROTO_CURRENT |
| 205 |
) |
| 206 |
)->parseAsBlock(); |
| 207 |
} else { |
| 208 |
return $this->getContext()->msg( |
| 209 |
$message, |
| 210 |
wfEscapeWikiText( $this->getTitle()->getText() ) |
| 211 |
)->parseAsBlock(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
protected function getFormAction(): string { |
| 219 |
$q = []; |
| 220 |
$q['action'] = 'delete'; |
| 221 |
|
| 222 |
if ( $this->oldImage ) { |
| 223 |
$q['oldimage'] = $this->oldImage; |
| 224 |
} |
| 225 |
|
| 226 |
return $this->getTitle()->getLocalURL( $q ); |
| 227 |
} |
| 228 |
|
| 229 |
protected function checkCanExecute( User $user ) { |
| 230 |
parent::checkCanExecute( $user ); |
| 231 |
|
| 232 |
if ( $this->getContext()->getConfig()->get( MainConfigNames::UploadMaintenance ) ) { |
| 233 |
throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' ); |
| 234 |
} |
| 235 |
} |
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
protected function getFormMessages(): array { |
| 242 |
return [ |
| 243 |
self::MSG_REASON_DROPDOWN => 'filedelete-reason-dropdown', |
| 244 |
self::MSG_REASON_DROPDOWN_SUPPRESS => 'filedelete-reason-dropdown-suppress', |
| 245 |
self::MSG_REASON_DROPDOWN_OTHER => 'filedelete-reason-otherlist', |
| 246 |
self::MSG_COMMENT => 'filedelete-comment', |
| 247 |
self::MSG_REASON_OTHER => 'filedelete-otherreason', |
| 248 |
self::MSG_SUBMIT => 'filedelete-submit', |
| 249 |
self::MSG_LEGEND => 'filedelete-legend', |
| 250 |
self::MSG_EDIT_REASONS => 'filedelete-edit-reasonlist', |
| 251 |
self::MSG_EDIT_REASONS_SUPPRESS => 'filedelete-edit-reasonlist-suppress', |
| 252 |
]; |
| 253 |
} |
| 254 |
} |