MediaWiki master
FileDeleteForm.php
Go to the documentation of this file.
1<?php
11namespace MediaWiki\Page\File;
12
21
42 public static function doDelete(
43 Title $title,
44 LocalFile $file,
45 ?string $oldimage,
46 $reason,
47 $suppress,
48 UserIdentity $user,
49 $tags = [],
50 bool $deleteTalk = false
51 ): Status {
52 $services = MediaWikiServices::getInstance();
53 if ( $oldimage ) {
54 $page = null;
55 $status = $file->deleteOldFile( $oldimage, $reason, $user, $suppress );
56 if ( $status->isOK() ) {
57 // Need to do a log item
58 $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
59 if ( trim( $reason ) !== '' ) {
60 $logComment .= wfMessage( 'colon-separator' )
61 ->inContentLanguage()->text() . $reason;
62 }
63
64 $logtype = $suppress ? 'suppress' : 'delete';
65
66 $logEntry = new ManualLogEntry( $logtype, 'delete' );
67 $logEntry->setPerformer( $user );
68 $logEntry->setTarget( $title );
69 $logEntry->setComment( $logComment );
70 $logEntry->addTags( $tags );
71 $logid = $logEntry->insert();
72 $logEntry->publish( $logid );
73
74 $status->value = $logid;
75 }
76 } else {
77 $status = Status::newFatal( 'cannotdelete',
79 );
80 $page = $services->getWikiPageFactory()->newFromTitle( $title );
81 '@phan-var \MediaWiki\Page\WikiFilePage $page';
82 $deleter = $services->getUserFactory()->newFromUserIdentity( $user );
83 $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $deleter );
84 if ( $deleteTalk ) {
85 $checkStatus = $deletePage->canProbablyDeleteAssociatedTalk();
86 if ( !$checkStatus->isGood() ) {
87 return Status::wrap( $checkStatus );
88 }
89 $deletePage->setDeleteAssociatedTalk( true );
90 }
91 $dbw = $services->getConnectionProvider()->getPrimaryDatabase();
92 $dbw->startAtomic( __METHOD__, $dbw::ATOMIC_CANCELABLE );
93 // delete the associated article first
94 $deleteStatus = $deletePage
95 ->setSuppress( $suppress )
96 ->setTags( $tags ?: [] )
97 ->deleteIfAllowed( $reason );
98
99 // DeletePage returns a non-fatal error status if the page
100 // or revision is missing, so check for isOK() rather than isGood().
101 if ( $deleteStatus->isOK() ) {
102 $status = $file->deleteFile( $reason, $user, $suppress );
103 if ( $status->isOK() ) {
104 if ( $deletePage->deletionsWereScheduled()[DeletePage::PAGE_BASE] ) {
105 $status->value = false;
106 } else {
107 $deletedID = $deletePage->getSuccessfulDeletionsIDs()[DeletePage::PAGE_BASE];
108 if ( $deletedID !== null ) {
109 $status->value = $deletedID;
110 } else {
111 // Means that the page/revision didn't exist, so create a log entry here.
112 $logtype = $suppress ? 'suppress' : 'delete';
113 $logEntry = new ManualLogEntry( $logtype, 'delete' );
114 $logEntry->setPerformer( $user );
115 $logEntry->setTarget( $title );
116 $logEntry->setComment( $reason );
117 $logEntry->addTags( $tags );
118 $logid = $logEntry->insert();
119 $dbw->onTransactionPreCommitOrIdle(
120 static function () use ( $logEntry, $logid ) {
121 $logEntry->publish( $logid );
122 },
123 __METHOD__
124 );
125 $status->value = $logid;
126 }
127 }
128 $dbw->endAtomic( __METHOD__ );
129 } else {
130 // Page deleted but file still there? rollback page delete
131 $dbw->cancelAtomic( __METHOD__ );
132 }
133 } else {
134 $dbw->endAtomic( __METHOD__ );
135 }
136 }
137
138 if ( $status->isOK() ) {
139 $legacyUser = $services->getUserFactory()
140 ->newFromUserIdentity( $user );
141 ( new HookRunner( $services->getHookContainer() ) )
142 ->onFileDeleteComplete( $file, $oldimage, $page, $legacyUser, $reason );
143 }
144
145 return $status;
146 }
147
154 public static function isValidOldSpec( $oldimage ) {
155 return strlen( $oldimage ) >= 16
156 && !str_contains( $oldimage, '/' )
157 && !str_contains( $oldimage, '\\' );
158 }
159}
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:81
deleteFile( $reason, UserIdentity $user, $suppress=false)
Delete all versions of the file.
deleteOldFile( $archiveName, $reason, UserIdentity $user, $suppress=false)
Delete an old version of the file.
This class provides an implementation of the core hook interfaces, forwarding hook calls to HookConta...
Class for creating new log entries and inserting them into the database.
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:44
Represents a title within MediaWiki.
Definition Title.php:69
getPrefixedText()
Get the prefixed title with spaces.
Definition Title.php:1857
Interface for objects representing user identity.