MediaWiki master
FileDeleteForm.php
Go to the documentation of this file.
1<?php
25namespace MediaWiki\Page\File;
26
27use LocalFile;
35
56 public static function doDelete(
57 Title $title,
58 LocalFile $file,
59 ?string $oldimage,
60 $reason,
61 $suppress,
62 UserIdentity $user,
63 $tags = [],
64 bool $deleteTalk = false
65 ): Status {
66 $services = MediaWikiServices::getInstance();
67 if ( $oldimage ) {
68 $page = null;
69 $status = $file->deleteOldFile( $oldimage, $reason, $user, $suppress );
70 if ( $status->isOK() ) {
71 // Need to do a log item
72 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
73 if ( trim( $reason ) !== '' ) {
74 $logComment .= wfMessage( 'colon-separator' )
75 ->inContentLanguage()->text() . $reason;
76 }
77
78 $logtype = $suppress ? 'suppress' : 'delete';
79
80 $logEntry = new ManualLogEntry( $logtype, 'delete' );
81 $logEntry->setPerformer( $user );
82 $logEntry->setTarget( $title );
83 $logEntry->setComment( $logComment );
84 $logEntry->addTags( $tags );
85 $logid = $logEntry->insert();
86 $logEntry->publish( $logid );
87
88 $status->value = $logid;
89 }
90 } else {
91 $status = Status::newFatal( 'cannotdelete',
93 );
94 $page = $services->getWikiPageFactory()->newFromTitle( $title );
95 '@phan-var \WikiFilePage $page';
96 $deleter = $services->getUserFactory()->newFromUserIdentity( $user );
97 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $deleter );
98 if ( $deleteTalk ) {
99 $checkStatus = $deletePage->canProbablyDeleteAssociatedTalk();
100 if ( !$checkStatus->isGood() ) {
101 return Status::wrap( $checkStatus );
102 }
103 $deletePage->setDeleteAssociatedTalk( true );
104 }
105 $dbw = $services->getConnectionProvider()->getPrimaryDatabase();
106 $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
107 // delete the associated article first
108 $deleteStatus = $deletePage
109 ->setSuppress( $suppress )
110 ->setTags( $tags ?: [] )
111 ->deleteIfAllowed( $reason );
112
113 // DeletePage returns a non-fatal error status if the page
114 // or revision is missing, so check for isOK() rather than isGood().
115 if ( $deleteStatus->isOK() ) {
116 $status = $file->deleteFile( $reason, $user, $suppress );
117 if ( $status->isOK() ) {
118 if ( $deletePage->deletionsWereScheduled()[DeletePage::PAGE_BASE] ) {
119 $status->value = false;
120 } else {
121 $deletedID = $deletePage->getSuccessfulDeletionsIDs()[DeletePage::PAGE_BASE];
122 if ( $deletedID !== null ) {
123 $status->value = $deletedID;
124 } else {
125 // Means that the page/revision didn't exist, so create a log entry here.
126 $logtype = $suppress ? 'suppress' : 'delete';
127 $logEntry = new ManualLogEntry( $logtype, 'delete' );
128 $logEntry->setPerformer( $user );
129 $logEntry->setTarget( $title );
130 $logEntry->setComment( $reason );
131 $logEntry->addTags( $tags );
132 $logid = $logEntry->insert();
133 $dbw->onTransactionPreCommitOrIdle(
134 static function () use ( $logEntry, $logid ) {
135 $logEntry->publish( $logid );
136 },
137 __METHOD__
138 );
139 $status->value = $logid;
140 }
141 }
142 $dbw->endAtomic( __METHOD__ );
143 } else {
144 // Page deleted but file still there? rollback page delete
145 $dbw->cancelAtomic( __METHOD__ );
146 }
147 } else {
148 $dbw->endAtomic( __METHOD__ );
149 }
150 }
151
152 if ( $status->isOK() ) {
153 $legacyUser = $services->getUserFactory()
154 ->newFromUserIdentity( $user );
155 ( new HookRunner( $services->getHookContainer() ) )
156 ->onFileDeleteComplete( $file, $oldimage, $page, $legacyUser, $reason );
157 }
158
159 return $status;
160 }
161
168 public static function isValidOldSpec( $oldimage ) {
169 return strlen( $oldimage ) >= 16
170 && strpos( $oldimage, '/' ) === false
171 && strpos( $oldimage, '\\' ) === false;
172 }
173}
174
176class_alias( FileDeleteForm::class, 'FileDeleteForm' );
wfEscapeWikiText( $input)
Escapes the given text so that it may be output using addWikiText() without any linking,...
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Local file in the wiki's own database.
Definition LocalFile.php:68
deleteOldFile( $archiveName, $reason, UserIdentity $user, $suppress=false)
Delete an old version of the file.
deleteFile( $reason, UserIdentity $user, $suppress=false)
Delete all versions of the file.
Class for creating new log entries and inserting them into the database.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Service locator for MediaWiki core services.
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?
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition Status.php:54
Represents a title within MediaWiki.
Definition Title.php:78
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1861
Interface for objects representing user identity.