Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
33.33% |
16 / 48 |
|
20.00% |
1 / 5 |
CRAP | |
0.00% |
0 / 1 |
DtdFormat | |
34.04% |
16 / 47 |
|
20.00% |
1 / 5 |
32.24 | |
0.00% |
0 / 1 |
getFileExtensions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readFromVariable | |
100.00% |
16 / 16 |
|
100.00% |
1 / 1 |
1 | |||
writeReal | |
0.00% |
0 / 18 |
|
0.00% |
0 / 1 |
20 | |||
doHeader | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
2 | |||
doAuthors | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\FileFormatSupport; |
5 | |
6 | use MediaWiki\Extension\Translate\MessageLoading\Message; |
7 | use MediaWiki\Extension\Translate\MessageLoading\MessageCollection; |
8 | use MediaWiki\Extension\Translate\Utilities\Utilities; |
9 | |
10 | /** |
11 | * File format support for DTD. |
12 | * @author Guillaume Duhamel |
13 | * @author Niklas Laxström |
14 | * @author Siebrand Mazeland |
15 | * @copyright Copyright © 2009-2010, Guillaume Duhamel, Niklas Laxström, Siebrand Mazeland |
16 | * @license GPL-2.0-or-later |
17 | * @ingroup FileFormatSupport |
18 | */ |
19 | class DtdFormat extends SimpleFormat { |
20 | public function getFileExtensions(): array { |
21 | return [ '.dtd' ]; |
22 | } |
23 | |
24 | public function readFromVariable( string $data ): array { |
25 | preg_match_all( ',# Author: ([^\n]+)\n,', $data, $matches ); |
26 | $authors = $matches[1]; |
27 | |
28 | preg_match_all( ',<!ENTITY[ ]+([^ ]+)\s+"([^"]+)"[^>]*>,', $data, $matches ); |
29 | [ , $keys, $messages ] = $matches; |
30 | $messages = array_combine( |
31 | $keys, |
32 | array_map( |
33 | static fn ( $message ) => html_entity_decode( $message, ENT_QUOTES ), |
34 | $messages |
35 | ) |
36 | ); |
37 | |
38 | $messages = $this->group->getMangler()->mangleArray( $messages ); |
39 | |
40 | return [ |
41 | 'AUTHORS' => $authors, |
42 | 'MESSAGES' => $messages, |
43 | ]; |
44 | } |
45 | |
46 | protected function writeReal( MessageCollection $collection ): string { |
47 | $collection->loadTranslations(); |
48 | |
49 | $header = "<!--\n"; |
50 | $header .= $this->doHeader( $collection ); |
51 | $header .= $this->doAuthors( $collection ); |
52 | $header .= "-->\n"; |
53 | |
54 | $output = ''; |
55 | $mangler = $this->group->getMangler(); |
56 | |
57 | /** @var Message $message */ |
58 | foreach ( $collection as $key => $message ) { |
59 | $key = $mangler->unmangle( $key ); |
60 | $trans = $message->translation() ?? ''; |
61 | if ( $trans === '' ) { |
62 | continue; |
63 | } |
64 | $trans = str_replace( TRANSLATE_FUZZY, '', $trans ); |
65 | |
66 | $trans = str_replace( '"', '"', $trans ); |
67 | $output .= "<!ENTITY $key \"$trans\">\n"; |
68 | } |
69 | |
70 | if ( $output ) { |
71 | return $header . $output; |
72 | } |
73 | |
74 | return ''; |
75 | } |
76 | |
77 | private function doHeader( MessageCollection $collection ): string { |
78 | global $wgSitename; |
79 | |
80 | $code = $collection->code; |
81 | $name = Utilities::getLanguageName( $code ); |
82 | $native = Utilities::getLanguageName( $code, $code ); |
83 | |
84 | $output = "# Messages for $name ($native)\n"; |
85 | $output .= "# Exported from $wgSitename\n\n"; |
86 | |
87 | return $output; |
88 | } |
89 | |
90 | private function doAuthors( MessageCollection $collection ): string { |
91 | $output = ''; |
92 | $authors = $collection->getAuthors(); |
93 | $authors = $this->filterAuthors( $authors, $collection->code ); |
94 | |
95 | foreach ( $authors as $author ) { |
96 | $output .= "# Author: $author\n"; |
97 | } |
98 | |
99 | return $output; |
100 | } |
101 | } |
102 | |
103 | class_alias( DtdFormat::class, 'DtdFFS' ); |