Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
86.84% |
33 / 38 |
|
0.00% |
0 / 4 |
CRAP | |
0.00% |
0 / 1 |
IniFormat | |
89.19% |
33 / 37 |
|
0.00% |
0 / 4 |
11.15 | |
0.00% |
0 / 1 |
supportsFuzzy | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getFileExtensions | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
readFromVariable | |
92.86% |
13 / 14 |
|
0.00% |
0 / 1 |
3.00 | |||
writeReal | |
95.24% |
20 / 21 |
|
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 | |
9 | /** |
10 | * IniFormat currently parses and generates flat ini files with language |
11 | * code as header key. |
12 | * |
13 | * @author Niklas Laxström |
14 | * @copyright Copyright © 2012-2013, Niklas Laxström |
15 | * @license GPL-2.0-or-later |
16 | * @ingroup FileFormatSupport |
17 | */ |
18 | class IniFormat extends SimpleFormat { |
19 | public function supportsFuzzy(): string { |
20 | return 'write'; |
21 | } |
22 | |
23 | public function getFileExtensions(): array { |
24 | return [ '.ini' ]; |
25 | } |
26 | |
27 | public function readFromVariable( string $data ): array { |
28 | $authors = []; |
29 | preg_match_all( '/^; Author: (.*)$/m', $data, $matches, PREG_SET_ORDER ); |
30 | foreach ( $matches as $match ) { |
31 | $authors[] = $match[1]; |
32 | } |
33 | |
34 | // Remove comments |
35 | $data = preg_replace( '/^\s*;.*$/m', '', $data ); |
36 | // Make sure values are quoted, PHP barks on stuff like ?{}|&~![()^ |
37 | $data = preg_replace( '/(^.+?=\s*)([^\'"].+)$/m', '\1"\2"', $data ); |
38 | |
39 | $messages = parse_ini_string( $data ); |
40 | if ( is_array( $messages ) ) { |
41 | $messages = $this->group->getMangler()->mangleArray( $messages ); |
42 | } else { |
43 | $messages = null; |
44 | } |
45 | |
46 | return [ |
47 | 'MESSAGES' => $messages, |
48 | 'AUTHORS' => $authors, |
49 | ]; |
50 | } |
51 | |
52 | protected function writeReal( MessageCollection $collection ): string { |
53 | $output = ''; |
54 | $mangler = $this->group->getMangler(); |
55 | |
56 | /** @var Message $m */ |
57 | foreach ( $collection as $key => $m ) { |
58 | $value = $m->translation(); |
59 | if ( $value === null ) { |
60 | continue; |
61 | } |
62 | |
63 | $comment = ''; |
64 | |
65 | if ( $m->hasTag( 'fuzzy' ) ) { |
66 | $value = str_replace( TRANSLATE_FUZZY, '', $value ); |
67 | $comment = "; Fuzzy\n"; |
68 | } |
69 | |
70 | $key = $mangler->unmangle( $key ); |
71 | $output .= "$comment$key = $value\n"; |
72 | } |
73 | |
74 | // Do not create empty files |
75 | if ( $output === '' ) { |
76 | return ''; |
77 | } |
78 | |
79 | global $wgSitename; |
80 | // Accumulator |
81 | $header = "; Exported from $wgSitename\n"; |
82 | |
83 | $authors = $collection->getAuthors(); |
84 | $authors = $this->filterAuthors( $authors, $collection->getLanguage() ); |
85 | foreach ( $authors as $author ) { |
86 | $header .= "; Author: $author\n"; |
87 | } |
88 | |
89 | $header .= '[' . $collection->getLanguage() . "]\n"; |
90 | |
91 | return $header . $output; |
92 | } |
93 | } |
94 | |
95 | class_alias( IniFormat::class, 'IniFFS' ); |