MediaWiki 1.40.4
FileDeleteForm.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Page\File;
26
27use Hooks;
28use LocalFile;
34use MWException;
35use Status;
36
58 public static function doDelete(
61 ?string $oldimage,
62 $reason,
63 $suppress,
64 UserIdentity $user,
65 $tags = [],
66 bool $deleteTalk = false
67 ): Status {
68 if ( $oldimage ) {
69 $page = null;
70 $status = $file->deleteOldFile( $oldimage, $reason, $user, $suppress );
71 if ( $status->isOK() ) {
72 // Need to do a log item
73 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
74 if ( trim( $reason ) !== '' ) {
75 $logComment .= wfMessage( 'colon-separator' )
76 ->inContentLanguage()->text() . $reason;
77 }
78
79 $logtype = $suppress ? 'suppress' : 'delete';
80
81 $logEntry = new ManualLogEntry( $logtype, 'delete' );
82 $logEntry->setPerformer( $user );
83 $logEntry->setTarget( $title );
84 $logEntry->setComment( $logComment );
85 $logEntry->addTags( $tags );
86 $logid = $logEntry->insert();
87 $logEntry->publish( $logid );
88
89 $status->value = $logid;
90 }
91 } else {
92 $status = Status::newFatal( 'cannotdelete',
93 wfEscapeWikiText( $title->getPrefixedText() )
94 );
96 $page = $services->getWikiPageFactory()->newFromTitle( $title );
97 '@phan-var \WikiFilePage $page';
98 $deleter = $services->getUserFactory()->newFromUserIdentity( $user );
99 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $deleter );
100 if ( $deleteTalk ) {
101 $checkStatus = $deletePage->canProbablyDeleteAssociatedTalk();
102 if ( !$checkStatus->isGood() ) {
103 return Status::wrap( $checkStatus );
104 }
105 $deletePage->setDeleteAssociatedTalk( true );
106 }
107 $dbw = wfGetDB( DB_PRIMARY );
108 $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
109 // delete the associated article first
110 $deleteStatus = $deletePage
111 ->setSuppress( $suppress )
112 ->setTags( $tags ?: [] )
113 ->deleteIfAllowed( $reason );
114
115 // DeletePage returns a non-fatal error status if the page
116 // or revision is missing, so check for isOK() rather than isGood().
117 if ( $deleteStatus->isOK() ) {
118 $status = $file->deleteFile( $reason, $user, $suppress );
119 if ( $status->isOK() ) {
120 if ( $deletePage->deletionsWereScheduled()[DeletePage::PAGE_BASE] ) {
121 $status->value = false;
122 } else {
123 $deletedID = $deletePage->getSuccessfulDeletionsIDs()[DeletePage::PAGE_BASE];
124 if ( $deletedID !== null ) {
125 $status->value = $deletedID;
126 } else {
127 // Means that the page/revision didn't exist, so create a log entry here.
128 $logtype = $suppress ? 'suppress' : 'delete';
129 $logEntry = new ManualLogEntry( $logtype, 'delete' );
130 $logEntry->setPerformer( $user );
131 $logEntry->setTarget( $title );
132 $logEntry->setComment( $reason );
133 $logEntry->addTags( $tags );
134 $logid = $logEntry->insert();
135 $dbw->onTransactionPreCommitOrIdle(
136 static function () use ( $logEntry, $logid ) {
137 $logEntry->publish( $logid );
138 },
139 __METHOD__
140 );
141 $status->value = $logid;
142 }
143 }
144 $dbw->endAtomic( __METHOD__ );
145 } else {
146 // Page deleted but file still there? rollback page delete
147 $dbw->cancelAtomic( __METHOD__ );
148 }
149 } else {
150 $dbw->endAtomic( __METHOD__ );
151 }
152 }
153
154 if ( $status->isOK() ) {
155 $legacyUser = MediaWikiServices::getInstance()
156 ->getUserFactory()
157 ->newFromUserIdentity( $user );
158 Hooks::runner()->onFileDeleteComplete( $file, $oldimage, $page, $legacyUser, $reason );
159 }
160
161 return $status;
162 }
163
170 public static function isValidOldSpec( $oldimage ) {
171 return strlen( $oldimage ) >= 16
172 && strpos( $oldimage, '/' ) === false
173 && strpos( $oldimage, '\\' ) === false;
174 }
175}
176
177class_alias( FileDeleteForm::class, 'FileDeleteForm' );
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
if(!defined('MW_SETUP_CALLBACK'))
The persistent session ID (if any) loaded at startup.
Definition WebStart.php:88
Hooks class.
Definition Hooks.php:38
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition Hooks.php:170
Local file in the wiki's own database.
Definition LocalFile.php:61
MediaWiki exception.
Class for creating new log entries and inserting them into the database.
Service locator for MediaWiki core services.
static getInstance()
Returns the global default instance of the top level service locator.
Backend logic for performing a page delete action.
const PAGE_BASE
Constants used for the return value of getSuccessfulDeletionsIDs() and deletionsWereScheduled()
File deletion user interface.
static doDelete(Title $title, LocalFile $file, ?string $oldimage, $reason, $suppress, UserIdentity $user, $tags=[], bool $deleteTalk=false)
Really delete the file.
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Represents a title within MediaWiki.
Definition Title.php:82
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:46
Interface for objects representing user identity.
const DB_PRIMARY
Definition defines.php:28
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition router.php:42