Follow-up to r51084:
[lhc/web/wiklou.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 $title = Title::newFromText( 'Filedelete-reason-dropdown', NS_MEDIAWIKI );
198 $link = $skin->link(
199 $title,
200 wfMsgHtml( 'filedelete-edit-reasonlist' ),
201 array(),
202 array( 'action' => 'edit' )
203 );
204 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
205 }
206
207 $wgOut->addHTML( $form );
208 }
209
210 /**
211 * Show deletion log fragments pertaining to the current file
212 */
213 private function showLogEntries() {
214 global $wgOut;
215 $wgOut->addHTML( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
216 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
217 }
218
219 /**
220 * Prepare a message referring to the file being deleted,
221 * showing an appropriate message depending upon whether
222 * it's a current file or an old version
223 *
224 * @param string $message Message base
225 * @return string
226 */
227 private function prepareMessage( $message ) {
228 global $wgLang;
229 if( $this->oldimage ) {
230 $url = $this->file->getArchiveUrl( $this->oldimage );
231 return wfMsgExt(
232 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
233 'parse',
234 $this->title->getText(),
235 $wgLang->date( $this->getTimestamp(), true ),
236 $wgLang->time( $this->getTimestamp(), true ),
237 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
238 } else {
239 return wfMsgExt(
240 $message,
241 'parse',
242 $this->title->getText()
243 );
244 }
245 }
246
247 /**
248 * Set headers, titles and other bits
249 */
250 private function setHeaders() {
251 global $wgOut, $wgUser;
252 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
253 $wgOut->setRobotPolicy( 'noindex,nofollow' );
254 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
255 }
256
257 /**
258 * Is the provided `oldimage` value valid?
259 *
260 * @return bool
261 */
262 public static function isValidOldSpec($oldimage) {
263 return strlen( $oldimage ) >= 16
264 && strpos( $oldimage, '/' ) === false
265 && strpos( $oldimage, '\\' ) === false;
266 }
267
268 /**
269 * Could we delete the file specified? If an `oldimage`
270 * value was provided, does it correspond to an
271 * existing, local, old version of this file?
272 *
273 * @return bool
274 */
275 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
276 return $oldimage
277 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
278 : $file && $file->exists() && $file->isLocal();
279 }
280
281 /**
282 * Prepare the form action
283 *
284 * @return string
285 */
286 private function getAction() {
287 $q = array();
288 $q[] = 'action=delete';
289 if( $this->oldimage )
290 $q[] = 'oldimage=' . urlencode( $this->oldimage );
291 return $this->title->getLocalUrl( implode( '&', $q ) );
292 }
293
294 /**
295 * Extract the timestamp of the old version
296 *
297 * @return string
298 */
299 private function getTimestamp() {
300 return $this->oldfile->getTimestamp();
301 }
302
303 }