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