Removed configuration storage in the MediaWiki class:
[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 $dbw = wfGetDB( DB_MASTER );
122 try {
123 // delete the associated article first
124 if( $article->doDeleteArticle( $reason, $suppress, $id, false ) ) {
125 global $wgRequest;
126 if( $wgRequest->getCheck( 'wpWatch' ) && $wgUser->isLoggedIn() ) {
127 Action::factory( 'watch', $article )->execute();
128 } elseif( $title->userIsWatching() ) {
129 Action::factory( 'unwatch', $article )->execute();
130 }
131 $status = $file->delete( $reason, $suppress );
132 if( $status->ok ) {
133 $dbw->commit();
134 wfRunHooks( 'ArticleDeleteComplete', array( &$article, &$wgUser, $reason, $id ) );
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 $skin = $wgUser->getSkin();
221 $title = Title::makeTitle( NS_MEDIAWIKI, 'Filedelete-reason-dropdown' );
222 $link = $skin->link(
223 $title,
224 wfMsgHtml( 'filedelete-edit-reasonlist' ),
225 array(),
226 array( 'action' => 'edit' )
227 );
228 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
229 }
230
231 $wgOut->addHTML( $form );
232 }
233
234 /**
235 * Show deletion log fragments pertaining to the current file
236 */
237 private function showLogEntries() {
238 global $wgOut;
239 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
240 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
241 }
242
243 /**
244 * Prepare a message referring to the file being deleted,
245 * showing an appropriate message depending upon whether
246 * it's a current file or an old version
247 *
248 * @param $message String: message base
249 * @return String
250 */
251 private function prepareMessage( $message ) {
252 global $wgLang;
253 if( $this->oldimage ) {
254 return wfMsgExt(
255 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
256 'parse',
257 $this->title->getText(),
258 $wgLang->date( $this->getTimestamp(), true ),
259 $wgLang->time( $this->getTimestamp(), true ),
260 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
261 } else {
262 return wfMsgExt(
263 $message,
264 'parse',
265 $this->title->getText()
266 );
267 }
268 }
269
270 /**
271 * Set headers, titles and other bits
272 */
273 private function setHeaders() {
274 global $wgOut, $wgUser;
275 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
276 $wgOut->setRobotPolicy( 'noindex,nofollow' );
277 $wgOut->setSubtitle( wfMsg(
278 'filedelete-backlink',
279 $wgUser->getSkin()->link(
280 $this->title,
281 null,
282 array(),
283 array(),
284 array( 'known', 'noclasses' )
285 )
286 ) );
287 }
288
289 /**
290 * Is the provided `oldimage` value valid?
291 *
292 * @return bool
293 */
294 public static function isValidOldSpec($oldimage) {
295 return strlen( $oldimage ) >= 16
296 && strpos( $oldimage, '/' ) === false
297 && strpos( $oldimage, '\\' ) === false;
298 }
299
300 /**
301 * Could we delete the file specified? If an `oldimage`
302 * value was provided, does it correspond to an
303 * existing, local, old version of this file?
304 *
305 * @return bool
306 */
307 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
308 return $oldimage
309 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
310 : $file && $file->exists() && $file->isLocal();
311 }
312
313 /**
314 * Prepare the form action
315 *
316 * @return string
317 */
318 private function getAction() {
319 $q = array();
320 $q['action'] = 'delete';
321
322 if( $this->oldimage )
323 $q['oldimage'] = $this->oldimage;
324
325 return $this->title->getLocalUrl( $q );
326 }
327
328 /**
329 * Extract the timestamp of the old version
330 *
331 * @return string
332 */
333 private function getTimestamp() {
334 return $this->oldfile->getTimestamp();
335 }
336 }