Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
FlatPhpFFS.php
Go to the documentation of this file.
1<?php
16class FlatPhpFFS extends SimpleFFS implements MetaYamlSchemaExtender {
17 public function getFileExtensions() {
18 return [ '.php' ];
19 }
20
25 public function readFromVariable( $data ) {
26 # Authors first
27 $matches = [];
28 preg_match_all( '/^ \* @author\s+(.+)$/m', $data, $matches );
29 $authors = $matches[1];
30
31 # Then messages
32 $matches = [];
33 $regex = '/^\$(.*?)\s*=\s*[\'"](.*?)[\'"];.*?$/mus';
34 preg_match_all( $regex, $data, $matches, PREG_SET_ORDER );
35 $messages = [];
36
37 foreach ( $matches as $_ ) {
38 $legal = Title::legalChars();
39 $key = preg_replace_callback( "/([^$legal]|\\\\)/u",
40 static function ( $m ) {
41 return '\x' . dechex( ord( $m[0] ) );
42 },
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 ) {
58 $output = $this->extra['header'] ?? "<?php\n";
59 $output .= $this->doHeader( $collection );
60
61 $mangler = $this->group->getMangler();
62
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 protected function doHeader( MessageCollection $collection ) {
83 global $wgServer, $wgTranslateDocumentationLanguageCode;
84
85 $code = $collection->code;
86 $name = TranslateUtils::getLanguageName( $code );
87 $native = TranslateUtils::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 $output = <<<PHP
109PHP;
110
111 return $output;
112 }
113
114 protected function doAuthors( MessageCollection $collection ) {
115 $output = '';
116 $authors = $collection->getAuthors();
117 $authors = $this->filterAuthors( $authors, $collection->code );
118
119 foreach ( $authors as $author ) {
120 $output .= " * @author $author\n";
121 }
122
123 return $output;
124 }
125
126 public static function getExtraSchema() {
127 $schema = [
128 'root' => [
129 '_type' => 'array',
130 '_children' => [
131 'FILES' => [
132 '_type' => 'array',
133 '_children' => [
134 'header' => [
135 '_type' => 'text',
136 ],
137 ]
138 ]
139 ]
140 ]
141 ];
142
143 return $schema;
144 }
145}
Core message collection class.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
static getLanguageName( $code, $language='en')
Returns a localised language name.
Message groups are usually configured in YAML, though the actual storage format does not matter,...