]> scripts.mit.edu Git - autoinstallsdev/mediawiki.git/blob - vendor/wikimedia/purtle/src/TurtleRdfWriter.php
MediaWiki 1.30.2
[autoinstallsdev/mediawiki.git] / vendor / wikimedia / purtle / src / TurtleRdfWriter.php
1 <?php
2
3 namespace Wikimedia\Purtle;
4
5 /**
6  * RdfWriter implementation for generating Turtle output.
7  *
8  * @license GPL-2.0+
9  * @author Daniel Kinzler
10  */
11 class TurtleRdfWriter extends N3RdfWriterBase {
12         /**
13          * @var bool
14          */
15         private $trustIRIs = true;
16
17         /**
18          * @return bool
19          */
20         public function getTrustIRIs() {
21                 return $this->trustIRIs;
22         }
23
24         /**
25          * @param bool $trustIRIs
26          */
27         public function setTrustIRIs( $trustIRIs ) {
28                 $this->trustIRIs = $trustIRIs;
29         }
30
31         public function __construct(
32                 $role = parent::DOCUMENT_ROLE,
33                 BNodeLabeler $labeler = null,
34                 N3Quoter $quoter = null
35         ) {
36                 parent::__construct( $role, $labeler, $quoter );
37                 $this->transitionTable[self::STATE_OBJECT] = [
38                         self::STATE_DOCUMENT => " .\n",
39                         self::STATE_SUBJECT => " .\n\n",
40                         self::STATE_PREDICATE => " ;\n\t",
41                         self::STATE_OBJECT => ",\n\t\t",
42                 ];
43                 $this->transitionTable[self::STATE_DOCUMENT][self::STATE_SUBJECT] = "\n";
44                 $this->transitionTable[self::STATE_SUBJECT][self::STATE_PREDICATE] = ' ';
45                 $this->transitionTable[self::STATE_PREDICATE][self::STATE_OBJECT] = ' ';
46                 $this->transitionTable[self::STATE_START][self::STATE_DOCUMENT] = function () {
47                         $this->beginDocument();
48                 };
49         }
50
51         /**
52          * Write prefixes
53          */
54         private function beginDocument() {
55                 foreach ( $this->getPrefixes() as $prefix => $uri ) {
56                         $this->write( "@prefix $prefix: <" . $this->quoter->escapeIRI( $uri ) . "> .\n" );
57                 }
58         }
59
60         protected function writeSubject( $base, $local = null ) {
61                 if ( $local !== null ) {
62                         $this->write( "$base:$local" );
63                 } else {
64                         $this->writeIRI( $base, $this->trustIRIs );
65                 }
66         }
67
68         protected function writePredicate( $base, $local = null ) {
69                 if ( $base === 'a' ) {
70                         $this->write( 'a' );
71                         return;
72                 }
73                 if ( $local !== null ) {
74                         $this->write( "$base:$local" );
75                 } else {
76                         $this->writeIRI( $base, $this->trustIRIs );
77                 }
78         }
79
80         protected function writeResource( $base, $local = null ) {
81                 if ( $local !== null ) {
82                         $this->write( "$base:$local" );
83                 } else {
84                         $this->writeIRI( $base );
85                 }
86         }
87
88         /**
89          * @param string $role
90          * @param BNodeLabeler $labeler
91          *
92          * @return RdfWriterBase
93          */
94         protected function newSubWriter( $role, BNodeLabeler $labeler ) {
95                 $writer = new self( $role, $labeler, $this->quoter );
96
97                 return $writer;
98         }
99
100         /**
101          * @return string a MIME type
102          */
103         public function getMimeType() {
104                 return 'text/turtle; charset=UTF-8';
105         }
106
107 }