fix synxtax
[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 if( $this->oldimage ) {
76 $status = $this->file->deleteOld( $this->oldimage, $reason );
77 if( $status->ok ) {
78 // Need to do a log item
79 $log = new LogPage( 'delete' );
80 $logComment = wfMsgForContent( 'deletedrevision', $this->oldimage );
81 if( trim( $reason ) != '' )
82 $logComment .= ": {$reason}";
83 $log->addEntry( 'delete', $this->title, $logComment );
84 }
85 } else {
86 $status = $this->file->delete( $reason );
87 if( $status->ok ) {
88 // Need to delete the associated article
89 $article = new Article( $this->title );
90 $article->doDeleteArticle( $reason );
91 }
92 }
93 if( !$status->isGood() )
94 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
95 if( $status->ok ) {
96 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
97 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
98 // Return to the main page if we just deleted all versions of the
99 // file, otherwise go back to the description page
100 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
101 }
102 return;
103 }
104
105 $this->showForm();
106 $this->showLogEntries();
107 }
108
109 /**
110 * Show the confirmation form
111 */
112 private function showForm() {
113 global $wgOut, $wgUser, $wgRequest, $wgContLang;
114 $align = $wgContLang->isRtl() ? 'left' : 'right';
115
116 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) ) .
117 Xml::openElement( 'fieldset' ) .
118 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
119 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
120 $this->prepareMessage( 'filedelete-intro' ) .
121 Xml::openElement( 'table' ) .
122 "<tr>
123 <td align='$align'>" .
124 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
125 "</td>
126 <td>" .
127 Xml::listDropDown( 'wpDeleteReasonList',
128 wfMsgForContent( 'filedelete-reason-dropdown' ),
129 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
130 "</td>
131 </tr>
132 <tr>
133 <td align='$align'>" .
134 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
135 "</td>
136 <td>" .
137 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
138 "</td>
139 </tr>
140 <tr>
141 <td></td>
142 <td>" .
143 Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
144 "</td>
145 </tr>" .
146 Xml::closeElement( 'table' ) .
147 Xml::closeElement( 'fieldset' ) .
148 Xml::closeElement( 'form' );
149
150 $wgOut->addHtml( $form );
151 }
152
153 /**
154 * Show deletion log fragments pertaining to the current file
155 */
156 private function showLogEntries() {
157 global $wgOut;
158 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
159 $reader = new LogViewer(
160 new LogReader(
161 new FauxRequest(
162 array(
163 'type' => 'delete',
164 'page' => $this->title->getPrefixedText(),
165 )
166 )
167 )
168 );
169 $reader->showList( $wgOut );
170 }
171
172 /**
173 * Prepare a message referring to the file being deleted,
174 * showing an appropriate message depending upon whether
175 * it's a current file or an old version
176 *
177 * @param string $message Message base
178 * @return string
179 */
180 private function prepareMessage( $message ) {
181 global $wgLang;
182 if( $this->oldimage ) {
183 $url = $this->file->getArchiveUrl( $this->oldimage );
184 return wfMsgExt(
185 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
186 'parse',
187 $this->title->getText(),
188 $wgLang->date( $this->getTimestamp(), true ),
189 $wgLang->time( $this->getTimestamp(), true ),
190 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
191 } else {
192 return wfMsgExt(
193 $message,
194 'parse',
195 $this->title->getText()
196 );
197 }
198 }
199
200 /**
201 * Set headers, titles and other bits
202 */
203 private function setHeaders() {
204 global $wgOut, $wgUser;
205 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
206 $wgOut->setRobotPolicy( 'noindex,nofollow' );
207 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
208 }
209
210 /**
211 * Is the provided `oldimage` value valid?
212 *
213 * @return bool
214 */
215 private function isValidOldSpec() {
216 return strlen( $this->oldimage ) >= 16
217 && strpos( $this->oldimage, '/' ) === false
218 && strpos( $this->oldimage, '\\' ) === false;
219 }
220
221 /**
222 * Could we delete the file specified? If an `oldimage`
223 * value was provided, does it correspond to an
224 * existing, local, old version of this file?
225 *
226 * @return bool
227 */
228 private function haveDeletableFile() {
229 return $this->oldimage
230 ? $this->oldfile && $this->oldfile->exists() && $this->oldfile->isLocal()
231 : $this->file && $this->file->exists() && $this->file->isLocal();
232 }
233
234 /**
235 * Prepare the form action
236 *
237 * @return string
238 */
239 private function getAction() {
240 $q = array();
241 $q[] = 'action=delete';
242 if( $this->oldimage )
243 $q[] = 'oldimage=' . urlencode( $this->oldimage );
244 return $this->title->getLocalUrl( implode( '&', $q ) );
245 }
246
247 /**
248 * Extract the timestamp of the old version
249 *
250 * @return string
251 */
252 private function getTimestamp() {
253 return $this->oldfile->getTimestamp();
254 }
255
256 }