r37610 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r37609‎ | r37610 | r37611 >
Date:13:08, 13 July 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* 2008-07-13:1 experimental alias-export for extensions
Modified paths:
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/README (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/_autoload.php (modified) (history)
  • /trunk/extensions/Translate/ffs/Wiki.php (modified) (history)
  • /trunk/extensions/Translate/scripts/alias-export.php (added) (history)

Diff [purge]

Index: trunk/extensions/Translate/Translate.php
@@ -11,7 +11,7 @@
1212 * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
1313 */
1414
15 -define( 'TRANSLATE_VERSION', '9 (2008-07-08:1)' );
 15+define( 'TRANSLATE_VERSION', '9 (2008-07-13:1)' );
1616
1717 $wgExtensionCredits['specialpage'][] = array(
1818 'name' => 'Translate',
@@ -25,6 +25,7 @@
2626 // Setup class autoloads
2727 $dir = dirname(__FILE__) . '/';
2828 require_once( $dir . '_autoload.php' );
 29+#require_once( $dir . 'MessageFunctions.php' );
2930
3031 $wgExtensionMessagesFiles['Translate'] = $dir . 'Translate.i18n.php';
3132 $wgExtensionAliasesFiles['Translate'] = $dir . 'Translate.alias.php';
Index: trunk/extensions/Translate/scripts/alias-export.php
@@ -0,0 +1,84 @@
 2+<?php
 3+/**
 4+ * Script to export translations of one message group to file(s).
 5+ *
 6+ * @author Niklas Laxstrom
 7+ *
 8+ * @copyright Copyright © 2008, Niklas Laxström
 9+ * @license http://www.gnu.org/copyleft/gpl.html GNU General Public License 2.0 or later
 10+ * @file
 11+ */
 12+
 13+$optionsWithArgs = array( 'lang', 'target', 'group' );
 14+require( dirname(__FILE__) . '/cli.inc' );
 15+
 16+function showUsage() {
 17+ STDERR( <<<EOT
 18+Alias exporter.
 19+
 20+Usage: php aliasexport.php [options...]
 21+
 22+Options:
 23+ --target Target directory for exported files
 24+ --lang Comma separated list of language codes
 25+ --group Group id
 26+EOT
 27+);
 28+ exit( 1 );
 29+}
 30+
 31+if ( isset($options['help']) || $args === 1 ) {
 32+ showUsage();
 33+}
 34+
 35+if ( !isset($options['target']) ) {
 36+ STDERR( "You need to specify target directory" );
 37+ exit(1);
 38+}
 39+if ( !isset($options['lang']) ) {
 40+ STDERR( "You need to specify languages to export" );
 41+ exit(1);
 42+}
 43+
 44+if ( !is_writable( $options['target'] ) ) {
 45+ STDERR( "Target directory is not writable" );
 46+ exit(1);
 47+}
 48+
 49+$langs = array_map( 'trim', explode( ',', $options['lang'] ) );
 50+
 51+
 52+if ( !file_exists(TRANSLATE_ALIASFILE) || !is_readable(TRANSLATE_ALIASFILE) ) {
 53+ STDERR( "Alias file not defined" );
 54+ exit(1);
 55+}
 56+
 57+$defines = file_get_contents( TRANSLATE_ALIASFILE );
 58+$sections = preg_split( "/\n\n/", $defines, -1, PREG_SPLIT_NO_EMPTY );
 59+
 60+foreach ( $sections as $section ) {
 61+ $lines = array_map( 'trim', preg_split( "/\n/", $section ) );
 62+ $name = '';
 63+ foreach ( $lines as $line ) {
 64+ if ( $line === '' ) continue;
 65+ if ( strpos( $line, '=' ) === false ) {
 66+ if ( $name === '' ) {
 67+ $name = $line;
 68+ } else {
 69+ throw new MWException( "Trying to define name twice: " . $line );
 70+ }
 71+ } else {
 72+ list( $key, $value ) = array_map( 'trim', explode( '=', $line, 2 ) );
 73+ if ( $key === 'file' ) $file = $value;
 74+ }
 75+ }
 76+
 77+ if ( $name !== '' ) {
 78+ // Fake a group
 79+ $group = new AliasMessageGroup( $name );
 80+ $group->setMessageFile( $file );
 81+ $group->setVariableName( 'aliases' );
 82+ $writer = $group->getWriter();
 83+ $writer->fileExport( $langs, $options['target'] );
 84+ }
 85+}
Property changes on: trunk/extensions/Translate/scripts/alias-export.php
___________________________________________________________________
Added: svn:eol-style
186 + native
Index: trunk/extensions/Translate/_autoload.php
@@ -19,6 +19,7 @@
2020
2121 $wgAutoloadClasses['MessageChecks'] = $dir . 'MessageChecks.php';
2222 $wgAutoloadClasses['MessageGroups'] = $dir . 'MessageGroups.php';
 23+$wgAutoloadClasses['AliasMessageGroup'] = $dir . 'MessageGroups.php';
2324
2425 $wgAutoloadClasses['MessageCollection'] = $dir . 'Message.php';
2526 $wgAutoloadClasses['TMessage'] = $dir . 'Message.php';
Index: trunk/extensions/Translate/README
@@ -34,6 +34,7 @@
3535
3636 == Changes in version 10 ==
3737
 38+* 2008-07-13:1 experimental alias-export for extensions
3839 * 2008-07-08:1 simple edit stats with phplot
3940 * 2008-07-05:1 fuzzy.php was ignoring the namespace
4041 * 2008-07-04:2
Index: trunk/extensions/Translate/MessageGroups.php
@@ -371,6 +371,57 @@
372372 }
373373 }
374374
 375+class AliasMessageGroup extends ExtensionMessageGroup {
 376+
 377+ public function fillCollection( MessageCollection $collection ) {
 378+ $this->fill( $collection );
 379+ $this->fillContents( $collection );
 380+ }
 381+
 382+
 383+ function fill( MessageCollection $messages ) {
 384+ $cache = $this->load( $messages->code );
 385+ foreach ( $messages->keys() as $key ) {
 386+ if ( isset($cache[$key]) ) {
 387+ if ( is_array($cache[$key]) ) {
 388+ $messages[$key]->infile = implode( ',', $cache[$key] );
 389+ } else {
 390+ $messages[$key]->infile = $cache[$key];
 391+ }
 392+ }
 393+ }
 394+ }
 395+
 396+ public function fillContents( MessageCollection $collection ) {
 397+ $data = TranslateUtils::getMessageContent( 'sp-translate-data-SpecialPageAliases', $collection->code );
 398+
 399+ if ( !$data ) return;
 400+
 401+ $lines = array_map( 'trim', explode( "\n", $data ) );
 402+ $array = array();
 403+ foreach ( $lines as $line ) {
 404+ if ( $line === '' || $line[0] === '#' || $line[0] === '<' ) continue;
 405+ if ( strpos( $line, '=' ) === false ) continue;
 406+
 407+ list( $name, $values ) = array_map( 'trim', explode( '=', $line, 2 ) );
 408+ if ( $name === '' || $values === '' ) continue;
 409+
 410+ if ( isset($collection[$name]) ) {
 411+ $collection[$name]->database = $values;
 412+ }
 413+ }
 414+
 415+ }
 416+
 417+ public function getWriter() {
 418+ $writer = new WikiExtensionFormatWriter( $this );
 419+ $writer->variableName = $this->getVariableName();
 420+ $writer->commaToArray = true;
 421+ return $writer;
 422+ }
 423+
 424+}
 425+
