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