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