Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
90.00% |
81 / 90 |
|
30.00% |
3 / 10 |
CRAP | |
0.00% |
0 / 1 |
AndroidXmlFormat | |
91.01% |
81 / 89 |
|
30.00% |
3 / 10 |
28.57 | |
0.00% |
0 / 1 |
__construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
supportsFuzzy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileExtensions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readFromVariable | |
91.67% |
22 / 24 |
|
0.00% |
0 / 1 |
7.03 | |||
scrapeAuthors | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
readElementContents | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
1 | |||
formatElementContents | |
80.00% |
4 / 5 |
|
0.00% |
0 / 1 |
2.03 | |||
doAuthors | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
3 | |||
writeReal | |
96.88% |
31 / 32 |
|
0.00% |
0 / 1 |
8 | |||
isContentEqual | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\FileFormatSupport; |
5 | |
6 | use DOMDocument; |
7 | use FileBasedMessageGroup; |
8 | use IntlChar; |
9 | use MediaWiki\Extension\Translate\MessageLoading\Message; |
10 | use MediaWiki\Extension\Translate\MessageLoading\MessageCollection; |
11 | use MediaWiki\Extension\Translate\MessageProcessing\ArrayFlattener; |
12 | use SimpleXMLElement; |
13 | |
14 | /** |
15 | * Support for XML translation format used by Android. |
16 | * @author Niklas Laxström |
17 | * @license GPL-2.0-or-later |
18 | * @ingroup FileFormatSupport |
19 | */ |
20 | class AndroidXmlFormat extends SimpleFormat { |
21 | private ArrayFlattener $flattener; |
22 | |
23 | public function __construct( FileBasedMessageGroup $group ) { |
24 | parent::__construct( $group ); |
25 | $this->flattener = new ArrayFlattener( '', true ); |
26 | } |
27 | |
28 | public function supportsFuzzy(): string { |
29 | return 'yes'; |
30 | } |
31 | |
32 | public function getFileExtensions(): array { |
33 | return [ '.xml' ]; |
34 | } |
35 | |
36 | public function readFromVariable( string $data ): array { |
37 | $reader = new SimpleXMLElement( $data ); |
38 | |
39 | $messages = []; |
40 | $mangler = $this->group->getMangler(); |
41 | |
42 | $regexBacktrackLimit = ini_get( 'pcre.backtrack_limit' ); |
43 | ini_set( 'pcre.backtrack_limit', '10' ); |
44 | |
45 | /** @var SimpleXMLElement $element */ |
46 | foreach ( $reader as $element ) { |
47 | $key = (string)$element['name']; |
48 | |
49 | if ( $element->getName() === 'string' ) { |
50 | $value = $this->readElementContents( $element ); |
51 | } elseif ( $element->getName() === 'plurals' ) { |
52 | $forms = []; |
53 | foreach ( $element as $item ) { |
54 | $forms[(string)$item['quantity']] = $this->readElementContents( $item ); |
55 | } |
56 | $value = $this->flattener->flattenCLDRPlurals( $forms ); |
57 | } else { |
58 | wfDebug( __METHOD__ . ': Unknown XML element name.' ); |
59 | continue; |
60 | } |
61 | |
62 | if ( isset( $element['fuzzy'] ) && (string)$element['fuzzy'] === 'true' ) { |
63 | $value = TRANSLATE_FUZZY . $value; |
64 | } |
65 | |
66 | $messages[$key] = $value; |
67 | } |
68 | |
69 | ini_set( 'pcre.backtrack_limit', $regexBacktrackLimit ); |
70 | |
71 | return [ |
72 | 'AUTHORS' => $this->scrapeAuthors( $data ), |
73 | 'MESSAGES' => $mangler->mangleArray( $messages ), |
74 | ]; |
75 | } |
76 | |
77 | private function scrapeAuthors( string $string ): array { |
78 | if ( !preg_match( '~<!-- Authors:\n((?:\* .*\n)*)-->~', $string, $match ) ) { |
79 | return []; |
80 | } |
81 | |
82 | $authors = $matches = []; |
83 | preg_match_all( '~\* (.*)~', $match[ 1 ], $matches ); |
84 | foreach ( $matches[1] as $author ) { |
85 | $authors[] = str_replace( "\u{2011}\u{2011}", '--', $author ); |
86 | } |
87 | return $authors; |
88 | } |
89 | |
90 | private function readElementContents( SimpleXMLElement $element ): string { |
91 | // Convert string of format \uNNNN (eg: \u1234) to symbols |
92 | $converted = preg_replace_callback( |
93 | '/(?<!\\\\)(?:\\\\{2})*+\\K\\\\u([0-9A-Fa-f]{4,6})+/', |
94 | static fn ( array $matches ) => IntlChar::chr( hexdec( $matches[1] ) ), |
95 | (string)$element |
96 | ); |
97 | |
98 | return stripcslashes( $converted ); |
99 | } |
100 | |
101 | private function formatElementContents( string $contents ): string { |
102 | // Kudos to the brilliant person who invented this braindead file format |
103 | $escaped = addcslashes( $contents, '"\'\\' ); |
104 | if ( substr( $escaped, 0, 1 ) === '@' ) { |
105 | // '@' at beginning of string refers to another string by name. |
106 | // Add backslash to escape it too. |
107 | $escaped = '\\' . $escaped; |
108 | } |
109 | // All html entities seen would be inserted by translators themselves. |
110 | // Treat them as plain text. |
111 | $escaped = str_replace( '&', '&', $escaped ); |
112 | |
113 | // Newlines must be escaped |
114 | return str_replace( "\n", '\n', $escaped ); |
115 | } |
116 | |
117 | private function doAuthors( MessageCollection $collection ): string { |
118 | $authors = $collection->getAuthors(); |
119 | $authors = $this->filterAuthors( $authors, $collection->code ); |
120 | |
121 | if ( !$authors ) { |
122 | return ''; |
123 | } |
124 | |
125 | $output = "\n<!-- Authors:\n"; |
126 | |
127 | foreach ( $authors as $author ) { |
128 | // Since -- is not allowed in XML comments, we rewrite them to |
129 | // U+2011 (non-breaking hyphen). |
130 | $author = str_replace( '--', "\u{2011}\u{2011}", $author ); |
131 | $output .= "* $author\n"; |
132 | } |
133 | |
134 | $output .= "-->\n"; |
135 | |
136 | return $output; |
137 | } |
138 | |
139 | protected function writeReal( MessageCollection $collection ): string { |
140 | global $wgTranslateDocumentationLanguageCode; |
141 | |
142 | $collection->filter( 'hastranslation', false ); |
143 | if ( count( $collection ) === 0 ) { |
144 | return ''; |
145 | } |
146 | |
147 | $template = '<?xml version="1.0" encoding="utf-8"?>'; |
148 | $template .= $this->doAuthors( $collection ); |
149 | $template .= '<resources></resources>'; |
150 | |
151 | $writer = new SimpleXMLElement( $template ); |
152 | |
153 | if ( $collection->getLanguage() === $wgTranslateDocumentationLanguageCode ) { |
154 | $writer->addAttribute( |
155 | 'tools:ignore', |
156 | 'all', |
157 | 'http://schemas.android.com/tools' |
158 | ); |
159 | } |
160 | |
161 | $mangler = $this->group->getMangler(); |
162 | /** @var Message $m */ |
163 | foreach ( $collection as $key => $m ) { |
164 | $key = $mangler->unmangle( $key ); |
165 | |
166 | $value = $m->translation(); |
167 | $value = str_replace( TRANSLATE_FUZZY, '', $value ); |
168 | |
169 | $plurals = $this->flattener->unflattenCLDRPlurals( '', $value ); |
170 | |
171 | if ( $plurals === false ) { |
172 | $element = $writer->addChild( 'string', $this->formatElementContents( $value ) ); |
173 | } else { |
174 | $element = $writer->addChild( 'plurals' ); |
175 | foreach ( $plurals as $quantity => $content ) { |
176 | $item = $element->addChild( 'item', $this->formatElementContents( $content ) ); |
177 | $item->addAttribute( 'quantity', $quantity ); |
178 | } |
179 | } |
180 | |
181 | $element->addAttribute( 'name', $key ); |
182 | // This is non-standard |
183 | if ( $m->hasTag( 'fuzzy' ) ) { |
184 | $element->addAttribute( 'fuzzy', 'true' ); |
185 | } |
186 | } |
187 | |
188 | // Make the output pretty with DOMDocument |
189 | $dom = new DOMDocument( '1.0' ); |
190 | $dom->formatOutput = true; |
191 | $dom->loadXML( $writer->asXML() ); |
192 | |
193 | return $dom->saveXML() ?: ''; |
194 | } |
195 | |
196 | public function isContentEqual( ?string $a, ?string $b ): bool { |
197 | return $this->flattener->compareContent( $a, $b ); |
198 | } |
199 | } |
200 | |
201 | class_alias( AndroidXmlFormat::class, 'AndroidXmlFFS' ); |