summaryrefslogtreecommitdiff
blob: 5392ca8e8e7c01e9d8359e7528d61dfd7cd4d37f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
<?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 Title;
use TMessage;

/**
 * Checks if the translation uses links that are discouraged. Valid links are those that link
 * to Special: or {{ns:special}}: or project pages trough MediaWiki messages like
 * {{MediaWiki:helppage-url}}:. Also links in the definition are allowed.
 * @license GPL-2.0-or-later
 * @since 2020.02
 */
class MediaWikiLinkValidator implements MessageValidator {
	public function getIssues( TMessage $message, string $targetLanguage ): ValidationIssues {
		$issues = new ValidationIssues();

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

		$links = $this->getLinksMissingInTarget( $definition, $translation );
		if ( $links !== [] ) {
			$issue = new ValidationIssue(
				'links',
				'missing',
				'translate-checks-links-missing',
				[
					[ 'PARAMS', $links ],
					[ 'COUNT', count( $links ) ],
				]
			);
			$issues->add( $issue );
		}

		$links = $this->getLinksMissingInTarget( $translation, $definition );
		if ( $links !== [] ) {
			$issue = new ValidationIssue(
				'links',
				'extra',
				'translate-checks-links',
				[
					[ 'PARAMS', $links ],
					[ 'COUNT', count( $links ) ],
				]
			);
			$issues->add( $issue );
		}

		return $issues;
	}

	private function getLinksMissingInTarget( string $source, string $target ): array {
		$tc = Title::legalChars() . '#%{}';
		$matches = $links = [];

		preg_match_all( "/\[\[([{$tc}]+)(\\|(.+?))?]]/sDu", $source, $matches );
		$count = count( $matches[0] );
		for ( $i = 0; $i < $count; $i++ ) {
			$backMatch = preg_quote( $matches[1][$i], '/' );
			if ( preg_match( "/\[\[$backMatch/", $target ) !== 1 ) {
				$links[] = "[[{$matches[1][$i]}{$matches[2][$i]}]]";
			}
		}

		return $links;
	}
}