Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
CRAP
100.00% covered (success)
100.00%
1 / 1
MediaWikiPageNameValidator
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
100.00% covered (success)
100.00%
1 / 1
 getIssues
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
3
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
6use MediaWiki\Extension\Translate\MessageLoading\Message;
7use MediaWiki\Extension\Translate\Validation\MessageValidator;
8use MediaWiki\Extension\Translate\Validation\ValidationIssue;
9use MediaWiki\Extension\Translate\Validation\ValidationIssues;
10
11/**
12 * Ensures that translations do not translate namespaces.
13 * @author Abijeet Patro
14 * @license GPL-2.0-or-later
15 * @since 2020.02
16 */
17class MediaWikiPageNameValidator implements MessageValidator {
18    public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
19        $issues = new ValidationIssues();
20
21        $definition = $message->definition();
22        $translation = $message->translation();
23
24        $namespaces = 'help|project|\{\{ns:project}}|mediawiki';
25        $matches = [];
26        if ( preg_match( "/^($namespaces):[\w\s]+$/ui", $definition, $matches ) &&
27            !preg_match( "/^{$matches[1]}:.+$/u", $translation )
28        ) {
29            $issue = new ValidationIssue(
30                'pagename',
31                'namespace',
32                'translate-checks-pagename'
33            );
34            $issues->add( $issue );
35        }
36
37        return $issues;
38    }
39}