MediaWiki  master
FileDeleteForm.php
Go to the documentation of this file.
1 <?php
25 namespace MediaWiki\Page\File;
26 
27 use LocalFile;
28 use ManualLogEntry;
34 use MWException;
35 use Status;
36 
58  public static function doDelete(
59  Title $title,
61  ?string $oldimage,
62  $reason,
63  $suppress,
64  UserIdentity $user,
65  $tags = [],
66  bool $deleteTalk = false
67  ): Status {
68  $services = MediaWikiServices::getInstance();
69  if ( $oldimage ) {
70  $page = null;
71  $status = $file->deleteOldFile( $oldimage, $reason, $user, $suppress );
72  if ( $status->isOK() ) {
73  // Need to do a log item
74  $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
75  if ( trim( $reason ) !== '' ) {
76  $logComment .= wfMessage( 'colon-separator' )
77  ->inContentLanguage()->text() . $reason;
78  }
79 
80  $logtype = $suppress ? 'suppress' : 'delete';
81 
82  $logEntry = new ManualLogEntry( $logtype, 'delete' );
83  $logEntry->setPerformer( $user );
84  $logEntry->setTarget( $title );
85  $logEntry->setComment( $logComment );
86  $logEntry->addTags( $tags );
87  $logid = $logEntry->insert();
88  $logEntry->publish( $logid );
89 
90  $status->value = $logid;
91  }
92  } else {
93  $status = Status::newFatal( 'cannotdelete',
94  wfEscapeWikiText( $title->getPrefixedText() )
95  );
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 = $services->getUserFactory()
156  ->newFromUserIdentity( $user );
157  ( new HookRunner( $services->getHookContainer() ) )
158  ->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 
177 class_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,...
Title string false $title
Definition: File.php:118
Local file in the wiki's own database.
Definition: LocalFile.php:61
MediaWiki exception.
Definition: MWException.php:32
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...
Definition: HookRunner.php:565
Service locator for MediaWiki core services.
Backend logic for performing a page delete action.
Definition: DeletePage.php:51
const PAGE_BASE
Constants used for the return value of getSuccessfulDeletionsIDs() and deletionsWereScheduled()
Definition: DeletePage.php:63
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
static newFatal( $message,... $parameters)
Factory function for fatal errors.
Definition: StatusValue.php:73
Generic operation result class Has warning/error list, boolean status and arbitrary value.
Definition: Status.php:46
static wrap( $sv)
Succinct helper method to wrap a StatusValue.
Definition: Status.php:64
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