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('deleterevision');
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 // Need to delete the associated article
109 $article = new Article( $title );
110 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)) ) {
111 if( $article->doDeleteArticle( $reason, $suppress ) )
112 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason));
116 if( $status->isGood() ) wfRunHooks('FileDeleteComplete', array(
117 &$file, &$oldimage, &$article, &$wgUser, &$reason));
123 * Show the confirmation form
125 private function showForm() {
126 global $wgOut, $wgUser, $wgRequest, $wgContLang;
127 $align = $wgContLang->isRtl() ?
'left' : 'right';
129 if( $wgUser->isAllowed( 'deleterevision' ) ) {
130 $suppress = "<tr id=\"wpDeleteSuppressRow\" name=\"wpDeleteSuppressRow\"><td></td><td>";
131 $suppress .= Xml
::checkLabel( wfMsg( 'revdelete-suppress' ), 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '2' ) );
132 $suppress .= "</td></tr>";
137 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) ) .
138 Xml
::openElement( 'fieldset' ) .
139 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
140 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
141 $this->prepareMessage( 'filedelete-intro' ) .
142 Xml
::openElement( 'table' ) .
144 <td align='$align'>" .
145 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
148 Xml
::listDropDown( 'wpDeleteReasonList',
149 wfMsgForContent( 'filedelete-reason-dropdown' ),
150 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
154 <td align='$align'>" .
155 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
158 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
165 Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
168 Xml
::closeElement( 'table' ) .
169 Xml
::closeElement( 'fieldset' ) .
170 Xml
::closeElement( 'form' );
172 if ( $wgUser->isAllowed( 'editinterface' ) ) {
173 $skin = $wgUser->getSkin();
174 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
175 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
178 $wgOut->addHtml( $form );
182 * Show deletion log fragments pertaining to the current file
184 private function showLogEntries() {
186 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
187 LogEventsList
::showLogExtract( $wgOut, 'delete', $this->title
->getPrefixedText() );
191 * Prepare a message referring to the file being deleted,
192 * showing an appropriate message depending upon whether
193 * it's a current file or an old version
195 * @param string $message Message base
198 private function prepareMessage( $message ) {
200 if( $this->oldimage
) {
201 $url = $this->file
->getArchiveUrl( $this->oldimage
);
203 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
205 $this->title
->getText(),
206 $wgLang->date( $this->getTimestamp(), true ),
207 $wgLang->time( $this->getTimestamp(), true ),
208 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
213 $this->title
->getText()
219 * Set headers, titles and other bits
221 private function setHeaders() {
222 global $wgOut, $wgUser;
223 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
224 $wgOut->setRobotPolicy( 'noindex,nofollow' );
225 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
229 * Is the provided `oldimage` value valid?
233 public static function isValidOldSpec($oldimage) {
234 return strlen( $oldimage ) >= 16
235 && strpos( $oldimage, '/' ) === false
236 && strpos( $oldimage, '\\' ) === false;
240 * Could we delete the file specified? If an `oldimage`
241 * value was provided, does it correspond to an
242 * existing, local, old version of this file?
246 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
248 ?
$oldfile && $oldfile->exists() && $oldfile->isLocal()
249 : $file && $file->exists() && $file->isLocal();
253 * Prepare the form action
257 private function getAction() {
259 $q[] = 'action=delete';
260 if( $this->oldimage
)
261 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
262 return $this->title
->getLocalUrl( implode( '&', $q ) );
266 * Extract the timestamp of the old version
270 private function getTimestamp() {
271 return $this->oldfile
->getTimestamp();