r29955 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r29954‎ | r29955 | r29956 >
Date:19:26, 19 January 2008
Author:nikerabbit
Status:old
Tags:
Comment:
* Some hacks for multiplefile exporter
Modified paths:
  • /trunk/extensions/Translate/Exporters.php (modified) (history)
  • /trunk/extensions/Translate/MessageGroups.php (modified) (history)
  • /trunk/extensions/Translate/Translate.php (modified) (history)
  • /trunk/extensions/Translate/export.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/Exporters.php
@@ -10,28 +10,23 @@
1111 */
1212
1313 interface MessageExporter {
14 - public function __construct( $group, $languages, $target );
15 - public function export();
 14+ public function __construct( MessageGroup $group );
 15+ public function export( Array $languages, $target );
1616 }
1717
1818 class CoreExporter implements MessageExporter {
1919 protected $group = null;
20 - protected $languages = array();
21 - protected $target = null;
22 -
23 - public function __construct( $group, $languages, $target ) {
 20+ public function __construct( MessageGroup $group ) {
2421 $this->group = $group;
25 - $this->languages = $languages;
26 - $this->target = $target;
2722 }
2823
29 - public function export() {
30 - foreach ( $this->languages as $code ) {
 24+ public function export( Array $languages, $target ) {
 25+ foreach ( $languages as $code ) {
3126 $taskOptions = new TaskOptions( $code, 0, 0, 0, null );
3227 $task = TranslateTasks::getTask( 'export-to-file' );
3328 $task->init( $this->group, $taskOptions );
3429 file_put_contents(
35 - $this->target . '/'. $this->group->getMessageFile( $code ),
 30+ $target . '/'. $this->group->getMessageFile( $code ),
3631 $task->execute()
3732 );
3833 }
@@ -41,40 +36,35 @@
4237
4338 class StandardExtensionExporter implements MessageExporter {
4439 protected $group = null;
45 - protected $languages = array();
46 - protected $target = null;
47 -
48 - public function __construct( $group, $languages, $target ) {
 40+ public function __construct( MessageGroup $group ) {
4941 $this->group = $group;
50 - $this->languages = $languages;
51 - $this->target = $target;
5242 }
5343
54 - public function export() {
 44+ public function export( Array $languages, $target ) {
5545 global $wgTranslateExtensionDirectory;
5646 $filename = $this->group->getMessageFile( '' /* Ignored */ );
5747 list( $header, $sections ) = $this->parse( $wgTranslateExtensionDirectory . '/' . $filename );
5848 $output = $header;
59 - $output .= $this->exportLanguage( 'en', $sections );
60 - $output .= $this->exportLanguage( 'qqq', $sections );
 49+ $output .= $this->exportLanguage( 'en', $languages, $sections );
 50+ $output .= $this->exportLanguage( 'qqq', $languages, $sections );
6151
62 - $languages = Language::getLanguageNames( false );
63 - foreach ( array_keys( $languages ) as $code ) {
 52+ $__languages = Language::getLanguageNames( false );
 53+ foreach ( array_keys( $__languages ) as $code ) {
6454 if ( $code === 'en' || $code === 'qqq' ) continue;
65 - $output .= $this->exportLanguage( $code, $sections );
 55+ $output .= $this->exportLanguage( $code, $languages, $sections );
6656 }
6757
6858 // The hacks, aka copies of another languages
6959 $output .= implode( '', $sections );
7060
71 - $targetFile = $this->target . '/' . $filename;
 61+ $targetFile = $target . '/' . $filename;
7262 wfMkdirParents( dirname( $targetFile ) );
7363 file_put_contents( $targetFile, $output );
7464 }
7565
76 - protected function exportLanguage( $code, &$sections ) {
 66+ private function exportLanguage( $code, Array $languages, &$sections ) {
7767 $output = '';
78 - if ( in_array( $code, $this->languages ) ) {
 68+ if ( in_array( $code, $languages ) ) {
7969 $taskOptions = new TaskOptions( $code, 0, 0, 0, null );
8070 $task = TranslateTasks::getTask( 'export-to-file' );
8171 $task->init( $this->group, $taskOptions );
@@ -91,11 +81,13 @@
9282 }
9383
9484 protected function parse( $filename ) {
 85+ $var = $this->group->getVariableName();
 86+
9587 $data = file_get_contents( $filename );
9688
97 - $headerP = '
 89+ $headerP = "
9890 .*? # Ungreedily eat header
99 - \$messages \s* = \s* array\(\);';
 91+ \$$var \s* = \s* array\(\);";
10092 /*
10193 * x to have nice syntax
10294 * u for utf-8
@@ -111,7 +103,7 @@
112104 list( , $header, $data) = $matches;
113105
114106 $sectionP = '(?: /\*\* .*? \*/ )? (?: ( [^\n]*? \S;\n ) | (?: .*? \n\);\n\n ) )';
115 - $codeP = '\$messages\[\' (.*?) \'\]';
 107+ $codeP = "\$$var\[' (.*?) '\]";
116108
117109 $sectionMatches = array();
118110 if ( !preg_match_all( "~$sectionP~xsu", $data, $sectionMatches, PREG_SET_ORDER ) ) {
@@ -135,4 +127,31 @@
136128
137129 return array( $header, $sections );
138130 }
 131+}
 132+
 133+class MultipleFileExtensionExporter extends StandardExtensionExporter {
 134+ protected $header = '';
 135+
 136+ public function export( Array $languages, $target ) {
 137+ foreach ( $languages as $code ) {
 138+ $output = "<?php\n";
 139+ $output .= $this->header;
 140+ $output .= $this->exportLang( $code );
 141+
 142+ $filename = $this->group->getMessageFile( $code );
 143+ $targetFile = $target . '/' . $filename;
 144+ wfMkdirParents( dirname( $targetFile ) );
 145+ file_put_contents( $targetFile, $output );
 146+ }
 147+ }
 148+
 149+ private function exportLang( $code ) {
 150+ $output = '';
 151+ $taskOptions = new TaskOptions( $code, 0, 0, 0, null );
 152+ $task = TranslateTasks::getTask( 'export-to-file' );
 153+ $task->init( $this->group, $taskOptions );
 154+ $output = $task->execute() . "\n";
 155+ return $output;
 156+ }
 157+
139158 }
\ No newline at end of file
Index: trunk/extensions/Translate/Translate.php
@@ -36,7 +36,9 @@
3737
3838 $wgAutoloadClasses['CoreExporter'] = $dir . 'Exporters.php';
3939 $wgAutoloadClasses['StandardExtensionExporter'] = $dir . 'Exporters.php';
 40+$wgAutoloadClasses['MultipleFileExtensionExporter'] = $dir . 'Exporters.php';
4041
 42+
4143 $wgAutoloadClasses['TranslateEditAddons'] = $dir . 'TranslateEditAddons.php';
4244 $wgAutoloadClasses['languages'] = $IP . '/maintenance/language/languages.inc';
4345 $wgAutoloadClasses['MessageWriter'] = $IP . '/maintenance/language/writeMessagesArray.inc';
Index: trunk/extensions/Translate/export.php
@@ -50,10 +50,8 @@
5151 }
5252
5353 if ( $group->canExportToFile() ) {
54 - $class = $group->getFileExporter();
55 - $exporter = new $class( $group, $langs, $options['target'] );
 54+ $exporter = $group->getFileExporter();
 55+ $exporter->export( $langs, $options['target'] );
5656 } else {
5757 echo "Cannot export to file\n\n";
58 -}
59 -
60 -$exporter->export();
\ No newline at end of file
 58+}
\ No newline at end of file
Index: trunk/extensions/Translate/MessageGroups.php
@@ -63,7 +63,7 @@
6464 * Returns a class name of file exporter.
6565 */
6666 public function getFileExporter() {
67 - return $this->fileExporter;
 67+ return new $this->fileExporter( $this );
6868 }
6969
7070 /**
@@ -340,6 +340,10 @@
341341
342342 public function getMessageFile( $code ) { return $this->messageFile; }
343343
 344+ public function getVariableName() {
 345+ return $this->arrName ? $this->arrName : 'messages';
 346+ }
 347+
344348 public function getMessage( $key, $code ) {
345349 $this->load( $code );
346350 return isset( $this->mcache[$code][$key] ) ? $this->mcache[$code][$key] : null;
@@ -1081,7 +1085,7 @@
10821086 }
10831087
10841088 class FlaggedRevsMessageGroup extends MultipleFileMessageGroup {
1085 - protected $fileExporter = null;
 1089+ protected $fileExporter = 'MultipleFileExtensionExporter';
10861090 protected $label = 'Flagged Revs';
10871091 protected $id = 'ext-flaggedrevs';
10881092

Status & tagging log