Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F1427952
Yaml.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
1 KB
Referenced Files
None
Subscribers
None
Yaml.php
View Options
<?php
declare
(
strict_types
=
1
);
namespace
MediaWiki\Extension\Translate\Utilities
;
use
InvalidArgumentException
;
use
RuntimeException
;
use
Spyc
;
/**
* A wrapper class to provide interface to parse
* and generate YAML files with phpyaml or spyc backend.
* @author Ævar Arnfjörð Bjarmason
* @author Niklas Laxström
* @copyright Copyright © 2009-2013, Niklas Laxström, Ævar Arnfjörð Bjarmason
* @license GPL-2.0-or-later
*/
class
Yaml
{
public
static
function
loadString
(
string
$text
):
array
{
global
$wgTranslateYamlLibrary
;
switch
(
$wgTranslateYamlLibrary
)
{
case
'phpyaml'
:
// Harden: do not support unserializing objects.
$previousValue
=
ini_set
(
'yaml.decode_php'
,
'0'
);
$ret
=
yaml_parse
(
$text
);
if
(
$previousValue
!==
false
)
{
ini_set
(
'yaml.decode_php'
,
$previousValue
);
}
if
(
$ret
===
false
)
{
// Convert failures to exceptions
throw
new
InvalidArgumentException
(
'Invalid Yaml string'
);
}
return
$ret
;
case
'spyc'
:
$yaml
=
spyc_load
(
$text
);
return
self
::
fixSpycSpaces
(
$yaml
);
default
:
throw
new
RuntimeException
(
'Unknown Yaml library'
);
}
}
private
static
function
fixSpycSpaces
(
array
&
$yaml
):
array
{
foreach
(
$yaml
as
$key
=>
&
$value
)
{
if
(
is_array
(
$value
)
)
{
self
::
fixSpycSpaces
(
$value
);
}
elseif
(
is_string
(
$value
)
&&
$key
===
'header'
)
{
$value
=
preg_replace
(
'~^
\*
~m'
,
' *'
,
$value
)
.
"
\n
"
;
}
}
return
$yaml
;
}
public
static
function
load
(
string
$file
):
array
{
$text
=
file_get_contents
(
$file
);
return
self
::
loadString
(
$text
);
}
public
static
function
dump
(
array
$text
):
string
{
global
$wgTranslateYamlLibrary
;
switch
(
$wgTranslateYamlLibrary
)
{
case
'phpyaml'
:
return
yaml_emit
(
$text
,
YAML_UTF8_ENCODING
);
case
'spyc'
:
return
Spyc
::
YAMLDump
(
$text
);
default
:
throw
new
RuntimeException
(
'Unknown Yaml library'
);
}
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Sat, May 16, 15:24 (14 h, 51 m)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
4c/d4/74d0b83dbdf9a311c133477867c3
Default Alt Text
Yaml.php (1 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment