(bug 22617), FileRepo::append() definition does not match child, change it to be...
[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 if( $oldimage ) {
106 $status = $file->deleteOld( $oldimage, $reason, $suppress );
107 if( $status->ok ) {
108 // Need to do a log item
109 $log = new LogPage( 'delete' );
110 $logComment = wfMsgForContent( 'deletedrevision', $oldimage );
111 if( trim( $reason ) != '' )
112 $logComment .= wfMsgForContent( 'colon-separator' ) . $reason;
113 $log->addEntry( 'delete', $title, $logComment );
114 }
115 } else {
116 $status = $file->delete( $reason, $suppress );
117 if( $status->ok ) {
118 $id = $title->getArticleID( GAID_FOR_UPDATE );
119 // Need to delete the associated article
120 $article = new Article( $title );
121 $error = '';
122 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason, &$error)) ) {
123 if( $article->doDeleteArticle( $reason, $suppress, $id ) ) {
124 global $wgRequest;
125 if( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
126 $article->doWatch();
127 } elseif( $title->userIsWatching() ) {
128 $article->doUnwatch();
129 }
130 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
131 }
132 }
133 }
134 }
135 if( $status->isGood() )
136 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
137
138 return $status;
139 }
140
141 /**
142 * Show the confirmation form
143 */
144 private function showForm() {
145 global $wgOut, $wgUser, $wgRequest;
146
147 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
148 $suppress = "<tr id=\"wpDeleteSuppressRow\">
149 <td></td>
150 <td class='mw-input'><strong>" .
151 Xml::checkLabel( wfMsg( 'revdelete-suppress' ),
152 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '3' ) ) .
153 "</strong></td>
154 </tr>";
155 } else {
156 $suppress = '';
157 }
158
159 $checkWatch = $wgUser->getBoolOption( 'watchdeletion' ) || $this->title->userIsWatching();
160 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(),
161 'id' => 'mw-img-deleteconfirm' ) ) .
162 Xml::openElement( 'fieldset' ) .
163 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
164 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
165 $this->prepareMessage( 'filedelete-intro' ) .
166 Xml::openElement( 'table', array( 'id' => 'mw-img-deleteconfirm-table' ) ) .
167 "<tr>
168 <td class='mw-label'>" .
169 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
170 "</td>
171 <td class='mw-input'>" .
172 Xml::listDropDown( 'wpDeleteReasonList',
173 wfMsgForContent( 'filedelete-reason-dropdown' ),
174 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
175 "</td>
176 </tr>
177 <tr>
178 <td class='mw-label'>" .
179 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
180 "</td>
181 <td class='mw-input'>" .
182 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ),
183 array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
184 "</td>
185 </tr>
186 {$suppress}";
187 if( $wgUser->isLoggedIn() ) {
188 $form .= "
189 <tr>
190 <td></td>
191 <td class='mw-input'>" .
192 Xml::checkLabel( wfMsg( 'watchthis' ),
193 'wpWatch', 'wpWatch', $checkWatch, array( 'tabindex' => '3' ) ) .
194 "</td>
195 </tr>";
196 }
197 $form .= "
198 <tr>
199 <td></td>
200 <td class='mw-submit'>" .
201 Xml::submitButton( wfMsg( 'filedelete-submit' ),
202 array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '4' ) ) .
203 "</td>
204 </tr>" .
205 Xml::closeElement( 'table' ) .
206 Xml::closeElement( 'fieldset' ) .
207 Xml::closeElement( 'form' );
208
209 if ( $wgUser->isAllowed( 'editinterface' ) ) {
210 $skin = $wgUser->getSkin();
211 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
212 $link = $skin->link(
213 $title,
214 wfMsgHtml( 'filedelete-edit-reasonlist' ),
215 array(),
216 array( 'action' => 'edit' )
217 );
218 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
219 }
220
221 $wgOut->addHTML( $form );
222 }
223
224 /**
225 * Show deletion log fragments pertaining to the current file
226 */
227 private function showLogEntries() {
228 global $wgOut;
229 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
230 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
231 }
232
233 /**
234 * Prepare a message referring to the file being deleted,
235 * showing an appropriate message depending upon whether
236 * it's a current file or an old version
237 *
238 * @param $message String: message base
239 * @return String
240 */
241 private function prepareMessage( $message ) {
242 global $wgLang;
243 if( $this->oldimage ) {
244 $url = $this->file->getArchiveUrl( $this->oldimage );
245 return wfMsgExt(
246 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
247 'parse',
248 $this->title->getText(),
249 $wgLang->date( $this->getTimestamp(), true ),
250 $wgLang->time( $this->getTimestamp(), true ),
251 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
252 } else {
253 return wfMsgExt(
254 $message,
255 'parse',
256 $this->title->getText()
257 );
258 }
259 }
260
261 /**
262 * Set headers, titles and other bits
263 */
264 private function setHeaders() {
265 global $wgOut, $wgUser;
266 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
267 $wgOut->setRobotPolicy( 'noindex,nofollow' );
268 $wgOut->setSubtitle( wfMsg(
269 'filedelete-backlink',
270 $wgUser->getSkin()->link(
271 $this->title,
272 null,
273 array(),
274 array(),
275 array( 'known', 'noclasses' )
276 )
277 ) );
278 }
279
280 /**
281 * Is the provided `oldimage` value valid?
282 *
283 * @return bool
284 */
285 public static function isValidOldSpec($oldimage) {
286 return strlen( $oldimage ) >= 16
287 && strpos( $oldimage, '/' ) === false
288 && strpos( $oldimage, '\\' ) === false;
289 }
290
291 /**
292 * Could we delete the file specified? If an `oldimage`
293 * value was provided, does it correspond to an
294 * existing, local, old version of this file?
295 *
296 * @return bool
297 */
298 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
299 return $oldimage
300 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
301 : $file && $file->exists() && $file->isLocal();
302 }
303
304 /**
305 * Prepare the form action
306 *
307 * @return string
308 */
309 private function getAction() {
310 $q = array();
311 $q['action'] = 'delete';
312
313 if( $this->oldimage )
314 $q['oldimage'] = $this->oldimage;
315
316 return $this->title->getLocalUrl( $q );
317 }
318
319 /**
320 * Extract the timestamp of the old version
321 *
322 * @return string
323 */
324 private function getTimestamp() {
325 return $this->oldfile->getTimestamp();
326 }
327 }