3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
21 namespace MediaWiki\Actions
;
28 use MediaWiki\Context\IContextSource
;
29 use MediaWiki\Html\Html
;
30 use MediaWiki\MainConfigNames
;
31 use MediaWiki\MediaWikiServices
;
32 use MediaWiki\Page\File\FileDeleteForm
;
33 use MediaWiki\Permissions\PermissionStatus
;
34 use MediaWiki\Title\Title
;
35 use MediaWiki\User\User
;
40 * Handle file deletion
44 class FileDeleteAction
extends DeleteAction
{
47 /** @var string Descriptor for the old version of the image, if applicable */
49 /** @var OldLocalFile|null Corresponding to oldImage, if applicable */
55 public function __construct( Article
$article, IContextSource
$context ) {
56 parent
::__construct( $article, $context );
57 $services = MediaWikiServices
::getInstance();
58 $this->file
= $this->getArticle()->getFile();
59 $this->oldImage
= $this->getRequest()->getText( 'oldimage', '' );
60 if ( $this->oldImage
!== '' ) {
61 $this->oldFile
= $services->getRepoGroup()->getLocalRepo()->newFromArchiveName(
68 protected function getPageTitle() {
69 $title = $this->getTitle();
70 return $this->msg( 'filedelete' )->plaintextParams( $title->getText() );
73 protected function tempDelete() {
75 /** @var LocalFile $file */'@phan-var LocalFile $file';
76 $this->tempExecute( $file );
79 private function tempExecute( LocalFile
$file ): void
{
80 $context = $this->getContext();
81 $title = $this->getTitle();
82 $article = $this->getArticle();
83 $outputPage = $context->getOutput();
84 $request = $context->getRequest();
86 $checkFile = $this->oldFile ?
: $file;
87 if ( !$checkFile->exists() ||
!$checkFile->isLocal() ) {
88 $outputPage->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
89 $outputPage->addReturnTo( $title );
93 // Perform the deletion if appropriate
94 $token = $request->getVal( 'wpEditToken' );
96 !$request->wasPosted() ||
97 !$context->getUser()->matchEditToken( $token, [ 'delete', $title->getPrefixedText() ] )
103 // Check to make sure the page has not been edited while the deletion was being confirmed
104 if ( $article->getRevIdFetched() !== $request->getIntOrNull( 'wpConfirmationRevId' ) ) {
105 $this->showEditedWarning();
106 $this->showConfirm();
110 $permissionStatus = PermissionStatus
::newEmpty();
111 if ( !$context->getAuthority()->authorizeWrite(
112 'delete', $title, $permissionStatus
114 throw new PermissionsError( 'delete', $permissionStatus );
117 $reason = $this->getDeleteReason();
119 # Flag to hide all contents of the archived revisions
120 $suppress = $request->getCheck( 'wpSuppress' ) &&
121 $context->getAuthority()->isAllowed( 'suppressrevision' );
123 $status = FileDeleteForm
::doDelete(
131 $request->getCheck( 'wpDeleteTalk' )
134 if ( !$status->isGood() ) {
135 $outputPage->setPageTitleMsg(
136 $this->msg( 'cannotdelete-title' )->plaintextParams( $title->getPrefixedText() )
138 $outputPage->addModuleStyles( 'mediawiki.codex.messagebox.styles' );
139 foreach ( $status->getMessages() as $msg ) {
140 $outputPage->addHTML( Html
::errorBox(
141 $context->msg( $msg )->parse()
145 if ( $status->isOK() ) {
146 $outputPage->setPageTitleMsg( $context->msg( 'actioncomplete' ) );
147 $outputPage->addHTML( $this->prepareMessage( 'filedelete-success' ) );
148 // Return to the main page if we just deleted all versions of the
149 // file, otherwise go back to the description page
150 $outputPage->addReturnTo( $this->oldImage ?
$title : Title
::newMainPage() );
152 $this->watchlistManager
->setWatch(
153 $request->getCheck( 'wpWatch' ),
154 $context->getAuthority(),
160 protected function showFormWarnings(): void
{
161 $this->getOutput()->addHTML( $this->prepareMessage( 'filedelete-intro' ) );
162 $this->showSubpagesWarnings();
166 * Show the confirmation form
168 private function showConfirm() {
169 $this->prepareOutputForForm();
170 $context = $this->getContext();
171 $article = $this->getArticle();
173 // oldid is set to the revision id of the page when the page was displayed.
174 // Check to make sure the page has not been edited between loading the page
175 // and clicking the delete link
176 $oldid = $context->getRequest()->getIntOrNull( 'oldid' );
177 if ( $oldid !== null && $oldid !== $article->getRevIdFetched() ) {
178 $this->showEditedWarning();
181 $this->showFormWarnings();
182 $form = $this->getForm();
183 if ( $form->show() ) {
186 $this->showEditReasonsLinks();
187 $this->showLogEntries();
191 * Prepare a message referring to the file being deleted,
192 * showing an appropriate message depending upon whether
193 * it's a current file or an old version
195 * @param string $message Message base
198 private function prepareMessage( string $message ) {
199 if ( $this->oldFile
) {
200 $lang = $this->getContext()->getLanguage();
202 # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
203 return $this->getContext()->msg(
205 wfEscapeWikiText( $this->getTitle()->getText() ),
206 $lang->date( $this->oldFile
->getTimestamp(), true ),
207 $lang->time( $this->oldFile
->getTimestamp(), true ),
208 (string)MediaWikiServices
::getInstance()->getUrlUtils()->expand(
209 $this->file
->getArchiveUrl( $this->oldImage
),
214 return $this->getContext()->msg(
216 wfEscapeWikiText( $this->getTitle()->getText() )
221 protected function getFormAction(): string {
223 $q['action'] = 'delete';
225 if ( $this->oldImage
) {
226 $q['oldimage'] = $this->oldImage
;
229 return $this->getTitle()->getLocalURL( $q );
232 protected function checkCanExecute( User
$user ) {
233 parent
::checkCanExecute( $user );
235 if ( $this->getContext()->getConfig()->get( MainConfigNames
::UploadMaintenance
) ) {
236 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
241 * TODO Do we need all these messages to be different?
244 protected function getFormMessages(): array {
246 self
::MSG_REASON_DROPDOWN
=> 'filedelete-reason-dropdown',
247 self
::MSG_REASON_DROPDOWN_SUPPRESS
=> 'filedelete-reason-dropdown-suppress',
248 self
::MSG_REASON_DROPDOWN_OTHER
=> 'filedelete-reason-otherlist',
249 self
::MSG_COMMENT
=> 'filedelete-comment',
250 self
::MSG_REASON_OTHER
=> 'filedelete-otherreason',
251 self
::MSG_SUBMIT
=> 'filedelete-submit',
252 self
::MSG_LEGEND
=> 'filedelete-legend',
253 self
::MSG_EDIT_REASONS
=> 'filedelete-edit-reasonlist',
254 self
::MSG_EDIT_REASONS_SUPPRESS
=> 'filedelete-edit-reasonlist-suppress',