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