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