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 $mDeletereasonother = Xml
::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' );
116 $mDeletereasonotherlist = wfMsgHtml( 'filedelete-reason-otherlist' );
117 $scDeleteReasonList = wfMsgForContent( 'filedelete-reason-dropdown' );
118 $mDeleteReasonList = '';
119 $delcom = Xml
::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' );
120 if ( $scDeleteReasonList != '' && $scDeleteReasonList != '-' ) {
121 $deleteReasonList = "<option value=\"other\">$mDeletereasonotherlist</option>";
123 foreach ( explode( "\n", $scDeleteReasonList ) as $option) {
124 $value = trim( htmlspecialchars($option) );
125 if ( $value == '' ) {
127 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
128 // A new group is starting ...
129 $value = trim( substr( $value, 1 ) );
130 $deleteReasonList .= "$optgroup<optgroup label=\"$value\">";
131 $optgroup = "</optgroup>";
132 } elseif ( substr( $value, 0, 2) == '**' ) {
135 $value = trim( substr( $value, 2 ) );
136 if ( $mDeleteReasonList === $value)
137 $selected = ' selected="selected"';
138 $deleteReasonList .= "<option value=\"$value\"$selected>$value</option>";
140 // groupless delete reason
142 if ( $this->DeleteReasonList
=== $value)
143 $selected = ' selected="selected"';
144 $deleteReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
148 $deleteReasonList .= $optgroup;
151 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
152 $form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
153 $form .= Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) );
154 $form .= '<table><tr><td colspan="2">';
155 $form .= $this->prepareMessage( 'filedelete-intro' );
156 $form .= "</td></tr><tr><td align=\"right\"> $delcom </td><td align=\"left\">";
157 $form .= "<select tabindex='2' id='wpDeleteReasonList' name=\"wpDeleteReasonList\">
160 $form .= "</td></tr><tr><td align=\"right\"> $mDeletereasonother </td><td align=\"left\">";
161 $form .= "<input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' ";
162 $form .= "value=\"". htmlspecialchars( $wgRequest->getText( 'wpReason' ) ) ."\" tabindex=\"1\" />";
163 $form .= '</td></tr><tr><td colspan="2">';
164 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
165 $form .= '</td></tr></table>';
166 $form .= '</fieldset>';
169 $wgOut->addHtml( $form );
173 * Show deletion log fragments pertaining to the current file
175 private function showLogEntries() {
177 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
178 $reader = new LogViewer(
183 'page' => $this->title
->getPrefixedText(),
188 $reader->showList( $wgOut );
192 * Prepare a message referring to the file being deleted,
193 * showing an appropriate message depending upon whether
194 * it's a current file or an old version
196 * @param string $message Message base
199 private function prepareMessage( $message ) {
200 global $wgLang, $wgServer;
201 if( $this->oldimage
) {
205 $this->title
->getText(),
206 $wgLang->date( $this->getTimestamp(), true ),
207 $wgLang->time( $this->getTimestamp(), true ),
208 $wgServer . $this->file
->getArchiveUrl( $this->oldimage
)
214 $this->title
->getText()
220 * Set headers, titles and other bits
222 private function setHeaders() {
223 global $wgOut, $wgUser;
224 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
225 $wgOut->setRobotPolicy( 'noindex,nofollow' );
226 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
230 * Is the provided `oldimage` value valid?
234 private function isValidOldSpec() {
235 return strlen( $this->oldimage
) >= 16
236 && strpos( $this->oldimage
, '/' ) === false
237 && strpos( $this->oldimage
, '\\' ) === false;
241 * Could we delete the file specified? If an `oldimage`
242 * value was provided, does it correspond to an
243 * existing, local, old version of this file?
247 private function haveDeletableFile() {
248 return $this->oldimage
249 ?
$this->oldfile
&& $this->oldfile
->exists() && $this->oldfile
->isLocal()
250 : $this->file
&& $this->file
->exists() && $this->file
->isLocal();
254 * Prepare the form action
258 private function getAction() {
260 $q[] = 'action=delete';
261 if( $this->oldimage
)
262 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
263 return $this->title
->getLocalUrl( implode( '&', $q ) );
267 * Extract the timestamp of the old version
271 private function getTimestamp() {
272 return $this->oldfile
->getTimestamp();