]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/purtle/src/N3Quoter.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / purtle / src / N3Quoter.php
1 <?php
2
3 namespace Wikimedia\Purtle;
4
5 /**
6  * Helper class for quoting literals and URIs in N3 output.
7  * Optionally supports shorthand and prefix resolution.
8  *
9  * @license GPL-2.0+
10  * @author Daniel Kinzler
11  */
12 class N3Quoter {
13
14         /**
15          * @var UnicodeEscaper
16          */
17         private $escaper = null;
18
19         /**
20          * @param bool $escapeUnicode
21          */
22         public function setEscapeUnicode( $escapeUnicode ) {
23                 $this->escaper = $escapeUnicode ? new UnicodeEscaper() : null;
24         }
25
26         public function escapeIRI( $iri ) {
27                 //FIXME: apply unicode escaping?!
28                 return strtr( $iri, [
29                                 ' ' => '%20',
30                                 '"' => '%22',
31                                 '<' => '%3C',
32                                 '>' => '%3E',
33                                 '\\' => '%5C',
34                                 '`' => '%60',
35                                 '^' => '%5E',
36                                 '|' => '%7C',
37                                 '{' => '%7B',
38                                 '}' => '%7D',
39                 ] );
40         }
41
42         public function escapeLiteral( $s ) {
43                 // String escapes. Note that the N3 spec is more restrictive than the Turtle and TR
44                 // specifications, see <https://www.w3.org/TeamSubmission/n3/#escaping>
45                 // and <https://www.w3.org/TR/turtle/#string>
46                 // and <https://www.w3.org/TR/n-triples/#grammar-production-literal>.
47                 // Allowed escapes according to the N3 spec are:
48                 // ECHAR        ::=     '\' [tbnrf"'\]
49                 // The single quote however does not require escaping when used in double quotes.
50                 $escaped = strtr( $s, [
51                         "\x00" => '\u0000',
52                         "\x01" => '\u0001',
53                         "\x02" => '\u0002',
54                         "\x03" => '\u0003',
55                         "\x04" => '\u0004',
56                         "\x05" => '\u0005',
57                         "\x06" => '\u0006',
58                         "\x07" => '\u0007',
59                         "\x08" => '\b',
60                         "\x09" => '\t',
61                         "\x0A" => '\n',
62                         "\x0B" => '\u000B',
63                         "\x0C" => '\f',
64                         "\x0D" => '\r',
65                         "\x0E" => '\u000E',
66                         "\x0F" => '\u000F',
67                         "\x10" => '\u0010',
68                         "\x11" => '\u0011',
69                         "\x12" => '\u0012',
70                         "\x13" => '\u0013',
71                         "\x14" => '\u0014',
72                         "\x15" => '\u0015',
73                         "\x16" => '\u0016',
74                         "\x17" => '\u0017',
75                         "\x18" => '\u0018',
76                         "\x19" => '\u0019',
77                         "\x1A" => '\u001A',
78                         "\x1B" => '\u001B',
79                         "\x1C" => '\u001C',
80                         "\x1D" => '\u001D',
81                         "\x1E" => '\u001E',
82                         "\x1F" => '\u001F',
83                         '"' => '\"',
84                         '\\' => '\\\\',
85                 ] );
86
87                 if ( $this->escaper !== null ) {
88                         $escaped = $this->escaper->escapeString( $escaped );
89                 }
90
91                 return $escaped;
92         }
93
94 }