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',
118 wfMsgForContent( 'filedelete-reason-dropdown' ),
119 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 );
121 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
122 $form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
123 $form .= Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) );
124 $form .= '<table><tr><td colspan="2">';
125 $form .= $this->prepareMessage( 'filedelete-intro' );
126 $form .= "</td></tr><tr><td align=\"right\"> $delcom </td><td align=\"left\">";
127 $form .= $reasonDropDown;
128 $form .= "</td></tr><tr><td align=\"right\"> $deletereasonother </td><td align=\"left\">";
129 $form .= "<input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' ";
130 $form .= "value=\"". htmlspecialchars( $wgRequest->getText( 'wpReason' ) ) ."\" tabindex=\"1\" />";
131 $form .= '</td></tr><tr><td colspan="2">';
132 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
133 $form .= '</td></tr></table>';
134 $form .= '</fieldset>';
137 $wgOut->addHtml( $form );
141 * Show deletion log fragments pertaining to the current file
143 private function showLogEntries() {
145 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
146 $reader = new LogViewer(
151 'page' => $this->title
->getPrefixedText(),
156 $reader->showList( $wgOut );
160 * Prepare a message referring to the file being deleted,
161 * showing an appropriate message depending upon whether
162 * it's a current file or an old version
164 * @param string $message Message base
167 private function prepareMessage( $message ) {
168 global $wgLang, $wgServer;
169 if( $this->oldimage
) {
173 $this->title
->getText(),
174 $wgLang->date( $this->getTimestamp(), true ),
175 $wgLang->time( $this->getTimestamp(), true ),
176 $wgServer . $this->file
->getArchiveUrl( $this->oldimage
)
182 $this->title
->getText()
188 * Set headers, titles and other bits
190 private function setHeaders() {
191 global $wgOut, $wgUser;
192 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
193 $wgOut->setRobotPolicy( 'noindex,nofollow' );
194 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
198 * Is the provided `oldimage` value valid?
202 private function isValidOldSpec() {
203 return strlen( $this->oldimage
) >= 16
204 && strpos( $this->oldimage
, '/' ) === false
205 && strpos( $this->oldimage
, '\\' ) === false;
209 * Could we delete the file specified? If an `oldimage`
210 * value was provided, does it correspond to an
211 * existing, local, old version of this file?
215 private function haveDeletableFile() {
216 return $this->oldimage
217 ?
$this->oldfile
&& $this->oldfile
->exists() && $this->oldfile
->isLocal()
218 : $this->file
&& $this->file
->exists() && $this->file
->isLocal();
222 * Prepare the form action
226 private function getAction() {
228 $q[] = 'action=delete';
229 if( $this->oldimage
)
230 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
231 return $this->title
->getLocalUrl( implode( '&', $q ) );
235 * Extract the timestamp of the old version
239 private function getTimestamp() {
240 return $this->oldfile
->getTimestamp();