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