4 * File deletion user interface
7 * @author Rob Church <robchur@gmail.com>
11 private $title = null;
13 private $oldimage = '';
18 * @param File $file File we're deleting
20 public function __construct( $file ) {
21 $this->title
= $file->getTitle();
26 * Fulfil the request; shows the form or deletes the file,
27 * pending authentication, confirmation, etc.
29 public function execute() {
30 global $wgOut, $wgRequest, $wgUser, $wgLang, $wgServer;
34 $wgOut->readOnlyPage();
36 } elseif( !$wgUser->isLoggedIn() ) {
37 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
39 } elseif( !$wgUser->isAllowed( 'delete' ) ) {
40 $wgOut->permissionError( 'delete' );
42 } elseif( $wgUser->isBlocked() ) {
43 $wgOut->blockedPage();
47 $this->oldimage
= $wgRequest->getText( 'oldimage', false );
48 $token = $wgRequest->getText( 'wpEditToken' );
49 if( $this->oldimage
&& !$this->isValidOldSpec() ) {
50 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage
) );
54 if( !$this->haveDeletableFile() ) {
55 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
56 $wgOut->addReturnTo( $this->title
);
60 // Don't allow accidental deletion of a single file revision
61 // if this is, in fact, the current revision; things might break
62 if( $this->oldimage
&& $this->file
->getTimestamp() == $this->getTimestamp() ) {
63 $wgOut->addHtml( wfMsgExt( 'filedelete-iscurrent', 'parse' ) );
64 $wgOut->addReturnTo( $this->title
);
68 // Perform the deletion if appropriate
69 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->oldimage
) ) {
70 $comment = $wgRequest->getText( 'wpComment' );
71 if( $this->oldimage
) {
72 $status = $this->file
->deleteOld( $this->oldimage
, $comment );
74 // Need to do a log item
75 $log = new LogPage( 'delete' );
76 $log->addEntry( 'delete', $this->title
, wfMsg( 'deletedrevision' , $this->oldimage
) );
79 $status = $this->file
->delete( $comment );
81 // Need to delete the associated article
82 $article = new Article( $this->title
);
83 $article->doDeleteArticle( $comment );
86 if( !$status->isGood() )
87 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
89 $wgOut->addHtml( $this->prepareMessage( 'filedelete-success' ) );
90 // Return to the main page if we just deleted all versions of the
91 // file, otherwise go back to the description page
92 $wgOut->addReturnTo( $this->oldimage ?
$this->title
: Title
::newMainPage() );
102 * Show the confirmation form
104 private function showForm() {
105 global $wgOut, $wgUser;
107 $form = Xml
::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
108 $form .= Xml
::hidden( 'wpEditToken', $wgUser->editToken( $this->oldimage
) );
109 $form .= '<fieldset><legend>' . wfMsgHtml( 'filedelete-legend' ) . '</legend>';
110 $form .= $this->prepareMessage( 'filedelete-intro' );
112 $form .= '<p>' . Xml
::inputLabel( wfMsg( 'filedelete-comment' ), 'wpComment', 'wpComment', 60 ) . '</p>';
113 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filedelete-submit' ) ) . '</p>';
114 $form .= '</fieldset>';
117 $wgOut->addHtml( $form );
121 * Prepare a message referring to the file being deleted,
122 * showing an appropriate message depending upon whether
123 * it's a current file or an old version
125 * @param string $message Message base
128 private function prepareMessage( $message ) {
129 global $wgLang, $wgServer;
130 if( $this->oldimage
) {
134 $this->title
->getText(),
135 $wgLang->date( $this->getTimestamp() ),
136 $wgLang->time( $this->getTimestamp() ),
137 $wgServer . $this->file
->getArchiveUrl( $this->oldimage
)
143 $this->title
->getText()
149 * Set headers, titles and other bits
151 private function setHeaders() {
153 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
154 $wgOut->setRobotPolicy( 'noindex,nofollow' );
158 * Is the provided `oldimage` value valid?
162 private function isValidOldSpec() {
163 return strlen( $this->oldimage
) >= 16
164 && strpos( $this->oldimage
, '/' ) === false
165 && strpos( $this->oldimage
, '\\' ) === false;
169 * Could we delete the file specified? If an `oldimage`
170 * value was provided, does it correspond to an
171 * existing, local, old version of this file?
175 private function haveDeletableFile() {
176 $file = wfFindFile( $this->title
, $this->oldimage
);
177 return $file && $file->exists() && $file->isLocal();
181 * Prepare the form action
185 private function getAction() {
187 $q[] = 'action=delete';
188 if( $this->oldimage
)
189 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
190 return $this->title
->getLocalUrl( implode( '&', $q ) );
194 * Extract the timestamp of the old version
198 private function getTimestamp() {
199 return substr( $this->oldimage
, 0, 14 );