* r109659: actually return the exact type we say we do
[lhc/web/wiklou.git] / includes / FileDeleteForm.php
1 <?php
2
3 /**
4 * File deletion user interface
5 *
6 * @ingroup Media
7 * @author Rob Church <robchur@gmail.com>
8 */
9 class FileDeleteForm {
10
11 /**
12 * @var Title
13 */
14 private $title = null;
15
16 /**
17 * @var File
18 */
19 private $file = null;
20
21 /**
22 * @var File
23 */
24 private $oldfile = null;
25 private $oldimage = '';
26
27 /**
28 * Constructor
29 *
30 * @param $file File object we're deleting
31 */
32 public function __construct( $file ) {
33 $this->title = $file->getTitle();
34 $this->file = $file;
35 }
36
37 /**
38 * Fulfil the request; shows the form or deletes the file,
39 * pending authentication, confirmation, etc.
40 */
41 public function execute() {
42 global $wgOut, $wgRequest, $wgUser, $wgUploadMaintenance;
43
44 $permissionErrors = $this->title->getUserPermissionsErrors( 'delete', $wgUser );
45 if ( count( $permissionErrors ) ) {
46 throw new PermissionsError( 'delete', $permissionErrors );
47 }
48
49 if ( wfReadOnly() ) {
50 throw new ReadOnlyError;
51 }
52
53 if ( $wgUploadMaintenance ) {
54 throw new ErrorPageError( 'filedelete-maintenance-title', 'filedelete-maintenance' );
55 }
56
57 $this->setHeaders();
58
59 $this->oldimage = $wgRequest->getText( 'oldimage', false );
60 $token = $wgRequest->getText( 'wpEditToken' );
61 # Flag to hide all contents of the archived revisions
62 $suppress = $wgRequest->getVal( 'wpSuppress' ) && $wgUser->isAllowed('suppressrevision');
63
64 if( $this->oldimage ) {
65 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
66 }
67
68 if( !self::haveDeletableFile($this->file, $this->oldfile, $this->oldimage) ) {
69 $wgOut->addHTML( $this->prepareMessage( 'filedelete-nofile' ) );
70 $wgOut->addReturnTo( $this->title );
71 return;
72 }
73
74 // Perform the deletion if appropriate
75 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
76 $deleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
77 $deleteReason = $wgRequest->getText( 'wpReason' );
78
79 if ( $deleteReasonList == 'other' ) {
80 $reason = $deleteReason;
81 } elseif ( $deleteReason != '' ) {
82 // Entry from drop down menu + additional comment
83 $reason = $deleteReasonList . wfMsgForContent( 'colon-separator' ) . $deleteReason;
84 } else {
85 $reason = $deleteReasonList;
86 }
87
88 $status = self::doDelete( $this->title, $this->file, $this->oldimage, $reason, $suppress, $wgUser );
89
90 if( !$status->isGood() ) {
91 $wgOut->addHTML( '<h2>' . $this->prepareMessage( 'filedeleteerror-short' ) . "</h2>\n" );
92 $wgOut->addHTML( '<span class="error">' );
93 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
94 $wgOut->addHTML( '</span>' );
95 }
96 if( $status->ok ) {
97 $wgOut->setPageTitle( wfMessage( 'actioncomplete' ) );
98 $wgOut->addHTML( $this->prepareMessage( 'filedelete-success' ) );
99 // Return to the main page if we just deleted all versions of the
100 // file, otherwise go back to the description page
101 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
102
103 if ( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
104 WatchAction::doWatch( $this->title, $wgUser );
105 } elseif ( $this->title->userIsWatching() ) {
106 WatchAction::doUnwatch( $this->title, $wgUser );
107 }
108 }
109 return;
110 }
111
112 $this->showForm();
113 $this->showLogEntries();
114 }
115
116 /**
117 * Really delete the file
118 *
119 * @param $title Title object
120 * @param $file File object
121 * @param $oldimage String: archive name
122 * @param $reason String: reason of the deletion
123 * @param $suppress Boolean: whether to mark all deleted versions as restricted
124 * @param $user User object performing the request
125 */
126 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
127 if ( $user === null ) {
128 global $wgUser;
129 $user = $wgUser;
130 }
131
132 if( $oldimage ) {
133 $page = null;
134 $status = $file->deleteOld( $oldimage, $reason, $suppress );
135 if( $status->ok ) {
136 // Need to do a log item
137 $log = new LogPage( 'delete' );
138 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
139 if( trim( $reason ) != '' ) {
140 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
141 }
142 $log->addEntry( 'delete', $title, $logComment );
143 }
144 } else {
145 $status = Status::newFatal( 'error' );
146 $page = WikiPage::factory( $title );
147 $dbw = wfGetDB( DB_MASTER );
148 try {
149 // delete the associated article first
150 $error = '';
151 if ( $page->doDeleteArticle( $reason, $suppress, 0, false, $error, $user ) ) {
152 $status = $file->delete( $reason, $suppress );
153 if( $status->ok ) {
154 $dbw->commit();
155 } else {
156 $dbw->rollback();
157 }
158 }
159 } catch ( MWException $e ) {
160 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
161 $dbw->rollback();
162 throw $e;
163 }
164 }
165
166 if ( $status->isGood() ) {
167 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
168 }
169
170 return $status;
171 }
172
173 /**
174 * Show the confirmation form
175 */
176 private function showForm() {
177 global $wgOut, $wgUser, $wgRequest;
178
179 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
180 $suppress = "<tr id=\"wpDeleteSuppressRow\">
181 <td></td>
182 <td class='mw-input'><strong>" .
183 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
184 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
185 "</strong></td>
186 </tr>";
187 } else {
188 $suppress = '';
189 }
190
191 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
192 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
193 'id' => 'mw-img-deleteconfirm' ) ) .
194 Xml::openElement( 'fieldset' ) .
195 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
196 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
197 $this->prepareMessage( 'filedelete-intro' ) .
198 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
199 "<tr>
200 <td class='mw-label'>" .
201 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
202 "</td>
203 <td class='mw-input'>" .
204 Xml::listDropDown( 'wpDeleteReasonList',
205 wfMsgForContent( 'filedelete-reason-dropdown' ),
206 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
207 "</td>
208 </tr>
209 <tr>
210 <td class='mw-label'>" .
211 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
212 "</td>
213 <td class='mw-input'>" .
214 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
215 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
216 "</td>
217 </tr>
218 {$suppress}";
219 if( $wgUser->isLoggedIn() ) {
220 $form .= "
221 <tr>
222 <td></td>
223 <td class='mw-input'>" .
224 Xml::checkLabel( wfMsg( 'watchthis' ),
225 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
226 "</td>
227 </tr>";
228 }
229 $form .= "
230 <tr>
231 <td></td>
232 <td class='mw-submit'>" .
233 Xml::submitButton( wfMsg( 'filedelete-submit' ),
234 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
235 "</td>
236 </tr>" .
237 Xml::closeElement( 'table' ) .
238 Xml::closeElement( 'fieldset' ) .
239 Xml::closeElement( 'form' );
240
241 if ( $wgUser->isAllowed( 'editinterface' ) ) {
242 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
243 $link = Linker::link(
244 $title,
245 wfMsgHtml( 'filedelete-edit-reasonlist' ),
246 array(),
247 array( 'action' => 'edit' )
248 );
249 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
250 }
251
252 $wgOut->addHTML( $form );
253 }
254
255 /**
256 * Show deletion log fragments pertaining to the current file
257 */
258 private function showLogEntries() {
259 global $wgOut;
260 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
261 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
262 }
263
264 /**
265 * Prepare a message referring to the file being deleted,
266 * showing an appropriate message depending upon whether
267 * it's a current file or an old version
268 *
269 * @param $message String: message base
270 * @return String
271 */
272 private function prepareMessage( $message ) {
273 global $wgLang;
274 if( $this->oldimage ) {
275 return wfMsgExt(
276 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
277 'parse',
278 wfEscapeWikiText( $this->title->getText() ),
279 $wgLang->date( $this->getTimestamp(), true ),
280 $wgLang->time( $this->getTimestamp(), true ),
281 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) );
282 } else {
283 return wfMsgExt(
284 $message,
285 'parse',
286 wfEscapeWikiText( $this->title->getText() )
287 );
288 }
289 }
290
291 /**
292 * Set headers, titles and other bits
293 */
294 private function setHeaders() {
295 global $wgOut;
296 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
297 $wgOut->setRobotPolicy( 'noindex,nofollow' );
298 $wgOut->addBacklinkSubtitle( $this->title );
299 }
300
301 /**
302 * Is the provided `oldimage` value valid?
303 *
304 * @return bool
305 */
306 public static function isValidOldSpec($oldimage) {
307 return strlen( $oldimage ) >= 16
308 && strpos( $oldimage, '/' ) === false
309 && strpos( $oldimage, '\\' ) === false;
310 }
311
312 /**
313 * Could we delete the file specified? If an `oldimage`
314 * value was provided, does it correspond to an
315 * existing, local, old version of this file?
316 *
317 * @param $file File
318 * @param $oldfile File
319 * @param $oldimage File
320 * @return bool
321 */
322 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
323 return $oldimage
324 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
325 : $file && $file->exists() && $file->isLocal();
326 }
327
328 /**
329 * Prepare the form action
330 *
331 * @return string
332 */
333 private function getAction() {
334 $q = array();
335 $q['action'] = 'delete';
336
337 if( $this->oldimage )
338 $q['oldimage'] = $this->oldimage;
339
340 return $this->title->getLocalUrl( $q );
341 }
342
343 /**
344 * Extract the timestamp of the old version
345 *
346 * @return string
347 */
348 private function getTimestamp() {
349 return $this->oldfile->getTimestamp();
350 }
351 }