Revert r26281 for the moment. Big patch, changes several existing practices. Will...
[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 if( $this->oldimage && !$this->isValidOldSpec() ) {
52 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage ) );
53 return;
54 }
55 if( $this->oldimage )
56 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
57
58 if( !$this->haveDeletableFile() ) {
59 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
60 $wgOut->addReturnTo( $this->title );
61 return;
62 }
63
64 // Perform the deletion if appropriate
65 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage ) ) {
66 $comment = $wgRequest->getText( 'wpReason' );
67 if( $this->oldimage ) {
68 $status = $this->file->deleteOld( $this->oldimage, $comment );
69 if( $status->ok ) {
70 // Need to do a log item
71 $log = new LogPage( 'delete' );
72 $logComment = wfMsg( 'deletedrevision', $this->oldimage );
73 if( trim( $comment ) != '' )
74 $logComment .= ": {$comment}";
75 $log->addEntry( 'delete', $this->title, $logComment );
76 }
77 } else {
78 $status = $this->file->delete( $comment );
79 if( $status->ok ) {
80 // Need to delete the associated article
81 $article = new Article( $this->title );
82 $article->doDeleteArticle( $comment );
83 }
84 }
85 if( !$status->isGood() )
86 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
87 if( $status->ok ) {
88 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
89 // Return to the main page if we just deleted all versions of the
90 // file, otherwise go back to the description page
91 $wgOut->addReturnTo( $this->oldimage ? $this->title : Title::newMainPage() );
92 }
93 return;
94 }
95
96 $this->showForm();
97 $this->showLogEntries();
98 }
99
100 /**
101 * Show the confirmation form
102 */
103 private function showForm() {
104 global $wgOut, $wgUser, $wgRequest;
105
106 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
107 $form .= Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage ) );
108 $form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
109 $form .= $this->prepareMessage( 'filedelete-intro' );
110
111 $form .= '<p>' . Xml::inputLabel( wfMsg( 'filedelete-comment' ), 'wpReason', 'wpReason',
112 60, $wgRequest->getText( 'wpReason' ) ) . '</p>';
113 $form .= '<p>' . Xml::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
114 $form .= '</fieldset>';
115 $form .= '</form>';
116
117 $wgOut->addHtml( $form );
118 }
119
120 /**
121 * Show deletion log fragments pertaining to the current file
122 */
123 private function showLogEntries() {
124 global $wgOut;
125 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage::logName( 'delete' ) ) . "</h2>\n" );
126 $reader = new LogViewer(
127 new LogReader(
128 new FauxRequest(
129 array(
130 'type' => 'delete',
131 'page' => $this->title->getPrefixedText(),
132 )
133 )
134 )
135 );
136 $reader->showList( $wgOut );
137 }
138
139 /**
140 * Prepare a message referring to the file being deleted,
141 * showing an appropriate message depending upon whether
142 * it's a current file or an old version
143 *
144 * @param string $message Message base
145 * @return string
146 */
147 private function prepareMessage( $message ) {
148 global $wgLang, $wgServer;
149 if( $this->oldimage ) {
150 return wfMsgExt(
151 "{$message}-old",
152 'parse',
153 $this->title->getText(),
154 $wgLang->date( $this->getTimestamp(), true ),
155 $wgLang->time( $this->getTimestamp(), true ),
156 $wgServer . $this->file->getArchiveUrl( $this->oldimage )
157 );
158 } else {
159 return wfMsgExt(
160 $message,
161 'parse',
162 $this->title->getText()
163 );
164 }
165 }
166
167 /**
168 * Set headers, titles and other bits
169 */
170 private function setHeaders() {
171 global $wgOut, $wgUser;
172 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title->getText() ) );
173 $wgOut->setRobotPolicy( 'noindex,nofollow' );
174 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
175 }
176
177 /**
178 * Is the provided `oldimage` value valid?
179 *
180 * @return bool
181 */
182 private function isValidOldSpec() {
183 return strlen( $this->oldimage ) >= 16
184 && strpos( $this->oldimage, '/' ) === false
185 && strpos( $this->oldimage, '\\' ) === false;
186 }
187
188 /**
189 * Could we delete the file specified? If an `oldimage`
190 * value was provided, does it correspond to an
191 * existing, local, old version of this file?
192 *
193 * @return bool
194 */
195 private function haveDeletableFile() {
196 return $this->oldimage
197 ? $this->oldfile && $this->oldfile->exists() && $this->oldfile->isLocal()
198 : $this->file && $this->file->exists() && $this->file->isLocal();
199 }
200
201 /**
202 * Prepare the form action
203 *
204 * @return string
205 */
206 private function getAction() {
207 $q = array();
208 $q[] = 'action=delete';
209 if( $this->oldimage )
210 $q[] = 'oldimage=' . urlencode( $this->oldimage );
211 return $this->title->getLocalUrl( implode( '&', $q ) );
212 }
213
214 /**
215 * Extract the timestamp of the old version
216 *
217 * @return string
218 */
219 private function getTimestamp() {
220 return $this->oldfile->getTimestamp();
221 }
222
223 }