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
|
<?php
/**
* Implements FFS for DTD file format.
*
* @file
* @author Guillaume Duhamel
* @author Niklas Laxström
* @author Siebrand Mazeland
* @copyright Copyright © 2009-2010, Guillaume Duhamel, Niklas Laxström, Siebrand Mazeland
* @license GPL-2.0-or-later
*/
/**
* File format support for DTD.
*
* @ingroup FFS
*/
class DtdFFS extends SimpleFFS {
public function getFileExtensions() {
return [ '.dtd' ];
}
/**
* @param string $data
* @return array Parsed data.
*/
public function readFromVariable( $data ) {
preg_match_all( ',# Author: ([^\n]+)\n,', $data, $matches );
$authors = $matches[1];
preg_match_all( ',<!ENTITY[ ]+([^ ]+)\s+"([^"]+)"[^>]*>,', $data, $matches );
list( , $keys, $messages ) = $matches;
$messages = array_combine(
$keys,
array_map(
function ( $message ) {
return html_entity_decode( $message, ENT_QUOTES );
},
$messages
)
);
$messages = $this->group->getMangler()->mangleArray( $messages );
return [
'AUTHORS' => $authors,
'MESSAGES' => $messages,
];
}
protected function writeReal( MessageCollection $collection ) {
$collection->loadTranslations();
$header = "<!--\n";
$header .= $this->doHeader( $collection );
$header .= $this->doAuthors( $collection );
$header .= "-->\n";
$output = '';
$mangler = $this->group->getMangler();
/** @var TMessage $m */
foreach ( $collection as $key => $m ) {
$key = $mangler->unmangle( $key );
$trans = $m->translation();
$trans = str_replace( TRANSLATE_FUZZY, '', $trans );
if ( $trans === '' ) {
continue;
}
$trans = str_replace( '"', '"', $trans );
$output .= "<!ENTITY $key \"$trans\">\n";
}
if ( $output ) {
return $header . $output;
}
return false;
}
protected function doHeader( MessageCollection $collection ) {
global $wgSitename;
$code = $collection->code;
$name = TranslateUtils::getLanguageName( $code );
$native = TranslateUtils::getLanguageName( $code, $code );
$output = "# Messages for $name ($native)\n";
$output .= "# Exported from $wgSitename\n\n";
return $output;
}
protected function doAuthors( MessageCollection $collection ) {
$output = '';
$authors = $collection->getAuthors();
$authors = $this->filterAuthors( $authors, $collection->code );
foreach ( $authors as $author ) {
$output .= "# Author: $author\n";
}
return $output;
}
}
|