FileBasedMessageGroup.php

Go to the documentation of this file.
00001 <?php
00019 class FileBasedMessageGroup extends MessageGroupBase {
00020     protected $reverseCodeMap;
00021 
00028     public static function newFromMessageGroup( $group ) {
00029         $conf = array(
00030             'BASIC' => array(
00031                 'class' => 'FileBasedMessageGroup',
00032                 'id' => $group->getId(),
00033                 'label' => $group->getLabel(),
00034                 'namespace' => $group->getNamespace(),
00035             ),
00036             'FILES' => array(
00037                 'sourcePattern' => '',
00038                 'targetPattern' => '',
00039             ),
00040         );
00041 
00042         return MessageGroupBase::factory( $conf );
00043     }
00044 
00045     public function exists() {
00046         return $this->getFFS()->exists();
00047     }
00048 
00049     public function load( $code ) {
00053         $ffs = $this->getFFS();
00054         $data = $ffs->read( $code );
00055 
00056         return $data ? $data['MESSAGES'] : array();
00057     }
00058 
00059     public function getSourceFilePath( $code ) {
00060         if ( $this->isSourceLanguage( $code ) ) {
00061             $pattern = $this->getFromConf( 'FILES', 'definitionFile' );
00062 
00063             if ( $pattern !== null ) {
00064                 return $this->replaceVariables( $pattern, $code );
00065             }
00066         }
00067 
00068         $pattern = $this->getFromConf( 'FILES', 'sourcePattern' );
00069 
00070         if ( $pattern === null ) {
00071             throw new MWException( 'No source file pattern defined.' );
00072         }
00073 
00074         return $this->replaceVariables( $pattern, $code );
00075     }
00076 
00077     public function getTargetFilename( $code ) {
00078         $pattern = $this->getFromConf( 'FILES', 'targetPattern' );
00079 
00080         if ( $pattern === null ) {
00081             throw new MWException( 'No target file pattern defined.' );
00082         }
00083 
00084         return $this->replaceVariables( $pattern, $code );
00085     }
00086 
00087     protected function replaceVariables( $pattern, $code ) {
00088         global $IP, $wgTranslateGroupRoot;
00089 
00090         $variables = array(
00091             '%CODE%' => $this->mapCode( $code ),
00092             '%MWROOT%' => $IP,
00093             '%GROUPROOT%' => $wgTranslateGroupRoot,
00094         );
00095 
00096         return str_replace( array_keys( $variables ), array_values( $variables ), $pattern );
00097     }
00098 
00103     public function mapCode( $code ) {
00104         if ( !isset( $this->conf['FILES']['codeMap'] ) ) {
00105             return $code;
00106         }
00107 
00108         if ( isset( $this->conf['FILES']['codeMap'][$code] ) ) {
00109             return $this->conf['FILES']['codeMap'][$code];
00110         } else {
00111             if ( !isset( $this->reverseCodeMap ) ) {
00112                 $this->reverseCodeMap = array_flip( $this->conf['FILES']['codeMap'] );
00113             }
00114 
00115             if ( isset( $this->reverseCodeMap[$code] ) ) {
00116                 return 'x-invalidLanguageCode';
00117             }
00118 
00119             return $code;
00120         }
00121     }
00122 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3