Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
FindUnsynchronizedDefinitionsMaintenanceScript.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\Diagnostics;
5
9use MediaWiki\Shell\Shell;
10use Title;
11
18 public function __construct() {
19 parent::__construct();
20 $this->addDescription(
21 'This scripts finds definition pages in the wiki that do not have the expected ' .
22 'content with regards to the message group definition cache for file based message ' .
23 'groups. This causes the definition diff to appear for translations when it should ' .
24 'not. See https://phabricator.wikimedia.org/T270844'
25 );
26
27 $this->addArg(
28 'group-pattern',
29 'For example page-*,main',
30 self::REQUIRED
31 );
32 $this->addOption(
33 'ignore-trailing-whitespace',
34 'Ignore trailing whitespace',
35 self::OPTIONAL,
36 self::NO_ARG,
37 'w'
38 );
39 $this->addOption(
40 'fix',
41 'Try to fix the issues by triggering reprocessing'
42 );
43
44 $this->requireExtension( 'Translate' );
45 }
46
48 public function execute() {
49 $spec = $this->getArg( 0 );
50 $ignoreTrailingWhitespace = $this->getOption( 'ignore-trailing-whitespace' );
51 $patterns = explode( ',', trim( $spec ) );
52 $groupIds = MessageGroups::expandWildcards( $patterns );
53 $groups = MessageGroups::getGroupsById( $groupIds );
54
55 foreach ( $groups as $index => $group ) {
56 if ( !$group instanceof FileBasedMessageGroup ) {
57 unset( $groups[ $index ] );
58 }
59 }
60
61 $matched = count( $groups );
62 $this->output( "Pattern matched $matched file based message group(s).\n" );
63 $this->output( "Left side is the expected value. Right side is the actual value in wiki.\n" );
64
65 $groupsWithIssues = [];
66 foreach ( $groups as $group ) {
67 $sourceLanguage = $group->getSourceLanguage();
68 $collection = $group->initCollection( $sourceLanguage );
69 $collection->loadTranslations();
70
71 foreach ( $collection->keys() as $mkey => $title ) {
72 $message = $collection[$mkey];
73 $definition = $message->definition() ?? '';
74 $translation = $message->translation() ?? '';
75
76 $differs = $ignoreTrailingWhitespace
77 ? rtrim( $definition ) !== $translation
78 : $definition !== $translation;
79
80 if ( $differs ) {
81 $groupsWithIssues[$group->getId()] = $group;
82 echo Title::newFromLinkTarget( $title )->getPrefixedText() . "\n";
83 echo $this->getSideBySide( "'$definition'", "'$translation'", 80 ) . "\n";
84 }
85 }
86 }
87
88 if ( $this->hasOption( 'fix' ) && $groupsWithIssues ) {
89 foreach ( $groupsWithIssues as $group ) {
90 '@phan-var FileBasedMessageGroup $group';
91 $cache = $group->getMessageGroupCache( $group->getSourceLanguage() );
92 $cache->invalidate();
93 }
94 $script = realpath( __DIR__ . '/../../scripts/importExternalTranslations.php' );
95 $groupPattern = implode( ',', array_keys( $groupsWithIssues ) );
96 $command = Shell::makeScriptCommand( $script, [ "--group", $groupPattern ] )->getCommandString();
97 echo "Now run the following command and finish the sync in the wiki:\n$command\n";
98 }
99 }
100
101 private function getSideBySide( string $a, string $b, int $width ): string {
102 $wrapWidth = (int)floor( ( $width - 3 ) / 2 );
103 $aArray = explode( "\n", wordwrap( $a, $wrapWidth, "\n", true ) );
104 $bArray = explode( "\n", wordwrap( $b, $wrapWidth, "\n", true ) );
105 $lines = max( count( $aArray ), count( $bArray ) );
106
107 $out = '';
108 for ( $i = 0; $i < $lines; $i++ ) {
109 $out .= sprintf(
110 "%-{$wrapWidth}s | %-{$wrapWidth}s\n",
111 $aArray[$i] ?? '',
112 $bArray[$i] ?? ''
113 );
114 }
115 return $out;
116 }
117}
This class implements default behavior for file based message groups.
Factory class for accessing message groups individually by id or all of them as a list.
Constants for making code for maintenance scripts more readable.