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