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