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