Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1428289
YamlFormat.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
4 KB
Referenced Files
None
Subscribers
None
YamlFormat.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Extension\Translate\FileFormatSupport
;
use
FileBasedMessageGroup
;
use
MediaWiki\Extension\Translate\MessageGroupConfiguration\MetaYamlSchemaExtender
;
use
MediaWiki\Extension\Translate\MessageLoading\Message
;
use
MediaWiki\Extension\Translate\MessageLoading\MessageCollection
;
use
MediaWiki\Extension\Translate\MessageProcessing\ArrayFlattener
;
use
MediaWiki\Extension\Translate\Utilities\Utilities
;
use
MediaWiki\Extension\Translate\Utilities\Yaml
;
use
RuntimeException
;
/**
* Implements support for message storage in YAML format.
*
* This class adds new key into FILES section: \c codeAsRoot.
* If it is set to true, all messages will under language code.
* @ingroup FileFormatSupport
*/
class
YamlFormat
extends
SimpleFormat
implements
MetaYamlSchemaExtender
{
private
ArrayFlattener
$flattener
;
public
function
__construct
(
FileBasedMessageGroup
$group
)
{
parent
::
__construct
(
$group
);
// Obtains object used to flatten and unflatten arrays. In this implementation
// we use the ArrayFlattener class which also supports CLDR pluralization rules.
$this
->
flattener
=
new
ArrayFlattener
(
$this
->
extra
[
'nestingSeparator'
]
??
'.'
,
$this
->
extra
[
'parseCLDRPlurals'
]
??
false
);
}
public
function
getFileExtensions
():
array
{
return
[
'.yaml'
,
'.yml'
];
}
/** @inheritDoc */
public
function
readFromVariable
(
string
$data
):
array
{
// Authors first.
$matches
=
[];
preg_match_all
(
'/^#
\s
*Author:
\s
*(.*)$/m'
,
$data
,
$matches
);
$authors
=
$matches
[
1
];
// Then messages.
$messages
=
Yaml
::
loadString
(
$data
);
// Some groups have messages under language code
if
(
isset
(
$this
->
extra
[
'codeAsRoot'
]
)
)
{
$messages
=
array_shift
(
$messages
)
??
[];
}
$messages
=
$this
->
flattener
->
flatten
(
$messages
);
$messages
=
$this
->
group
->
getMangler
()->
mangleArray
(
$messages
);
foreach
(
$messages
as
&
$value
)
{
$value
=
rtrim
(
$value
,
"
\n
"
);
}
return
[
'AUTHORS'
=>
$authors
,
'MESSAGES'
=>
$messages
,
];
}
protected
function
writeReal
(
MessageCollection
$collection
):
string
{
$output
=
$this
->
doHeader
(
$collection
);
$output
.=
$this
->
doAuthors
(
$collection
);
$mangler
=
$this
->
group
->
getMangler
();
$messages
=
[];
$collection
->
filter
(
'hastranslation'
,
false
);
/** @var Message $m */
foreach
(
$collection
as
$key
=>
$m
)
{
$key
=
$mangler
->
unmangle
(
$key
);
$value
=
$m
->
translation
();
if
(
$value
===
null
)
{
throw
new
RuntimeException
(
"Expected translation to be present for $key, but found null."
);
}
$value
=
str_replace
(
TRANSLATE_FUZZY
,
''
,
$value
);
if
(
$value
===
''
)
{
continue
;
}
$messages
[
$key
]
=
$value
;
}
if
(
!
count
(
$messages
)
)
{
return
''
;
}
$messages
=
$this
->
flattener
->
unflatten
(
$messages
);
// Some groups have messages under language code.
if
(
isset
(
$this
->
extra
[
'codeAsRoot'
]
)
)
{
$code
=
$this
->
group
->
mapCode
(
$collection
->
code
);
$messages
=
[
$code
=>
$messages
];
}
$output
.=
Yaml
::
dump
(
$messages
);
return
$output
;
}
private
function
doHeader
(
MessageCollection
$collection
):
string
{
global
$wgSitename
;
global
$wgTranslateYamlLibrary
;
$code
=
$collection
->
code
;
$name
=
Utilities
::
getLanguageName
(
$code
);
$native
=
Utilities
::
getLanguageName
(
$code
,
$code
);
$output
=
"# Messages for $name ($native)
\n
"
;
$output
.=
"# Exported from $wgSitename
\n
"
;
if
(
isset
(
$wgTranslateYamlLibrary
)
)
{
$output
.=
"# Export driver: $wgTranslateYamlLibrary
\n
"
;
}
return
$output
;
}
private
function
doAuthors
(
MessageCollection
$collection
):
string
{
$output
=
''
;
$authors
=
$collection
->
getAuthors
();
$authors
=
$this
->
filterAuthors
(
$authors
,
$collection
->
code
);
foreach
(
$authors
as
$author
)
{
$output
.=
"# Author: $author
\n
"
;
}
return
$output
;
}
public
function
isContentEqual
(
?
string
$a
,
?
string
$b
):
bool
{
return
$this
->
flattener
->
compareContent
(
$a
,
$b
);
}
public
static
function
getExtraSchema
():
array
{
return
[
'root'
=>
[
'_type'
=>
'array'
,
'_children'
=>
[
'FILES'
=>
[
'_type'
=>
'array'
,
'_children'
=>
[
'codeAsRoot'
=>
[
'_type'
=>
'boolean'
,
],
'nestingSeparator'
=>
[
'_type'
=>
'text'
,
],
'parseCLDRPlurals'
=>
[
'_type'
=>
'boolean'
,
]
]
]
]
]
];
}
}
class_alias
(
YamlFormat
::
class
,
'YamlFFS'
);
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 15:59 (14 h, 13 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
e6/fa/1ac8a1ae3738c53d9787ad614e2a
Default Alt Text
YamlFormat.php (4 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment