Fixed broken HTML
[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 = wfMsg( '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;
114
115 $mDeletereasonother = Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' );
116 $mDeletereasonotherlist = wfMsgHtml( 'filedelete-reason-otherlist' );
117 $scDeleteReasonList = wfMsgForContent( 'filedelete-reason-dropdown' );
118 $mDeleteReasonList = '';
119 $delcom = Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' );
120 if ( $scDeleteReasonList != '' && $scDeleteReasonList != '-' ) {
121 $deleteReasonList = "<option value=\"other\">$mDeletereasonotherlist</option>";
122 $optgroup = "";
123 foreach ( explode( "\n", $scDeleteReasonList ) as $option) {
124 $value = trim( htmlspecialchars($option) );
125 if ( $value == '' ) {
126 continue;
127 } elseif ( substr( $value, 0, 1) == '*' && substr( $value, 1, 1) != '*' ) {
128 // A new group is starting ...
129 $value = trim( substr( $value, 1 ) );
130 $deleteReasonList .= "$optgroup<optgroup label=\"$value\">";
131 $optgroup = "</optgroup>";
132 } elseif ( substr( $value, 0, 2) == '**' ) {
133 // groupmember
134 $selected = "";
135 $value = trim( substr( $value, 2 ) );
136 if ( $mDeleteReasonList === $value)
137 $selected = ' selected="selected"';
138 $deleteReasonList .= "<option value=\"$value\"$selected>$value</option>";
139 } else {
140 // groupless delete reason
141 $selected = "";
142 if ( $this->DeleteReasonList === $value)
143 $selected = ' selected="selected"';
144 $deleteReasonList .= "$optgroup<option value=\"$value\"$selected>$value</option>";
145 $optgroup = "";
146 }
147 }
148 $deleteReasonList .= $optgroup;
149 }
150
151 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
152 $form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
153 $form .= Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) );
154 $form .= '<table><tr><td colspan="2">';
155 $form .= $this->prepareMessage( 'filedelete-intro' );
156 $form .= "</td></tr><tr><td align=\"right\"> $delcom </td><td align=\"left\">";
157 $form .= "<select tabindex='2' id='wpDeleteReasonList' name=\"wpDeleteReasonList\">
158 $deleteReasonList
159 </select>";
160 $form .= "</td></tr><tr><td align=\"right\"> $mDeletereasonother </td><td align=\"left\">";
161 $form .= "<input type='text' maxlength='255' size='60' name='wpReason' id='wpReason' ";
162 $form .= "value=\"". htmlspecialchars( $wgRequest->getText( 'wpReason' ) ) ."\" tabindex=\"1\" />";
163 $form .= '</td></tr><tr><td colspan="2">';
164 $form .= '<p>' . Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
165 $form .= '</td></tr></table>';
166 $form .= '</fieldset>';
167 $form .= '</form>';
168
169 $wgOut->addHtml( $form );
170 }
171
172 /**
173 * Show deletion log fragments pertaining to the current file
174 */
175 private function showLogEntries() {
176 global $wgOut;
177 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
178 $reader = new LogViewer(
179 new LogReader(
180 new FauxRequest(
181 array(
182 'type' => 'delete',
183 'page' => $this->title->getPrefixedText(),
184 )
185 )
186 )
187 );
188 $reader->showList( $wgOut );
189 }
190
191 /**
192 * Prepare a message referring to the file being deleted,
193 * showing an appropriate message depending upon whether
194 * it's a current file or an old version
195 *
196 * @param string $message Message base
197 * @return string
198 */
199 private function prepareMessage( $message ) {
200 global $wgLang, $wgServer;
201 if( $this->oldimage ) {
202 return wfMsgExt(
203 "{$message}-old",
204 'parse',
205 $this->title->getText(),
206 $wgLang->date( $this->getTimestamp(), true ),
207 $wgLang->time( $this->getTimestamp(), true ),
208 $wgServer . $this->file->getArchiveUrl( $this->oldimage )
209 );
210 } else {
211 return wfMsgExt(
212 $message,
213 'parse',
214 $this->title->getText()
215 );
216 }
217 }
218
219 /**
220 * Set headers, titles and other bits
221 */
222 private function setHeaders() {
223 global $wgOut, $wgUser;
224 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
225 $wgOut->setRobotPolicy( 'noindex,nofollow' );
226 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
227 }
228
229 /**
230 * Is the provided `oldimage` value valid?
231 *
232 * @return bool
233 */
234 private function isValidOldSpec() {
235 return strlen( $this->oldimage ) >= 16
236 && strpos( $this->oldimage, '/' ) === false
237 && strpos( $this->oldimage, '\\' ) === false;
238 }
239
240 /**
241 * Could we delete the file specified? If an `oldimage`
242 * value was provided, does it correspond to an
243 * existing, local, old version of this file?
244 *
245 * @return bool
246 */
247 private function haveDeletableFile() {
248 return $this->oldimage
249 ? $this->oldfile && $this->oldfile->exists() && $this->oldfile->isLocal()
250 : $this->file && $this->file->exists() && $this->file->isLocal();
251 }
252
253 /**
254 * Prepare the form action
255 *
256 * @return string
257 */
258 private function getAction() {
259 $q = array();
260 $q[] = 'action=delete';
261 if( $this->oldimage )
262 $q[] = 'oldimage=' . urlencode( $this->oldimage );
263 return $this->title->getLocalUrl( implode( '&', $q ) );
264 }
265
266 /**
267 * Extract the timestamp of the old version
268 *
269 * @return string
270 */
271 private function getTimestamp() {
272 return $this->oldfile->getTimestamp();
273 }
274
275 }