4 * File deletion user interface
7 * @author Rob Church <robchur@gmail.com>
11 private $title = null;
14 private $oldfile = null;
15 private $oldimage = '';
20 * @param File $file File we're deleting
22 public function __construct( $file ) {
23 $this->title
= $file->getTitle();
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;
36 $wgOut->readOnlyPage();
38 } elseif( !$wgUser->isLoggedIn() ) {
39 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
41 } elseif( !$wgUser->isAllowed( 'delete' ) ) {
42 $wgOut->permissionRequired( 'delete' );
44 } elseif( $wgUser->isBlocked() ) {
45 $wgOut->blockedPage();
50 # $this->oldimage = $wgRequest->getText( 'oldimage', false );
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');
57 if( $this->oldimage
&& !$this->isValidOldSpec() ) {
58 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->oldimage
) );
62 $this->oldfile
= RepoGroup
::singleton()->getLocalRepo()->newFromArchiveName( $this->title
, $this->oldimage
);
64 if( !$this->haveDeletableFile() ) {
65 $wgOut->addHtml( $this->prepareMessage( 'filedelete-nofile' ) );
66 $wgOut->addReturnTo( $this->title
);
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 );
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 );
84 $status = $this->file
->delete( $comment, $suppress );
86 // Need to delete the associated article
87 $article = new Article( $this->title
);
88 $article->doDeleteArticle( $comment );
91 if( !$status->isGood() )
92 $wgOut->addWikiText( $status->getWikiText( 'filedeleteerror-short', 'filedeleteerror-long' ) );
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() );
103 $this->showLogEntries();
107 * Show the confirmation form
109 private function showForm() {
110 global $wgOut, $wgUser, $wgRequest;
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' );
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>';
123 $wgOut->addHtml( $form );
127 * Show deletion log fragments pertaining to the current file
129 private function showLogEntries() {
131 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
132 $reader = new LogViewer(
137 'page' => $this->title
->getPrefixedText(),
142 $reader->showList( $wgOut );
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
150 * @param string $message Message base
153 private function prepareMessage( $message ) {
154 global $wgLang, $wgServer;
155 if( $this->oldimage
) {
159 $this->title
->getText(),
160 $wgLang->date( $this->getTimestamp(), true ),
161 $wgLang->time( $this->getTimestamp(), true ),
162 $wgServer . $this->file
->getArchiveUrl( $this->oldimage
)
168 $this->title
->getText()
174 * Set headers, titles and other bits
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
) ) );
184 * Is the provided `oldimage` value valid?
188 private function isValidOldSpec() {
189 return strlen( $this->oldimage
) >= 16
190 && strpos( $this->oldimage
, '/' ) === false
191 && strpos( $this->oldimage
, '\\' ) === false;
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?
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();
208 * Prepare the form action
212 private function getAction() {
214 $q[] = 'action=delete';
215 if( $this->oldimage
)
216 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
217 return $this->title
->getLocalUrl( implode( '&', $q ) );
221 * Extract the timestamp of the old version
225 private function getTimestamp() {
226 return $this->oldfile
->getTimestamp();