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