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