Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MediaWikiLinkValidator.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Validation\Validators;
5
10
19 public function getIssues( Message $message, string $targetLanguage ): ValidationIssues {
20 $issues = new ValidationIssues();
21
22 $definition = $message->definition();
23 $translation = $message->translation();
24
25 $links = $this->getLinksMissingInTarget( $definition, $translation );
26 if ( $links !== [] ) {
27 $issue = new ValidationIssue(
28 'links',
29 'missing',
30 'translate-checks-links-missing',
31 [
32 [ 'PARAMS', $links ],
33 [ 'COUNT', count( $links ) ],
34 ]
35 );
36 $issues->add( $issue );
37 }
38
39 $links = $this->getLinksMissingInTarget( $translation, $definition );
40 if ( $links !== [] ) {
41 $issue = new ValidationIssue(
42 'links',
43 'extra',
44 'translate-checks-links',
45 [
46 [ 'PARAMS', $links ],
47 [ 'COUNT', count( $links ) ],
48 ]
49 );
50 $issues->add( $issue );
51 }
52
53 return $issues;
54 }
55
56 private function getLinksMissingInTarget( string $source, string $target ): array {
57 global $wgLegalTitleChars;
58 $tc = $wgLegalTitleChars . '#%{}';
59 $matches = $links = [];
60
61 preg_match_all( "/\[\[([{$tc}]+)(\\|(.+?))?]]/sDu", $source, $matches );
62 $count = count( $matches[0] );
63 for ( $i = 0; $i < $count; $i++ ) {
64 $backMatch = preg_quote( $matches[1][$i], '/' );
65 if ( preg_match( "/\[\[$backMatch/", $target ) !== 1 ) {
66 $links[] = "[[{$matches[1][$i]}{$matches[2][$i]}]]";
67 }
68 }
69
70 return $links;
71 }
72}
Interface for message objects used by MessageCollection.
Definition Message.php:13
Checks if the translation uses links that are discouraged.