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();
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
) );
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
);
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 );
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 );
78 $status = $this->file
->delete( $comment );
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' ) );
88 $wgOut->setPagetitle( wfMsg( 'actioncomplete' ) );
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() );
98 $this->showLogEntries();
102 * Show the confirmation form
104 private function showForm() {
105 global $wgOut, $wgUser, $wgRequest;
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' ), 'wpReason', 'wpReason',
113 60, $wgRequest->getText( 'wpReason' ) ) . '</p>';
114 $form .= '<p>' . Xml
::submitButton( wfMsg( 'filedelete-submit' ), array( 'name' => 'mw-filedelete-submit', 'id' => 'mw-filedelete-submit' ) ) . '</p>';
115 $form .= '</fieldset>';
118 $wgOut->addHtml( $form );
122 * Show deletion log fragments pertaining to the current file
124 private function showLogEntries() {
126 $wgOut->addHtml( '<h2>' . htmlspecialchars( LogPage
::logName( 'delete' ) ) . "</h2>\n" );
127 $reader = new LogViewer(
132 'page' => $this->title
->getPrefixedText(),
137 $reader->showList( $wgOut );
141 * Prepare a message referring to the file being deleted,
142 * showing an appropriate message depending upon whether
143 * it's a current file or an old version
145 * @param string $message Message base
148 private function prepareMessage( $message ) {
149 global $wgLang, $wgServer;
150 if( $this->oldimage
) {
154 $this->title
->getText(),
155 $wgLang->date( $this->getTimestamp(), true ),
156 $wgLang->time( $this->getTimestamp(), true ),
157 $wgServer . $this->file
->getArchiveUrl( $this->oldimage
)
163 $this->title
->getText()
169 * Set headers, titles and other bits
171 private function setHeaders() {
172 global $wgOut, $wgUser;
173 $wgOut->setPageTitle( wfMsg( 'filedelete', $this->title
->getText() ) );
174 $wgOut->setRobotPolicy( 'noindex,nofollow' );
175 $wgOut->setSubtitle( wfMsg( 'filedelete-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title
) ) );
179 * Is the provided `oldimage` value valid?
183 private function isValidOldSpec() {
184 return strlen( $this->oldimage
) >= 16
185 && strpos( $this->oldimage
, '/' ) === false
186 && strpos( $this->oldimage
, '\\' ) === false;
190 * Could we delete the file specified? If an `oldimage`
191 * value was provided, does it correspond to an
192 * existing, local, old version of this file?
196 private function haveDeletableFile() {
197 return $this->oldimage
198 ?
$this->oldfile
&& $this->oldfile
->exists() && $this->oldfile
->isLocal()
199 : $this->file
&& $this->file
->exists() && $this->file
->isLocal();
203 * Prepare the form action
207 private function getAction() {
209 $q[] = 'action=delete';
210 if( $this->oldimage
)
211 $q[] = 'oldimage=' . urlencode( $this->oldimage
);
212 return $this->title
->getLocalUrl( implode( '&', $q ) );
216 * Extract the timestamp of the old version
220 private function getTimestamp() {
221 return $this->oldfile
->getTimestamp();