* r113886: use <div> instead of span to wrap element that has p tags.
[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( '<div class="error">' );
93 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
94 $wgOut->addHTML( '</div>' );
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: 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 * @return bool|Status
126 */
127 public static function doDelete( &$title, &$file, &$oldimage, $reason, $suppress, User $user = null ) {
128 if ( $user === null ) {
129 global $wgUser;
130 $user = $wgUser;
131 }
132
133 if( $oldimage ) {
134 $page = null;
135 $status = $file->deleteOld( $oldimage, $reason, $suppress );
136 if( $status->ok ) {
137 // Need to do a log item
138 $log = new LogPage( 'delete' );
139 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
140 if( trim( $reason ) != '' ) {
141 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
142 }
143 $log->addEntry( 'delete', $title, $logComment );
144 }
145 } else {
146 $status = Status::newFatal( 'cannotdelete',
147 wfEscapeWikiText( $title->getPrefixedText() )
148 );
149 $page = WikiPage::factory( $title );
150 $dbw = wfGetDB( DB_MASTER );
151 try {
152 // delete the associated article first
153 $error = '';
154 if ( $page->doDeleteArticleReal( $reason, $suppress, 0, false, $error, $user ) >= WikiPage::DELETE_SUCCESS ) {
155 $status = $file->delete( $reason, $suppress );
156 if( $status->isOK() ) {
157 $dbw->commit( __METHOD__ );
158 } else {
159 $dbw->rollback( __METHOD__ );
160 }
161 }
162 } catch ( MWException $e ) {
163 // rollback before returning to prevent UI from displaying incorrect "View or restore N deleted edits?"
164 $dbw->rollback( __METHOD__ );
165 throw $e;
166 }
167 }
168
169 if ( $status->isOK() ) {
170 wfRunHooks( 'FileDeleteComplete', array( &$file, &$oldimage, &$page, &$user, &$reason ) );
171 }
172
173 return $status;
174 }
175
176 /**
177 * Show the confirmation form
178 */
179 private function showForm() {
180 global $wgOut, $wgUser, $wgRequest;
181
182 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
183 $suppress = "<tr id=\"wpDeleteSuppressRow\">
184 <td></td>
185 <td class='mw-input'><strong>" .
186 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
187 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
188 "</strong></td>
189 </tr>";
190 } else {
191 $suppress = '';
192 }
193
194 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
195 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
196 'id' => 'mw-img-deleteconfirm' ) ) .
197 Xml::openElement( 'fieldset' ) .
198 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
199 Html::hidden( 'wpEditToken', $wgUser->getEditToken( $this->oldimage ) ) .
200 $this->prepareMessage( 'filedelete-intro' ) .
201 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
202 "<tr>
203 <td class='mw-label'>" .
204 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
205 "</td>
206 <td class='mw-input'>" .
207 Xml::listDropDown( 'wpDeleteReasonList',
208 wfMsgForContent( 'filedelete-reason-dropdown' ),
209 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
210 "</td>
211 </tr>
212 <tr>
213 <td class='mw-label'>" .
214 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
215 "</td>
216 <td class='mw-input'>" .
217 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
218 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
219 "</td>
220 </tr>
221 {$suppress}";
222 if( $wgUser->isLoggedIn() ) {
223 $form .= "
224 <tr>
225 <td></td>
226 <td class='mw-input'>" .
227 Xml::checkLabel( wfMsg( 'watchthis' ),
228 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
229 "</td>
230 </tr>";
231 }
232 $form .= "
233 <tr>
234 <td></td>
235 <td class='mw-submit'>" .
236 Xml::submitButton( wfMsg( 'filedelete-submit' ),
237 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
238 "</td>
239 </tr>" .
240 Xml::closeElement( 'table' ) .
241 Xml::closeElement( 'fieldset' ) .
242 Xml::closeElement( 'form' );
243
244 if ( $wgUser->isAllowed( 'editinterface' ) ) {
245 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
246 $link = Linker::link(
247 $title,
248 wfMsgHtml( 'filedelete-edit-reasonlist' ),
249 array(),
250 array( 'action' => 'edit' )
251 );
252 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
253 }
254
255 $wgOut->addHTML( $form );
256 }
257
258 /**
259 * Show deletion log fragments pertaining to the current file
260 */
261 private function showLogEntries() {
262 global $wgOut;
263 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
264 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title );
265 }
266
267 /**
268 * Prepare a message referring to the file being deleted,
269 * showing an appropriate message depending upon whether
270 * it's a current file or an old version
271 *
272 * @param $message String: message base
273 * @return String
274 */
275 private function prepareMessage( $message ) {
276 global $wgLang;
277 if( $this->oldimage ) {
278 return wfMsgExt(
279 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
280 'parse',
281 wfEscapeWikiText( $this->title->getText() ),
282 $wgLang->date( $this->getTimestamp(), true ),
283 $wgLang->time( $this->getTimestamp(), true ),
284 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ), PROTO_CURRENT ) );
285 } else {
286 return wfMsgExt(
287 $message,
288 'parse',
289 wfEscapeWikiText( $this->title->getText() )
290 );
291 }
292 }
293
294 /**
295 * Set headers, titles and other bits
296 */
297 private function setHeaders() {
298 global $wgOut;
299 $wgOut->setPageTitle( wfMessage( 'filedelete', $this->title->getText() ) );
300 $wgOut->setRobotPolicy( 'noindex,nofollow' );
301 $wgOut->addBacklinkSubtitle( $this->title );
302 }
303
304 /**
305 * Is the provided `oldimage` value valid?
306 *
307 * @return bool
308 */
309 public static function isValidOldSpec($oldimage) {
310 return strlen( $oldimage ) >= 16
311 && strpos( $oldimage, '/' ) === false
312 && strpos( $oldimage, '\\' ) === false;
313 }
314
315 /**
316 * Could we delete the file specified? If an `oldimage`
317 * value was provided, does it correspond to an
318 * existing, local, old version of this file?
319 *
320 * @param $file File
321 * @param $oldfile File
322 * @param $oldimage File
323 * @return bool
324 */
325 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
326 return $oldimage
327 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
328 : $file && $file->exists() && $file->isLocal();
329 }
330
331 /**
332 * Prepare the form action
333 *
334 * @return string
335 */
336 private function getAction() {
337 $q = array();
338 $q['action'] = 'delete';
339
340 if( $this->oldimage )
341 $q['oldimage'] = $this->oldimage;
342
343 return $this->title->getLocalUrl( $q );
344 }
345
346 /**
347 * Extract the timestamp of the old version
348 *
349 * @return string
350 */
351 private function getTimestamp() {
352 return $this->oldfile->getTimestamp();
353 }
354 }