MediaWiki  master
FileDeleteForm.php
Go to the documentation of this file.
1 <?php
28 
50  public static function doDelete( Title $title, LocalFile $file, ?string $oldimage, $reason,
51  $suppress, UserIdentity $user, $tags = [], bool $deleteTalk = false
52  ): Status {
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',
78  wfEscapeWikiText( $title->getPrefixedText() )
79  );
80  $services = MediaWikiServices::getInstance();
81  $page = $services->getWikiPageFactory()->newFromTitle( $title );
82  '@phan-var WikiFilePage $page';
83  $deleter = $services->getUserFactory()->newFromUserIdentity( $user );
84  $deletePage = $services->getDeletePageFactory()->newDeletePage( $page, $deleter );
85  if ( $deleteTalk ) {
86  $checkStatus = $deletePage->canProbablyDeleteAssociatedTalk();
87  if ( !$checkStatus->isGood() ) {
88  return Status::wrap( $checkStatus );
89  }
90  $deletePage->setDeleteAssociatedTalk( true );
91  }
92  $dbw = wfGetDB( DB_PRIMARY );
93  $dbw->startAtomic( __METHOD__ );
94  // delete the associated article first
95  $deleteStatus = $deletePage
96  ->setSuppress( $suppress )
97  ->setTags( $tags ?: [] )
98  ->deleteUnsafe( $reason );
99 
100  // DeletePage returns a non-fatal error status if the page
101  // or revision is missing, so check for isOK() rather than isGood().
102  if ( $deleteStatus->isOK() ) {
103  $status = $file->deleteFile( $reason, $user, $suppress );
104  if ( $status->isOK() ) {
105  if ( $deletePage->deletionsWereScheduled()[DeletePage::PAGE_BASE] ) {
106  $status->value = false;
107  } else {
108  $deletedID = $deletePage->getSuccessfulDeletionsIDs()[DeletePage::PAGE_BASE];
109  if ( $deletedID !== null ) {
110  $status->value = $deletedID;
111  } else {
112  // Means that the page/revision didn't exist, so create a log entry here.
113  $logtype = $suppress ? 'suppress' : 'delete';
114  $logEntry = new ManualLogEntry( $logtype, 'delete' );
115  $logEntry->setPerformer( $user );
116  $logEntry->setTarget( $title );
117  $logEntry->setComment( $reason );
118  $logEntry->addTags( $tags );
119  $logid = $logEntry->insert();
120  $dbw->onTransactionPreCommitOrIdle(
121  static function () use ( $logEntry, $logid ) {
122  $logEntry->publish( $logid );
123  },
124  __METHOD__
125  );
126  $status->value = $logid;
127  }
128  }
129  $dbw->endAtomic( __METHOD__ );
130  } else {
131  // Page deleted but file still there? rollback page delete
132  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
133  $lbFactory->rollbackPrimaryChanges( __METHOD__ );
134  }
135  } else {
136  $dbw->endAtomic( __METHOD__ );
137  }
138  }
139 
140  if ( $status->isOK() ) {
141  $legacyUser = MediaWikiServices::getInstance()
142  ->getUserFactory()
143  ->newFromUserIdentity( $user );
144  Hooks::runner()->onFileDeleteComplete( $file, $oldimage, $page, $legacyUser, $reason );
145  }
146 
147  return $status;
148  }
149 
156  public static function isValidOldSpec( $oldimage ) {
157  return strlen( $oldimage ) >= 16
158  && strpos( $oldimage, '/' ) === false
159  && strpos( $oldimage, '\\' ) === false;
160  }
161 }
StatusValue\newFatal
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:70
MediaWiki\MediaWikiServices
MediaWikiServices is the service locator for the application scope of MediaWiki.
Definition: MediaWikiServices.php:203
$file
if(PHP_SAPI !='cli-server') if(!isset( $_SERVER['SCRIPT_FILENAME'])) $file
Item class for a filearchive table row.
Definition: router.php:42
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1167
MediaWiki\User\UserIdentity
Interface for objects representing user identity.
Definition: UserIdentity.php:39
Status
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:44
Status\wrap
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:62
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:2186
FileDeleteForm
File deletion user interface.
Definition: FileDeleteForm.php:34
$title
$title
Definition: testCompression.php:38
LocalFile
Class to represent a local file in the wiki's own database.
Definition: LocalFile.php:63
DB_PRIMARY
const DB_PRIMARY
Definition: defines.php:27
Hooks\runner
static runner()
Get a HookRunner instance for calling hooks using the new interfaces.
Definition: Hooks.php:173
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1440
Title
Represents a title within MediaWiki.
Definition: Title.php:47
ManualLogEntry
Class for creating new log entries and inserting them into the database.
Definition: ManualLogEntry.php:45
Page\DeletePage
Definition: DeletePage.php:49
FileDeleteForm\doDelete
static doDelete(Title $title, LocalFile $file, ?string $oldimage, $reason, $suppress, UserIdentity $user, $tags=[], bool $deleteTalk=false)
Really delete the file.
Definition: FileDeleteForm.php:50
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:156