Some code cleanup per Nikerabbit
[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 .= ': ' . $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 if( wfRunHooks('ArticleDelete', array(&$article, &$wgUser, &$reason)) ) {
112 if( $article->doDeleteArticle( $reason, $suppress, $id ) )
113 wfRunHooks('ArticleDeleteComplete', array(&$article, &$wgUser, $reason, $id));
114 }
115 }
116 }
117 if( $status->isGood() )
118 wfRunHooks('FileDeleteComplete', array( &$file, &$oldimage, &$article, &$wgUser, &$reason));
119
120 return $status;
121 }
122
123 /**
124 * Show the confirmation form
125 */
126 private function showForm() {
127 global $wgOut, $wgUser, $wgRequest, $wgContLang;
128 $align = $wgContLang->isRtl() ? 'left' : 'right';
129
130 if( $wgUser->isAllowed( 'suppressrevision' ) ) {
131 $suppress = "<tr id=\"wpDeleteSuppressRow\" name=\"wpDeleteSuppressRow\"><td></td><td>";
132 $suppress .= Xml::checkLabel( wfMsg( 'revdelete-suppress' ), 'wpSuppress', 'wpSuppress', false, array( 'tabindex' => '2' ) );
133 $suppress .= "</td></tr>";
134 } else {
135 $suppress = '';
136 }
137
138 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction(), 'id' => 'mw-img-deleteconfirm' ) ) .
139 Xml::openElement( 'fieldset' ) .
140 Xml::element( 'legend', null, wfMsg( 'filedelete-legend' ) ) .
141 Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) ) .
142 $this->prepareMessage( 'filedelete-intro' ) .
143 Xml::openElement( 'table' ) .
144 "<tr>
145 <td align='$align'>" .
146 Xml::label( wfMsg( 'filedelete-comment' ), 'wpDeleteReasonList' ) .
147 "</td>
148 <td>" .
149 Xml::listDropDown( 'wpDeleteReasonList',
150 wfMsgForContent( 'filedelete-reason-dropdown' ),
151 wfMsgForContent( 'filedelete-reason-otherlist' ), '', 'wpReasonDropDown', 1 ) .
152 "</td>
153 </tr>
154 <tr>
155 <td align='$align'>" .
156 Xml::label( wfMsg( 'filedelete-otherreason' ), 'wpReason' ) .
157 "</td>
158 <td>" .
159 Xml::input( 'wpReason', 60, $wgRequest->getText( 'wpReason' ), array( 'type' => 'text', 'maxlength' => '255', 'tabindex' => '2', 'id' => 'wpReason' ) ) .
160 "</td>
161 </tr>
162 {$suppress}
163 <tr>
164 <td></td>
165 <td>" .
166 Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit', 'tabindex' => '3' ) ) .
167 "</td>
168 </tr>" .
169 Xml::closeElement( 'table' ) .
170 Xml::closeElement( 'fieldset' ) .
171 Xml::closeElement( 'form' );
172
173 if ( $wgUser->isAllowed( 'editinterface' ) ) {
174 $skin = $wgUser->getSkin();
175 $link = $skin->makeLink ( 'MediaWiki:Filedelete-reason-dropdown', wfMsgHtml( 'filedelete-edit-reasonlist' ) );
176 $form .= '<p class="mw-filedelete-editreasons">' . $link . '</p>';
177 }
178
179 $wgOut->addHtml( $form );
180 }
181
182 /**
183 * Show deletion log fragments pertaining to the current file
184 */
185 private function showLogEntries() {
186 global $wgOut;
187 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
188 LogEventsList::showLogExtract( $wgOut, 'delete', $this->title->getPrefixedText() );
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;
201 if( $this->oldimage ) {
202 $url = $this->file->getArchiveUrl( $this->oldimage );
203 return wfMsgExt(
204 "{$message}-old", # To ensure grep will find them: 'filedelete-intro-old', 'filedelete-nofile-old', 'filedelete-success-old'
205 'parse',
206 $this->title->getText(),
207 $wgLang->date( $this->getTimestamp(), true ),
208 $wgLang->time( $this->getTimestamp(), true ),
209 wfExpandUrl( $this->file->getArchiveUrl( $this->oldimage ) ) );
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 public static function isValidOldSpec($oldimage) {
235 return strlen( $oldimage ) >= 16
236 && strpos( $oldimage, '/' ) === false
237 && strpos( $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 public static function haveDeletableFile(&$file, &$oldfile, $oldimage) {
248 return $oldimage
249 ? $oldfile && $oldfile->exists() && $oldfile->isLocal()
250 : $file && $file->exists() && $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 }