IniFFS.php

Go to the documentation of this file.
00001 <?php
00018 class IniFFS extends SimpleFFS {
00019     public static function isValid( $data ) {
00020         $conf = array( 'BASIC' => array( 'class' => 'FileBasedMessageGroup', 'namespace' => 8 ) );
00024         $group = MessageGroupBase::factory( $conf );
00025 
00026         wfSuppressWarnings();
00027         $ffs = new IniFFS( $group );
00028         $parsed = $ffs->readFromVariable( $data );
00029         wfRestoreWarnings();
00030 
00031         return !!count( $parsed['MESSAGES'] );
00032     }
00033 
00034     public function supportsFuzzy() {
00035         return 'write';
00036     }
00037 
00038     public function getFileExtensions() {
00039         return array( '.ini' );
00040     }
00041 
00042     public function readFromVariable( $data ) {
00043         $authors = array();
00044         preg_match_all( '/^; Author: (.*)$/m', $data, $matches, PREG_SET_ORDER );
00045         foreach ( $matches as $match ) {
00046             $authors[] = $match[1];
00047         }
00048 
00049         // Remove comments
00050         $data = preg_replace( '/^\s*;.*$/m', '', $data );
00051         // Make sure values are quoted, PHP barks on stuff like ?{}|&~![()^
00052         $data = preg_replace( '/(^.+?=\s*)([^\'"].+)$/m', '\1"\2"', $data );
00053 
00054         $messages = parse_ini_string( $data );
00055         if ( is_array( $messages ) ) {
00056             $messages = $this->group->getMangler()->mangle( $messages );
00057         } else {
00058             $messages = null;
00059         }
00060 
00061         return array(
00062             'MESSAGES' => $messages,
00063             'AUTHORS' => $authors,
00064         );
00065     }
00066 
00067     protected function writeReal( MessageCollection $collection ) {
00068         $output = '';
00069         $mangler = $this->group->getMangler();
00070 
00074         foreach ( $collection as $key => $m ) {
00075             $value = $m->translation();
00076             if ( $value === null ) {
00077                 continue;
00078             }
00079 
00080             $comment = '';
00081 
00082             if ( $m->hasTag( 'fuzzy' ) ) {
00083                 $value = str_replace( TRANSLATE_FUZZY, '', $value );
00084                 $comment = "; Fuzzy\n";
00085             }
00086 
00087             $key = $mangler->unmangle( $key );
00088             $output .= "$comment$key = $value\n";
00089         }
00090 
00091         // Do not create empty files
00092         if ( $output === '' ) {
00093             return '';
00094         }
00095 
00096         global $wgSitename;
00097         // Accumulator
00098         $header = "; Exported from $wgSitename\n";
00099 
00100         $authors = $collection->getAuthors();
00101         $authors = $this->filterAuthors( $authors, $collection->getLanguage() );
00102         foreach ( $authors as $author ) {
00103             $header .= "; Author: $author\n";
00104         }
00105 
00106         $header .= '[' . $collection->getLanguage() . "]\n";
00107 
00108         return $header . $output;
00109     }
00110 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3