MediaWiki  REL1_24
RevertAction.php
Go to the documentation of this file.
00001 <?php
00031 class RevertAction extends FormAction {
00035     protected $oldFile;
00036 
00037     public function getName() {
00038         return 'revert';
00039     }
00040 
00041     public function getRestriction() {
00042         return 'upload';
00043     }
00044 
00045     protected function checkCanExecute( User $user ) {
00046         if ( $this->getTitle()->getNamespace() !== NS_FILE ) {
00047             throw new ErrorPageError( $this->msg( 'nosuchaction' ), $this->msg( 'nosuchactiontext' ) );
00048         }
00049         parent::checkCanExecute( $user );
00050 
00051         $oldimage = $this->getRequest()->getText( 'oldimage' );
00052         if ( strlen( $oldimage ) < 16
00053             || strpos( $oldimage, '/' ) !== false
00054             || strpos( $oldimage, '\\' ) !== false
00055         ) {
00056             throw new ErrorPageError( 'internalerror', 'unexpected', array( 'oldimage', $oldimage ) );
00057         }
00058 
00059         $this->oldFile = RepoGroup::singleton()->getLocalRepo()->newFromArchiveName(
00060             $this->getTitle(),
00061             $oldimage
00062         );
00063 
00064         if ( !$this->oldFile->exists() ) {
00065             throw new ErrorPageError( '', 'filerevert-badversion' );
00066         }
00067     }
00068 
00069     protected function alterForm( HTMLForm $form ) {
00070         $form->setWrapperLegendMsg( 'filerevert-legend' );
00071         $form->setSubmitTextMsg( 'filerevert-submit' );
00072         $form->addHiddenField( 'oldimage', $this->getRequest()->getText( 'oldimage' ) );
00073         $form->setTokenSalt( array( 'revert', $this->getTitle()->getPrefixedDBkey() ) );
00074     }
00075 
00076     protected function getFormFields() {
00077         global $wgContLang;
00078 
00079         $timestamp = $this->oldFile->getTimestamp();
00080 
00081         $user = $this->getUser();
00082         $lang = $this->getLanguage();
00083         $userDate = $lang->userDate( $timestamp, $user );
00084         $userTime = $lang->userTime( $timestamp, $user );
00085         $siteDate = $wgContLang->date( $timestamp, false, false );
00086         $siteTime = $wgContLang->time( $timestamp, false, false );
00087 
00088         return array(
00089             'intro' => array(
00090                 'type' => 'info',
00091                 'vertical-label' => true,
00092                 'raw' => true,
00093                 'default' => $this->msg( 'filerevert-intro',
00094                     $this->getTitle()->getText(), $userDate, $userTime,
00095                     wfExpandUrl(
00096                         $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
00097                         PROTO_CURRENT
00098                     ) )->parseAsBlock()
00099             ),
00100             'comment' => array(
00101                 'type' => 'text',
00102                 'label-message' => 'filerevert-comment',
00103                 'default' => $this->msg( 'filerevert-defaultcomment', $siteDate, $siteTime
00104                     )->inContentLanguage()->text()
00105             )
00106         );
00107     }
00108 
00109     public function onSubmit( $data ) {
00110         $source = $this->page->getFile()->getArchiveVirtualUrl(
00111             $this->getRequest()->getText( 'oldimage' )
00112         );
00113         $comment = $data['comment'];
00114 
00115         // TODO: Preserve file properties from database instead of reloading from file
00116         return $this->page->getFile()->upload(
00117             $source,
00118             $comment,
00119             $comment,
00120             0,
00121             false,
00122             false,
00123             $this->getUser()
00124         );
00125     }
00126 
00127     public function onSuccess() {
00128         $timestamp = $this->oldFile->getTimestamp();
00129         $user = $this->getUser();
00130         $lang = $this->getLanguage();
00131         $userDate = $lang->userDate( $timestamp, $user );
00132         $userTime = $lang->userTime( $timestamp, $user );
00133 
00134         $this->getOutput()->addWikiMsg( 'filerevert-success', $this->getTitle()->getText(),
00135             $userDate, $userTime,
00136             wfExpandUrl( $this->page->getFile()->getArchiveUrl( $this->getRequest()->getText( 'oldimage' ) ),
00137                 PROTO_CURRENT
00138         ) );
00139         $this->getOutput()->returnToMain( false, $this->getTitle() );
00140     }
00141 
00142     protected function getPageTitle() {
00143         return $this->msg( 'filerevert', $this->getTitle()->getText() );
00144     }
00145 
00146     protected function getDescription() {
00147         $this->getOutput()->addBacklinkSubtitle( $this->getTitle() );
00148 
00149         return '';
00150     }
00151 }