MediaWiki  1.23.0
FileDeleteForm.php
Go to the documentation of this file.
1 <?php
31 
35  private $title = null;
36 
40  private $file = null;
41 
45  private $oldfile = null;
46  private $oldimage = '';
47 
53  public function __construct( $file ) {
54  $this->title = $file->getTitle();
55  $this->file = $file;
56  }
57 
62  public function execute() {
63  global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
64 
65  $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
66  if ( count( $permissionErrors ) ) {
67  throw new PermissionsError( 'delete', $permissionErrors );
68  }
69 
70  if ( wfReadOnly() ) {
71  throw new ReadOnlyError;
72  }
73 
74  if ( $wgUploadMaintenance ) {
75  throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
76  }
77 
78  $this->setHeaders();
79 
80  $this->oldimage = $wgRequest->getText( 'oldimage', false );
81  $token = $wgRequest->getText( 'wpEditToken' );
82  # Flag to hide all contents of the archived revisions
83  $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed( 'suppressrevision' );
84 
85  if ( $this->oldimage ) {
86  $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
87  }
88 
89  if ( !self::haveDeletableFile( $this->file, $this->oldfile, $this->oldimage ) ) {
90  $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
91  $wgOut->addReturnTo( $this->title );
92  return;
93  }
94 
95  // Perform the deletion if appropriate
96  if ( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
97  $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
98  $deleteReason = $wgRequest->getText( 'wpReason' );
99 
100  if ( $deleteReasonList == 'other' ) {
101  $reason = $deleteReason;
102  } elseif ( $deleteReason != '' ) {
103  // Entry from drop down menu + additional comment
104  $reason = $deleteReasonList . wfMessage( 'colon-separator' )
105  ->inContentLanguage()->text() . $deleteReason;
106  } else {
107  $reason = $deleteReasonList;
108  }
109 
110  $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress, $wgUser );
111 
112  if ( !$status->isGood() ) {
113  $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
114  $wgOut->addWikiText( '<div class="error">' . $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) . '</div>' );
115  }
116  if ( $status->ok ) {
117  $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
118  $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
119  // Return to the main page if we just deleted all versions of the
120  // file, otherwise go back to the description page
121  $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
122 
123  WatchAction::doWatchOrUnwatch( $wgRequest->getCheck( 'wpWatch' ), $this->title, $wgUser );
124  }
125  return;
126  }
127 
128  $this->showForm();
129  $this->showLogEntries();
130  }
131 
144  public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
145  if ( $user === null ) {
146  global $wgUser;
147  $user = $wgUser;
148  }
149 
150  if ( $oldimage ) {
151  $page = null;
152  $status = $file->deleteOld( $oldimage, $reason, $suppress );
153  if ( $status->ok ) {
154  // Need to do a log item
155  $logComment = wfMessage( 'deletedrevision', $oldimage )->inContentLanguage()->text();
156  if ( trim( $reason ) != '' ) {
157  $logComment .= wfMessage( 'colon-separator' )
158  ->inContentLanguage()->text() . $reason;
159  }
160 
161  $logtype = $suppress ? 'suppress' : 'delete';
162 
163  $logEntry = new ManualLogEntry( $logtype, 'delete' );
164  $logEntry->setPerformer( $user );
165  $logEntry->setTarget( $title );
166  $logEntry->setComment( $logComment );
167  $logid = $logEntry->insert();
168  $logEntry->publish( $logid );
169  }
170  } else {
171  $status = Status::newFatal( 'cannotdelete',
173  );
174  $page = WikiPage::factory( $title );
175  $dbw = wfGetDB( DB_MASTER );
176  try {
177  // delete the associated article first
178  $error = '';
179  $deleteStatus = $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user );
180  // doDeleteArticleReal() returns a non-fatal error status if the page
181  // or revision is missing, so check for isOK() rather than isGood()
182  if ( $deleteStatus->isOK() ) {
183  $status = $file->delete( $reason, $suppress );
184  if ( $status->isOK() ) {
185  $dbw->commit( __METHOD__ );
186  } else {
187  $dbw->rollback( __METHOD__ );
188  }
189  }
190  } catch ( MWException $e ) {
191  // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
192  $dbw->rollback( __METHOD__ );
193  throw $e;
194  }
195  }
196 
197  if ( $status->isOK() ) {
198  wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
199  }
200 
201  return $status;
202  }
203 
207  private function showForm() {
208  global $wgOut, $wgUser, $wgRequest;
209 
210  if ( $wgUser->isAllowed( 'suppressrevision' ) ) {
211  $suppress = "<tr id=\"wpDeleteSuppressRow\">
212  <td></td>
213  <td class='mw-input'><strong>" .
214  Xml::checkLabel( wfMessage( 'revdelete-suppress' )->text(),
215  'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
216  "</strong></td>
217  </tr>";
218  } else {
219  $suppress = '';
220  }
221 
222  $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $wgUser->isWatched( $this->title );
223  $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
224  'id' => 'mw-img-deleteconfirm' ) ) .
225  Xml::openElement( 'fieldset' ) .
226  Xml::element( 'legend', null, wfMessage( 'filedelete-legend' )->text() ) .
227  Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
228  $this->prepareMessage( 'filedelete-intro' ) .
229  Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
230  "<tr>
231  <td class='mw-label'>" .
232  Xml::label( wfMessage( 'filedelete-comment' )->text(), 'wpDeleteReasonList' ) .
233  "</td>
234  <td class='mw-input'>" .
236  'wpDeleteReasonList',
237  wfMessage( 'filedelete-reason-dropdown' )->inContentLanguage()->text(),
238  wfMessage( 'filedelete-reason-otherlist' )->inContentLanguage()->text(),
239  '',
240  'wpReasonDropDown',
241  1
242  ) .
243  "</td>
244  </tr>
245  <tr>
246  <td class='mw-label'>" .
247  Xml::label( wfMessage( 'filedelete-otherreason' )->text(), 'wpReason' ) .
248  "</td>
249  <td class='mw-input'>" .
250  Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
251  array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
252  "</td>
253  </tr>
254  {$suppress}";
255  if ( $wgUser->isLoggedIn() ) {
256  $form .= "
257  <tr>
258  <td></td>
259  <td class='mw-input'>" .
260  Xml::checkLabel( wfMessage( 'watchthis' )->text(),
261  'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
262  "</td>
263  </tr>";
264  }
265  $form .= "
266  <tr>
267  <td></td>
268  <td class='mw-submit'>" .
269  Xml::submitButton( wfMessage( 'filedelete-submit' )->text(),
270  array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
271  "</td>
272  </tr>" .
273  Xml::closeElement( 'table' ) .
274  Xml::closeElement( 'fieldset' ) .
275  Xml::closeElement( 'form' );
276 
277  if ( $wgUser->isAllowed( 'editinterface' ) ) {
278  $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
280  $title,
281  wfMessage( 'filedelete-edit-reasonlist' )->escaped(),
282  array(),
283  array( 'action' => 'edit' )
284  );
285  $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
286  }
287 
288  $wgOut->addHTML( $form );
289  }
290 
294  private function showLogEntries() {
295  global $wgOut;
296  $deleteLogPage = new LogPage( 'delete' );
297  $wgOut->addHTML( '<h2>' . $deleteLogPage->getName()->escaped() . "</h2>\n" );
298  LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
299  }
300 
309  private function prepareMessage( $message ) {
310  global $wgLang;
311  if ( $this->oldimage ) {
312  return wfMessage(
313  "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
314  wfEscapeWikiText( $this->title->getText() ),
315  $wgLang->date( $this->getTimestamp(), true ),
316  $wgLang->time( $this->getTimestamp(), true ),
317  wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) )->parseAsBlock();
318  } else {
319  return wfMessage(
320  $message,
321  wfEscapeWikiText( $this->title->getText() )
322  )->parseAsBlock();
323  }
324  }
325 
329  private function setHeaders() {
330  global $wgOut;
331  $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
332  $wgOut->setRobotPolicy( 'noindex,nofollow' );
333  $wgOut->addBacklinkSubtitle( $this->title );
334  }
335 
341  public static function isValidOldSpec( $oldimage ) {
342  return strlen( $oldimage ) >= 16
343  && strpos( $oldimage, '/' ) === false
344  && strpos( $oldimage, '\\' ) === false;
345  }
346 
357  public static function haveDeletableFile( &$file, &$oldfile, $oldimage ) {
358  return $oldimage
359  ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
360  : $file && $file->exists() && $file->isLocal();
361  }
362 
368  private function getAction() {
369  $q = array();
370  $q['action'] = 'delete';
371 
372  if ( $this->oldimage ) {
373  $q['oldimage'] = $this->oldimage;
374  }
375 
376  return $this->title->getLocalURL( $q );
377  }
378 
384  private function getTimestamp() {
385  return $this->oldfile->getTimestamp();
386  }
387 }
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
Xml\checkLabel
static checkLabel( $label, $name, $id, $checked=false, $attribs=array())
Convenience function to build an HTML checkbox with a label.
Definition: Xml.php:433
Title\makeTitle
static & makeTitle( $ns, $title, $fragment='', $interwiki='')
Create a new Title from a namespace index and a DB key.
Definition: Title.php:398
$wgUser
$wgUser
Definition: Setup.php:552
DB_MASTER
const DB_MASTER
Definition: Defines.php:56
RepoGroup\singleton
static singleton()
Get a RepoGroup instance.
Definition: RepoGroup.php:53
php
skin txt MediaWiki includes four core it has been set as the default in MediaWiki since the replacing Monobook it had been been the default skin since before being replaced by Vector largely rewritten in while keeping its appearance Several legacy skins were removed in the as the burden of supporting them became too heavy to bear Those in etc for skin dependent CSS etc for skin dependent JavaScript These can also be customised on a per user by etc This feature has led to a wide variety of user styles becoming that gallery is a good place to ending in php
Definition: skin.txt:62
wfGetDB
& wfGetDB( $db, $groups=array(), $wiki=false)
Get a Database object.
Definition: GlobalFunctions.php:3650
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:441
$form
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead $form
Definition: hooks.txt:2573
Title\getPrefixedText
getPrefixedText()
Get the prefixed title with spaces.
Definition: Title.php:1369
FileDeleteForm\__construct
__construct( $file)
Constructor.
Definition: FileDeleteForm.php:50
wfReadOnly
wfReadOnly()
Check whether the wiki is in read-only mode.
Definition: GlobalFunctions.php:1313
Html\hidden
static hidden( $name, $value, $attribs=array())
Convenience function to produce an input element with type=hidden.
Definition: Html.php:662
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:381
$link
set to $title object and return false for a match for latest after cache objects are set use the ContentHandler facility to handle CSS and JavaScript for highlighting & $link
Definition: hooks.txt:2149
Xml\openElement
static openElement( $element, $attribs=null)
This opens an XML element.
Definition: Xml.php:109
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:1530
File\exists
exists()
Returns true if file exists in the repository.
Definition: File.php:781
Linker\link
static link( $target, $html=null, $customAttribs=array(), $query=array(), $options=array())
This function returns an HTML link to the given target.
Definition: Linker.php:192
title
to move a page</td >< td > &*You are moving the page across *A non empty talk page already exists under the new or *You uncheck the box below In those you will have to move or merge the page manually if desired</td >< td > be sure to &You are responsible for making sure that links continue to point where they are supposed to go Note that the page will &a page at the new title
Definition: All_system_messages.txt:2703
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
FileDeleteForm\getAction
getAction()
Prepare the form action.
Definition: FileDeleteForm.php:365
File
Implements some public methods and some protected utility functions which are required by multiple ch...
Definition: File.php:50
MWException
MediaWiki exception.
Definition: MWException.php:26
WikiPage\factory
static factory(Title $title)
Create a WikiPage object of the appropriate class for the given title.
Definition: WikiPage.php:103
FileDeleteForm\doDelete
static doDelete(&$title, &$file, &$oldimage, $reason, $suppress, User $user=null)
Really delete the file.
Definition: FileDeleteForm.php:141
$wgOut
$wgOut
Definition: Setup.php:562
LogPage
Class to simplify the use of log pages.
Definition: LogPage.php:32
PROTO_CURRENT
const PROTO_CURRENT
Definition: Defines.php:270
wfMessage
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 externallinks including delete and has completed for all link tables 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
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
wfRunHooks
wfRunHooks( $event, array $args=array(), $deprecatedVersion=null)
Call hook functions defined in $wgHooks.
Definition: GlobalFunctions.php:4001
FileDeleteForm
File deletion user interface.
Definition: FileDeleteForm.php:30
WatchAction\doWatchOrUnwatch
static doWatchOrUnwatch( $watch, Title $title, User $user)
Watch or unwatch a page.
Definition: WatchAction.php:106
array
the array() calling protocol came about after MediaWiki 1.4rc1.
List of Api Query prop modules.
global
when a variable name is used in a it is silently declared as a new masking the global
Definition: design.txt:93
FileDeleteForm\$oldfile
File $oldfile
Definition: FileDeleteForm.php:42
will
</td >< td > &</td >< td > t want your writing to be edited mercilessly and redistributed at will
Definition: All_system_messages.txt:914
FileDeleteForm\prepareMessage
prepareMessage( $message)
Prepare a message referring to the file being deleted, showing an appropriate message depending upon ...
Definition: FileDeleteForm.php:306
FileDeleteForm\showForm
showForm()
Show the confirmation form.
Definition: FileDeleteForm.php:204
wfEscapeWikiText
wfEscapeWikiText( $text)
Escapes the given text so that it may be output using addWikiText() without any linking,...
Definition: GlobalFunctions.php:2077
FileDeleteForm\$oldimage
$oldimage
Definition: FileDeleteForm.php:43
File\delete
delete( $reason, $suppress=false)
Delete all versions of the file.
Definition: File.php:1641
$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:237
File\getTitle
getTitle()
Return the associated title object.
Definition: File.php:302
Title
Represents a title within MediaWiki.
Definition: Title.php:35
$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
FileDeleteForm\execute
execute()
Fulfil the request; shows the form or deletes the file, pending authentication, confirmation,...
Definition: FileDeleteForm.php:59
FileDeleteForm\$file
File $file
Definition: FileDeleteForm.php:38
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:497
FileDeleteForm\showLogEntries
showLogEntries()
Show deletion log fragments pertaining to the current file.
Definition: FileDeleteForm.php:291
File\isLocal
isLocal()
Returns true if the file comes from the local file repository.
Definition: File.php:1548
FileDeleteForm\setHeaders
setHeaders()
Set headers, titles and other bits.
Definition: FileDeleteForm.php:326
FileDeleteForm\$title
Title $title
Definition: FileDeleteForm.php:34
ManualLogEntry
Class for creating log entries manually, for example to inject them into the database.
Definition: LogEntry.php:339
Xml\submitButton
static submitButton( $value, $attribs=array())
Convenience function to build an HTML submit button.
Definition: Xml.php:463
NS_MEDIAWIKI
const NS_MEDIAWIKI
Definition: Defines.php:87
Xml\input
static input( $name, $size=false, $value=false, $attribs=array())
Convenience function to build an HTML text input field.
Definition: Xml.php:294
$error
usually copyright or history_copyright This message must be in HTML not wikitext $subpages will be ignored and the rest of subPageSubtitle() will run. 'SkinTemplateBuildNavUrlsNav_urlsAfterPermalink' whether MediaWiki currently thinks this is a CSS JS page Hooks may change this value to override the return value of Title::isCssOrJsPage(). 'TitleIsAlwaysKnown' whether MediaWiki currently thinks this page is known isMovable() always returns false. $title whether MediaWiki currently thinks this page is movable Hooks may change this value to override the return value of Title::isMovable(). 'TitleIsWikitextPage' whether MediaWiki currently thinks this is a wikitext page Hooks may change this value to override the return value of Title::isWikitextPage() 'TitleMove' use UploadVerification and UploadVerifyFile instead where the first element is the message key and the remaining elements are used as parameters to the message based on mime etc Preferred in most cases over UploadVerification object with all info about the upload string as detected by MediaWiki Handlers will typically only apply for specific mime types object & $error
Definition: hooks.txt:2573
$e
if( $useReadline) $e
Definition: eval.php:66
Xml\label
static label( $label, $id, $attribs=array())
Convenience function to build an HTML form label.
Definition: Xml.php:374
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:59
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:354
wfExpandUrl
wfExpandUrl( $url, $defaultProto=PROTO_CURRENT)
Expand a potentially local URL to a fully-qualified URL.
Definition: GlobalFunctions.php:497
LogEventsList\showLogExtract
static showLogExtract(&$out, $types=array(), $page='', $user='', $param=array())
Show log extract.
Definition: LogEventsList.php:507
Status\newFatal
static newFatal( $message)
Factory function for fatal errors.
Definition: Status.php:63
FileDeleteForm\isValidOldSpec
static isValidOldSpec( $oldimage)
Is the provided oldimage value valid?
Definition: FileDeleteForm.php:338