Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 72 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
FlatPhpFormat | |
0.00% |
0 / 71 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
getFileExtensions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readFromVariable | |
0.00% |
0 / 21 |
|
0.00% |
0 / 1 |
6 | |||
writeReal | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
doHeader | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
6 | |||
doAuthors | |
0.00% |
0 / 6 |
|
0.00% |
0 / 1 |
6 | |||
getExtraSchema | |
0.00% |
0 / 15 |
|
0.00% |
0 / 1 |
2 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\FileFormatSupport; |
5 | |
6 | use MediaWiki\Extension\Translate\MessageGroupConfiguration\MetaYamlSchemaExtender; |
7 | use MediaWiki\Extension\Translate\MessageLoading\Message; |
8 | use MediaWiki\Extension\Translate\MessageLoading\MessageCollection; |
9 | use MediaWiki\Extension\Translate\Utilities\Utilities; |
10 | use MediaWiki\Title\Title; |
11 | |
12 | /** |
13 | * Implements file format support for PHP files which consist of multiple |
14 | * variable assignments. |
15 | * |
16 | * @author Niklas Laxström |
17 | * @author Siebrand Mazeland |
18 | * @copyright Copyright © 2008-2010, Niklas Laxström, Siebrand Mazeland |
19 | * @license GPL-2.0-or-later |
20 | */ |
21 | class FlatPhpFormat extends SimpleFormat implements MetaYamlSchemaExtender { |
22 | public function getFileExtensions(): array { |
23 | return [ '.php' ]; |
24 | } |
25 | |
26 | public function readFromVariable( string $data ): array { |
27 | # Authors first |
28 | $matches = []; |
29 | preg_match_all( '/^ \* @author\s+(.+)$/m', $data, $matches ); |
30 | $authors = $matches[1]; |
31 | |
32 | # Then messages |
33 | $matches = []; |
34 | $regex = '/^\$(.*?)\s*=\s*[\'"](.*?)[\'"];.*?$/mus'; |
35 | preg_match_all( $regex, $data, $matches, PREG_SET_ORDER ); |
36 | $messages = []; |
37 | |
38 | foreach ( $matches as $_ ) { |
39 | $legal = Title::legalChars(); |
40 | $key = preg_replace_callback( |
41 | "/([^$legal]|\\\\)/u", |
42 | static fn ( $m ) => '\x' . dechex( ord( $m[0] ) ), |
43 | $_[1] |
44 | ); |
45 | $value = str_replace( [ "\'", "\\\\" ], [ "'", "\\" ], $_[2] ); |
46 | $messages[$key] = $value; |
47 | } |
48 | |
49 | $messages = $this->group->getMangler()->mangleArray( $messages ); |
50 | |
51 | return [ |
52 | 'AUTHORS' => $authors, |
53 | 'MESSAGES' => $messages, |
54 | ]; |
55 | } |
56 | |
57 | protected function writeReal( MessageCollection $collection ): string { |
58 | $output = $this->extra['header'] ?? "<?php\n"; |
59 | $output .= $this->doHeader( $collection ); |
60 | |
61 | $mangler = $this->group->getMangler(); |
62 | |
63 | /** @var Message $item */ |
64 | foreach ( $collection as $item ) { |
65 | $key = $mangler->unmangle( $item->key() ); |
66 | $key = stripcslashes( $key ); |
67 | |
68 | $value = $item->translation(); |
69 | if ( $value === null ) { |
70 | continue; |
71 | } |
72 | |
73 | $value = str_replace( TRANSLATE_FUZZY, '', $value ); |
74 | $value = addcslashes( $value, "'" ); |
75 | |
76 | $output .= "\$$key = '$value';\n"; |
77 | } |
78 | |
79 | return $output; |
80 | } |
81 | |
82 | private function doHeader( MessageCollection $collection ): string { |
83 | global $wgServer, $wgTranslateDocumentationLanguageCode; |
84 | |
85 | $code = $collection->code; |
86 | $name = Utilities::getLanguageName( $code ); |
87 | $native = Utilities::getLanguageName( $code, $code ); |
88 | |
89 | if ( $wgTranslateDocumentationLanguageCode ) { |
90 | $docu = "\n * See the $wgTranslateDocumentationLanguageCode 'language' for " . |
91 | 'message documentation incl. usage of parameters'; |
92 | } else { |
93 | $docu = ''; |
94 | } |
95 | |
96 | $authors = $this->doAuthors( $collection ); |
97 | |
98 | return <<<PHP |
99 | /** $name ($native) |
100 | * $docu |
101 | * To improve a translation please visit $wgServer |
102 | * |
103 | * @ingroup Language |
104 | * @file |
105 | * |
106 | $authors */ |
107 | |
108 | |
109 | PHP; |
110 | } |
111 | |
112 | private function doAuthors( MessageCollection $collection ): string { |
113 | $output = ''; |
114 | $authors = $collection->getAuthors(); |
115 | $authors = $this->filterAuthors( $authors, $collection->code ); |
116 | |
117 | foreach ( $authors as $author ) { |
118 | $output .= " * @author $author\n"; |
119 | } |
120 | |
121 | return $output; |
122 | } |
123 | |
124 | public static function getExtraSchema(): array { |
125 | return [ |
126 | 'root' => [ |
127 | '_type' => 'array', |
128 | '_children' => [ |
129 | 'FILES' => [ |
130 | '_type' => 'array', |
131 | '_children' => [ |
132 | 'header' => [ |
133 | '_type' => 'text', |
134 | ], |
135 | ] |
136 | ] |
137 | ] |
138 | ] |
139 | ]; |
140 | } |
141 | } |
142 | |
143 | class_alias( FlatPhpFormat::class, 'FlatPhpFFS' ); |