wikimedia/mediawiki-extensions-Translate

View on GitHub
src/Validation/Validators/MediaWikiPageNameValidator.php

Summary

Maintainability
A
0 mins
Test Coverage
<?php
declare( strict_types = 1 );

namespace MediaWiki\Extension\Translate\Validation\Validators;

use MediaWiki\Extension\Translate\MessageLoading\Message;
use MediaWiki\Extension\Translate\Validation\MessageValidator;
use MediaWiki\Extension\Translate\Validation\ValidationIssue;
use MediaWiki\Extension\Translate\Validation\ValidationIssues;

/**
 * Ensures that translations do not translate namespaces.
 * @author Abijeet Patro
 * @license GPL-2.0-or-later
 * @since 2020.02
 */
class MediaWikiPageNameValidator implements MessageValidator {
    public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
        $issues = new ValidationIssues();

        $definition = $message->definition();
        $translation = $message->translation();

        $namespaces = 'help|project|\{\{ns:project}}|mediawiki';
        $matches = [];
        if ( preg_match( "/^($namespaces):[\w\s]+$/ui", $definition, $matches ) &&
            !preg_match( "/^{$matches[1]}:.+$/u", $translation )
        ) {
            $issue = new ValidationIssue(
                'pagename',
                'namespace',
                'translate-checks-pagename'
            );
            $issues->add( $issue );
        }

        return $issues;
    }
}