Localisation updates for core messages from Betawiki (2008-03-14 18:02 CET)
[lhc/web/wiklou.git] / includes / FileDeleteForm.php
1 <?php
2
3 /**
4 * File deletion user interface
5 *
6 * @addtogroup 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 } elseif( !$wgUser->isLoggedIn() ) {
39 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
40 return;
41 } elseif( !$wgUser->isAllowed( 'delete' ) ) {
42 $wgOut->permissionRequired( 'delete' );
43 return;
44 } elseif( $wgUser->isBlocked() ) {
45 $wgOut->blockedPage();
46 return;
47 }
48
49 $this->oldimage = $wgRequest->getText( 'oldimage', false );
50 $token = $wgRequest->getText( 'wpEditToken' );
51 if( $this->oldimage && !$this->isValidOldSpec() ) {
52 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage ) );
53 return;
54 }
55 if( $this->oldimage )
56 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
57
58 if( !$this->haveDeletableFile() ) {
59 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
60 $wgOut->addReturnTo( $this->title );
61 return;
62 }
63
64 // Perform the deletion if appropriate
65 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
66 $this->DeleteReasonList = $wgRequest->getText( 'wpDeleteReasonList' );
67 $this->DeleteReason = $wgRequest->getText( 'wpReason' );
68 $reason = $this->DeleteReasonList;
69 if ( $reason != 'other' && $this->DeleteReason != '') {
70 // Entry from drop down menu + additional comment
71 $reason .= ': ' . $this->DeleteReason;
72 } elseif ( $reason == 'other' ) {
73 $reason = $this->DeleteReason;
74 }
75
76 $article = null;
77 if( $this->oldimage ) {
78 $status = $this->file->deleteOld( $this->oldimage, $reason );
79 if( $status->ok ) {
80 // Need to do a log item
81 $log = new LogPage( 'delete' );
82 $logComment = wfMsgForContent( 'deletedrevision', $this->oldimage );
83 if( trim( $reason ) != '' )
84 $logComment .= ": {$reason}";
85 $log->addEntry( 'delete', $this->title, $logComment );
86 }
87 } else {
88 $status = $this->file->delete( $reason );
89 if( $status->ok ) {
90 // Need to delete the associated article
91 $article = new Article( $this->title );
92 $article->doDeleteArticle( $reason );
93 }
94 }
95 if( $status->isGood() ) wfRunHooks('FileDeleteComplete', array(
96 &$this->file, &$this->oldimage, &$article, &$wgUser, &$reason));
97
98 if( !$status->isGood() )
99 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
100 if( $status->ok ) {
101 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
102 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
103 // Return to the main page if we just deleted all versions of the
104 // file, otherwise go back to the description page
105 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
106 }
107 return;
108 }
109
110 $this->showForm();
111 $this->showLogEntries();
112 }
113
114 /**
115 * Show the confirmation form
116 */
117 private function showForm() {
118 global $wgOut, $wgUser, $wgRequest, $wgContLang;
119 $align = $wgContLang->isRtl() ? 'left' : 'right';
120
121 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) ) .
122 Xml::openElement( 'fieldset' ) .
123 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
124 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
125 $this->prepareMessage( 'filedelete-intro' ) .
126 Xml::openElement( 'table' ) .
127 "<tr>
128 <td align='$align'>" .
129 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
130 "</td>
131 <td>" .
132 Xml::listDropDown( 'wpDeleteReasonList',
133 wfMsgForContent( 'filedelete-reason-dropdown' ),
134 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
135 "</td>
136 </tr>
137 <tr>
138 <td align='$align'>" .
139 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
140 "</td>
141 <td>" .
142 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
143 "</td>
144 </tr>
145 <tr>
146 <td></td>
147 <td>" .
148 Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
149 "</td>
150 </tr>" .
151 Xml::closeElement( 'table' ) .
152 Xml::closeElement( 'fieldset' ) .
153 Xml::closeElement( 'form' );
154
155 if ( $wgUser->isAllowed( 'editinterface' ) ) {
156 $skin = $wgUser->getSkin();
157 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
158 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
159 }
160
161 $wgOut->addHtml( $form );
162 }
163
164 /**
165 * Show deletion log fragments pertaining to the current file
166 */
167 private function showLogEntries() {
168 global $wgOut;
169 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
170 $reader = new LogViewer(
171 new LogReader(
172 new FauxRequest(
173 array(
174 'type' => 'delete',
175 'page' => $this->title->getPrefixedText(),
176 )
177 )
178 )
179 );
180 $reader->showList( $wgOut );
181 }
182
183 /**
184 * Prepare a message referring to the file being deleted,
185 * showing an appropriate message depending upon whether
186 * it's a current file or an old version
187 *
188 * @param string $message Message base
189 * @return string
190 */
191 private function prepareMessage( $message ) {
192 global $wgLang;
193 if( $this->oldimage ) {
194 $url = $this->file->getArchiveUrl( $this->oldimage );
195 return wfMsgExt(
196 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
197 'parse',
198 $this->title->getText(),
199 $wgLang->date( $this->getTimestamp(), true ),
200 $wgLang->time( $this->getTimestamp(), true ),
201 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
202 } else {
203 return wfMsgExt(
204 $message,
205 'parse',
206 $this->title->getText()
207 );
208 }
209 }
210
211 /**
212 * Set headers, titles and other bits
213 */
214 private function setHeaders() {
215 global $wgOut, $wgUser;
216 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
217 $wgOut->setRobotPolicy( 'noindex,nofollow' );
218 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
219 }
220
221 /**
222 * Is the provided `oldimage` value valid?
223 *
224 * @return bool
225 */
226 private function isValidOldSpec() {
227 return strlen( $this->oldimage ) >= 16
228 && strpos( $this->oldimage, '/' ) === false
229 && strpos( $this->oldimage, '\\' ) === false;
230 }
231
232 /**
233 * Could we delete the file specified? If an `oldimage`
234 * value was provided, does it correspond to an
235 * existing, local, old version of this file?
236 *
237 * @return bool
238 */
239 private function haveDeletableFile() {
240 return $this->oldimage
241 ? $this->oldfile && $this->oldfile->exists() && $this->oldfile->isLocal()
242 : $this->file && $this->file->exists() && $this->file->isLocal();
243 }
244
245 /**
246 * Prepare the form action
247 *
248 * @return string
249 */
250 private function getAction() {
251 $q = array();
252 $q[] = 'action=delete';
253 if( $this->oldimage )
254 $q[] = 'oldimage=' . urlencode( $this->oldimage );
255 return $this->title->getLocalUrl( implode( '&', $q ) );
256 }
257
258 /**
259 * Extract the timestamp of the old version
260 *
261 * @return string
262 */
263 private function getTimestamp() {
264 return $this->oldfile->getTimestamp();
265 }
266
267 }