00001 <?php
00002
00009 class FileRevertForm {
00010
00011 protected $title = null;
00012 protected $file = null;
00013 protected $archiveName = '';
00014 protected $timestamp = false;
00015 protected $oldFile;
00016
00022 public function __construct( $file ) {
00023 $this->title = $file->getTitle();
00024 $this->file = $file;
00025 }
00026
00031 public function execute() {
00032 global $wgOut, $wgRequest, $wgUser, $wgLang;
00033 $this->setHeaders();
00034
00035 if( wfReadOnly() ) {
00036 $wgOut->readOnlyPage();
00037 return;
00038 } elseif( !$wgUser->isLoggedIn() ) {
00039 $wgOut->showErrorPage( 'uploadnologin', 'uploadnologintext' );
00040 return;
00041 } elseif( !$this->title->userCan( 'edit' ) || !$this->title->userCan( 'upload' ) ) {
00042
00043
00044 $article = new Article( $this->title );
00045 $wgOut->readOnlyPage( $article->getContent(), true );
00046 return;
00047 } elseif( $wgUser->isBlocked() ) {
00048 $wgOut->blockedPage();
00049 return;
00050 }
00051
00052 $this->archiveName = $wgRequest->getText( 'oldimage' );
00053 $token = $wgRequest->getText( 'wpEditToken' );
00054 if( !$this->isValidOldSpec() ) {
00055 $wgOut->showUnexpectedValueError( 'oldimage', htmlspecialchars( $this->archiveName ) );
00056 return;
00057 }
00058
00059 if( !$this->haveOldVersion() ) {
00060 $wgOut->addHTML( wfMsgExt( 'filerevert-badversion', 'parse' ) );
00061 $wgOut->returnToMain( false, $this->title );
00062 return;
00063 }
00064
00065
00066 if( $wgRequest->wasPosted() && $wgUser->matchEditToken( $token, $this->archiveName ) ) {
00067 $source = $this->file->getArchiveVirtualUrl( $this->archiveName );
00068 $comment = $wgRequest->getText( 'wpComment' );
00069
00070 $status = $this->file->upload( $source, $comment, $comment );
00071 if( $status->isGood() ) {
00072 $wgOut->addHTML( wfMsgExt( 'filerevert-success', 'parse', $this->title->getText(),
00073 $wgLang->date( $this->getTimestamp(), true ),
00074 $wgLang->time( $this->getTimestamp(), true ),
00075 wfExpandUrl( $this->file->getArchiveUrl( $this->archiveName ) ) ) );
00076 $wgOut->returnToMain( false, $this->title );
00077 } else {
00078 $wgOut->addWikiText( $status->getWikiText() );
00079 }
00080 return;
00081 }
00082
00083
00084 $this->showForm();
00085 }
00086
00090 protected function showForm() {
00091 global $wgOut, $wgUser, $wgRequest, $wgLang, $wgContLang;
00092 $timestamp = $this->getTimestamp();
00093
00094 $form = Xml::openElement( 'form', array( 'method' => 'post', 'action' => $this->getAction() ) );
00095 $form .= Xml::hidden( 'wpEditToken', $wgUser->editToken( $this->archiveName ) );
00096 $form .= '<fieldset><legend>' . wfMsgHtml( 'filerevert-legend' ) . '</legend>';
00097 $form .= wfMsgExt( 'filerevert-intro', 'parse', $this->title->getText(),
00098 $wgLang->date( $timestamp, true ), $wgLang->time( $timestamp, true ),
00099 wfExpandUrl( $this->file->getArchiveUrl( $this->archiveName ) ) );
00100 $form .= '<p>' . Xml::inputLabel( wfMsg( 'filerevert-comment' ), 'wpComment', 'wpComment',
00101 60, wfMsgForContent( 'filerevert-defaultcomment',
00102 $wgContLang->date( $timestamp, false, false ), $wgContLang->time( $timestamp, false, false ) ) ) . '</p>';
00103 $form .= '<p>' . Xml::submitButton( wfMsg( 'filerevert-submit' ) ) . '</p>';
00104 $form .= '</fieldset>';
00105 $form .= '</form>';
00106
00107 $wgOut->addHTML( $form );
00108 }
00109
00113 protected function setHeaders() {
00114 global $wgOut, $wgUser;
00115 $wgOut->setPageTitle( wfMsg( 'filerevert', $this->title->getText() ) );
00116 $wgOut->setRobotPolicy( 'noindex,nofollow' );
00117 $wgOut->setSubtitle( wfMsg( 'filerevert-backlink', $wgUser->getSkin()->makeKnownLinkObj( $this->title ) ) );
00118 }
00119
00125 protected function isValidOldSpec() {
00126 return strlen( $this->archiveName ) >= 16
00127 && strpos( $this->archiveName, '/' ) === false
00128 && strpos( $this->archiveName, '\\' ) === false;
00129 }
00130
00137 protected function haveOldVersion() {
00138 return $this->getOldFile()->exists();
00139 }
00140
00146 protected function getAction() {
00147 $q = array();
00148 $q[] = 'action=revert';
00149 $q[] = 'oldimage=' . urlencode( $this->archiveName );
00150 return $this->title->getLocalUrl( implode( '&', $q ) );
00151 }
00152
00158 protected function getTimestamp() {
00159 if( $this->timestamp === false ) {
00160 $this->timestamp = $this->getOldFile()->getTimestamp();
00161 }
00162 return $this->timestamp;
00163 }
00164
00165 protected function getOldFile() {
00166 if ( !isset( $this->oldFile ) ) {
00167 $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName( $this->title, $this->archiveName );
00168 }
00169 return $this->oldFile;
00170 }
00171 }