"' onload='send_me_your_credit_card_info()" would be an interesting page name, don...
[mediawiki.git] / includes / FileDeleteForm.php
blob6c257e54e5912601e0fca7955194054dca315d97
1 <?php
3 /**
4 * File deletion user interface
6 * @addtogroup Media
7 * @author Rob Church <robchur@gmail.com>
8 */
9 class FileDeleteForm {
11 private $title = null;
12 private $file = null;
14 private $oldfile = null;
15 private $oldimage = '';
17 /**
18 * Constructor
20 * @param File $file File we're deleting
22 public function __construct( $file ) {
23 $this->title = $file->getTitle();
24 $this->file = $file;
27 /**
28 * Fulfil the request; shows the form or deletes the file,
29 * pending authentication, confirmation, etc.
31 public function execute() {
32 global $wgOut, $wgRequest, $wgUser;
33 $this->setHeaders();
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;
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;
55 if( $this->oldimage )
56 $this->oldfile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->oldimage );
58 if( !$this->haveDeletableFile() ) {
59 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
60 $wgOut->addReturnTo( $this->title );
61 return;
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 );
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 );
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() );
93 return;
96 $this->showForm();
97 $this->showLogEntries();
101 * Show the confirmation form
103 private function showForm() {
104 global $wgOut, $wgUser, $wgRequest;
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' );
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>';
117 $wgOut->addHtml( $form );
121 * Show deletion log fragments pertaining to the current file
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(),
136 $reader->showList( $wgOut );
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
144 * @param string $message Message base
145 * @return string
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 )
158 } else {
159 return wfMsgExt(
160 $message,
161 'parse',
162 $this->title->getText()
168 * Set headers, titles and other bits
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 ) ) );
178 * Is the provided `oldimage` value valid?
180 * @return bool
182 private function isValidOldSpec() {
183 return strlen( $this->oldimage ) >= 16
184 && strpos( $this->oldimage, '/' ) === false
185 && strpos( $this->oldimage, '\\' ) === false;
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?
193 * @return bool
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();
202 * Prepare the form action
204 * @return string
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 ) );
215 * Extract the timestamp of the old version
217 * @return string
219 private function getTimestamp() {
220 return $this->oldfile->getTimestamp();