Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
IniFFS.php
Go to the documentation of this file.
1<?php
18class IniFFS extends SimpleFFS {
19 public static function isValid( $data ) {
20 $conf = [
21 'BASIC' => [ 'class' => FileBasedMessageGroup::class, 'namespace' => 8 ],
22 'FILES' => []
23 ];
25 $group = MessageGroupBase::factory( $conf );
26 '@phan-var FileBasedMessageGroup $group';
27
28 $ffs = new self( $group );
29 $parsed = $ffs->readFromVariable( $data );
30
31 return (bool)count( $parsed['MESSAGES'] );
32 }
33
34 public function supportsFuzzy() {
35 return 'write';
36 }
37
38 public function getFileExtensions() {
39 return [ '.ini' ];
40 }
41
46 public function readFromVariable( $data ) {
47 $authors = [];
48 preg_match_all( '/^; Author: (.*)$/m', $data, $matches, PREG_SET_ORDER );
49 foreach ( $matches as $match ) {
50 $authors[] = $match[1];
51 }
52
53 // Remove comments
54 $data = preg_replace( '/^\s*;.*$/m', '', $data );
55 // Make sure values are quoted, PHP barks on stuff like ?{}|&~![()^
56 $data = preg_replace( '/(^.+?=\s*)([^\'"].+)$/m', '\1"\2"', $data );
57
58 $messages = parse_ini_string( $data );
59 if ( is_array( $messages ) ) {
60 $messages = $this->group->getMangler()->mangleArray( $messages );
61 } else {
62 $messages = null;
63 }
64
65 return [
66 'MESSAGES' => $messages,
67 'AUTHORS' => $authors,
68 ];
69 }
70
71 protected function writeReal( MessageCollection $collection ) {
72 $output = '';
73 $mangler = $this->group->getMangler();
74
76 foreach ( $collection as $key => $m ) {
77 $value = $m->translation();
78 if ( $value === null ) {
79 continue;
80 }
81
82 $comment = '';
83
84 if ( $m->hasTag( 'fuzzy' ) ) {
85 $value = str_replace( TRANSLATE_FUZZY, '', $value );
86 $comment = "; Fuzzy\n";
87 }
88
89 $key = $mangler->unmangle( $key );
90 $output .= "$comment$key = $value\n";
91 }
92
93 // Do not create empty files
94 if ( $output === '' ) {
95 return '';
96 }
97
98 global $wgSitename;
99 // Accumulator
100 $header = "; Exported from $wgSitename\n";
101
102 $authors = $collection->getAuthors();
103 $authors = $this->filterAuthors( $authors, $collection->getLanguage() );
104 foreach ( $authors as $author ) {
105 $header .= "; Author: $author\n";
106 }
107
108 $header .= '[' . $collection->getLanguage() . "]\n";
109
110 return $header . $output;
111 }
112}
IniFFS currently parses and generates flat ini files with language code as header key.
Definition IniFFS.php:18
getFileExtensions()
Return the commonly used file extensions for these formats.
Definition IniFFS.php:38
writeReal(MessageCollection $collection)
Definition IniFFS.php:71
supportsFuzzy()
Query the capabilities of this FFS.
Definition IniFFS.php:34
readFromVariable( $data)
Definition IniFFS.php:46
Core message collection class.
getAuthors()
Lists all translators that have contributed to the latest revisions of each translation.
filterAuthors(array $authors, $code)
Remove excluded authors.