Wiki.php

Go to the documentation of this file.
00001 <?php
00014 class WikiFormatReader extends SimpleFormatReader {
00015     // Set by creater
00016     public $variableName = 'messages';
00017 
00023     public function parseAuthors() {
00024         if ( $this->filename === false ) {
00025             return array();
00026         }
00027         $contents = file_get_contents( $this->filename );
00028         $m = array();
00029         preg_match_all( '/@author (.*)/', $contents, $m );
00030 
00031         return $m[1];
00032     }
00033 
00037     public function parseStaticHeader() {
00038         if ( $this->filename === false ) {
00039             return '';
00040         }
00041 
00042         $contents = file_get_contents( $this->filename );
00043 
00044         // @todo Handle the case where the first comment is missing */
00045         // $dollarstart = strpos( $contents, '$' );
00046 
00047         $start = strpos( $contents, '*/' );
00048         $end = strpos( $contents, '$messages' );
00049 
00050         if ( $start === false ) {
00051             return '';
00052         }
00053 
00054         if ( $start === $end ) {
00055             return '';
00056         }
00057 
00058         $start += 2; // Get over the comment ending
00059 
00060         if ( $end === false ) {
00061             return trim( substr( $contents, $start ) );
00062         }
00063 
00064         return trim( substr( $contents, $start, $end - $start ) );
00065     }
00066 
00067     public function parseMessages( StringMangler $mangler ) {
00068         if ( $this->filename === false ) {
00069             return array();
00070         }
00071 
00072         ${$this->variableName} = array();
00073         require $this->filename;
00074 
00075         return $mangler->mangle( ${$this->variableName} );
00076     }
00077 }
00078 
00082 class WikiFormatWriter extends SimpleFormatWriter {
00083     public $commaToArray = false;
00084 
00085     public function makeHeader( $handle, $code ) {
00086         list( $name, $native ) = $this->getLanguageNames( $code );
00087         $authors = $this->formatAuthors( ' * @author ', $code );
00088 
00089         fwrite( $handle, <<<HEADER
00090 <?php
00101 HEADER
00102         );
00103     }
00104 
00105     protected function exportStaticHeader( $target ) {
00106         if ( $this->staticHeader ) {
00107             fwrite( $target, "\n" . $this->staticHeader . "\n" );
00108         }
00109     }
00110 
00111     protected function exportMessages( $handle, MessageCollection $collection ) {
00112         fwrite( $handle, "\n\$messages = array(\n" );
00113 
00114         $messages = $this->makeExportArray( $collection );
00115 
00116         $dir = $this->group->getMetaDataPrefix();
00117         if ( !$dir ) {
00118             $this->writeMessagesBlock( $handle, $messages );
00119             fwrite( $handle, ");\n" );
00120 
00121             return;
00122         }
00123 
00124         require $dir . '/messages.inc';
00125 
00126         # Sort messages to blocks
00127         $sortedMessages['unknown'] = $messages;
00128         foreach ( $wgMessageStructure as $blockName => $block ) {
00129             foreach ( $block as $key ) {
00130                 if ( array_key_exists( $key, $sortedMessages['unknown'] ) ) {
00131                     $sortedMessages[$blockName][$key] = $sortedMessages['unknown'][$key];
00132                     unset( $sortedMessages['unknown'][$key] );
00133                 }
00134             }
00135         }
00136 
00137         foreach ( $sortedMessages as $block => $messages ) {
00138             # Skip if it's the block of unknown messages - handle that in the end of file
00139             if ( $block === 'unknown' ) {
00140                 continue;
00141             }
00142 
00143             $this->writeMessagesBlockComment( $handle, $wgBlockComments[$block] );
00144             $this->writeMessagesBlock( $handle, $messages );
00145             fwrite( $handle, "\n" );
00146         }
00147 
00148         # Write the unknown messages, alphabetically sorted.
00149         if ( count( $sortedMessages['unknown'] ) ) {
00150             ksort( $sortedMessages['unknown'] );
00151             $this->writeMessagesBlockComment( $handle, 'Unknown messages' );
00152             $this->writeMessagesBlock( $handle, $sortedMessages['unknown'] );
00153         }
00154 
00155         fwrite( $handle, ");\n" );
00156     }
00157 
00165     public function makeExportArray( MessageCollection $messages ) {
00166         // We copy only relevant translations to this new array
00167         $new = array();
00168         $mangler = $this->group->getMangler();
00169 
00170         foreach ( $messages as $key => $m ) {
00171             $key = $mangler->unMangle( $key );
00172             # Remove fuzzy markings before export
00173             $translation = str_replace( TRANSLATE_FUZZY, '', $m->translation() );
00174             $new[$key] = $translation;
00175         }
00176 
00177         return $new;
00178     }
00179 
00180     protected function writeMessagesBlockComment( $handle, $blockComment ) {
00181         # Format the block comment (if exists); check for multiple lines comments
00182         if ( !empty( $blockComment ) ) {
00183             if ( strpos( $blockComment, "\n" ) === false ) {
00184                 fwrite( $handle, "# $blockComment\n" );
00185             } else {
00186                 fwrite( $handle, "/*\n$blockComment\n*/\n" );
00187             }
00188         }
00189     }
00190 
00191     protected function writeMessagesBlock( $handle, $messages, $prefix = '' ) {
00192         # Skip the block if it includes no messages
00193         if ( empty( $messages ) ) {
00194             return;
00195         }
00196 
00197         foreach ( $messages as $key => $value ) {
00198             fwrite( $handle, $prefix );
00199             $this->exportItemPad( $handle, $key, $value );
00200         }
00201     }
00202 
00203     protected function exportItemPad( $handle, $key, $value, $pad = 0 ) {
00204         # Add the key name
00205         fwrite( $handle, "'$key'" );
00206 
00207         # Add the appropriate block whitespace
00208         if ( $pad ) {
00209             fwrite( $handle, str_repeat( ' ', $pad - strlen( $key ) ) );
00210         }
00211 
00212         fwrite( $handle, ' => ' );
00213 
00214         if ( $this->commaToArray ) {
00215             fwrite( $handle, 'array( ' );
00216             $values = array_map( 'trim', explode( ',', $value ) );
00217             $values = array_map( array( __CLASS__, 'quote' ), $values );
00218             fwrite( $handle, implode( ', ', $values ) );
00219             fwrite( $handle, " ),\n" );
00220         } else {
00221             fwrite( $handle, self::quote( $value ) );
00222             fwrite( $handle, ",\n" );
00223         }
00224     }
00225 
00226     public static function quote( $value ) {
00227         # Check for the appropriate apostrophe and add the value
00228         # Quote \ here, because it needs always escaping
00229         $value = addcslashes( $value, '\\' );
00230 
00231         # For readability
00232         $single = "'";
00233         $double = '"';
00234         $quote = $single; // Default
00235 
00236         # It is safe to use '-quoting, unless there is '-quote in the text
00237         if ( strpos( $value, $single ) !== false ) {
00238             # In case there are no variables that need to be escaped, just use "-quote
00239             if ( strpos( $value, $double ) === false && !preg_match( '/\$[^0-9]/', $value ) ) {
00240                 $quote = $double;
00241             } else {
00242                 # Something needs quoting, so pick the quote which causes less quoting
00243                 $doubleEsc = substr_count( $value, $double ) + substr_count( $value, '$' );
00244                 $singleEsc = substr_count( $value, $single );
00245 
00246                 if ( $doubleEsc < $singleEsc ) {
00247                     $quote = $double;
00248                     $extra = '$';
00249                 } else {
00250                     $extra = '';
00251                 }
00252 
00253                 $value = addcslashes( $value, $quote . $extra );
00254             }
00255         }
00256 
00257         return $quote . $value . $quote;
00258     }
00259 }
Generated on Tue Oct 29 00:00:23 2013 for MediaWiki Translate Extension by  doxygen 1.6.3