diff options
Diffstat (limited to 'MLEB/Translate/src/Validation')
29 files changed, 54 insertions, 160 deletions
diff --git a/MLEB/Translate/src/Validation/LegacyValidatorAdapter.php b/MLEB/Translate/src/Validation/LegacyValidatorAdapter.php deleted file mode 100644 index 1b194334..00000000 --- a/MLEB/Translate/src/Validation/LegacyValidatorAdapter.php +++ /dev/null @@ -1,63 +0,0 @@ -<?php -/** - * @file - * @author Niklas Laxström - * @license GPL-2.0-or-later - */ - -declare( strict_types = 1 ); - -namespace MediaWiki\Extension\Translate\Validation; - -use MediaWiki\Extension\Translate\TranslatorInterface\Insertable\InsertablesSuggester; -use TMessage; - -/** - * Object adapter for message validators that implement the deprecated interface. - * - * @since 2020.06 - */ -class LegacyValidatorAdapter implements MessageValidator, InsertablesSuggester { - /** @var Validator */ - private $validator; - - public function __construct( Validator $validator ) { - $this->validator = $validator; - } - - /** @inheritDoc */ - public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues { - $notices = []; - $this->validator->validate( $message, $targetLanguage, $notices ); - return $this->convertNoticesToValidationIssues( $notices, $message->key() ); - } - - private function convertNoticesToValidationIssues( - array $notices, - string $messageKey - ): ValidationIssues { - $issues = new ValidationIssues(); - foreach ( $notices[$messageKey] ?? [] as $notice ) { - $issue = new ValidationIssue( - $notice[0][0], - $notice[0][1], - $notice[1], - array_slice( $notice, 2 ) - ); - $issues->add( $issue ); - } - - return $issues; - } - - /** @inheritDoc */ - public function getInsertables( string $text ): array { - if ( $this->validator instanceof InsertablesSuggester ) { - return $this->validator->getInsertables( $text ); - } - - return []; - } -} - -class_alias( LegacyValidatorAdapter::class, '\MediaWiki\Extensions\Translate\LegacyValidatorAdapter' ); diff --git a/MLEB/Translate/src/Validation/MessageValidator.php b/MLEB/Translate/src/Validation/MessageValidator.php index 4946d0a9..b7f1cd1f 100644 --- a/MLEB/Translate/src/Validation/MessageValidator.php +++ b/MLEB/Translate/src/Validation/MessageValidator.php @@ -22,5 +22,3 @@ use TMessage; interface MessageValidator { public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues; } - -class_alias( MessageValidator::class, '\MediaWiki\Extensions\Translate\MessageValidator' ); diff --git a/MLEB/Translate/src/Validation/ValidationIssue.php b/MLEB/Translate/src/Validation/ValidationIssue.php index 822aeb1c..1d544894 100644 --- a/MLEB/Translate/src/Validation/ValidationIssue.php +++ b/MLEB/Translate/src/Validation/ValidationIssue.php @@ -52,5 +52,3 @@ class ValidationIssue { return $this->messageParams; } } - -class_alias( ValidationIssue::class, '\MediaWiki\Extensions\Translate\ValidationIssue' ); diff --git a/MLEB/Translate/src/Validation/ValidationIssues.php b/MLEB/Translate/src/Validation/ValidationIssues.php index f9278995..fdaa0069 100644 --- a/MLEB/Translate/src/Validation/ValidationIssues.php +++ b/MLEB/Translate/src/Validation/ValidationIssues.php @@ -51,5 +51,3 @@ class ValidationIssues implements Countable, IteratorAggregate { return count( $this->issues ); } } - -class_alias( ValidationIssues::class, '\MediaWiki\Extensions\Translate\ValidationIssues' ); diff --git a/MLEB/Translate/src/Validation/ValidationResult.php b/MLEB/Translate/src/Validation/ValidationResult.php index 2b5a62d7..ec7d3e0a 100644 --- a/MLEB/Translate/src/Validation/ValidationResult.php +++ b/MLEB/Translate/src/Validation/ValidationResult.php @@ -100,5 +100,3 @@ class ValidationResult { return $out; } } - -class_alias( ValidationResult::class, '\MediaWiki\Extensions\Translate\ValidationResult' ); diff --git a/MLEB/Translate/src/Validation/ValidationRunner.php b/MLEB/Translate/src/Validation/ValidationRunner.php index b5b345bc..5e6c41da 100644 --- a/MLEB/Translate/src/Validation/ValidationRunner.php +++ b/MLEB/Translate/src/Validation/ValidationRunner.php @@ -14,6 +14,7 @@ namespace MediaWiki\Extension\Translate\Validation; use Exception; use FormatJson; use InvalidArgumentException; +use MediaWiki\Extension\Translate\Services; use MediaWiki\Extension\Translate\TranslatorInterface\Insertable\InsertablesSuggester; use PHPVariableLoader; use RuntimeException; @@ -105,7 +106,8 @@ class ValidationRunner { 'instance' => $validator, 'insertable' => $isInsertable, 'enforce' => $validatorConfig['enforce'] ?? false, - 'keymatch' => $validatorConfig['keymatch'] ?? false, + 'include' => $validatorConfig['keymatch'] ?? $validatorConfig['include'] ?? false, + 'exclude' => $validatorConfig['exclude'] ?? false ]; } @@ -116,7 +118,7 @@ class ValidationRunner { */ public function getValidators(): array { return array_map( - function ( $validator ) { + static function ( $validator ) { return $validator['instance']; }, $this->validators @@ -202,19 +204,25 @@ class ValidationRunner { /** @internal Should only be used by tests and inside this class. */ public static function reloadIgnorePatterns(): void { - global $wgTranslateCheckBlacklist; + $validationExclusionFile = Services::getInstance()->getConfigHelper()->getValidationExclusionFile(); - if ( $wgTranslateCheckBlacklist === false ) { + if ( $validationExclusionFile === false ) { self::$ignorePatterns = []; return; } $list = PHPVariableLoader::loadVariableFromPHPFile( - $wgTranslateCheckBlacklist, - 'checkBlacklist' + $validationExclusionFile, + 'validationExclusionList' ); $keys = [ 'group', 'check', 'subcheck', 'code', 'message' ]; + if ( $list && !is_array( $list ) ) { + throw new InvalidArgumentException( + "validationExclusionList defined in $validationExclusionFile must be an array" + ); + } + foreach ( $list as $key => $pattern ) { foreach ( $keys as $checkKey ) { if ( !isset( $pattern[$checkKey] ) ) { @@ -287,8 +295,7 @@ class ValidationRunner { /** * Check if key matches validator's key patterns. - * - * Only relevant if the 'keymatch' option is specified in the validator. + * Only relevant if the 'include' or 'exclude' option is specified in the validator. * * @param string $key * @param string[] $keyMatches @@ -352,8 +359,13 @@ class ValidationRunner { } try { - $keyMatches = $validatorData['keymatch']; - if ( $keyMatches !== false && !$this->doesKeyMatch( $message->key(), $keyMatches ) ) { + $includedKeys = $validatorData['include']; + if ( $includedKeys !== false && !$this->doesKeyMatch( $message->key(), $includedKeys ) ) { + return; + } + + $excludedKeys = $validatorData['exclude']; + if ( $excludedKeys !== false && $this->doesKeyMatch( $message->key(), $excludedKeys ) ) { return; } @@ -371,5 +383,3 @@ class ValidationRunner { } } } - -class_alias( ValidationRunner::class, '\MediaWiki\Extensions\Translate\ValidationRunner' ); diff --git a/MLEB/Translate/src/Validation/Validator.php b/MLEB/Translate/src/Validation/Validator.php deleted file mode 100644 index c89f349b..00000000 --- a/MLEB/Translate/src/Validation/Validator.php +++ /dev/null @@ -1,23 +0,0 @@ -<?php -/** - * Interface to be implemented by Validators. - * - * @file - * @author Abijeet Patro - * @license GPL-2.0-or-later - */ - -namespace MediaWiki\Extension\Translate\Validation; - -use TMessage; - -/** - * Interface class built to be implement by validators - * @since 2019.06 - * @deprecated since 2020.06 - */ -interface Validator { - public function validate( TMessage $message, $code, array &$notices ); -} - -class_alias( Validator::class, '\MediaWiki\Extensions\Translate\Validator' ); diff --git a/MLEB/Translate/src/Validation/ValidatorFactory.php b/MLEB/Translate/src/Validation/ValidatorFactory.php index 20561f08..b0dcb040 100644 --- a/MLEB/Translate/src/Validation/ValidatorFactory.php +++ b/MLEB/Translate/src/Validation/ValidatorFactory.php @@ -18,6 +18,7 @@ use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiParameterValida use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiPluralValidator; use MediaWiki\Extension\Translate\Validation\Validators\MediaWikiTimeListValidator; use MediaWiki\Extension\Translate\Validation\Validators\NewlineValidator; +use MediaWiki\Extension\Translate\Validation\Validators\NotEmptyValidator; use MediaWiki\Extension\Translate\Validation\Validators\NumericalParameterValidator; use MediaWiki\Extension\Translate\Validation\Validators\PrintfValidator; use MediaWiki\Extension\Translate\Validation\Validators\PythonInterpolationValidator; @@ -50,6 +51,7 @@ class ValidatorFactory { 'MediaWikiPlural' => MediaWikiPluralValidator::class, 'MediaWikiTimeList' => MediaWikiTimeListValidator::class, 'Newline' => NewlineValidator::class, + 'NotEmpty' => NotEmptyValidator::class, 'NumericalParameter' => NumericalParameterValidator::class, 'Printf' => PrintfValidator::class, 'PythonInterpolation' => PythonInterpolationValidator::class, @@ -91,13 +93,7 @@ class ValidatorFactory { throw new InvalidArgumentException( "Could not find validator class - '$class'. " ); } - $validator = new $class( $params ); - - if ( $validator instanceof Validator ) { - return new LegacyValidatorAdapter( $validator ); - } - - return $validator; + return new $class( $params ); } /** @@ -114,5 +110,3 @@ class ValidatorFactory { self::$validators[ $id ] = $ns . $validator; } } - -class_alias( ValidatorFactory::class, '\MediaWiki\Extensions\Translate\ValidatorFactory' ); diff --git a/MLEB/Translate/src/Validation/Validators/BraceBalanceValidator.php b/MLEB/Translate/src/Validation/Validators/BraceBalanceValidator.php index 2cc25114..32cd3ec0 100644 --- a/MLEB/Translate/src/Validation/Validators/BraceBalanceValidator.php +++ b/MLEB/Translate/src/Validation/Validators/BraceBalanceValidator.php @@ -53,5 +53,3 @@ class BraceBalanceValidator implements MessageValidator { return substr_count( $source, $str1 ) - substr_count( $source, $str2 ); } } - -class_alias( BraceBalanceValidator::class, '\MediaWiki\Extensions\Translate\BraceBalanceValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/EscapeCharacterValidator.php b/MLEB/Translate/src/Validation/Validators/EscapeCharacterValidator.php index c333ca98..08a466ef 100644 --- a/MLEB/Translate/src/Validation/Validators/EscapeCharacterValidator.php +++ b/MLEB/Translate/src/Validation/Validators/EscapeCharacterValidator.php @@ -87,5 +87,3 @@ class EscapeCharacterValidator implements MessageValidator { return $prefix . $regex; } } - -class_alias( EscapeCharacterValidator::class, '\MediaWiki\Extensions\Translate\EscapeCharacterValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/GettextNewlineValidator.php b/MLEB/Translate/src/Validation/Validators/GettextNewlineValidator.php index 3b52859d..46172b04 100644 --- a/MLEB/Translate/src/Validation/Validators/GettextNewlineValidator.php +++ b/MLEB/Translate/src/Validation/Validators/GettextNewlineValidator.php @@ -45,5 +45,3 @@ class GettextNewlineValidator extends NewlineValidator { return $str; } } - -class_alias( GettextNewlineValidator::class, '\MediaWiki\Extensions\Translate\GettextNewlineValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/GettextPluralValidator.php b/MLEB/Translate/src/Validation/Validators/GettextPluralValidator.php index 36c6affd..0fdc5a59 100644 --- a/MLEB/Translate/src/Validation/Validators/GettextPluralValidator.php +++ b/MLEB/Translate/src/Validation/Validators/GettextPluralValidator.php @@ -104,5 +104,3 @@ class GettextPluralValidator implements MessageValidator { return [ 'ok', [] ]; } } - -class_alias( GettextPluralValidator::class, '\MediaWiki\Extensions\Translate\GettextPluralValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/InsertableRegexValidator.php b/MLEB/Translate/src/Validation/Validators/InsertableRegexValidator.php index fb056873..2c6f1198 100644 --- a/MLEB/Translate/src/Validation/Validators/InsertableRegexValidator.php +++ b/MLEB/Translate/src/Validation/Validators/InsertableRegexValidator.php @@ -76,5 +76,3 @@ class InsertableRegexValidator extends RegexInsertablesSuggester implements Mess return $issues; } } - -class_alias( InsertableRegexValidator::class, '\MediaWiki\Extensions\Translate\InsertableRegexValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/InsertableRubyVariableValidator.php b/MLEB/Translate/src/Validation/Validators/InsertableRubyVariableValidator.php index 50b67214..eda700a1 100644 --- a/MLEB/Translate/src/Validation/Validators/InsertableRubyVariableValidator.php +++ b/MLEB/Translate/src/Validation/Validators/InsertableRubyVariableValidator.php @@ -14,8 +14,3 @@ class InsertableRubyVariableValidator extends InsertableRegexValidator { parent::__construct( '/%{[a-zA-Z_]+}/' ); } } - -class_alias( - InsertableRubyVariableValidator::class, - '\MediaWiki\Extensions\Translate\InsertableRubyVariableValidator' -); diff --git a/MLEB/Translate/src/Validation/Validators/IosVariableValidator.php b/MLEB/Translate/src/Validation/Validators/IosVariableValidator.php index c981e194..dcb0142d 100644 --- a/MLEB/Translate/src/Validation/Validators/IosVariableValidator.php +++ b/MLEB/Translate/src/Validation/Validators/IosVariableValidator.php @@ -19,5 +19,3 @@ class IosVariableValidator extends InsertableRegexValidator { ); } } - -class_alias( IosVariableValidator::class, '\MediaWiki\Extensions\Translate\IosVariableValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/MatchSetValidator.php b/MLEB/Translate/src/Validation/Validators/MatchSetValidator.php index a81bba20..70c4f836 100644 --- a/MLEB/Translate/src/Validation/Validators/MatchSetValidator.php +++ b/MLEB/Translate/src/Validation/Validators/MatchSetValidator.php @@ -62,5 +62,3 @@ class MatchSetValidator implements MessageValidator { return $issues; } } - -class_alias( MatchSetValidator::class, '\MediaWiki\Extensions\Translate\MatchSetValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/MediaWikiLinkValidator.php b/MLEB/Translate/src/Validation/Validators/MediaWikiLinkValidator.php index 0f872c6e..5392ca8e 100644 --- a/MLEB/Translate/src/Validation/Validators/MediaWikiLinkValidator.php +++ b/MLEB/Translate/src/Validation/Validators/MediaWikiLinkValidator.php @@ -70,5 +70,3 @@ class MediaWikiLinkValidator implements MessageValidator { return $links; } } - -class_alias( MediaWikiLinkValidator::class, '\MediaWiki\Extensions\Translate\MediaWikiLinkValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/MediaWikiPageNameValidator.php b/MLEB/Translate/src/Validation/Validators/MediaWikiPageNameValidator.php index 344c5108..815d1963 100644 --- a/MLEB/Translate/src/Validation/Validators/MediaWikiPageNameValidator.php +++ b/MLEB/Translate/src/Validation/Validators/MediaWikiPageNameValidator.php @@ -37,5 +37,3 @@ class MediaWikiPageNameValidator implements MessageValidator { return $issues; } } - -class_alias( MediaWikiPageNameValidator::class, '\MediaWiki\Extensions\Translate\MediaWikiPageNameValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/MediaWikiParameterValidator.php b/MLEB/Translate/src/Validation/Validators/MediaWikiParameterValidator.php index 5c953b4c..66d9aecb 100644 --- a/MLEB/Translate/src/Validation/Validators/MediaWikiParameterValidator.php +++ b/MLEB/Translate/src/Validation/Validators/MediaWikiParameterValidator.php @@ -13,5 +13,3 @@ class MediaWikiParameterValidator extends InsertableRegexValidator { parent::__construct( '/\$[1-9]/' ); } } - -class_alias( MediaWikiParameterValidator::class, '\MediaWiki\Extensions\Translate\MediaWikiParameterValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/MediaWikiPluralValidator.php b/MLEB/Translate/src/Validation/Validators/MediaWikiPluralValidator.php index b7606d8f..678f5042 100644 --- a/MLEB/Translate/src/Validation/Validators/MediaWikiPluralValidator.php +++ b/MLEB/Translate/src/Validation/Validators/MediaWikiPluralValidator.php @@ -98,7 +98,7 @@ class MediaWikiPluralValidator implements MessageValidator { // Stores the forms from plural invocations $plurals = []; - $cb = function ( $parser, $frame, $args ) use ( &$plurals ) { + $cb = static function ( $parser, $frame, $args ) use ( &$plurals ) { $forms = []; foreach ( $args as $index => $form ) { @@ -143,5 +143,3 @@ class MediaWikiPluralValidator implements MessageValidator { return array_values( $forms ); } } - -class_alias( MediaWikiPluralValidator::class, '\MediaWiki\Extensions\Translate\MediaWikiPluralValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/MediaWikiTimeListValidator.php b/MLEB/Translate/src/Validation/Validators/MediaWikiTimeListValidator.php index e1fb1279..fad5fd70 100644 --- a/MLEB/Translate/src/Validation/Validators/MediaWikiTimeListValidator.php +++ b/MLEB/Translate/src/Validation/Validators/MediaWikiTimeListValidator.php @@ -73,12 +73,9 @@ class MediaWikiTimeListValidator implements MessageValidator { ); $issues->add( $issue ); - continue; } } return $issues; } } - -class_alias( MediaWikiTimeListValidator::class, '\MediaWiki\Extensions\Translate\MediaWikiTimeListValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/NewlineValidator.php b/MLEB/Translate/src/Validation/Validators/NewlineValidator.php index 36525569..367a546d 100644 --- a/MLEB/Translate/src/Validation/Validators/NewlineValidator.php +++ b/MLEB/Translate/src/Validation/Validators/NewlineValidator.php @@ -98,5 +98,3 @@ class NewlineValidator implements MessageValidator { return $issues; } } - -class_alias( NewlineValidator::class, '\MediaWiki\Extensions\Translate\NewlineValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/NotEmptyValidator.php b/MLEB/Translate/src/Validation/Validators/NotEmptyValidator.php new file mode 100644 index 00000000..53bc591f --- /dev/null +++ b/MLEB/Translate/src/Validation/Validators/NotEmptyValidator.php @@ -0,0 +1,28 @@ +<?php +declare( strict_types = 1 ); + +namespace MediaWiki\Extension\Translate\Validation\Validators; + +use MediaWiki\Extension\Translate\Validation\MessageValidator; +use MediaWiki\Extension\Translate\Validation\ValidationIssue; +use MediaWiki\Extension\Translate\Validation\ValidationIssues; +use TMessage; + +class NotEmptyValidator implements MessageValidator { + public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues { + $translation = $message->translation(); + $issues = new ValidationIssues(); + + if ( $translation !== null && trim( $translation ) === '' ) { + $issues->add( + new ValidationIssue( + 'empty', + 'empty', + 'translate-checks-empty' + ) + ); + } + + return $issues; + } +} diff --git a/MLEB/Translate/src/Validation/Validators/NumericalParameterValidator.php b/MLEB/Translate/src/Validation/Validators/NumericalParameterValidator.php index 84bbf0e3..a26f1509 100644 --- a/MLEB/Translate/src/Validation/Validators/NumericalParameterValidator.php +++ b/MLEB/Translate/src/Validation/Validators/NumericalParameterValidator.php @@ -13,5 +13,3 @@ class NumericalParameterValidator extends InsertableRegexValidator { parent::__construct( '/\$\d+/' ); } } - -class_alias( NumericalParameterValidator::class, '\MediaWiki\Extensions\Translate\NumericalParameterValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/PrintfValidator.php b/MLEB/Translate/src/Validation/Validators/PrintfValidator.php index 6121e002..2f150f6d 100644 --- a/MLEB/Translate/src/Validation/Validators/PrintfValidator.php +++ b/MLEB/Translate/src/Validation/Validators/PrintfValidator.php @@ -14,5 +14,3 @@ class PrintfValidator extends InsertableRegexValidator { parent::__construct( '/%(\d+\$)?(\.\d+)?[sduf]/U' ); } } - -class_alias( PrintfValidator::class, '\MediaWiki\Extensions\Translate\PrintfValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/PythonInterpolationValidator.php b/MLEB/Translate/src/Validation/Validators/PythonInterpolationValidator.php index ca9a98e3..990cf319 100644 --- a/MLEB/Translate/src/Validation/Validators/PythonInterpolationValidator.php +++ b/MLEB/Translate/src/Validation/Validators/PythonInterpolationValidator.php @@ -14,5 +14,3 @@ class PythonInterpolationValidator extends InsertableRegexValidator { parent::__construct( '/\%(?:\([a-zA-Z0-9_]*?\))?[diouxXeEfFgGcrs]/U' ); } } - -class_alias( PythonInterpolationValidator::class, '\MediaWiki\Extensions\Translate\PythonInterpolationValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/ReplacementValidator.php b/MLEB/Translate/src/Validation/Validators/ReplacementValidator.php index 46b5382c..72fc1791 100644 --- a/MLEB/Translate/src/Validation/Validators/ReplacementValidator.php +++ b/MLEB/Translate/src/Validation/Validators/ReplacementValidator.php @@ -50,5 +50,3 @@ class ReplacementValidator implements MessageValidator { return $issues; } } - -class_alias( ReplacementValidator::class, '\MediaWiki\Extensions\Translate\ReplacementValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/SmartFormatPluralValidator.php b/MLEB/Translate/src/Validation/Validators/SmartFormatPluralValidator.php index 41d3ea44..969032aa 100644 --- a/MLEB/Translate/src/Validation/Validators/SmartFormatPluralValidator.php +++ b/MLEB/Translate/src/Validation/Validators/SmartFormatPluralValidator.php @@ -108,5 +108,3 @@ class SmartFormatPluralValidator implements MessageValidator, InsertablesSuggest return $insertables; } } - -class_alias( SmartFormatPluralValidator::class, '\MediaWiki\Extensions\Translate\SmartFormatPluralValidator' ); diff --git a/MLEB/Translate/src/Validation/Validators/UnicodePluralValidator.php b/MLEB/Translate/src/Validation/Validators/UnicodePluralValidator.php index da6a9eef..ddd9baa1 100644 --- a/MLEB/Translate/src/Validation/Validators/UnicodePluralValidator.php +++ b/MLEB/Translate/src/Validation/Validators/UnicodePluralValidator.php @@ -108,5 +108,3 @@ class UnicodePluralValidator implements MessageValidator { return [ 'ok', [] ]; } } - -class_alias( UnicodePluralValidator::class, '\MediaWiki\Extensions\Translate\UnicodePluralValidator' ); |