summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/ffs/GettextFFS.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/ffs/GettextFFS.php')
-rw-r--r--www/wiki/extensions/Translate/ffs/GettextFFS.php139
1 files changed, 78 insertions, 61 deletions
diff --git a/www/wiki/extensions/Translate/ffs/GettextFFS.php b/www/wiki/extensions/Translate/ffs/GettextFFS.php
index 1bd86e75..c404da34 100644
--- a/www/wiki/extensions/Translate/ffs/GettextFFS.php
+++ b/www/wiki/extensions/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'];
}
@@ -135,7 +135,6 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender {
// 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,7 +308,13 @@ 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;
@@ -318,7 +322,13 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender {
$snippet = preg_replace( "/[^$wgLegalTitleChars]/", ' ', $snippet );
$snippet = preg_replace( "/[:&%\/_]/", ' ', $snippet );
$snippet = preg_replace( '/ {2,}/', ' ', $snippet );
- $snippet = $lang->truncate( $snippet, 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( $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" );
@@ -375,10 +385,8 @@ class GettextFFS extends SimpleFFS implements MetaYamlSchemaExtender {
/** @var TMessage $m */
foreach ( $collection as $key => $m ) {
- $transTemplate = isset( $template['TEMPLATE'][$key] ) ?
- $template['TEMPLATE'][$key] : array();
- $potTemplate = isset( $pot['TEMPLATE'][$key] ) ?
- $pot['TEMPLATE'][$key] : array();
+ $transTemplate = $template['TEMPLATE'][$key] ?? [];
+ $potTemplate = $pot['TEMPLATE'][$key] ?? [];
$output .= $this->formatMessageBlock( $key, $m, $transTemplate, $potTemplate, $pluralCount );
}
@@ -409,7 +417,7 @@ PHP;
// Make sure there is no empty line before msgid
$output = trim( $output ) . "\n";
- $specs = isset( $template['HEADERS'] ) ? $template['HEADERS'] : array();
+ $specs = $template['HEADERS'] ?? [];
$timestamp = wfTimestampNow();
$specs['PO-Revision-Date'] = self::formatTime( $timestamp );
@@ -420,8 +428,8 @@ PHP;
}
$specs['Content-Type'] = 'text/plain; charset=UTF-8';
$specs['Content-Transfer-Encoding'] = '8bit';
- $specs['Language'] = wfBCP47( $this->group->mapCode( $code ) );
- Hooks::run( 'Translate:GettextFFS:headerFields', array( &$specs, $this->group, $code ) );
+ $specs['Language'] = LanguageCode::bcp47( $this->group->mapCode( $code ) );
+ Hooks::run( 'Translate:GettextFFS:headerFields', [ &$specs, $this->group, $code ] );
$specs['X-Generator'] = $this->getGenerator();
if ( $this->offlineMode ) {
@@ -436,7 +444,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 +485,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 ) {
@@ -527,10 +535,10 @@ PHP;
if ( $flags ) {
sort( $flags );
- $header .= '#, ' . implode( ', ', array_unique( $flags ) ) . "\n";
+ $header .= '#, ' . implode( ', ', array_unique( $flags ) ) . "\n";
}
- $output = $header ? $header : "#\n";
+ $output = $header ?: "#\n";
$output .= $content . "\n";
return $output;
@@ -608,8 +616,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 +644,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 +673,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;
}