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 = wfMsgForContent( '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, $wgContLang;
114 $align = $wgContLang->isRtl() ?
'left' : 'right';
116 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) ) .
117 Xml
::openElement( 'fieldset' ) .
118 Xml
::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
119 Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) ) .
120 $this->prepareMessage( 'filedelete-intro' ) .
121 Xml
::openElement( 'table' ) .
123 <td align='$align'>" .
124 Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
127 Xml
::listDropDown( 'wpDeleteReasonList',
128 wfMsgForContent( 'filedelete-reason-dropdown' ),
129 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
133 <td align='$align'>" .
134 Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
137 Xml
::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
143 Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
146 Xml
::closeElement( 'table' ) .
147 Xml
::closeElement( 'fieldset' ) .
148 Xml
::closeElement( 'form' );
150 $wgOut->addHtml( $form );
154 * Show deletion log fragments pertaining to the current file
156 private function showLogEntries() {
158 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
159 $reader = new LogViewer(
164 'page' => $this->title
->getPrefixedText(),
169 $reader->showList( $wgOut );
173 * Prepare a message referring to the file being deleted,
174 * showing an appropriate message depending upon whether
175 * it's a current file or an old version
177 * @param string $message Message base
180 private function prepareMessage( $message ) {
182 if( $this->oldimage
) {
183 $url = $this->file
->getArchiveUrl( $this->oldimage
);
185 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
187 $this->title
->getText(),
188 $wgLang->date( $this->getTimestamp(), true ),
189 $wgLang->time( $this->getTimestamp(), true ),
190 wfExpandUrl( $this->file
->getArchiveUrl( $this->oldimage
) ) );
195 $this->title
->getText()
201 * Set headers, titles and other bits
203 private function setHeaders() {
204 global $wgOut, $wgUser;
205 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
206 $wgOut->setRobotPolicy( 'noindex,nofollow' );
207 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
211 * Is the provided `oldimage` value valid?
215 private function isValidOldSpec() {
216 return strlen( $this->oldimage
) >= 16
217 && strpos( $this->oldimage
, '/' ) === false
218 && strpos( $this->oldimage
, '\\' ) === false;
222 * Could we delete the file specified? If an `oldimage`
223 * value was provided, does it correspond to an
224 * existing, local, old version of this file?
228 private function haveDeletableFile() {
229 return $this->oldimage
230 ?
$this->oldfile
&& $this->oldfile
->exists() && $this->oldfile
->isLocal()
231 : $this->file
&& $this->file
->exists() && $this->file
->isLocal();
235 * Prepare the form action
239 private function getAction() {
241 $q[] = 'action=delete';
242 if( $this->oldimage
)
243 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
244 return $this->title
->getLocalUrl( implode( '&', $q ) );
248 * Extract the timestamp of the old version
252 private function getTimestamp() {
253 return $this->oldfile
->getTimestamp();