YamlFFS.php

Go to the documentation of this file.
00001 <?php
00002 
00010 class YamlFFS extends SimpleFFS {
00011     public function getFileExtensions() {
00012         return array( '.yaml', '.yml' );
00013     }
00014 
00019     public function readFromVariable( $data ) {
00020         // Authors first.
00021         $matches = array();
00022         preg_match_all( '/^#\s*Author:\s*(.*)$/m', $data, $matches );
00023         $authors = $matches[1];
00024 
00025         // Then messages.
00026         $messages = TranslateYaml::loadString( $data );
00027 
00028         // Some groups have messages under language code
00029         if ( isset( $this->extra['codeAsRoot'] ) ) {
00030             $messages = array_shift( $messages );
00031         }
00032 
00033         $messages = $this->flatten( $messages );
00034         $messages = $this->group->getMangler()->mangle( $messages );
00035         foreach ( $messages as &$value ) {
00036             $value = rtrim( $value, "\n" );
00037         }
00038 
00039         return array(
00040             'AUTHORS' => $authors,
00041             'MESSAGES' => $messages,
00042         );
00043     }
00044 
00049     protected function writeReal( MessageCollection $collection ) {
00050         $output = $this->doHeader( $collection );
00051         $output .= $this->doAuthors( $collection );
00052 
00053         $mangler = $this->group->getMangler();
00054 
00055         $messages = array();
00059         foreach ( $collection as $key => $m ) {
00060             $key = $mangler->unmangle( $key );
00061             $value = $m->translation();
00062             $value = str_replace( TRANSLATE_FUZZY, '', $value );
00063 
00064             if ( $value === '' ) {
00065                 continue;
00066             }
00067 
00068             $messages[$key] = $value;
00069         }
00070 
00071         if ( !count( $messages ) ) {
00072             return false;
00073         }
00074 
00075         $messages = $this->unflatten( $messages );
00076 
00077         // Some groups have messages under language code.
00078         if ( isset( $this->extra['codeAsRoot'] ) ) {
00079             $code = $this->group->mapCode( $collection->code );
00080             $messages = array( $code => $messages );
00081         }
00082 
00083         $output .= TranslateYaml::dump( $messages );
00084 
00085         return $output;
00086     }
00087 
00092     protected function doHeader( MessageCollection $collection ) {
00093         global $wgSitename;
00094         global $wgTranslateYamlLibrary;
00095 
00096         $code = $collection->code;
00097         $name = TranslateUtils::getLanguageName( $code );
00098         $native = TranslateUtils::getLanguageName( $code, $code );
00099         $output = "# Messages for $name ($native)\n";
00100         $output .= "# Exported from $wgSitename\n";
00101 
00102         if ( isset( $wgTranslateYamlLibrary ) ) {
00103             $output .= "# Export driver: $wgTranslateYamlLibrary\n";
00104         }
00105 
00106         return $output;
00107     }
00108 
00113     protected function doAuthors( MessageCollection $collection ) {
00114         $output = '';
00115         $authors = $collection->getAuthors();
00116         $authors = $this->filterAuthors( $authors, $collection->code );
00117 
00118         foreach ( $authors as $author ) {
00119             $output .= "# Author: $author\n";
00120         }
00121 
00122         return $output;
00123     }
00124 
00133     protected function flatten( $messages ) {
00134         $flat = true;
00135 
00136         foreach ( $messages as $v ) {
00137             if ( !is_array( $v ) ) {
00138                 continue;
00139             }
00140 
00141             $flat = false;
00142             break;
00143         }
00144 
00145         if ( $flat ) {
00146             return $messages;
00147         }
00148 
00149         $array = array();
00150         foreach ( $messages as $key => $value ) {
00151             if ( !is_array( $value ) ) {
00152                 $array[$key] = $value;
00153             } else {
00154                 $plural = $this->flattenPlural( $value );
00155                 if ( $plural ) {
00156                     $array[$key] = $plural;
00157                 } else {
00158                     $newArray = array();
00159                     foreach ( $value as $newKey => $newValue ) {
00160                         $newArray["$key.$newKey"] = $newValue;
00161                     }
00162                     $array += $this->flatten( $newArray );
00163                 }
00164             }
00165 
00169             unset( $messages[$key] );
00170         }
00171 
00172         return $array;
00173     }
00174 
00183     protected function unflatten( $messages ) {
00184         $array = array();
00185         foreach ( $messages as $key => $value ) {
00186             $plurals = $this->unflattenPlural( $key, $value );
00187 
00188             if ( $plurals === false ) {
00189                 continue;
00190             }
00191 
00192             foreach ( $plurals as $keyPlural => $valuePlural ) {
00193                 $path = explode( '.', $keyPlural );
00194                 if ( count( $path ) == 1 ) {
00195                     $array[$keyPlural] = $valuePlural;
00196                     continue;
00197                 }
00198 
00199                 $pointer = &$array;
00200                 do {
00204                     $level = array_shift( $path );
00205                     if ( !isset( $pointer[$level] ) ) {
00206                         $pointer[$level] = array();
00207                     }
00208 
00212                     $tmpPointer = &$pointer[$level];
00213                     unset( $pointer );
00214                     $pointer = &$tmpPointer;
00215                     unset( $tmpPointer );
00216 
00220                     if ( count( $path ) === 1 ) {
00221                         $lastKey = array_shift( $path );
00222                         $pointer[$lastKey] = $valuePlural;
00223                     }
00224                 } while ( count( $path ) );
00225             }
00226         }
00227 
00228         return $array;
00229     }
00230 
00235     public function flattenPlural( $value ) {
00236         return false;
00237     }
00238 
00247     public function unflattenPlural( $key, $value ) {
00248         return array( $key => $value );
00249     }
00250 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3