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