375426 class CoreMostUsedMessageGroup extends CoreMessageGroup {
376427 protected $label = 'MediaWiki messages (most used)';
377428 protected $id = 'core-mostused';
Index: trunk/extensions/Translate/ffs/Wiki.php
@@ -58,6 +58,7 @@
5959 }
6060
6161 class WikiFormatWriter extends SimpleFormatWriter {
 62+ public $commaToArray = false;
6263
6364 public function makeHeader( $handle, $code ) {
6465 list( $name, $native ) = $this->getLanguageNames($code);
@@ -156,6 +157,19 @@
157158 fwrite( $handle, str_repeat( ' ', $pad - strlen($key) ) );
158159 fwrite( $handle, ' => ' );
159160
 161+ if ( $this->commaToArray ) {
 162+ fwrite( $handle, 'array( ' );
 163+ $values = array_map( 'trim', explode( ',', $value ) );
 164+ $values = array_map( array( __CLASS__, 'quote' ), $values );
 165+ fwrite( $handle, implode( ', ', $values ) );
 166+ fwrite( $handle, " ),\n" );
 167+ } else {
 168+ fwrite( $handle, self::quote($value) );
 169+ fwrite( $handle, ",\n" );
 170+ }
 171+ }
 172+
 173+ public static function quote( $value ) {
160174 # Check for the appropriate apostrophe and add the value
161175 # Quote \ here, because it needs always escaping
162176 $value = addcslashes( $value, '\\' );
@@ -188,7 +202,6 @@
189203 }
190204 }
191205
192 - fwrite( $handle, $quote . $value . $quote );
193 - fwrite( $handle, ",\n" );
 206+ return $quote . $value . $quote;
194207 }
195208 }

Status & tagging log