2007-08-17 20:53:17 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* File deletion user interface
|
|
|
|
|
*
|
|
|
|
|
* @addtogroup Media
|
|
|
|
|
* @author Rob Church <robchur@gmail.com>
|
|
|
|
|
*/
|
|
|
|
|
class FileDeleteForm {
|
|
|
|
|
|
|
|
|
|
private $title = null;
|
|
|
|
|
private $file = null;
|
2007-08-20 14:49:07 +00:00
|
|
|
|
|
|
|
|
private $oldfile = null;
|
2007-08-17 20:53:17 +00:00
|
|
|
private $oldimage = '';
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Constructor
|
|
|
|
|
*
|
|
|
|
|
* @param File $file File we're deleting
|
|
|
|
|
*/
|
|
|
|
|
public function __construct( $file ) {
|
|
|
|
|
$this->title = $file->getTitle();
|
|
|
|
|
$this->file = $file;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Fulfil the request; shows the form or deletes the file,
|
|
|
|
|
* pending authentication, confirmation, etc.
|
|
|
|
|
*/
|
|
|
|
|
public function execute() {
|
2007-08-21 03:57:54 +00:00
|
|
|
global $wgOut, $wgRequest, $wgUser;
|
2007-08-17 20:53:17 +00:00
|
|
|
$this->setHeaders();
|
|
|
|
|
|
|
|
|
|
if( wfReadOnly() ) {
|
|
|
|
|
$wgOut->readOnlyPage();
|
|
|
|
|
return;
|
|
|
|
|
} elseif( !$wgUser->isLoggedIn() ) {
|
|
|
|
|
$wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
|
|
|
|
|
return;
|
|
|
|
|
} elseif( !$wgUser->isAllowed( 'delete' ) ) {
|
2007-09-28 00:49:44 +00:00
|
|
|
$wgOut->permissionRequired( 'delete' );
|
2007-08-17 20:53:17 +00:00
|
|
|
return;
|
|
|
|
|
} elseif( $wgUser->isBlocked() ) {
|
|
|
|
|
$wgOut->blockedPage();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-10-01 19:50:25 +00:00
|
|
|
$this->oldimage = $wgRequest->getText( 'oldimage', false );
|
2007-08-17 20:53:17 +00:00
|
|
|
$token = $wgRequest->getText( 'wpEditToken' );
|
|
|
|
|
if( $this->oldimage && !$this->isValidOldSpec() ) {
|
|
|
|
|
$wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage ) );
|
|
|
|
|
return;
|
|
|
|
|
}
|
2007-08-20 14:49:07 +00:00
|
|
|
if( $this->oldimage )
|
|
|
|
|
$this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
|
2007-08-17 20:53:17 +00:00
|
|
|
|
|
|
|
|
if( !$this->haveDeletableFile() ) {
|
|
|
|
|
$wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
|
|
|
|
|
$wgOut->addReturnTo( $this->title );
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Perform the deletion if appropriate
|
|
|
|
|
if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
|
2008-01-24 15:35:44 +00:00
|
|
|
$this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
|
|
|
|
|
$this->DeleteReason = $wgRequest->getText( 'wpReason' );
|
|
|
|
|
$reason = $this->DeleteReasonList;
|
|
|
|
|
if ( $reason != 'other' && $this->DeleteReason != '') {
|
|
|
|
|
// Entry from drop down menu + additional comment
|
|
|
|
|
$reason .= ': ' . $this->DeleteReason;
|
|
|
|
|
} elseif ( $reason == 'other' ) {
|
|
|
|
|
$reason = $this->DeleteReason;
|
|
|
|
|
}
|
2007-08-17 20:53:17 +00:00
|
|
|
if( $this->oldimage ) {
|
2008-01-24 15:35:44 +00:00
|
|
|
$status = $this->file->deleteOld( $this->oldimage, $reason );
|
2007-08-17 20:53:17 +00:00
|
|
|
if( $status->ok ) {
|
|
|
|
|
// Need to do a log item
|
|
|
|
|
$log = new LogPage( 'delete' );
|
2007-09-10 19:38:44 +00:00
|
|
|
$logComment = wfMsg( 'deletedrevision', $this->oldimage );
|
2008-01-24 15:35:44 +00:00
|
|
|
if( trim( $reason ) != '' )
|
|
|
|
|
$logComment .= ": {$reason}";
|
2007-09-10 19:38:44 +00:00
|
|
|
$log->addEntry( 'delete', $this->title, $logComment );
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
|
|
|
|
} else {
|
2008-01-24 15:35:44 +00:00
|
|
|
$status = $this->file->delete( $reason );
|
2007-08-17 20:53:17 +00:00
|
|
|
if( $status->ok ) {
|
|
|
|
|
// Need to delete the associated article
|
|
|
|
|
$article = new Article( $this->title );
|
2008-01-24 15:35:44 +00:00
|
|
|
$article->doDeleteArticle( $reason );
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if( !$status->isGood() )
|
|
|
|
|
$wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
|
|
|
|
|
if( $status->ok ) {
|
2007-12-11 16:45:28 +00:00
|
|
|
$wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
|
2007-08-17 20:53:17 +00:00
|
|
|
$wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
|
|
|
|
|
// Return to the main page if we just deleted all versions of the
|
|
|
|
|
// file, otherwise go back to the description page
|
|
|
|
|
$wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-27 15:55:02 +00:00
|
|
|
$this->showForm();
|
|
|
|
|
$this->showLogEntries();
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
2007-09-13 08:51:13 +00:00
|
|
|
|
2007-08-17 20:53:17 +00:00
|
|
|
/**
|
|
|
|
|
* Show the confirmation form
|
|
|
|
|
*/
|
|
|
|
|
private function showForm() {
|
2007-08-27 13:52:51 +00:00
|
|
|
global $wgOut, $wgUser, $wgRequest;
|
2008-01-24 15:35:44 +00:00
|
|
|
|
|
|
|
|
$mDeletereasonother = Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' );
|
|
|
|
|
$mDeletereasonotherlist = wfMsgHtml( 'filedelete-reason-otherlist' );
|
|
|
|
|
$scDeleteReasonList = wfMsgForContent( 'filedelete-reason-dropdown' );
|
|
|
|
|
$mDeleteReasonList = '';
|
|
|
|
|
$delcom = Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' );
|
|
|
|
|
if ( $scDeleteReasonList != '' && $scDeleteReasonList != '-' ) {
|
|
|
|
|
$deleteReasonList = "<option value=\"other\">$mDeletereasonotherlist</option>";
|
|
|
|
|
$optgroup = "";
|
|
|
|
|
foreach ( explode( "\n", $scDeleteReasonList ) as $option) {
|
|
|
|
|
$value = trim( htmlspecialchars($option) );
|
|
|
|
|
if ( $value == '' ) {
|
|
|
|
|
continue;
|
|
|
|
|
} elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
|
|
|
|
|
// A new group is starting ...
|
|
|
|
|
$value = trim( substr( $value, 1 ) );
|
|
|
|
|
$deleteReasonList .= "$optgroup<optgroup label=\"$value\">";
|
|
|
|
|
$optgroup = "</optgroup>";
|
|
|
|
|
} elseif ( substr( $value, 0, 2) == '**' ) {
|
|
|
|
|
// groupmember
|
|
|
|
|
$selected = "";
|
|
|
|
|
$value = trim( substr( $value, 2 ) );
|
|
|
|
|
if ( $mDeleteReasonList === $value)
|
|
|
|
|
$selected = ' selected="selected"';
|
|
|
|
|
$deleteReasonList .= "<option value=\"$value\"$selected>$value</option>";
|
|
|
|
|
} else {
|
|
|
|
|
// groupless delete reason
|
|
|
|
|
$selected = "";
|
|
|
|
|
if ( $this->DeleteReasonList === $value)
|
|
|
|
|
$selected = ' selected="selected"';
|
|
|
|
|
$deleteReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
|
|
|
|
|
$optgroup = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
$deleteReasonList .= $optgroup;
|
|
|
|
|
}
|
2007-09-13 08:51:13 +00:00
|
|
|
|
2007-08-17 20:53:17 +00:00
|
|
|
$form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
|
|
|
|
|
$form .= Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) );
|
|
|
|
|
$form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
|
2008-01-24 15:35:44 +00:00
|
|
|
$form .= '<table><tr>';
|
2007-08-17 20:53:17 +00:00
|
|
|
$form .= $this->prepareMessage( 'filedelete-intro' );
|
2008-01-24 15:35:44 +00:00
|
|
|
$form .= "<td align=\"right\"> $delcom </td><td align=\"left\">";
|
|
|
|
|
$form .= "<select tabindex='2' id='wpDeleteReasonList' name=\"wpDeleteReasonList\">
|
|
|
|
|
$deleteReasonList
|
|
|
|
|
</select>";
|
|
|
|
|
$form .= "</tr><tr><td align=\"right\"> $mDeletereasonother </td><td align=\"left\">";
|
|
|
|
|
$form .= "<input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' ";
|
|
|
|
|
$form .= "value=\"". htmlspecialchars( $wgRequest->getText( 'wpReason' ) ) ."\" tabindex=\"1\" />";
|
|
|
|
|
$form .= '</td></tr><tr><td colspan=2>';
|
2007-09-13 08:51:13 +00:00
|
|
|
$form .= '<p>' . Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
|
2008-01-24 15:35:44 +00:00
|
|
|
$form .= '</tr></table>';
|
2007-08-17 20:53:17 +00:00
|
|
|
$form .= '</fieldset>';
|
|
|
|
|
$form .= '</form>';
|
2007-09-13 08:51:13 +00:00
|
|
|
|
2007-08-17 20:53:17 +00:00
|
|
|
$wgOut->addHtml( $form );
|
|
|
|
|
}
|
2007-09-13 08:51:13 +00:00
|
|
|
|
2007-08-27 15:55:02 +00:00
|
|
|
/**
|
|
|
|
|
* Show deletion log fragments pertaining to the current file
|
|
|
|
|
*/
|
|
|
|
|
private function showLogEntries() {
|
|
|
|
|
global $wgOut;
|
|
|
|
|
$wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
|
|
|
|
|
$reader = new LogViewer(
|
|
|
|
|
new LogReader(
|
|
|
|
|
new FauxRequest(
|
|
|
|
|
array(
|
|
|
|
|
'type' => 'delete',
|
|
|
|
|
'page' => $this->title->getPrefixedText(),
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
)
|
|
|
|
|
);
|
|
|
|
|
$reader->showList( $wgOut );
|
|
|
|
|
}
|
|
|
|
|
|
2007-08-17 20:53:17 +00:00
|
|
|
/**
|
|
|
|
|
* Prepare a message referring to the file being deleted,
|
|
|
|
|
* showing an appropriate message depending upon whether
|
|
|
|
|
* it's a current file or an old version
|
|
|
|
|
*
|
|
|
|
|
* @param string $message Message base
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function prepareMessage( $message ) {
|
|
|
|
|
global $wgLang, $wgServer;
|
|
|
|
|
if( $this->oldimage ) {
|
|
|
|
|
return wfMsgExt(
|
|
|
|
|
"{$message}-old",
|
|
|
|
|
'parse',
|
|
|
|
|
$this->title->getText(),
|
2007-08-20 14:35:59 +00:00
|
|
|
$wgLang->date( $this->getTimestamp(), true ),
|
|
|
|
|
$wgLang->time( $this->getTimestamp(), true ),
|
2007-08-17 20:53:17 +00:00
|
|
|
$wgServer . $this->file->getArchiveUrl( $this->oldimage )
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
return wfMsgExt(
|
|
|
|
|
$message,
|
|
|
|
|
'parse',
|
|
|
|
|
$this->title->getText()
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Set headers, titles and other bits
|
|
|
|
|
*/
|
|
|
|
|
private function setHeaders() {
|
2007-08-22 05:35:40 +00:00
|
|
|
global $wgOut, $wgUser;
|
2007-08-17 20:53:17 +00:00
|
|
|
$wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
|
|
|
|
|
$wgOut->setRobotPolicy( 'noindex,nofollow' );
|
2007-08-22 05:35:40 +00:00
|
|
|
$wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Is the provided `oldimage` value valid?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function isValidOldSpec() {
|
|
|
|
|
return strlen( $this->oldimage ) >= 16
|
|
|
|
|
&& strpos( $this->oldimage, '/' ) === false
|
|
|
|
|
&& strpos( $this->oldimage, '\\' ) === false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Could we delete the file specified? If an `oldimage`
|
|
|
|
|
* value was provided, does it correspond to an
|
|
|
|
|
* existing, local, old version of this file?
|
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
private function haveDeletableFile() {
|
2007-08-20 14:49:07 +00:00
|
|
|
return $this->oldimage
|
|
|
|
|
? $this->oldfile && $this->oldfile->exists() && $this->oldfile->isLocal()
|
|
|
|
|
: $this->file && $this->file->exists() && $this->file->isLocal();
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Prepare the form action
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getAction() {
|
|
|
|
|
$q = array();
|
|
|
|
|
$q[] = 'action=delete';
|
|
|
|
|
if( $this->oldimage )
|
|
|
|
|
$q[] = 'oldimage=' . urlencode( $this->oldimage );
|
|
|
|
|
return $this->title->getLocalUrl( implode( '&', $q ) );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extract the timestamp of the old version
|
|
|
|
|
*
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
private function getTimestamp() {
|
2007-08-20 14:49:07 +00:00
|
|
|
return $this->oldfile->getTimestamp();
|
2007-08-17 20:53:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|