Add a note about change in $wgTidyInternal behavior
[mediawiki.git] / includes / FileDeleteForm.php
blob3d16931792f7efe47648119aefa84b739070f887
1 <?php
3 /**
4 * File deletion user interface
6 * @addtogroup Media
7 * @author Rob Church <robchur@gmail.com>
8 */
9 class FileDeleteForm {
11 private $title = null;
12 private $file = null;
14 private $oldfile = null;
15 private $oldimage = '';
17 /**
18 * Constructor
20 * @param File $file File we're deleting
22 public function __construct( $file ) {
23 $this->title = $file->getTitle();
24 $this->file = $file;
27 /**
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;
33 $this->setHeaders();
35 if( wfReadOnly() ) {
36 $wgOut->readOnlyPage();
37 return;
38 } elseif( !$wgUser->isLoggedIn() ) {
39 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
40 return;
41 } elseif( !$wgUser->isAllowed( 'delete' ) ) {
42 $wgOut->permissionRequired( 'delete' );
43 return;
44 } elseif( $wgUser->isBlocked() ) {
45 $wgOut->blockedPage();
46 return;
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 ) );
53 return;
55 if( $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 );
61 return;
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 );
77 if( $status->ok ) {
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 );
85 } else {
86 $status = $this->file->delete( $reason );
87 if( $status->ok ) {
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' ) );
95 if( $status->ok ) {
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() );
102 return;
105 $this->showForm();
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' ) .
122 "<tr>
123 <td align='$align'>" .
124 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
125 "</td>
126 <td>" .
127 Xml::listDropDown( 'wpDeleteReasonList',
128 wfMsgForContent( 'filedelete-reason-dropdown' ),
129 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
130 "</td>
131 </tr>
132 <tr>
133 <td align='$align'>" .
134 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
135 "</td>
136 <td>" .
137 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '1', 'id' => 'wpReason' ) ) .
138 "</td>
139 </tr>
140 <tr>
141 <td></td>
142 <td>" .
143 Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) .
144 "</td>
145 </tr>" .
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() {
157 global $wgOut;
158 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
159 $reader = new LogViewer(
160 new LogReader(
161 new FauxRequest(
162 array(
163 'type' => 'delete',
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
178 * @return string
180 private function prepareMessage( $message ) {
181 global $wgLang, $wgServer;
182 if( $this->oldimage ) {
183 return wfMsgExt(
184 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
185 'parse',
186 $this->title->getText(),
187 $wgLang->date( $this->getTimestamp(), true ),
188 $wgLang->time( $this->getTimestamp(), true ),
189 $wgServer . $this->file->getArchiveUrl( $this->oldimage )
191 } else {
192 return wfMsgExt(
193 $message,
194 'parse',
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?
213 * @return bool
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?
226 * @return bool
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
237 * @return string
239 private function getAction() {
240 $q = array();
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
250 * @return string
252 private function getTimestamp() {
253 return $this->oldfile->getTimestamp();