* Fix bug 13002
[lhc/web/wiklou.git] / includes / FileDeleteForm.php
1 <?php
2
3 /**
4 * File deletion user interface
5 *
6 * @addtogroup Media
7 * @author Rob Church <robchur@gmail.com>
8 */
9 class FileDeleteForm {
10
11 private $title = null;
12 private $file = null;
13
14 private $oldfile = null;
15 private $oldimage = '';
16
17 /**
18 * Constructor
19 *
20 * @param File $file File we're deleting
21 */
22 public function __construct( $file ) {
23 $this->title = $file->getTitle();
24 $this->file = $file;
25 }
26
27 /**
28 * Fulfil the request; shows the form or deletes the file,
29 * pending authentication, confirmation, etc.
30 */
31 public function execute() {
32 global $wgOut, $wgRequest, $wgUser;
33 $this->setHeaders();
34
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;
47 }
48
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;
54 }
55 if( $this->oldimage )
56 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
57
58 if( !$this->haveDeletableFile() ) {
59 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
60 $wgOut->addReturnTo( $this->title );
61 return;
62 }
63
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;
74 }
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 = wfMsgForContent( 'deletedrevision', $this->oldimage );
81 if( trim( $reason ) != '' )
82 $logComment .= ": {$reason}";
83 $log->addEntry( 'delete', $this->title, $logComment );
84 }
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 );
91 }
92 }
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() );
101 }
102 return;
103 }
104
105 $this->showForm();
106 $this->showLogEntries();
107 }
108
109 /**
110 * Show the confirmation form
111 */
112 private function showForm() {
113 global $wgOut, $wgUser, $wgRequest, $wgContLang;
114 $align = $wgContLang->isRtl() ? 'left' : 'right';
115
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' => '2', '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', 'tabindex' => '3' ) ) .
144 "</td>
145 </tr>" .
146 Xml::closeElement( 'table' ) .
147 Xml::closeElement( 'fieldset' ) .
148 Xml::closeElement( 'form' );
149
150 $wgOut->addHtml( $form );
151 }
152
153 /**
154 * Show deletion log fragments pertaining to the current file
155 */
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(),
165 )
166 )
167 )
168 );
169 $reader->showList( $wgOut );
170 }
171
172 /**
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
176 *
177 * @param string $message Message base
178 * @return string
179 */
180 private function prepareMessage( $message ) {
181 global $wgLang, $wgServer;
182 if( $this->oldimage ) {
183 $url = $this->file->getArchiveUrl( $this->oldimage );
184 if( substr( $url, 0, 1 ) == '/' ) {
185 // Fully-qualify the URL if necessary
186 $url = $wgServer . $url;
187 }
188 return wfMsgExt(
189 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
190 'parse',
191 $this->title->getText(),
192 $wgLang->date( $this->getTimestamp(), true ),
193 $wgLang->time( $this->getTimestamp(), true ),
194 $url
195 );
196 } else {
197 return wfMsgExt(
198 $message,
199 'parse',
200 $this->title->getText()
201 );
202 }
203 }
204
205 /**
206 * Set headers, titles and other bits
207 */
208 private function setHeaders() {
209 global $wgOut, $wgUser;
210 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
211 $wgOut->setRobotPolicy( 'noindex,nofollow' );
212 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
213 }
214
215 /**
216 * Is the provided `oldimage` value valid?
217 *
218 * @return bool
219 */
220 private function isValidOldSpec() {
221 return strlen( $this->oldimage ) >= 16
222 && strpos( $this->oldimage, '/' ) === false
223 && strpos( $this->oldimage, '\\' ) === false;
224 }
225
226 /**
227 * Could we delete the file specified? If an `oldimage`
228 * value was provided, does it correspond to an
229 * existing, local, old version of this file?
230 *
231 * @return bool
232 */
233 private function haveDeletableFile() {
234 return $this->oldimage
235 ? $this->oldfile && $this->oldfile->exists() && $this->oldfile->isLocal()
236 : $this->file && $this->file->exists() && $this->file->isLocal();
237 }
238
239 /**
240 * Prepare the form action
241 *
242 * @return string
243 */
244 private function getAction() {
245 $q = array();
246 $q[] = 'action=delete';
247 if( $this->oldimage )
248 $q[] = 'oldimage=' . urlencode( $this->oldimage );
249 return $this->title->getLocalUrl( implode( '&', $q ) );
250 }
251
252 /**
253 * Extract the timestamp of the old version
254 *
255 * @return string
256 */
257 private function getTimestamp() {
258 return $this->oldfile->getTimestamp();
259 }
260
261 }