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