diff options
author | Brian Evans <grknight@gentoo.org> | 2018-11-20 10:51:06 -0500 |
---|---|---|
committer | Brian Evans <grknight@gentoo.org> | 2018-11-20 10:51:06 -0500 |
commit | 1ea74fa59d8d1c6c12d20be6c8e7d5ac7f370fdb (patch) | |
tree | ad113bd05db878a61b503938c05fe046eca25ee0 /MLEB/Translate/ffs/GettextFFS.php | |
parent | LinkAttributes: Update to v0.2 (diff) | |
download | extensions-1ea74fa59d8d1c6c12d20be6c8e7d5ac7f370fdb.tar.gz extensions-1ea74fa59d8d1c6c12d20be6c8e7d5ac7f370fdb.tar.bz2 extensions-1ea74fa59d8d1c6c12d20be6c8e7d5ac7f370fdb.zip |
Update to MediaWikiLanguageExtensionBundle-2018.10
Signed-off-by: Brian Evans <grknight@gentoo.org>
Diffstat (limited to 'MLEB/Translate/ffs/GettextFFS.php')
-rw-r--r-- | MLEB/Translate/ffs/GettextFFS.php | 153 |
1 files changed, 86 insertions, 67 deletions
diff --git a/MLEB/Translate/ffs/GettextFFS.php b/MLEB/Translate/ffs/GettextFFS.php index f1e8e4d0..7176e953 100644 --- a/MLEB/Translate/ffs/GettextFFS.php +++ b/MLEB/Translate/ffs/GettextFFS.php @@ -5,14 +5,14 @@ * @author Niklas Laxström * @author Siebrand Mazeland * @copyright Copyright © 2008-2010, Niklas Laxström, Siebrand Mazeland - * @license GPL-2.0+ + * @license GPL-2.0-or-later * @file */ /** * Identifies Gettext plural exceptions. */ -class GettextPluralException extends MwException { +class GettextPluralException extends MWException { } /** @@ -25,13 +25,13 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { } public function getFileExtensions() { - return array( '.pot', '.po' ); + return [ '.pot', '.po' ]; } protected $offlineMode = false; /** - * @param $value bool + * @param bool $value */ public function setOfflineMode( $value ) { $this->offlineMode = $value; @@ -43,7 +43,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { */ public function readFromVariable( $data ) { # Authors first - $matches = array(); + $matches = []; preg_match_all( '/^#\s*Author:\s*(.*)$/m', $data, $matches ); $authors = $matches[1]; @@ -63,7 +63,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { public function parseGettext( $data ) { $mangler = $this->group->getMangler(); $useCtxtAsKey = isset( $this->extra['CtxtAsKey'] ) && $this->extra['CtxtAsKey']; - $keyAlgorithm = 'legacy'; + $keyAlgorithm = 'simple'; if ( isset( $this->extra['keyAlgorithm'] ) ) { $keyAlgorithm = $this->extra['keyAlgorithm']; } @@ -111,11 +111,11 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { throw new MWException( "Gettext file header was not found:\n\n$data" ); } - $template = array(); - $messages = array(); + $template = []; + $messages = []; // Extract some metadata from headers for easier use - $metadata = array(); + $metadata = []; if ( isset( $headers['X-Language-Code'] ) ) { $metadata['code'] = $headers['X-Language-Code']; } @@ -127,15 +127,14 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { /* At this stage we are only interested how many plurals forms we should * be expecting when parsing the rest of this file. */ $pluralCount = false; - if ( isset( $headers['Plural-Forms'] ) ) { - if ( preg_match( '/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches ) ) { - $pluralCount = $metadata['plural'] = $matches[1]; - } + if ( isset( $headers['Plural-Forms'] ) && + preg_match( '/nplurals=([0-9]+).*;/', $headers['Plural-Forms'], $matches ) + ) { + $pluralCount = $metadata['plural'] = $matches[1]; } // Then parse the messages foreach ( $sections as $section ) { - $item = self::parseGettextSection( $section, $pluralCount, $metadata ); if ( $item === false ) { continue; @@ -156,16 +155,15 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { $template[$key] = $item; } - return array( + return [ 'MESSAGES' => $messages, 'TEMPLATE' => $template, 'METADATA' => $metadata, 'HEADERS' => $headers - ); + ]; } public static function parseGettextSection( $section, $pluralCount, &$metadata ) { - if ( trim( $section ) === '' ) { return false; } @@ -178,13 +176,13 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { return false; } - $item = array( + $item = [ 'ctxt' => false, 'id' => '', 'str' => '', - 'flags' => array(), - 'comments' => array(), - ); + 'flags' => [], + 'comments' => [], + ]; $match = self::expectKeyword( 'msgid', $section ); if ( $match !== null ) { @@ -233,7 +231,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { $item['flags'] = $flags; // Rest of the comments - $matches = array(); + $matches = []; if ( preg_match_all( '/^#(.?) (.*)$/m', $section, $matches, PREG_SET_ORDER ) ) { foreach ( $matches as $match ) { if ( $match[1] !== ',' && strpos( $match[1], '[Wiki]' ) !== 0 ) { @@ -246,7 +244,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { } public static function processGettextPluralMessage( $pluralCount, $section ) { - $actualForms = array(); + $actualForms = []; for ( $i = 0; $i < $pluralCount; $i++ ) { $match = self::expectKeyword( "msgstr\\[$i\\]", $section ); @@ -267,11 +265,11 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { } public static function parseFlags( $section ) { - $matches = array(); + $matches = []; if ( preg_match( '/^#,(.*)$/mu', $section, $matches ) ) { return array_map( 'trim', explode( ',', $matches[1] ) ); } else { - return array(); + return []; } } @@ -281,7 +279,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { */ $poformat = '".*"\n?(^".*"$\n?)*'; - $matches = array(); + $matches = []; if ( preg_match( "/^$name\s($poformat)/mx", $section, $matches ) ) { return $matches[1]; } else { @@ -296,7 +294,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { * @param string $algorithm Algorithm used to generate message keys: simple or legacy * @return string */ - public static function generateKeyFromItem( array $item, $algorithm = 'legacy' ) { + public static function generateKeyFromItem( array $item, $algorithm = 'simple' ) { $lang = Language::factory( 'en' ); if ( $item['ctxt'] === '' ) { @@ -310,15 +308,27 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { if ( $algorithm === 'simple' ) { $hash = substr( $hash, 0, 6 ); - $snippet = $lang->truncate( $item['id'], 30, '' ); + if ( !is_callable( [ $lang, 'truncateForDatabase' ] ) ) { + // Backwards compatibility code; remove once MW 1.30 is + // no longer supported (aka once MW 1.33 is released) + $snippet = $lang->truncate( $item['id'], 30, '' ); + } else { + $snippet = $lang->truncateForDatabase( $item['id'], 30, '' ); + } $snippet = str_replace( ' ', '_', trim( $snippet ) ); } else { // legacy global $wgLegalTitleChars; $snippet = $item['id']; $snippet = preg_replace( "/[^$wgLegalTitleChars]/", ' ', $snippet ); $snippet = preg_replace( "/[:&%\/_]/", ' ', $snippet ); - $snippet = preg_replace( "/ {2,}/", ' ', $snippet ); - $snippet = $lang->truncate( $snippet, 30, '' ); + $snippet = preg_replace( '/ {2,}/', ' ', $snippet ); + if ( !is_callable( [ $lang, 'truncateForDatabase' ] ) ) { + // Backwards compatibility code; remove once MW 1.30 is + // no longer supported (aka once MW 1.33 is released) + $snippet = $lang->truncate( $snippet, 30, '' ); + } else { + $snippet = $lang->truncateForDatabase( $snippet, 30, '' ); + } $snippet = str_replace( ' ', '_', trim( $snippet ) ); } @@ -330,8 +340,8 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { * not allowed in MediaWiki pages, the default action is to append * \-character at the end of the message. You can also choose to ignore it * and use the trim action instead. - * @param $data - * @param $whitespace string + * @param string $data + * @param string $whitespace * @throws MWException * @return string */ @@ -355,7 +365,7 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { } public static function parseHeaderTags( $headers ) { - $tags = array(); + $tags = []; foreach ( explode( "\n", $headers ) as $line ) { if ( strpos( $line, ':' ) === false ) { error_log( __METHOD__ . ": $line" ); @@ -376,9 +386,9 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender { /** @var TMessage $m */ foreach ( $collection as $key => $m ) { $transTemplate = isset( $template['TEMPLATE'][$key] ) ? - $template['TEMPLATE'][$key] : array(); + $template['TEMPLATE'][$key] : []; $potTemplate = isset( $pot['TEMPLATE'][$key] ) ? - $pot['TEMPLATE'][$key] : array(); + $pot['TEMPLATE'][$key] : []; $output .= $this->formatMessageBlock( $key, $m, $transTemplate, $potTemplate, $pluralCount ); } @@ -409,7 +419,7 @@ PHP; // Make sure there is no empty line before msgid $output = trim( $output ) . "\n"; - $specs = isset( $template['HEADERS'] ) ? $template['HEADERS'] : array(); + $specs = isset( $template['HEADERS'] ) ? $template['HEADERS'] : []; $timestamp = wfTimestampNow(); $specs['PO-Revision-Date'] = self::formatTime( $timestamp ); @@ -420,8 +430,8 @@ PHP; } $specs['Content-Type'] = 'text/plain; charset=UTF-8'; $specs['Content-Transfer-Encoding'] = '8bit'; - $specs['Language'] = wfBCP47( $this->group->mapCode( $code ) ); - wfRunHooks( 'Translate:GettextFFS:headerFields', array( &$specs, $this->group, $code ) ); + $specs['Language'] = TranslateUtils::bcp47( $this->group->mapCode( $code ) ); + Hooks::run( 'Translate:GettextFFS:headerFields', [ &$specs, $this->group, $code ] ); $specs['X-Generator'] = $this->getGenerator(); if ( $this->offlineMode ) { @@ -436,7 +446,7 @@ PHP; $specs['Plural-Forms'] = 'nplurals=2; plural=(n != 1);'; } - $match = array(); + $match = []; preg_match( '/nplurals=(\d+);/', $specs['Plural-Forms'], $match ); $pluralCount = $match[1]; @@ -477,14 +487,14 @@ PHP; $header = $this->formatDocumentation( $key ); $content = ''; - $comments = self::chainGetter( 'comments', $pot, $trans, array() ); + $comments = self::chainGetter( 'comments', $pot, $trans, [] ); foreach ( $comments as $type => $typecomments ) { foreach ( $typecomments as $comment ) { $header .= "#$type $comment\n"; } } - $flags = self::chainGetter( 'flags', $pot, $trans, array() ); + $flags = self::chainGetter( 'flags', $pot, $trans, [] ); $flags = array_merge( $m->getTags(), $flags ); if ( $this->offlineMode ) { @@ -506,13 +516,13 @@ PHP; if ( preg_match( '/{{PLURAL:GETTEXT/i', $msgid ) ) { $forms = $this->splitPlural( $msgid, 2 ); - $content .= 'msgid ' . $this->escape( $forms[0] ) . "\n"; - $content .= 'msgid_plural ' . $this->escape( $forms[1] ) . "\n"; + $content .= 'msgid ' . self::escape( $forms[0] ) . "\n"; + $content .= 'msgid_plural ' . self::escape( $forms[1] ) . "\n"; try { $forms = $this->splitPlural( $msgstr, $pluralCount ); foreach ( $forms as $index => $form ) { - $content .= "msgstr[$index] " . $this->escape( $form ) . "\n"; + $content .= "msgstr[$index] " . self::escape( $form ) . "\n"; } } catch ( GettextPluralException $e ) { $flags[] = 'invalid-plural'; @@ -527,7 +537,7 @@ PHP; if ( $flags ) { sort( $flags ); - $header .= "#, " . implode( ', ', array_unique( $flags ) ) . "\n"; + $header .= '#, ' . implode( ', ', array_unique( $flags ) ) . "\n"; } $output = $header ? $header : "#\n"; @@ -567,7 +577,7 @@ PHP; protected function getGenerator() { return 'MediaWiki ' . SpecialVersion::getVersion() . - "; Translate " . TRANSLATE_VERSION; + '; Translate ' . TRANSLATE_VERSION; } protected function formatDocumentation( $key ) { @@ -608,8 +618,8 @@ PHP; /** * Returns plural rule for Gettext. - * @param $code \string Language code. - * @return \string + * @param string $code Language code. + * @return string */ public static function getPluralRule( $code ) { $rulefile = __DIR__ . '/../data/plural-gettext.txt'; @@ -636,13 +646,13 @@ PHP; # |/| is commonly used in KDE to support inflections $text = str_replace( '|/|', $placeholder, $text ); - $plurals = array(); + $plurals = []; $match = preg_match_all( '/{{PLURAL:GETTEXT\|(.*)}}/iUs', $text, $plurals ); if ( !$match ) { throw new GettextPluralException( "Failed to find plural in: $text" ); } - $splitPlurals = array(); + $splitPlurals = []; for ( $i = 0; $i < $forms; $i++ ) { # Start with the hole string $pluralForm = $text; @@ -665,29 +675,38 @@ PHP; return $splitPlurals; } + public function shouldOverwrite( $a, $b ) { + $regex = '/^"(.+)-Date: \d\d\d\d-\d\d-\d\d \d\d:\d\d:\d\d\+\d\d\d\d\\\\n"$/m'; + + $a = preg_replace( $regex, '', $a ); + $b = preg_replace( $regex, '', $b ); + + return $a !== $b; + } + public static function getExtraSchema() { - $schema = array( - 'root' => array( + $schema = [ + 'root' => [ '_type' => 'array', - '_children' => array( - 'FILES' => array( + '_children' => [ + 'FILES' => [ '_type' => 'array', - '_children' => array( - 'header' => array( + '_children' => [ + 'header' => [ '_type' => 'text', - ), - 'keyAlgorithm' => array( + ], + 'keyAlgorithm' => [ '_type' => 'enum', - '_values' => array( 'simple', 'legacy' ), - ), - 'CtxtAsKey' => array( + '_values' => [ 'simple', 'legacy' ], + ], + 'CtxtAsKey' => [ '_type' => 'boolean', - ), - ) - ) - ) - ) - ); + ], + ] + ] + ] + ] + ]; return $schema; } |