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