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