summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/MessageChecks.php
diff options
context:
space:
mode:
Diffstat (limited to 'www/wiki/extensions/Translate/MessageChecks.php')
-rw-r--r--www/wiki/extensions/Translate/MessageChecks.php130
1 files changed, 71 insertions, 59 deletions
diff --git a/www/wiki/extensions/Translate/MessageChecks.php b/www/wiki/extensions/Translate/MessageChecks.php
index df6d6046..27037847 100644
--- a/www/wiki/extensions/Translate/MessageChecks.php
+++ b/www/wiki/extensions/Translate/MessageChecks.php
@@ -5,8 +5,7 @@
* @file
* @defgroup MessageCheckers Message Checkers
* @author Niklas Laxström
- * @copyright Copyright © 2008-2013, Niklas Laxström
- * @license GPL-2.0+
+ * @license GPL-2.0-or-later
*/
/**
@@ -45,23 +44,23 @@
* @ingroup MessageCheckers
*/
class MessageChecker {
- protected $checks = array();
- protected $group = null;
- private static $globalBlacklist = null;
+ protected $checks = [];
+ protected $group;
+ private static $globalBlacklist;
/**
* Constructs a suitable checker for given message group.
- * @param $group MessageGroup
+ * @param MessageGroup $group
*/
public function __construct( MessageGroup $group ) {
global $wgTranslateCheckBlacklist;
if ( $wgTranslateCheckBlacklist === false ) {
- self::$globalBlacklist = array();
+ self::$globalBlacklist = [];
} elseif ( self::$globalBlacklist === null ) {
$file = $wgTranslateCheckBlacklist;
$list = PHPVariableLoader::loadVariableFromPHPFile( $file, 'checkBlacklist' );
- $keys = array( 'group', 'check', 'subcheck', 'code', 'message' );
+ $keys = [ 'group', 'check', 'subcheck', 'code', 'message' ];
foreach ( $list as $key => $pattern ) {
foreach ( $keys as $checkKey ) {
@@ -69,7 +68,7 @@ class MessageChecker {
$list[$key][$checkKey] = '#';
} elseif ( is_array( $pattern[$checkKey] ) ) {
$list[$key][$checkKey] =
- array_map( array( $this, 'foldValue' ), $pattern[$checkKey] );
+ array_map( [ $this, 'foldValue' ], $pattern[$checkKey] );
} else {
$list[$key][$checkKey] = $this->foldValue( $pattern[$checkKey] );
}
@@ -95,7 +94,7 @@ class MessageChecker {
* Set the tests for this checker. Array of callables with descriptive keys.
* @param array $checks List of checks (suitable methods in this class)
*/
- public function setChecks( $checks ) {
+ public function setChecks( array $checks ) {
foreach ( $checks as $k => $c ) {
if ( !is_callable( $c ) ) {
unset( $checks[$k] );
@@ -110,10 +109,8 @@ class MessageChecker {
* @see setChecks()
* @param callable $check
*/
- public function addCheck( $check ) {
- if ( is_callable( $check ) ) {
- $this->checks[] = $check;
- }
+ public function addCheck( callable $check ) {
+ $this->checks[] = $check;
}
/**
@@ -125,16 +122,16 @@ class MessageChecker {
* @return array
*/
public function checkMessage( TMessage $message, $code ) {
- $warningsArray = array();
- $messages = array( $message );
+ $warningsArray = [];
+ $messages = [ $message ];
foreach ( $this->checks as $check ) {
- call_user_func_array( $check, array( $messages, $code, &$warningsArray ) );
+ call_user_func_array( $check, [ $messages, $code, &$warningsArray ] );
}
$warningsArray = $this->filterWarnings( $warningsArray );
if ( !count( $warningsArray ) ) {
- return array();
+ return [];
}
$warnings = $warningsArray[$message->key()];
@@ -150,11 +147,11 @@ class MessageChecker {
* @return bool True if there is a problem, false otherwise.
*/
public function checkMessageFast( TMessage $message, $code ) {
- $warningsArray = array();
- $messages = array( $message );
+ $warningsArray = [];
+ $messages = [ $message ];
foreach ( $this->checks as $check ) {
- call_user_func_array( $check, array( $messages, $code, &$warningsArray ) );
+ call_user_func_array( $check, [ $messages, $code, &$warningsArray ] );
if ( count( $warningsArray ) ) {
return true;
}
@@ -168,7 +165,7 @@ class MessageChecker {
* @param array $warningsArray List of warnings produces by checkMessage().
* @return array List of filtered warnings.
*/
- protected function filterWarnings( $warningsArray ) {
+ protected function filterWarnings( array $warningsArray ) {
$groupId = $this->group->getId();
// There is an array of messages...
@@ -226,12 +223,12 @@ class MessageChecker {
* @throws MWException
* @return array List of warning messages with parameters.
*/
- protected function fixMessageParams( $warnings ) {
+ protected function fixMessageParams( array $warnings ) {
$lang = RequestContext::getMain()->getLanguage();
foreach ( $warnings as $wkey => $warning ) {
array_shift( $warning );
- $message = array( array_shift( $warning ) );
+ $message = [ array_shift( $warning ) ];
foreach ( $warning as $param ) {
if ( !is_array( $param ) ) {
@@ -259,8 +256,8 @@ class MessageChecker {
* @param array $trans
* @return array Items of $defs that are not in $trans.
*/
- protected static function compareArrays( $defs, $trans ) {
- $missing = array();
+ protected static function compareArrays( array $defs, array $trans ) {
+ $missing = [];
foreach ( $defs as $defVar ) {
if ( !in_array( $defVar, $trans ) ) {
@@ -276,7 +273,7 @@ class MessageChecker {
* translations.
* @param TMessage[] $messages Iterable list of TMessage objects.
* @param string $code Language code
- * @param array $warnings Array where warnings are appended to.
+ * @param array &$warnings Array where warnings are appended to.
*/
protected function printfCheck( $messages, $code, array &$warnings ) {
$this->parameterCheck( $messages, $code, $warnings, '/%(\d+\$)?[sduf]/U' );
@@ -287,7 +284,7 @@ class MessageChecker {
* translations.
* @param TMessage[] $messages Iterable list of TMessage objects.
* @param string $code Language code
- * @param array $warnings Array where warnings are appended to.
+ * @param array &$warnings Array where warnings are appended to.
*/
protected function rubyVariableCheck( $messages, $code, array &$warnings ) {
$this->parameterCheck( $messages, $code, $warnings, '/%{[a-zA-Z_]+}/' );
@@ -298,7 +295,7 @@ class MessageChecker {
* translations.
* @param TMessage[] $messages Iterable list of TMessage objects.
* @param string $code Language code
- * @param array $warnings Array where warnings are appended to.
+ * @param array &$warnings Array where warnings are appended to.
*/
protected function pythonInterpolationCheck( $messages, $code, array &$warnings ) {
$pattern = '/\%\([a-zA-Z0-9]*?\)[diouxXeEfFgGcrs]/U';
@@ -308,9 +305,12 @@ class MessageChecker {
/**
* Checks if the translation has even number of opening and closing
* parentheses. {, [ and ( are checked.
+ * Note that this will not add a warning if the message definition
+ * has an unbalanced amount of braces.
+ *
* @param TMessage[] $messages Iterable list of TMessage objects.
* @param string $code Language code
- * @param array $warnings Array where warnings are appended to.
+ * @param array &$warnings Array where warnings are appended to.
*/
protected function braceBalanceCheck( $messages, $code, array &$warnings ) {
foreach ( $messages as $message ) {
@@ -319,11 +319,11 @@ class MessageChecker {
$translation = preg_replace( '/[^{}[\]()]/u', '', $translation );
$subcheck = 'brace';
- $counts = array(
+ $counts = [
'{' => 0, '}' => 0,
'[' => 0, ']' => 0,
'(' => 0, ')' => 0,
- );
+ ];
$len = strlen( $translation );
for ( $i = 0; $i < $len; $i++ ) {
@@ -331,36 +331,48 @@ class MessageChecker {
$counts[$char]++;
}
- $balance = array();
- if ( $counts['['] !== $counts[']'] ) {
+ $definition = $message->definition();
+
+ $balance = [];
+ if ( $counts['['] !== $counts[']'] && self::checkStringCountEqual( $definition, '[', ']' ) ) {
$balance[] = '[]: ' . ( $counts['['] - $counts[']'] );
}
- if ( $counts['{'] !== $counts['}'] ) {
+ if ( $counts['{'] !== $counts['}'] && self::checkStringCountEqual( $definition, '{', '}' ) ) {
$balance[] = '{}: ' . ( $counts['{'] - $counts['}'] );
}
- if ( $counts['('] !== $counts[')'] ) {
+ if ( $counts['('] !== $counts[')'] && self::checkStringCountEqual( $definition, '(', ')' ) ) {
$balance[] = '(): ' . ( $counts['('] - $counts[')'] );
}
if ( count( $balance ) ) {
- $warnings[$key][] = array(
- array( 'balance', $subcheck, $key, $code ),
+ $warnings[$key][] = [
+ [ 'balance', $subcheck, $key, $code ],
'translate-checks-balance',
- array( 'PARAMS', $balance ),
- array( 'COUNT', count( $balance ) ),
- );
+ [ 'PARAMS', $balance ],
+ [ 'COUNT', count( $balance ) ],
+ ];
}
}
}
/**
+ * @param string $source
+ * @param string $str1
+ * @param string $str2
+ * @return bool whether $source has an equal number of occurences of $str1 and $str2
+ */
+ protected static function checkStringCountEqual( $source, $str1, $str2 ) {
+ return substr_count( $source, $str1 ) === substr_count( $source, $str2 );
+ }
+
+ /**
* Checks for missing and unknown printf formatting characters in
* translations.
* @param TMessage[] $messages Iterable list of TMessage objects.
* @param string $code Language code
- * @param array $warnings Array where warnings are appended to.
+ * @param array &$warnings Array where warnings are appended to.
* @param string $pattern Regular expression for matching variables.
*/
protected function parameterCheck( $messages, $code, array &$warnings, $pattern ) {
@@ -377,12 +389,12 @@ class MessageChecker {
$params = self::compareArrays( $defVars[0], $transVars[0] );
if ( count( $params ) ) {
- $warnings[$key][] = array(
- array( 'variable', $subcheck, $key, $code ),
+ $warnings[$key][] = [
+ [ 'variable', $subcheck, $key, $code ],
'translate-checks-parameters',
- array( 'PARAMS', $params ),
- array( 'COUNT', count( $params ) ),
- );
+ [ 'PARAMS', $params ],
+ [ 'COUNT', count( $params ) ],
+ ];
}
// Check for unknown variables in the translatio
@@ -390,12 +402,12 @@ class MessageChecker {
$params = self::compareArrays( $transVars[0], $defVars[0] );
if ( count( $params ) ) {
- $warnings[$key][] = array(
- array( 'variable', $subcheck, $key, $code ),
+ $warnings[$key][] = [
+ [ 'variable', $subcheck, $key, $code ],
'translate-checks-parameters-unknown',
- array( 'PARAMS', $params ),
- array( 'COUNT', count( $params ) ),
- );
+ [ 'PARAMS', $params ],
+ [ 'COUNT', count( $params ) ],
+ ];
}
}
}
@@ -403,7 +415,7 @@ class MessageChecker {
/**
* @param TMessage[] $messages Iterable list of TMessage objects.
* @param string $code Language code
- * @param array $warnings Array where warnings are appended to.
+ * @param array &$warnings Array where warnings are appended to.
*/
protected function balancedTagsCheck( $messages, $code, array &$warnings ) {
foreach ( $messages as $message ) {
@@ -418,7 +430,7 @@ class MessageChecker {
}
$errors = libxml_get_errors();
- $params = array();
+ $params = [];
foreach ( $errors as $error ) {
if ( $error->code !== 76 && $error->code !== 73 ) {
continue;
@@ -430,12 +442,12 @@ class MessageChecker {
continue;
}
- $warnings[$key][] = array(
- array( 'tags', 'balance', $key, $code ),
+ $warnings[$key][] = [
+ [ 'tags', 'balance', $key, $code ],
'translate-checks-format',
- array( 'PARAMS', $params ),
- array( 'COUNT', count( $params ) ),
- );
+ [ 'PARAMS', $params ],
+ [ 'COUNT', count( $params ) ],
+ ];
}
libxml_clear_errors();