SpecialImportTranslations.php

Go to the documentation of this file.
00001 <?php
00017 class SpecialImportTranslations extends SpecialPage {
00021     public function __construct() {
00022         parent::__construct( 'ImportTranslations', 'translate-import' );
00023     }
00024 
00028     public function execute( $parameters ) {
00029         $this->setHeaders();
00030 
00031         // Security and validity checks
00032         if ( !$this->userCanExecute( $this->getUser() ) ) {
00033             $this->displayRestrictionError();
00034 
00035             return;
00036         }
00037 
00038         if ( !$this->getRequest()->wasPosted() ) {
00039             $this->outputForm();
00040 
00041             return;
00042         }
00043 
00044         if ( !$this->getUser()->matchEditToken( $this->getRequest()->getVal( 'token' ) ) ) {
00045             $this->getOutput()->addWikiMsg( 'session_fail_preview' );
00046             $this->outputForm();
00047 
00048             return;
00049         }
00050 
00051         if ( $this->getRequest()->getCheck( 'process' ) ) {
00052             $data = $this->getCachedData();
00053             if ( !$data ) {
00054                 $this->getOutput()->addWikiMsg( 'session_fail_preview' );
00055                 $this->outputForm();
00056 
00057                 return;
00058             }
00059         } else {
00064             $file = null;
00065             $msg = $this->loadFile( $file );
00066             if ( $this->checkError( $msg ) ) {
00067                 return;
00068             }
00069 
00070             $msg = $this->parseFile( $file );
00071             if ( $this->checkError( $msg ) ) {
00072                 return;
00073             }
00074 
00075             $data = $msg[1];
00076             $this->setCachedData( $data );
00077         }
00078 
00079         $messages = $data['MESSAGES'];
00080         $group = $data['METADATA']['group'];
00081         $code = $data['METADATA']['code'];
00082 
00083         if ( !MessageGroups::exists( $group ) ) {
00084             $errorWrap = "<div class='error'>\n$1\n</div>";
00085             $this->getOutput()->wrapWikiMsg( $errorWrap, 'translate-import-err-stale-group' );
00086 
00087             return;
00088         }
00089 
00090         $importer = new MessageWebImporter( $this->getTitle(), $group, $code );
00091         $alldone = $importer->execute( $messages );
00092 
00093         if ( $alldone ) {
00094             $this->deleteCachedData();
00095         }
00096     }
00097 
00105     protected function checkError( $msg ) {
00106         // Give grep a chance to find the usages:
00107         // translate-import-err-dl-failed, translate-import-err-ul-failed,
00108         // translate-import-err-invalid-title, translate-import-err-no-such-file,
00109         // translate-import-err-stale-group, translate-import-err-no-headers,
00110         // translate-import-err-warnings
00111         if ( $msg[0] !== 'ok' ) {
00112             $errorWrap = "<div class='error'>\n$1\n</div>";
00113             $msg[0] = 'translate-import-err-' . $msg[0];
00114             $this->getOutput()->wrapWikiMsg( $errorWrap, $msg );
00115             $this->outputForm();
00116 
00117             return true;
00118         }
00119 
00120         return false;
00121     }
00122 
00126     protected function outputForm() {
00127         $this->getOutput()->addModules( 'ext.translate.special.importtranslations' );
00128         TranslateUtils::addSpecialHelpLink(
00129             $this->getOutput(),
00130             'Help:Extension:Translate/Off-line_translation'
00131         );
00135         $this->getOutput()->addHTML(
00136             Xml::openElement( 'form', array(
00137                 'action' => $this->getTitle()->getLocalUrl(),
00138                 'method' => 'post',
00139                 'enctype' => 'multipart/form-data',
00140                 'id' => 'mw-translate-import',
00141             ) ) .
00142                 Html::hidden( 'token', $this->getUser()->getEditToken() ) .
00143                 Html::hidden( 'title', $this->getTitle()->getPrefixedText() ) .
00144                 Xml::inputLabel(
00145                     $this->msg( 'translate-import-from-local' )->text(),
00146                     'upload-local', // name
00147                     'mw-translate-up-local-input', // id
00148                     50, // size
00149                     $this->getRequest()->getText( 'upload-local' ),
00150                     array( 'type' => 'file' )
00151                 ) .
00152                 Xml::submitButton( $this->msg( 'translate-import-load' )->text() ) .
00153                 Xml::closeElement( 'form' )
00154         );
00155     }
00156 
00162     protected function loadFile( &$filedata ) {
00163         $filename = $this->getRequest()->getFileTempname( 'upload-local' );
00164 
00165         if ( !is_uploaded_file( $filename ) ) {
00166             return array( 'ul-failed' );
00167         }
00168 
00169         $filedata = file_get_contents( $filename );
00170 
00171         return array( 'ok' );
00172     }
00173 
00179     protected function parseFile( $data ) {
00184         $group = MessageGroupBase::factory( array(
00185             'FILES' => array(
00186                 'class' => 'GettextFFS',
00187                 'CtxtAsKey' => true,
00188             ),
00189             'BASIC' => array(
00190                 'class' => 'FileBasedMessageGroup',
00191                 'namespace' => -1,
00192             )
00193         ) );
00194 
00195         $ffs = new GettextFFS( $group );
00196         $data = $ffs->readFromVariable( $data );
00197 
00201         $metadata = $data['METADATA'];
00202 
00206         if ( !isset( $metadata['code'] ) || !isset( $metadata['group'] ) ) {
00207             return array( 'no-headers' );
00208         }
00209 
00214         if ( isset( $metadata['warnings'] ) ) {
00215             return array( 'warnings', $this->getLanguage()->commaList( $metadata['warnings'] ) );
00216         }
00217 
00218         return array( 'ok', $data );
00219     }
00220 
00221     protected function setCachedData( $data ) {
00222         $key = wfMemcKey( 'translate', 'webimport', $this->getUser()->getId() );
00223         wfGetCache( CACHE_DB )->set( $key, $data, 60 * 30 );
00224     }
00225 
00226     protected function getCachedData() {
00227         $key = wfMemcKey( 'translate', 'webimport', $this->getUser()->getId() );
00228 
00229         return wfGetCache( CACHE_DB )->get( $key );
00230     }
00231 
00232     protected function deleteCachedData() {
00233         $key = wfMemcKey( 'translate', 'webimport', $this->getUser()->getId() );
00234 
00235         return wfGetCache( CACHE_DB )->delete( $key );
00236     }
00237 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3