00001 <?php
00002
00009 class FileDeleteForm {
00010
00011 private $title = null;
00012 private $file = null;
00013
00014 private $oldfile = null;
00015 private $oldimage = '';
00016
00022 public function __construct( $file ) {
00023 $this->title = $file->getTitle();
00024 $this->file = $file;
00025 }
00026
00031 public function execute() {
00032 global $wgOut, $wgRequest, $wgUser;
00033 $this->setHeaders();
00034
00035 if( wfReadOnly() ) {
00036 $wgOut->readOnlyPage();
00037 return;
00038 }
00039 $permission_errors = $this->title->getUserPermissionsErrors('delete', $wgUser);
00040 if (count($permission_errors)>0) {
00041 $wgOut->showPermissionsErrorPage( $permission_errors );
00042 return;
00043 }
00044
00045 $this->oldimage = $wgRequest->getText( 'oldimage', false );
00046 $token = $wgRequest->getText( 'wpEditToken' );
00047 # Flag to hide all contents of the archived revisions
00048 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
00049
00050 if( $this->oldimage && !self::isValidOldSpec($this->oldimage) ) {
00051 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage ) );
00052 return;
00053 }
00054 if( $this->oldimage )
00055 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
00056
00057 if( !self::haveDeletableFile($this->file, $this->oldfile, $this->oldimage) ) {
00058 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
00059 $wgOut->addReturnTo( $this->title );
00060 return;
00061 }
00062
00063
00064 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
00065 $this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
00066 $this->DeleteReason = $wgRequest->getText( 'wpReason' );
00067 $reason = $this->DeleteReasonList;
00068 if ( $reason != 'other' && $this->DeleteReason != '') {
00069
00070 $reason .= wfMsgForContent( 'colon-separator' ) . $this->DeleteReason;
00071 } elseif ( $reason == 'other' ) {
00072 $reason = $this->DeleteReason;
00073 }
00074
00075 $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress );
00076
00077 if( !$status->isGood() )
00078 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
00079 if( $status->ok ) {
00080 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
00081 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
00082
00083
00084 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
00085 }
00086 return;
00087 }
00088
00089 $this->showForm();
00090 $this->showLogEntries();
00091 }
00092
00093 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
00094 $article = null;
00095 if( $oldimage ) {
00096 $status = $file->deleteOld( $oldimage, $reason, $suppress );
00097 if( $status->ok ) {
00098
00099 $log = new LogPage( 'delete' );
00100 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
00101 if( trim( $reason ) != '' )
00102 $logComment .= ": {$reason}";
00103 $log->addEntry( 'delete', $title, $logComment );
00104 }
00105 } else {
00106 $status = $file->delete( $reason, $suppress );
00107 if( $status->ok ) {
00108 $id = $title->getArticleID( GAID_FOR_UPDATE );
00109
00110 $article = new Article( $title );
00111 $error = '';
00112 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason, &$error)) ) {
00113 if( $article->doDeleteArticle( $reason, $suppress, $id ) ) {
00114 global $wgRequest;
00115 if( $wgRequest->getCheck( 'wpWatch' ) ) {
00116 $article->doWatch();
00117 } elseif( $title->userIsWatching() ) {
00118 $article->doUnwatch();
00119 }
00120 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
00121 }
00122 }
00123 }
00124 }
00125 if( $status->isGood() )
00126 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
00127
00128 return $status;
00129 }
00130
00134 private function showForm() {
00135 global $wgOut, $wgUser, $wgRequest;
00136
00137 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
00138 $suppress = "<tr id=\"wpDeleteSuppressRow\">
00139 <td></td>
00140 <td class='mw-input'>" .
00141 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
00142 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
00143 "</td>
00144 </tr>";
00145 } else {
00146 $suppress = '';
00147 }
00148
00149 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
00150 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
00151 'id' => 'mw-img-deleteconfirm' ) ) .
00152 Xml::openElement( 'fieldset' ) .
00153 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
00154 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
00155 $this->prepareMessage( 'filedelete-intro' ) .
00156 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
00157 "<tr>
00158 <td class='mw-label'>" .
00159 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
00160 "</td>
00161 <td class='mw-input'>" .
00162 Xml::listDropDown( 'wpDeleteReasonList',
00163 wfMsgForContent( 'filedelete-reason-dropdown' ),
00164 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
00165 "</td>
00166 </tr>
00167 <tr>
00168 <td class='mw-label'>" .
00169 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
00170 "</td>
00171 <td class='mw-input'>" .
00172 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
00173 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
00174 "</td>
00175 </tr>
00176 {$suppress}
00177 <tr>
00178 <td></td>
00179 <td class='mw-input'>" .
00180 Xml::checkLabel( wfMsg( 'watchthis' ),
00181 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
00182 "</td>
00183 </tr>
00184 <tr>
00185 <td></td>
00186 <td class='mw-submit'>" .
00187 Xml::submitButton( wfMsg( 'filedelete-submit' ),
00188 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
00189 "</td>
00190 </tr>" .
00191 Xml::closeElement( 'table' ) .
00192 Xml::closeElement( 'fieldset' ) .
00193 Xml::closeElement( 'form' );
00194
00195 if ( $wgUser->isAllowed( 'editinterface' ) ) {
00196 $skin = $wgUser->getSkin();
00197 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
00198 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
00199 }
00200
00201 $wgOut->addHTML( $form );
00202 }
00203
00207 private function showLogEntries() {
00208 global $wgOut;
00209 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
00210 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
00211 }
00212
00221 private function prepareMessage( $message ) {
00222 global $wgLang;
00223 if( $this->oldimage ) {
00224 $url = $this->file->getArchiveUrl( $this->oldimage );
00225 return wfMsgExt(
00226 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
00227 'parse',
00228 $this->title->getText(),
00229 $wgLang->date( $this->getTimestamp(), true ),
00230 $wgLang->time( $this->getTimestamp(), true ),
00231 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
00232 } else {
00233 return wfMsgExt(
00234 $message,
00235 'parse',
00236 $this->title->getText()
00237 );
00238 }
00239 }
00240
00244 private function setHeaders() {
00245 global $wgOut, $wgUser;
00246 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
00247 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00248 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
00249 }
00250
00256 public static function isValidOldSpec($oldimage) {
00257 return strlen( $oldimage ) >= 16
00258 && strpos( $oldimage, '/' ) === false
00259 && strpos( $oldimage, '\\' ) === false;
00260 }
00261
00269 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
00270 return $oldimage
00271 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
00272 : $file && $file->exists() && $file->isLocal();
00273 }
00274
00280 private function getAction() {
00281 $q = array();
00282 $q[] = 'action=delete';
00283 if( $this->oldimage )
00284 $q[] = 'oldimage=' . urlencode( $this->oldimage );
00285 return $this->title->getLocalUrl( implode( '&', $q ) );
00286 }
00287
00293 private function getTimestamp() {
00294 return $this->oldfile->getTimestamp();
00295 }
00296
00297 }