Page Menu
Home
WickedGov Phorge
Search
Configure Global Search
Log In
Files
F4129584
N3Quoter.php
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Flag For Later
Award Token
Size
2 KB
Referenced Files
None
Subscribers
None
N3Quoter.php
View Options
<?php
namespace
Wikimedia\Purtle
;
/**
* Helper class for quoting literals and URIs in N3 output.
* Optionally supports shorthand and prefix resolution.
*
* @license GPL-2.0-or-later
* @author Daniel Kinzler
*/
class
N3Quoter
{
/**
* @var UnicodeEscaper
*/
private
$escaper
=
null
;
/**
* @param bool $escapeUnicode
*/
public
function
setEscapeUnicode
(
$escapeUnicode
)
{
$this
->
escaper
=
$escapeUnicode
?
new
UnicodeEscaper
()
:
null
;
}
/**
* @param string $iri
*
* @return string
*/
public
function
escapeIRI
(
$iri
)
{
// FIXME: apply unicode escaping?!
return
strtr
(
$iri
,
[
' '
=>
'%20'
,
'"'
=>
'%22'
,
'<'
=>
'%3C'
,
'>'
=>
'%3E'
,
'
\\
'
=>
'%5C'
,
'`'
=>
'%60'
,
'^'
=>
'%5E'
,
'|'
=>
'%7C'
,
'{'
=>
'%7B'
,
'}'
=>
'%7D'
,
]
);
}
/**
* @param string $s
*
* @return string
*/
public
function
escapeLiteral
(
$s
)
{
// Performance: If the entire string is just (a safe subset) of ASCII, let it through.
// Ok are space (31), ! (32), # (35) - [ (91) and ] (93) to ~ (126), excludes " (34) and \ (92).
if
(
preg_match
(
'/^[ !#-[
\]
-~]*
\z
/'
,
$s
)
)
{
return
$s
;
}
// String escapes. Note that the N3 spec is more restrictive than the Turtle and TR
// specifications, see <https://www.w3.org/TeamSubmission/n3/#escaping>
// and <https://www.w3.org/TR/turtle/#string>
// and <https://www.w3.org/TR/n-triples/#grammar-production-literal>.
// Allowed escapes according to the N3 spec are:
// ECHAR ::= '\' [tbnrf"'\]
// The single quote however does not require escaping when used in double quotes.
$escaped
=
strtr
(
$s
,
[
"
\x
00"
=>
'
\u
0000'
,
"
\x
01"
=>
'
\u
0001'
,
"
\x
02"
=>
'
\u
0002'
,
"
\x
03"
=>
'
\u
0003'
,
"
\x
04"
=>
'
\u
0004'
,
"
\x
05"
=>
'
\u
0005'
,
"
\x
06"
=>
'
\u
0006'
,
"
\x
07"
=>
'
\u
0007'
,
"
\x
08"
=>
'
\b
'
,
"
\x
09"
=>
'
\t
'
,
"
\x
0A"
=>
'
\n
'
,
"
\x
0B"
=>
'
\u
000B'
,
"
\x
0C"
=>
'
\f
'
,
"
\x
0D"
=>
'
\r
'
,
"
\x
0E"
=>
'
\u
000E'
,
"
\x
0F"
=>
'
\u
000F'
,
"
\x
10"
=>
'
\u
0010'
,
"
\x
11"
=>
'
\u
0011'
,
"
\x
12"
=>
'
\u
0012'
,
"
\x
13"
=>
'
\u
0013'
,
"
\x
14"
=>
'
\u
0014'
,
"
\x
15"
=>
'
\u
0015'
,
"
\x
16"
=>
'
\u
0016'
,
"
\x
17"
=>
'
\u
0017'
,
"
\x
18"
=>
'
\u
0018'
,
"
\x
19"
=>
'
\u
0019'
,
"
\x
1A"
=>
'
\u
001A'
,
"
\x
1B"
=>
'
\u
001B'
,
"
\x
1C"
=>
'
\u
001C'
,
"
\x
1D"
=>
'
\u
001D'
,
"
\x
1E"
=>
'
\u
001E'
,
"
\x
1F"
=>
'
\u
001F'
,
'"'
=>
'
\"
'
,
'
\\
'
=>
'
\\\\
'
,
]
);
if
(
$this
->
escaper
!==
null
)
{
$escaped
=
$this
->
escaper
->
escapeString
(
$escaped
);
}
return
$escaped
;
}
}
File Metadata
Details
Attached
Mime Type
text/x-php
Expires
Wed, Aug 19, 09:58 (2 w, 5 d ago)
Storage Engine
local-disk
Storage Format
Raw Data
Storage Handle
f0/a0/333e41bd1f0036bb75d1967813b2
Default Alt Text
N3Quoter.php (2 KB)
Attached To
Mode
rMWPROD MediaWiki Production
Attached
Detach File
Event Timeline
Log In to Comment