1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
<?php
/**
* Support for ini message file format.
*
* @file
* @author Niklas Laxström
* @copyright Copyright © 2012-2013, Niklas Laxström
* @license GPL-2.0-or-later
*/
/**
* IniFFS currently parses and generates flat ini files with language
* code as header key.
*
* @ingroup FFS
* @since 2012-11-19
*/
class IniFFS extends SimpleFFS {
public static function isValid( $data ) {
$conf = [ 'BASIC' => [ 'class' => 'FileBasedMessageGroup', 'namespace' => 8 ] ];
/**
* @var FileBasedMessageGroup $group
*/
$group = MessageGroupBase::factory( $conf );
MediaWiki\suppressWarnings();
$ffs = new IniFFS( $group );
$parsed = $ffs->readFromVariable( $data );
MediaWiki\restoreWarnings();
return (bool)count( $parsed['MESSAGES'] );
}
public function supportsFuzzy() {
return 'write';
}
public function getFileExtensions() {
return [ '.ini' ];
}
/**
* @param string $data
* @return array Parsed data.
*/
public function readFromVariable( $data ) {
$authors = [];
preg_match_all( '/^; Author: (.*)$/m', $data, $matches, PREG_SET_ORDER );
foreach ( $matches as $match ) {
$authors[] = $match[1];
}
// Remove comments
$data = preg_replace( '/^\s*;.*$/m', '', $data );
// Make sure values are quoted, PHP barks on stuff like ?{}|&~![()^
$data = preg_replace( '/(^.+?=\s*)([^\'"].+)$/m', '\1"\2"', $data );
$messages = parse_ini_string( $data );
if ( is_array( $messages ) ) {
$messages = $this->group->getMangler()->mangle( $messages );
} else {
$messages = null;
}
return [
'MESSAGES' => $messages,
'AUTHORS' => $authors,
];
}
protected function writeReal( MessageCollection $collection ) {
$output = '';
$mangler = $this->group->getMangler();
/**
* @var $m ThinMessage
*/
foreach ( $collection as $key => $m ) {
$value = $m->translation();
if ( $value === null ) {
continue;
}
$comment = '';
if ( $m->hasTag( 'fuzzy' ) ) {
$value = str_replace( TRANSLATE_FUZZY, '', $value );
$comment = "; Fuzzy\n";
}
$key = $mangler->unmangle( $key );
$output .= "$comment$key = $value\n";
}
// Do not create empty files
if ( $output === '' ) {
return '';
}
global $wgSitename;
// Accumulator
$header = "; Exported from $wgSitename\n";
$authors = $collection->getAuthors();
$authors = $this->filterAuthors( $authors, $collection->getLanguage() );
foreach ( $authors as $author ) {
$header .= "; Author: $author\n";
}
$header .= '[' . $collection->getLanguage() . "]\n";
return $header . $output;
}
}
|