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
;
75 if( $this->oldimage
) {
76 $status = $this->file
->deleteOld( $this->oldimage
, $reason );
78 // Need to do a log item
79 $log = new LogPage( 'delete' );
80 $logComment = wfMsg( 'deletedrevision', $this->oldimage
);
81 if( trim( $reason ) != '' )
82 $logComment .= ": {$reason}";
83 $log->addEntry( 'delete', $this->title
, $logComment );
86 $status = $this->file
->delete( $reason );
88 // Need to delete the associated article
89 $article = new Article( $this->title
);
90 $article->doDeleteArticle( $reason );
93 if( !$status->isGood() )
94 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
96 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
97 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
98 // Return to the main page if we just deleted all versions of the
99 // file, otherwise go back to the description page
100 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
106 $this->showLogEntries();
110 * Show the confirmation form
112 private function showForm() {
113 global $wgOut, $wgUser, $wgRequest;
115 $deletereasonother = Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' );
116 $delcom = Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' );
117 $reasonDropDown = Xml
::listDropDown( 'wpDeleteReasonList', wfMsgHtml( 'filedelete-reason-dropdown' ),
118 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 );
120 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
121 $form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
122 $form .= Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) );
123 $form .= '<table><tr><td colspan="2">';
124 $form .= $this->prepareMessage( 'filedelete-intro' );
125 $form .= "</td></tr><tr><td align=\"right\"> $delcom </td><td align=\"left\">";
126 $form .= $reasonDropDown;
127 $form .= "</td></tr><tr><td align=\"right\"> $deletereasonother </td><td align=\"left\">";
128 $form .= "<input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' ";
129 $form .= "value=\"". htmlspecialchars( $wgRequest->getText( 'wpReason' ) ) ."\" tabindex=\"1\" />";
130 $form .= '</td></tr><tr><td colspan="2">';
131 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
132 $form .= '</td></tr></table>';
133 $form .= '</fieldset>';
136 $wgOut->addHtml( $form );
140 * Show deletion log fragments pertaining to the current file
142 private function showLogEntries() {
144 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
145 $reader = new LogViewer(
150 'page' => $this->title
->getPrefixedText(),
155 $reader->showList( $wgOut );
159 * Prepare a message referring to the file being deleted,
160 * showing an appropriate message depending upon whether
161 * it's a current file or an old version
163 * @param string $message Message base
166 private function prepareMessage( $message ) {
167 global $wgLang, $wgServer;
168 if( $this->oldimage
) {
172 $this->title
->getText(),
173 $wgLang->date( $this->getTimestamp(), true ),
174 $wgLang->time( $this->getTimestamp(), true ),
175 $wgServer . $this->file
->getArchiveUrl( $this->oldimage
)
181 $this->title
->getText()
187 * Set headers, titles and other bits
189 private function setHeaders() {
190 global $wgOut, $wgUser;
191 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
192 $wgOut->setRobotPolicy( 'noindex,nofollow' );
193 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
197 * Is the provided `oldimage` value valid?
201 private function isValidOldSpec() {
202 return strlen( $this->oldimage
) >= 16
203 && strpos( $this->oldimage
, '/' ) === false
204 && strpos( $this->oldimage
, '\\' ) === false;
208 * Could we delete the file specified? If an `oldimage`
209 * value was provided, does it correspond to an
210 * existing, local, old version of this file?
214 private function haveDeletableFile() {
215 return $this->oldimage
216 ?
$this->oldfile
&& $this->oldfile
->exists() && $this->oldfile
->isLocal()
217 : $this->file
&& $this->file
->exists() && $this->file
->isLocal();
221 * Prepare the form action
225 private function getAction() {
227 $q[] = 'action=delete';
228 if( $this->oldimage
)
229 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
230 return $this->title
->getLocalUrl( implode( '&', $q ) );
234 * Extract the timestamp of the old version
238 private function getTimestamp() {
239 return $this->oldfile
->getTimestamp();