r59340 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r59339‎ | r59340 | r59341 >
Date:19:00, 22 November 2009
Author:nikerabbit
Status:ok
Tags:
Comment:
Kill one ugly hack and be more error resistant
Modified paths:
  • /trunk/extensions/Translate/ffs/Gettext.php (modified) (history)

Diff [purge]

Index: trunk/extensions/Translate/ffs/Gettext.php
@@ -34,16 +34,20 @@
3535 }
3636 $data = file_get_contents( $this->filename );
3737 $parse = GettextFFS::parseGettextData( $data );
38 - // Ugly ugly hack! part 1
39 - $parse['TEMPLATE']['HEADERS'] = $parse['HEADERS'];
4038 return $parse['TEMPLATE'];
4139 }
4240
 41+ public function parseFileExt() {
 42+ if ( $this->filename === false ) {
 43+ return array();
 44+ }
 45+ $data = file_get_contents( $this->filename );
 46+ return GettextFFS::parseGettextData( $data );
 47+ }
4348
 49+
4450 public function parseMessages( StringMangler $mangler ) {
4551 $defs = $this->parseFile();
46 - // Ugly ugly hack! part 2
47 - unset( $defs['HEADERS'] );
4852 $messages = array();
4953 foreach ( $defs as $key => $def ) {
5054 if ( $this->pot ) {
@@ -69,9 +73,7 @@
7074 if ( $reader instanceof GettextFormatReader ) {
7175 $this->addAuthors( $reader->parseAuthors(), $code );
7276 $this->staticHeader = $reader->parseStaticHeader();
73 - $this->owndata = $reader->parseFile();
74 - // Ugly ugly hack! part 3
75 - $this->headers = $this->owndata['HEADERS'];
 77+ $this->owndata = $reader->parseFileExt();
7678 }
7779 if ( $readerEn instanceof GettextFormatReader ) {
7880 $this->data = $readerEn->parseFile();
@@ -91,7 +93,7 @@
9294 $label = $this->group->getLabel();
9395 $languageName = TranslateUtils::getLanguageName( $code );
9496
95 - $headers = $this->headers;
 97+ $headers = $this->owndata['HEADERS'];
9698 $headers['Project-Id-Version'] = $label;
9799 // TODO: make this customisable or something
98100 // $headers['Report-Msgid-Bugs-To'] = $wgServer;
@@ -161,8 +163,14 @@
162164 if ( isset( $this->data[$key]['ctxt'] ) ) {
163165 $ckey = $this->data[$key]['ctxt'];
164166 }
165 - fwrite( $handle, $this->formatmsg( $m->definition(), $translation, $ckey ) );
166167
 168+ $pluralForms = false;
 169+ if ( isset($this->owndata['METADATA']['plural']) ) {
 170+ $pluralForms = $this->owndata['METADATA']['plural'];
 171+ }
 172+
 173+ fwrite( $handle, $this->formatmsg( $m->definition(), $translation, $ckey, $pluralForms ) );
 174+
167175 }
168176
169177 return $out;
@@ -203,7 +211,7 @@
204212 return implode( "\n", $output ) . "\n";
205213 }
206214
207 - protected function formatmsg( $msgid, $msgstr, $msgctxt = false ) {
 215+ protected function formatmsg( $msgid, $msgstr, $msgctxt = false, $pluralForms = false ) {
208216 $output = array();
209217
210218 // FIXME: very ugly hack to allow gettext plurals to be exported.
@@ -218,7 +226,7 @@
219227 $output[] = 'msgid ' . $this->escape( $forms[0] );
220228 $output[] = 'msgid_plural ' . $this->escape( $forms[1] );
221229
222 - $forms = $this->splitPlural( $msgstr, $this->plural[1] );
 230+ $forms = $this->splitPlural( $msgstr, $pluralForms );
223231 foreach( $forms as $index => $form ) {
224232 $output[] = "msgstr[$index] " . $this->escape( $form );
225233 }
@@ -318,15 +326,6 @@
319327 throw new MWException( "Gettext file header was not found:\n\n$data" );
320328 }
321329
322 - /* At this stage we are only interested how many plurals forms we should
323 - * be expecting when parsing the rest of this file. */
324 - $pluralCount = false;
325 - if ( isset($headers['Plural-Forms']) ) {
326 - if ( preg_match( '/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches ) ) {
327 - $pluralCount = $matches[1];
328 - }
329 - }
330 -
331330 // Extract some metadata from headers for easier use
332331 $metadata = array();
333332 if ( isset($headers['X-Language-Code']) ) {
@@ -337,6 +336,15 @@
338337 $metadata['group'] = $headers['X-Message-Group'];
339338 }
340339
 340+ /* At this stage we are only interested how many plurals forms we should
 341+ * be expecting when parsing the rest of this file. */
 342+ $pluralCount = false;
 343+ if ( isset($headers['Plural-Forms']) ) {
 344+ if ( preg_match( '/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches ) ) {
 345+ $pluralCount = $metadata['plural'] = $matches[1];
 346+ }
 347+ }
 348+
341349 // Then parse the messages
342350 foreach ( $sections as $section ) {
343351 if ( trim( $section ) === '' ) continue;
@@ -380,7 +388,8 @@
381389 if ( $match !== null ) {
382390 $actualForms[] = self::formatForWiki( $match );
383391 } else {
384 - throw new MWException( "Plural $i not found, expecting total of $pluralCount" );
 392+ $actualForms[] = '';
 393+ error_log( "Plural $i not found, expecting total of $pluralCount for {$item['id']}" );
385394 }
386395 }
387396

Status & tagging log