Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
FlatPhpFFS.php
Go to the documentation of this file.
1<?php
15
20class FlatPhpFFS extends SimpleFFS implements MetaYamlSchemaExtender {
21 public function getFileExtensions() {
22 return [ '.php' ];
23 }
24
29 public function readFromVariable( $data ) {
30 # Authors first
31 $matches = [];
32 preg_match_all( '/^ \* @author\s+(.+)$/m', $data, $matches );
33 $authors = $matches[1];
34
35 # Then messages
36 $matches = [];
37 $regex = '/^\$(.*?)\s*=\s*[\'"](.*?)[\'"];.*?$/mus';
38 preg_match_all( $regex, $data, $matches, PREG_SET_ORDER );
39 $messages = [];
40
41 foreach ( $matches as $_ ) {
42 $legal = Title::legalChars();
43 $key = preg_replace_callback( "/([^$legal]|\\\\)/u",
44 static function ( $m ) {
45 return '\x' . dechex( ord( $m[0] ) );
46 },
47 $_[1]
48 );
49 $value = str_replace( [ "\'", "\\\\" ], [ "'", "\\" ], $_[2] );
50 $messages[$key] = $value;
51 }
52
53 $messages = $this->group->getMangler()->mangleArray( $messages );
54
55 return [
56 'AUTHORS' => $authors,
57 'MESSAGES' => $messages,
58 ];
59 }
60
61 protected function writeReal( MessageCollection $collection ) {
62 $output = $this->extra['header'] ?? "<?php\n";
63 $output .= $this->doHeader( $collection );
64
65 $mangler = $this->group->getMangler();
66
68 foreach ( $collection as $item ) {
69 $key = $mangler->unmangle( $item->key() );
70 $key = stripcslashes( $key );
71
72 $value = $item->translation();
73 if ( $value === null ) {
74 continue;
75 }
76
77 $value = str_replace( TRANSLATE_FUZZY, '', $value );
78 $value = addcslashes( $value, "'" );
79
80 $output .= "\$$key = '$value';\n";
81 }
82
83 return $output;
84 }
85
86 protected function doHeader( MessageCollection $collection ) {
87 global $wgServer, $wgTranslateDocumentationLanguageCode;
88
89 $code = $collection->code;
90 $name = Utilities::getLanguageName( $code );
91 $native = Utilities::getLanguageName( $code, $code );
92
93 if ( $wgTranslateDocumentationLanguageCode ) {
94 $docu = "\n * See the $wgTranslateDocumentationLanguageCode 'language' for " .
95 'message documentation incl. usage of parameters';
96 } else {
97 $docu = '';
98 }
99
100 $authors = $this->doAuthors( $collection );
101
102 $output =
103 <<<PHP
114 PHP;
115
116 return $output;
117 }
118
119 protected function doAuthors( MessageCollection $collection ) {
120 $output = '';
121 $authors = $collection->getAuthors();
122 $authors = $this->filterAuthors( $authors, $collection->code );
123
124 foreach ( $authors as $author ) {
125 $output .= " * @author $author\n";
126 }
127
128 return $output;
129 }
130
131 public static function getExtraSchema(): array {
132 $schema = [
133 'root' => [
134 '_type' => 'array',
135 '_children' => [
136 'FILES' => [
137 '_type' => 'array',
138 '_children' => [
139 'header' => [
140 '_type' => 'text',
141 ],
142 ]
143 ]
144 ]
145 ]
146 ];
147
148 return $schema;
149 }
150}
This file contains the class for core message collections implementation.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:30
Message groups are usually configured in YAML, though the actual storage format does not matter,...
static getExtraSchema()
Return a data structure that will be merged with the base schema.