MessageGroupBase.php

Go to the documentation of this file.
00001 <?php
00020 abstract class MessageGroupBase implements MessageGroup {
00021     protected $conf;
00022     protected $namespace;
00023     protected $groups;
00024 
00028     protected $mangler;
00029 
00030     protected function __construct() {
00031     }
00032 
00038     public static function factory( $conf ) {
00039         $obj = new $conf['BASIC']['class']();
00040         $obj->conf = $conf;
00041         $obj->namespace = $obj->parseNamespace();
00042 
00043         return $obj;
00044     }
00045 
00046     public function getConfiguration() {
00047         return $this->conf;
00048     }
00049 
00050     public function getId() {
00051         return $this->getFromConf( 'BASIC', 'id' );
00052     }
00053 
00054     public function getLabel( IContextSource $context = null ) {
00055         return $this->getFromConf( 'BASIC', 'label' );
00056     }
00057 
00058     public function getDescription( IContextSource $context = null ) {
00059         return $this->getFromConf( 'BASIC', 'description' );
00060     }
00061 
00062     public function getIcon() {
00063         return $this->getFromConf( 'BASIC', 'icon' );
00064     }
00065 
00066     public function getNamespace() {
00067         return $this->namespace;
00068     }
00069 
00070     public function isMeta() {
00071         return $this->getFromConf( 'BASIC', 'meta' );
00072     }
00073 
00074     public function getSourceLanguage() {
00075         $conf = $this->getFromConf( 'BASIC', 'sourcelanguage' );
00076 
00077         return $conf !== null ? $conf : 'en';
00078     }
00079 
00080     public function getDefinitions() {
00081         $defs = $this->load( $this->getSourceLanguage() );
00082 
00083         return $defs;
00084     }
00085 
00086     protected function getFromConf( $section, $key ) {
00087         return isset( $this->conf[$section][$key] ) ? $this->conf[$section][$key] : null;
00088     }
00089 
00090     public function getFFS() {
00091         $class = $this->getFromConf( 'FILES', 'class' );
00092 
00093         if ( $class === null ) {
00094             return null;
00095         }
00096 
00097         if ( !class_exists( $class ) ) {
00098             throw new MWException( "FFS class $class does not exist." );
00099         }
00100 
00101         return new $class( $this );
00102     }
00103 
00104     public function getChecker() {
00105         $class = $this->getFromConf( 'CHECKER', 'class' );
00106 
00107         if ( $class === null ) {
00108             return null;
00109         }
00110 
00111         if ( !class_exists( $class ) ) {
00112             throw new MWException( "Checker class $class does not exist." );
00113         }
00114 
00115         $checker = new $class( $this );
00116         $checks = $this->getFromConf( 'CHECKER', 'checks' );
00117 
00118         if ( !is_array( $checks ) ) {
00119             throw new MWException( "Checker class $class not supplied with proper checks." );
00120         }
00121 
00122         foreach ( $checks as $check ) {
00123             $checker->addCheck( array( $checker, $check ) );
00124         }
00125 
00126         return $checker;
00127     }
00128 
00129     public function getMangler() {
00130         if ( !isset( $this->mangler ) ) {
00131             $class = $this->getFromConf( 'MANGLER', 'class' );
00132 
00133             if ( $class === null ) {
00134                 $this->mangler = StringMatcher::emptyMatcher();
00135 
00136                 return $this->mangler;
00137             }
00138 
00139             if ( !class_exists( $class ) ) {
00140                 throw new MWException( "Mangler class $class does not exist." );
00141             }
00142 
00146             $this->mangler = new $class();
00147             $this->mangler->setConf( $this->conf['MANGLER'] );
00148         }
00149 
00150         return $this->mangler;
00151     }
00152 
00157     public function getInsertablesSuggester() {
00158         $class = $this->getFromConf( 'INSERTABLES', 'class' );
00159 
00160         if ( !$class ) {
00161             return null;
00162         }
00163 
00164         if ( !class_exists( $class ) ) {
00165             throw new MWException( "InsertablesSuggester class $class does not exist." );
00166         }
00167 
00168         return new $class();
00169     }
00170 
00176     public function getKeys() {
00177         $cache = new MessageGroupCache( $this, $this->getSourceLanguage() );
00178         if ( !$cache->exists() ) {
00179             return array_keys( $this->getDefinitions() );
00180         } else {
00181             return $cache->getKeys();
00182         }
00183     }
00184 
00185     public function initCollection( $code ) {
00186         $namespace = $this->getNamespace();
00187         $messages = array();
00188 
00189         $cache = new MessageGroupCache( $this, $this->getSourceLanguage() );
00190         if ( !$cache->exists() ) {
00191             wfWarn( "By-passing message group cache for {$this->getId()}" );
00192             $messages = $this->getDefinitions();
00193         } else {
00194             foreach ( $cache->getKeys() as $key ) {
00195                 $messages[$key] = $cache->get( $key );
00196             }
00197         }
00198 
00199         $definitions = new MessageDefinitions( $messages, $namespace );
00200         $collection = MessageCollection::newFromDefinitions( $definitions, $code );
00201         $this->setTags( $collection );
00202 
00203         return $collection;
00204     }
00205 
00206     public function getMessage( $key, $code ) {
00207         $cache = new MessageGroupCache( $this, $code );
00208         if ( $cache->exists() ) {
00209             $msg = $cache->get( $key );
00210 
00211             if ( $msg !== false ) {
00212                 return $msg;
00213             }
00214 
00215             // Try harder
00216             $nkey = str_replace( ' ', '_', strtolower( $key ) );
00217             $keys = $cache->getKeys();
00218 
00219             foreach ( $keys as $k ) {
00220                 if ( $nkey === str_replace( ' ', '_', strtolower( $k ) ) ) {
00221                     return $cache->get( $k );
00222                 }
00223             }
00224 
00225             return null;
00226         } else {
00227             return null;
00228         }
00229     }
00230 
00231     public function getTags( $type = null ) {
00232         if ( $type === null ) {
00233             $taglist = array();
00234 
00235             foreach ( $this->getRawTags() as $type => $patterns ) {
00236                 $taglist[$type] = $this->parseTags( $patterns );
00237             }
00238 
00239             return $taglist;
00240         } else {
00241             return $this->parseTags( $this->getRawTags( $type ) );
00242         }
00243     }
00244 
00245     protected function parseTags( $patterns ) {
00246         $messageKeys = $this->getKeys();
00247 
00248         $matches = array();
00249 
00253         foreach ( $patterns as $index => $pattern ) {
00254             if ( strpos( $pattern, '*' ) === false ) {
00255                 $matches[] = $pattern;
00256                 unset( $patterns[$index] );
00257             }
00258         }
00259 
00260         if ( count( $patterns ) ) {
00264             $mangler = new StringMatcher( '', $patterns );
00265 
00269             foreach ( $messageKeys as $key ) {
00270                 if ( $mangler->match( $key ) ) {
00271                     $matches[] = $key;
00272                 }
00273             }
00274         }
00275 
00276         return $matches;
00277     }
00278 
00279     protected function getRawTags( $type = null ) {
00280         if ( !isset( $this->conf['TAGS'] ) ) {
00281             return array();
00282         }
00283 
00284         $tags = $this->conf['TAGS'];
00285         if ( !$type ) {
00286             return $tags;
00287         }
00288 
00289         if ( isset( $tags[$type] ) ) {
00290             return $tags[$type];
00291         }
00292 
00293         return array();
00294     }
00295 
00296     protected function setTags( MessageCollection $collection ) {
00297         foreach ( $this->getTags() as $type => $tags ) {
00298             $collection->setTags( $type, $tags );
00299         }
00300     }
00301 
00302     protected function parseNamespace() {
00303         $ns = $this->getFromConf( 'BASIC', 'namespace' );
00304 
00305         if ( is_int( $ns ) ) {
00306             return $ns;
00307         }
00308 
00309         if ( defined( $ns ) ) {
00310             return constant( $ns );
00311         }
00312 
00313         global $wgContLang;
00314 
00315         $index = $wgContLang->getNsIndex( $ns );
00316 
00317         if ( !$index ) {
00318             throw new MWException( "No valid namespace defined, got $ns." );
00319         }
00320 
00321         return $index;
00322     }
00323 
00324     protected function isSourceLanguage( $code ) {
00325         return $code === $this->getSourceLanguage();
00326     }
00327 
00331     public function getWorkflowConfiguration() {
00332         global $wgTranslateWorkflowStates;
00333         if ( !$wgTranslateWorkflowStates ) {
00334             // Not configured
00335             $conf = array();
00336         } else {
00337             $conf = $wgTranslateWorkflowStates;
00338         }
00339 
00340         return $conf;
00341     }
00342 
00347     public function getMessageGroupStates() {
00348         $conf = $this->getWorkflowConfiguration();
00349 
00350         return new MessageGroupStates( $conf );
00351     }
00352 
00358     public function getTranslatableLanguages() {
00359         $groupConfiguration = $this->getConfiguration();
00360         if ( !isset( $groupConfiguration['LANGUAGES'] ) ) {
00361             // No LANGUAGES section in the configuration.
00362             return null;
00363         }
00364 
00365         $lists = $groupConfiguration['LANGUAGES'];
00366         $codes = array(); // The list of languages to return
00367 
00368         if ( isset( $lists['blacklist'] ) ) {
00369             $blacklist = $lists['blacklist'];
00370             if ( is_array( $blacklist ) ) {
00371                 $codes = array_flip( array_keys( TranslateUtils::getLanguageNames( 'en' ) ) );
00372                 foreach ( $blacklist as $code ) {
00373                     unset( $codes[$code] );
00374                 }
00375             } else {
00376                 // All languages blacklisted. This is very rare but not impossible.
00377                 $codes = array();
00378             }
00379         }
00380 
00381         $whitelist = array();
00382         if ( isset( $lists['whitelist'] ) ) {
00383             $whitelist = $lists['whitelist'];
00384             if ( $whitelist === "*" ) {
00385                 // All languages whitelisted
00386                 return null;
00387             }
00388         }
00389 
00390         foreach ( $whitelist as $code ) {
00391             $codes[$code] = true;
00392         }
00393 
00394         return $codes;
00395     }
00396 
00403     public function getTranslationAids() {
00404         return TranslationAid::getTypes();
00405     }
00406 }
Generated on Tue Oct 29 00:00:24 2013 for MediaWiki Translate Extension by  doxygen 1.6.3