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();
38 } elseif( !$wgUser->isLoggedIn() ) {
39 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
41 } elseif( !$wgUser->isAllowed( 'delete' ) ) {
42 $wgOut->permissionRequired( 'delete' );
44 } elseif( $wgUser->isBlocked() ) {
45 $wgOut->blockedPage();
49 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
50 $token = $wgRequest->getText( 'wpEditToken' );
51 if( $this->oldimage
&& !$this->isValidOldSpec() ) {
52 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage
) );
56 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
58 if( !$this->haveDeletableFile() ) {
59 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
60 $wgOut->addReturnTo( $this->title
);
64 // Perform the deletion if appropriate
65 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
66 $this->DeleteReasonList
= $wgRequest->getText( 'wpDeleteReasonList' );
67 $this->DeleteReason
= $wgRequest->getText( 'wpReason' );
68 $reason = $this->DeleteReasonList
;
69 if ( $reason != 'other' && $this->DeleteReason
!= '') {
70 // Entry from drop down menu + additional comment
71 $reason .= ': ' . $this->DeleteReason
;
72 } elseif ( $reason == 'other' ) {
73 $reason = $this->DeleteReason
;
77 if( $this->oldimage
) {
78 $status = $this->file
->deleteOld( $this->oldimage
, $reason );
80 // Need to do a log item
81 $log = new LogPage( 'delete' );
82 $logComment = wfMsgForContent( 'deletedrevision', $this->oldimage
);
83 if( trim( $reason ) != '' )
84 $logComment .= ": {$reason}";
85 $log->addEntry( 'delete', $this->title
, $logComment );
88 $status = $this->file
->delete( $reason );
90 // Need to delete the associated article
91 $article = new Article( $this->title
);
92 $article->doDeleteArticle( $reason );
95 if( $status->isGood() ) wfRunHooks('FileDeleteComplete', array(
96 &$this->file
, &$this->oldimage
, &$article, &$wgUser, &$reason));
98 if( !$status->isGood() )
99 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
101 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
102 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
103 // Return to the main page if we just deleted all versions of the
104 // file, otherwise go back to the description page
105 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
111 $this->showLogEntries();
115 * Show the confirmation form
117 private function showForm() {
118 global $wgOut, $wgUser, $wgRequest, $wgContLang;
119 $align = $wgContLang->isRtl() ?
'left' : 'right';
121 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) ) .
122 Xml
::openElement( 'fieldset' ) .
123 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
124 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
125 $this->prepareMessage( 'filedelete-intro' ) .
126 Xml
::openElement( 'table' ) .
128 <td align='$align'>" .
129 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
132 Xml
::listDropDown( 'wpDeleteReasonList',
133 wfMsgForContent( 'filedelete-reason-dropdown' ),
134 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
138 <td align='$align'>" .
139 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
142 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
148 Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
151 Xml
::closeElement( 'table' ) .
152 Xml
::closeElement( 'fieldset' ) .
153 Xml
::closeElement( 'form' );
155 if ( $wgUser->isAllowed( 'editinterface' ) ) {
156 $skin = $wgUser->getSkin();
157 $link = $skin->makeLink ( 'MediaWiki:filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
158 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
161 $wgOut->addHtml( $form );
165 * Show deletion log fragments pertaining to the current file
167 private function showLogEntries() {
169 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
170 $reader = new LogViewer(
175 'page' => $this->title
->getPrefixedText(),
180 $reader->showList( $wgOut );
184 * Prepare a message referring to the file being deleted,
185 * showing an appropriate message depending upon whether
186 * it's a current file or an old version
188 * @param string $message Message base
191 private function prepareMessage( $message ) {
193 if( $this->oldimage
) {
194 $url = $this->file
->getArchiveUrl( $this->oldimage
);
196 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
198 $this->title
->getText(),
199 $wgLang->date( $this->getTimestamp(), true ),
200 $wgLang->time( $this->getTimestamp(), true ),
201 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
206 $this->title
->getText()
212 * Set headers, titles and other bits
214 private function setHeaders() {
215 global $wgOut, $wgUser;
216 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
217 $wgOut->setRobotPolicy( 'noindex,nofollow' );
218 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
222 * Is the provided `oldimage` value valid?
226 private function isValidOldSpec() {
227 return strlen( $this->oldimage
) >= 16
228 && strpos( $this->oldimage
, '/' ) === false
229 && strpos( $this->oldimage
, '\\' ) === false;
233 * Could we delete the file specified? If an `oldimage`
234 * value was provided, does it correspond to an
235 * existing, local, old version of this file?
239 private function haveDeletableFile() {
240 return $this->oldimage
241 ?
$this->oldfile
&& $this->oldfile
->exists() && $this->oldfile
->isLocal()
242 : $this->file
&& $this->file
->exists() && $this->file
->isLocal();
246 * Prepare the form action
250 private function getAction() {
252 $q[] = 'action=delete';
253 if( $this->oldimage
)
254 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
255 return $this->title
->getLocalUrl( implode( '&', $q ) );
259 * Extract the timestamp of the old version
263 private function getTimestamp() {
264 return $this->oldfile
->getTimestamp();