MediaWiki  1.29.1
FileDeleteForm.php
Go to the documentation of this file.
1 <?php
25 
32 
36  private $title = null;
37 
41  private $file = null;
42 
46  private $oldfile = null;
47  private $oldimage = '';
48 
54  public function __construct( $file ) {
55  $this->title = $file->getTitle();
56  $this->file = $file;
57  }
58 
63  public function execute() {
64  global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
65 
66  $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
67  if ( count( $permissionErrors ) ) {
68  throw new PermissionsError( 'delete', $permissionErrors );
69  }
70 
71  if ( wfReadOnly() ) {
72  throw new ReadOnlyError;
73  }
74 
75  if ( $wgUploadMaintenance ) {
76  throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
77  }
78 
79  $this->setHeaders();
80 
81  $this->oldimage = $wgRequest->getText( 'oldimage', false );
82  $token = $wgRequest->getText( 'wpEditToken' );
83  # Flag to hide all contents of the archived revisions
84  $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
85 
86  if ( $this->oldimage ) {
87  $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
88  $this->title,
89  $this->oldimage
90  );
91  }
92 
93  if ( !self::haveDeletableFile( $this->file, $this->oldfile, $this->oldimage ) ) {
94  $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
95  $wgOut->addReturnTo( $this->title );
96  return;
97  }
98 
99  // Perform the deletion if appropriate
100  if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
101  $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
102  $deleteReason = $wgRequest->getText( 'wpReason' );
103 
104  if ( $deleteReasonList == 'other' ) {
105  $reason = $deleteReason;
106  } elseif ( $deleteReason != '' ) {
107  // Entry from drop down menu + additional comment
108  $reason = $deleteReasonList . wfMessage( 'colon-separator' )
109  ->inContentLanguage()->text() . $deleteReason;
110  } else {
111  $reason = $deleteReasonList;
112  }
113 
115  $this->title,
116  $this->file,
117  $this->oldimage,
118  $reason,
119  $suppress,
120  $wgUser
121  );
122 
123  if ( !$status->isGood() ) {
124  $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
125  $wgOut->addWikiText( '<div class="error">' .
126  $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' )
127  . '</div>' );
128  }
129  if ( $status->isOK() ) {
130  $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
131  $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
132  // Return to the main page if we just deleted all versions of the
133  // file, otherwise go back to the description page
134  $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
135 
136  WatchAction::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title, $wgUser );
137  }
138  return;
139  }
140 
141  $this->showForm();
142  $this->showLogEntries();
143  }
144 
158  public static function doDelete( &$title, &$file, &$oldimage, $reason,
159  $suppress, User $user = null, $tags = []
160  ) {
161  if ( $user === null ) {
162  global $wgUser;
163  $user = $wgUser;
164  }
165 
166  if ( $oldimage ) {
167  $page = null;
168  $status = $file->deleteOld( $oldimage, $reason, $suppress, $user );
169  if ( $status->ok ) {
170  // Need to do a log item
171  $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
172  if ( trim( $reason ) != '' ) {
173  $logComment .= wfMessage( 'colon-separator' )
174  ->inContentLanguage()->text() . $reason;
175  }
176 
177  $logtype = $suppress ? 'suppress' : 'delete';
178 
179  $logEntry = new ManualLogEntry( $logtype, 'delete' );
180  $logEntry->setPerformer( $user );
181  $logEntry->setTarget( $title );
182  $logEntry->setComment( $logComment );
183  $logEntry->setTags( $tags );
184  $logid = $logEntry->insert();
185  $logEntry->publish( $logid );
186 
187  $status->value = $logid;
188  }
189  } else {
190  $status = Status::newFatal( 'cannotdelete',
192  );
194  $dbw = wfGetDB( DB_MASTER );
195  $dbw->startAtomic( __METHOD__ );
196  // delete the associated article first
197  $error = '';
198  $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error,
199  $user, $tags );
200  // doDeleteArticleReal() returns a non-fatal error status if the page
201  // or revision is missing, so check for isOK() rather than isGood()
202  if ( $deleteStatus->isOK() ) {
203  $status = $file->delete( $reason, $suppress, $user );
204  if ( $status->isOK() ) {
205  $status->value = $deleteStatus->value; // log id
206  $dbw->endAtomic( __METHOD__ );
207  } else {
208  // Page deleted but file still there? rollback page delete
209  $lbFactory = MediaWikiServices::getInstance()->getDBLoadBalancerFactory();
210  $lbFactory->rollbackMasterChanges( __METHOD__ );
211  }
212  } else {
213  // Done; nothing changed
214  $dbw->endAtomic( __METHOD__ );
215  }
216  }
217 
218  if ( $status->isOK() ) {
219  Hooks::run( 'FileDeleteComplete', [ &$file, &$oldimage, &$page, &$user, &$reason ] );
220  }
221 
222  return $status;
223  }
224 
228  private function showForm() {
230 
231  if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
232  $suppress = "<tr id=\"wpDeleteSuppressRow\">
233  <td></td>
234  <td class='mw-input'><strong>" .
235  Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
236  'wpSuppress', 'wpSuppress', false, [ 'tabindex' => '3' ] ) .
237  "</strong></td>
238  </tr>";
239  } else {
240  $suppress = '';
241  }
242 
243  $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
244  $form = Xml::openElement( 'form', [ 'method' => 'post', 'action' => $this->getAction(),
245  'id' => 'mw-img-deleteconfirm' ] ) .
246  Xml::openElement( 'fieldset' ) .
247  Xml::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
248  Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
249  $this->prepareMessage( 'filedelete-intro' ) .
250  Xml::openElement( 'table', [ 'id' => 'mw-img-deleteconfirm-table' ] ) .
251  "<tr>
252  <td class='mw-label'>" .
253  Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
254  "</td>
255  <td class='mw-input'>" .
257  'wpDeleteReasonList',
258  wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
259  wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
260  '',
261  'wpReasonDropDown',
262  1
263  ) .
264  "</td>
265  </tr>
266  <tr>
267  <td class='mw-label'>" .
268  Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
269  "</td>
270  <td class='mw-input'>" .
271  Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
272  [ 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ] ) .
273  "</td>
274  </tr>
275  {$suppress}";
276  if ( $wgUser->isLoggedIn() ) {
277  $form .= "
278  <tr>
279  <td></td>
280  <td class='mw-input'>" .
281  Xml::checkLabel( wfMessage( 'watchthis' )->text(),
282  'wpWatch', 'wpWatch', $checkWatch, [ 'tabindex' => '3' ] ) .
283  "</td>
284  </tr>";
285  }
286  $form .= "
287  <tr>
288  <td></td>
289  <td class='mw-submit'>" .
291  wfMessage( 'filedelete-submit' )->text(),
292  [
293  'name' => 'mw-filedelete-submit',
294  'id' => 'mw-filedelete-submit',
295  'tabindex' => '4'
296  ]
297  ) .
298  "</td>
299  </tr>" .
300  Xml::closeElement( 'table' ) .
301  Xml::closeElement( 'fieldset' ) .
302  Xml::closeElement( 'form' );
303 
304  if ( $wgUser->isAllowed( 'editinterface' ) ) {
305  $title = wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->getTitle();
306  $linkRenderer = MediaWikiServices::getInstance()->getLinkRenderer();
307  $link = $linkRenderer->makeKnownLink(
308  $title,
309  wfMessage( 'filedelete-edit-reasonlist' )->text(),
310  [],
311  [ 'action' => 'edit' ]
312  );
313  $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
314  }
315 
316  $wgOut->addHTML( $form );
317  }
318 
322  private function showLogEntries() {
323  global $wgOut;
324  $deleteLogPage = new LogPage( 'delete' );
325  $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
326  LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
327  }
328 
337  private function prepareMessage( $message ) {
338  global $wgLang;
339  if ( $this->oldimage ) {
340  # Message keys used:
341  # 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
342  return wfMessage(
343  "{$message}-old",
344  wfEscapeWikiText( $this->title->getText() ),
345  $wgLang->date( $this->getTimestamp(), true ),
346  $wgLang->time( $this->getTimestamp(), true ),
347  wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) )->parseAsBlock();
348  } else {
349  return wfMessage(
350  $message,
351  wfEscapeWikiText( $this->title->getText() )
352  )->parseAsBlock();
353  }
354  }
355 
359  private function setHeaders() {
360  global $wgOut;
361  $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
362  $wgOut->setRobotPolicy( 'noindex,nofollow' );
363  $wgOut->addBacklinkSubtitle( $this->title );
364  }
365 
372  public static function isValidOldSpec( $oldimage ) {
373  return strlen( $oldimage ) >= 16
374  && strpos( $oldimage, '/' ) === false
375  && strpos( $oldimage, '\\' ) === false;
376  }
377 
388  public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
389  return $oldimage
390  ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
391  : $file && $file->exists() && $file->isLocal();
392  }
393 
399  private function getAction() {
400  $q = [];
401  $q['action'] = 'delete';
402 
403  if ( $this->oldimage ) {
404  $q['oldimage'] = $this->oldimage;
405  }
406 
407  return $this->title->getLocalURL( $q );
408  }
409 
415  private function getTimestamp() {
416  return $this->oldfile->getTimestamp();
417  }
418 }
ReadOnlyError
Show an error when the wiki is locked/read-only and the user tries to do something that requires writ...
Definition: ReadOnlyError.php:28
$wgUser
$wgUser
Definition: Setup.php:781
file
We ve cleaned up the code here by removing clumps of infrequently used code and moving them off somewhere else It s much easier for someone working with this code to see what s _really_ going and make changes or fix bugs In we can take all the code that deals with the little used title reversing we can concentrate it all in an extension file
Definition: hooks.txt:93
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:59
Xml\label
static label( $label, $id, $attribs=[])
Convenience function to build an HTML form label.
Definition: Xml.php:358
captcha-old.count
count
Definition: captcha-old.py:225
text
design txt This is a brief overview of the new design More thorough and up to date information is available on the documentation wiki at etc Handles the details of getting and saving to the user table of the and dealing with sessions and cookies OutputPage Encapsulates the entire HTML page that will be sent in response to any server request It is used by calling its functions to add text
Definition: design.txt:12
Title\newMainPage
static newMainPage()
Create a new Title for the Main Page.
Definition: Title.php:559
$status
this hook is for auditing only RecentChangesLinked and Watchlist RecentChangesLinked and Watchlist Do not use this to implement individual filters if they are compatible with the ChangesListFilter and ChangesListFilterGroup structure use sub classes of those in conjunction with the ChangesListSpecialPageStructuredFilters hook This hook can be used to implement filters that do not implement that or custom behavior that is not an individual filter e g Watchlist and Watchlist you will want to construct new ChangesListBooleanFilter or ChangesListStringOptionsFilter objects When constructing you specify which group they belong to You can reuse existing or create your you must register them with $special registerFilterGroup removed from all revisions and log entries to which it was applied This gives extensions a chance to take it off their books as the deletion has already been partly carried out by this point or something similar the user will be unable to create the tag set $status
Definition: hooks.txt:1049
use
as see the revision history and available at free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to use
Definition: MIT-LICENSE.txt:10
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1451
$user
please add to it if you re going to add events to the MediaWiki code where normally authentication against an external auth plugin would be creating a account $user
Definition: hooks.txt:246
StatusValue\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: StatusValue.php:63
FileDeleteForm\__construct
__construct( $file)
Constructor.
Definition: FileDeleteForm.php:54
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1277
$linkRenderer
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return an< a > element with HTML attributes $attribs and contents $html will be returned If you return $ret will be returned and may include noclasses after processing after in associative array form before processing starts Return false to skip default processing and return $ret $linkRenderer
Definition: hooks.txt:1956
File\delete
delete( $reason, $suppress=false, $user=null)
Delete all versions of the file.
Definition: File.php:1930
PermissionsError
Show an error when a user tries to do something they do not have the necessary permissions for.
Definition: PermissionsError.php:28
FileDeleteForm\getTimestamp
getTimestamp()
Extract the timestamp of the old version.
Definition: FileDeleteForm.php:415
$lbFactory
$lbFactory
Definition: doMaintenance.php:117
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
php
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition: injection.txt:35
File\exists
exists()
Returns true if file exists in the repository.
Definition: File.php:877
FileDeleteForm\getAction
getAction()
Prepare the form action.
Definition: FileDeleteForm.php:399
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:51
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:120
wfGetDB
wfGetDB( $db, $groups=[], $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3060
$page
do that in ParserLimitReportFormat instead use this to modify the parameters of the image and a DIV can begin in one section and end in another Make sure your code can handle that case gracefully See the EditSectionClearerLink extension for an example zero but section is usually empty its values are the globals values before the output is cached $page
Definition: hooks.txt:2536
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:31
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:220
Xml\element
static element( $element, $attribs=null, $contents='', $allowShortTag=true)
Format an XML element with given attributes and, optionally, text content.
Definition: Xml.php:39
FileDeleteForm
File deletion user interface.
Definition: FileDeleteForm.php:31
WatchAction\doWatchOrUnwatch
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:93
$wgLang
this class mediates it Skin Encapsulates a look and feel for the wiki All of the functions that render HTML and make choices about how to render it are here and are called from various other places when and is meant to be subclassed with other skins that may override some of its functions The User object contains a reference to a and so rather than having a global skin object we just rely on the global User and get the skin with $wgUser and also has some character encoding functions and other locale stuff The current user interface language is instantiated as $wgLang
Definition: design.txt:56
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=[], $page='', $user='', $param=[])
Show log extract.
Definition: LogEventsList.php:564
DB_MASTER
const DB_MASTER
Definition: defines.php:26
FileDeleteForm\$oldfile
File $oldfile
Definition: FileDeleteForm.php:46
Html\hidden
static hidden( $name, $value, array $attribs=[])
Convenience function to produce an input element with type=hidden.
Definition: Html.php:746
title
title
Definition: parserTests.txt:211
FileDeleteForm\prepareMessage
prepareMessage( $message)
Prepare a message referring to the file being deleted, showing an appropriate message depending upon ...
Definition: FileDeleteForm.php:337
FileDeleteForm\showForm
showForm()
Show the confirmation form.
Definition: FileDeleteForm.php:228
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:1657
FileDeleteForm\$oldimage
$oldimage
Definition: FileDeleteForm.php:47
File\getTitle
getTitle()
Return the associated title object.
Definition: File.php:326
Title
Represents a title within MediaWiki.
Definition: Title.php:39
FileDeleteForm\execute
execute()
Fulfil the request; shows the form or deletes the file, pending authentication, confirmation,...
Definition: FileDeleteForm.php:63
FileDeleteForm\$file
File $file
Definition: FileDeleteForm.php:41
Xml\closeElement
static closeElement( $element)
Shortcut to close an XML element.
Definition: Xml.php:118
Xml\listDropDown
static listDropDown( $name='', $list='', $other='', $selected='', $class='', $tabindex=null)
Build a drop-down box from a textual list.
Definition: Xml.php:507
FileDeleteForm\showLogEntries
showLogEntries()
Show deletion log fragments pertaining to the current file.
Definition: FileDeleteForm.php:322
File\isLocal
isLocal()
Returns true if the file comes from the local file repository.
Definition: File.php:1836
FileDeleteForm\setHeaders
setHeaders()
Set headers, titles and other bits.
Definition: FileDeleteForm.php:359
$link
usually copyright or history_copyright This message must be in HTML not wikitext & $link
Definition: hooks.txt:2929
true
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition: hooks.txt:1956
FileDeleteForm\$title
Title $title
Definition: FileDeleteForm.php:36
ManualLogEntry
Class for creating log entries manually, to inject them into the database.
Definition: LogEntry.php:396
wfMessage
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null, $tags=[])
Really delete the file.
Definition: FileDeleteForm.php:158
Xml\input
static input( $name, $size=false, $value=false, $attribs=[])
Convenience function to build an HTML text input field.
Definition: Xml.php:274
$wgRequest
if(! $wgDBerrorLogTZ) $wgRequest
Definition: Setup.php:639
MediaWikiServices
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency MediaWikiServices
Definition: injection.txt:23
$wgOut
$wgOut
Definition: Setup.php:791
ErrorPageError
An error page which can definitely be safely rendered using the OutputPage.
Definition: ErrorPageError.php:27
User
The User object encapsulates all of the user-specific settings (user_id, name, rights,...
Definition: User.php:50
Hooks\run
static run( $event, array $args=[], $deprecatedVersion=null)
Call hook functions defined in Hooks::register and $wgHooks.
Definition: Hooks.php:131
FileDeleteForm\haveDeletableFile
static haveDeletableFile(&$file, &$oldfile, $oldimage)
Could we delete the file specified? If an oldimage value was provided, does it correspond to an exist...
Definition: FileDeleteForm.php:388
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:552
Xml\submitButton
static submitButton( $value, $attribs=[])
Convenience function to build an HTML submit button When $wgUseMediaWikiUIEverywhere is true it will ...
Definition: Xml.php:459
Xml\checkLabel
static checkLabel( $label, $name, $id, $checked=false, $attribs=[])
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:419
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:372