4 * File deletion user interface
7 * @author Rob Church <robchur@gmail.com>
11 private $title = null;
14 private $oldfile = null;
15 private $oldimage = '';
20 * @param File $file File we're deleting
22 public function __construct( $file ) {
23 $this->title
= $file->getTitle();
28 * Fulfil the request; shows the form or deletes the file,
29 * pending authentication, confirmation, etc.
31 public function execute() {
32 global $wgOut, $wgRequest, $wgUser;
36 $wgOut->readOnlyPage();
39 $permission_errors = $this->title
->getUserPermissionsErrors('delete', $wgUser);
40 if (count($permission_errors)>0) {
41 $wgOut->showPermissionsErrorPage( $permission_errors );
45 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
46 $token = $wgRequest->getText( 'wpEditToken' );
47 # Flag to hide all contents of the archived revisions
48 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
50 if( $this->oldimage
&& !self
::isValidOldSpec($this->oldimage
) ) {
51 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage
) );
55 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
57 if( !self
::haveDeletableFile($this->file
, $this->oldfile
, $this->oldimage
) ) {
58 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
59 $wgOut->addReturnTo( $this->title
);
63 // Perform the deletion if appropriate
64 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
65 $this->DeleteReasonList
= $wgRequest->getText( 'wpDeleteReasonList' );
66 $this->DeleteReason
= $wgRequest->getText( 'wpReason' );
67 $reason = $this->DeleteReasonList
;
68 if ( $reason != 'other' && $this->DeleteReason
!= '') {
69 // Entry from drop down menu + additional comment
70 $reason .= ': ' . $this->DeleteReason
;
71 } elseif ( $reason == 'other' ) {
72 $reason = $this->DeleteReason
;
75 $status = self
::doDelete( $this->title
, $this->file
, $this->oldimage
, $reason, $suppress );
77 if( !$status->isGood() )
78 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
80 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
81 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
82 // Return to the main page if we just deleted all versions of the
83 // file, otherwise go back to the description page
84 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
90 $this->showLogEntries();
93 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress ) {
96 $status = $file->deleteOld( $oldimage, $reason, $suppress );
98 // Need to do a log item
99 $log = new LogPage( 'delete' );
100 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
101 if( trim( $reason ) != '' )
102 $logComment .= ": {$reason}";
103 $log->addEntry( 'delete', $title, $logComment );
106 $status = $file->delete( $reason, $suppress );
108 $id = $title->getArticleID( GAID_FOR_UPDATE
);
109 // Need to delete the associated article
110 $article = new Article( $title );
111 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)) ) {
112 if( $article->doDeleteArticle( $reason, $suppress, $id ) )
113 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
117 if( $status->isGood() )
118 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
124 * Show the confirmation form
126 private function showForm() {
127 global $wgOut, $wgUser, $wgRequest, $wgContLang;
128 $align = $wgContLang->isRtl() ?
'left' : 'right';
130 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
131 $suppress = "<tr id=\"wpDeleteSuppressRow\" name=\"wpDeleteSuppressRow\"><td></td><td>";
132 $suppress .= Xml
::checkLabel( wfMsg( 'revdelete-suppress' ), 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '2' ) );
133 $suppress .= "</td></tr>";
138 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(), 'id' => 'mw-img-deleteconfirm' ) ) .
139 Xml
::openElement( 'fieldset' ) .
140 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
141 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
142 $this->prepareMessage( 'filedelete-intro' ) .
143 Xml
::openElement( 'table' ) .
145 <td align='$align'>" .
146 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
149 Xml
::listDropDown( 'wpDeleteReasonList',
150 wfMsgForContent( 'filedelete-reason-dropdown' ),
151 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
155 <td align='$align'>" .
156 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
159 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
166 Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
169 Xml
::closeElement( 'table' ) .
170 Xml
::closeElement( 'fieldset' ) .
171 Xml
::closeElement( 'form' );
173 if ( $wgUser->isAllowed( 'editinterface' ) ) {
174 $skin = $wgUser->getSkin();
175 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
176 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
179 $wgOut->addHtml( $form );
183 * Show deletion log fragments pertaining to the current file
185 private function showLogEntries() {
187 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
188 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
->getPrefixedText() );
192 * Prepare a message referring to the file being deleted,
193 * showing an appropriate message depending upon whether
194 * it's a current file or an old version
196 * @param string $message Message base
199 private function prepareMessage( $message ) {
201 if( $this->oldimage
) {
202 $url = $this->file
->getArchiveUrl( $this->oldimage
);
204 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
206 $this->title
->getText(),
207 $wgLang->date( $this->getTimestamp(), true ),
208 $wgLang->time( $this->getTimestamp(), true ),
209 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
214 $this->title
->getText()
220 * Set headers, titles and other bits
222 private function setHeaders() {
223 global $wgOut, $wgUser;
224 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
225 $wgOut->setRobotPolicy( 'noindex,nofollow' );
226 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
230 * Is the provided `oldimage` value valid?
234 public static function isValidOldSpec($oldimage) {
235 return strlen( $oldimage ) >= 16
236 && strpos( $oldimage, '/' ) === false
237 && strpos( $oldimage, '\\' ) === false;
241 * Could we delete the file specified? If an `oldimage`
242 * value was provided, does it correspond to an
243 * existing, local, old version of this file?
247 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
249 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
250 : $file && $file->exists() && $file->isLocal();
254 * Prepare the form action
258 private function getAction() {
260 $q[] = 'action=delete';
261 if( $this->oldimage
)
262 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
263 return $this->title
->getLocalUrl( implode( '&', $q ) );
267 * Extract the timestamp of the old version
271 private function getTimestamp() {
272 return $this->oldfile
->getTimestamp();