Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 75 |
|
0.00% |
0 / 6 |
CRAP | |
0.00% |
0 / 1 |
FlatPhpFormat | |
0.00% |
0 / 74 |
|
0.00% |
0 / 6 |
132 | |
0.00% |
0 / 1 |
getFileExtensions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readFromVariable | |
0.00% |
0 / 22 |
|
0.00% |
0 / 1 |
6 | |||
writeReal | |
0.00% |
0 / 13 |
|
0.00% |
0 / 1 |
12 | |||
doHeader | |
0.00% |
0 / 17 |
|
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( "/([^$legal]|\\\\)/u", |
41 | static function ( $m ) { |
42 | return '\x' . dechex( ord( $m[0] ) ); |
43 | }, |
44 | $_[1] |
45 | ); |
46 | $value = str_replace( [ "\'", "\\\\" ], [ "'", "\\" ], $_[2] ); |
47 | $messages[$key] = $value; |
48 | } |
49 | |
50 | $messages = $this->group->getMangler()->mangleArray( $messages ); |
51 | |
52 | return [ |
53 | 'AUTHORS' => $authors, |
54 | 'MESSAGES' => $messages, |
55 | ]; |
56 | } |
57 | |
58 | protected function writeReal( MessageCollection $collection ): string { |
59 | $output = $this->extra['header'] ?? "<?php\n"; |
60 | $output .= $this->doHeader( $collection ); |
61 | |
62 | $mangler = $this->group->getMangler(); |
63 | |
64 | /** @var Message $item */ |
65 | foreach ( $collection as $item ) { |
66 | $key = $mangler->unmangle( $item->key() ); |
67 | $key = stripcslashes( $key ); |
68 | |
69 | $value = $item->translation(); |
70 | if ( $value === null ) { |
71 | continue; |
72 | } |
73 | |
74 | $value = str_replace( TRANSLATE_FUZZY, '', $value ); |
75 | $value = addcslashes( $value, "'" ); |
76 | |
77 | $output .= "\$$key = '$value';\n"; |
78 | } |
79 | |
80 | return $output; |
81 | } |
82 | |
83 | private function doHeader( MessageCollection $collection ): string { |
84 | global $wgServer, $wgTranslateDocumentationLanguageCode; |
85 | |
86 | $code = $collection->code; |
87 | $name = Utilities::getLanguageName( $code ); |
88 | $native = Utilities::getLanguageName( $code, $code ); |
89 | |
90 | if ( $wgTranslateDocumentationLanguageCode ) { |
91 | $docu = "\n * See the $wgTranslateDocumentationLanguageCode 'language' for " . |
92 | 'message documentation incl. usage of parameters'; |
93 | } else { |
94 | $docu = ''; |
95 | } |
96 | |
97 | $authors = $this->doAuthors( $collection ); |
98 | |
99 | $output = |
100 | <<<PHP |
101 | /** $name ($native) |
102 | * $docu |
103 | * To improve a translation please visit $wgServer |
104 | * |
105 | * @ingroup Language |
106 | * @file |
107 | * |
108 | $authors */ |
109 | |
110 | |
111 | PHP; |
112 | |
113 | return $output; |
114 | } |
115 | |
116 | private function doAuthors( MessageCollection $collection ): string { |
117 | $output = ''; |
118 | $authors = $collection->getAuthors(); |
119 | $authors = $this->filterAuthors( $authors, $collection->code ); |
120 | |
121 | foreach ( $authors as $author ) { |
122 | $output .= " * @author $author\n"; |
123 | } |
124 | |
125 | return $output; |
126 | } |
127 | |
128 | public static function getExtraSchema(): array { |
129 | return [ |
130 | 'root' => [ |
131 | '_type' => 'array', |
132 | '_children' => [ |
133 | 'FILES' => [ |
134 | '_type' => 'array', |
135 | '_children' => [ |
136 | 'header' => [ |
137 | '_type' => 'text', |
138 | ], |
139 | ] |
140 | ] |
141 | ] |
142 | ] |
143 | ]; |
144 | } |
145 | } |
146 | |
147 | class_alias( FlatPhpFormat::class, 'FlatPhpFFS' ); |