summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/includes/export/SMW_Serializer_Turtle.php
blob: d7a6ff2a27d1e3b2da0d67ffbc94f272bee6f05a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
<?php

use SMW\InMemoryPoolCache;

/**
 * File holding the SMWTurtleSerializer class that provides basic functions for
 * serialising OWL data in Turtle syntax.
 *
 * @ingroup SMW
 *
 * @author Markus Krötzsch
 */

/**
 * Class for serializing exported data (encoded as SMWExpData object) in
 * Turtle syntax.
 *
 * @ingroup SMW
 */
class SMWTurtleSerializer extends SMWSerializer {
	/**
	 * Array of non-trivial sub-SMWExpData elements that cannot be nested while
	 * serializing some SMWExpData. The elements of the array are serialized
	 * later during the same serialization step (so this is not like another
	 * queue for declarations or the like; it just unfolds an SMWExpData
	 * object).
	 *
	 * @var array of SMWExpData
	 */
	protected $subexpdata;

	/**
	 * If true, do not serialize namespace declarations and record them in
	 * $sparql_namespaces instead for later retrieval.
	 * @var boolean
	 */
	protected $sparqlmode;

	/**
	 * Array of retrieved namespaces (abbreviation => URI) for later use.
	 * @var array of string
	 */
	protected $sparql_namespaces;

	public function __construct( $sparqlMode = false ) {
		parent::__construct();
		$this->sparqlmode = $sparqlMode;
	}

	public function clear() {
		parent::clear();
		$this->sparql_namespaces = [];
	}

	/**
	 * @since 2.3
	 */
	public static function reset() {
		InMemoryPoolCache::getInstance()->resetPoolCacheById( 'turtle.serializer' );
	}

	/**
	 * Get an array of namespace prefixes used in SPARQL mode.
	 * Namespaces are not serialized among triples in SPARQL mode but are
	 * collected separately. This method returns the prefixes and empties
	 * the collected list afterwards.
	 *
	 * @return array shortName => namespace URI
	 */
	public function flushSparqlPrefixes() {
		$result = $this->sparql_namespaces;
		$this->sparql_namespaces = [];
		return $result;
	}

	protected function serializeHeader() {
		if ( $this->sparqlmode ) {
			$this->pre_ns_buffer = '';
			$this->sparql_namespaces = [
				"rdf" => SMWExporter::getInstance()->expandURI( '&rdf;' ),
				"rdfs" => SMWExporter::getInstance()->expandURI( '&rdfs;' ),
				"owl" => SMWExporter::getInstance()->expandURI( '&owl;' ),
				"swivt" => SMWExporter::getInstance()->expandURI( '&swivt;' ),
				"wiki" => SMWExporter::getInstance()->expandURI( '&wiki;' ),
				"category" => SMWExporter::getInstance()->expandURI( '&category;' ),
				"property" => SMWExporter::getInstance()->expandURI( '&property;' ),
				"xsd" => "http://www.w3.org/2001/XMLSchema#" ,
				"wikiurl" => SMWExporter::getInstance()->expandURI( '&wikiurl;' )
			];
		} else {
			$this->pre_ns_buffer =
			"@prefix rdf: <" . SMWExporter::getInstance()->expandURI( '&rdf;' ) . "> .\n" .
			"@prefix rdfs: <" . SMWExporter::getInstance()->expandURI( '&rdfs;' ) . "> .\n" .
			"@prefix owl: <" . SMWExporter::getInstance()->expandURI( '&owl;' ) . "> .\n" .
			"@prefix swivt: <" . SMWExporter::getInstance()->expandURI( '&swivt;' ) . "> .\n" .
			// A note on "wiki": this namespace is crucial as a fallback when it would be illegal to start e.g. with a number.
			// In this case, one can always use wiki:... followed by "_" and possibly some namespace, since _ is legal as a first character.
			"@prefix wiki: <" . SMWExporter::getInstance()->expandURI( '&wiki;' ) . "> .\n" .
			"@prefix category: <" . SMWExporter::getInstance()->expandURI( '&category;' ) . "> .\n" .
			"@prefix property: <" . SMWExporter::getInstance()->expandURI( '&property;' ) . "> .\n" .
			"@prefix xsd: <http://www.w3.org/2001/XMLSchema#> .\n" . // note that this XSD URI is hardcoded below (its unlikely to change, of course)
			"@prefix wikiurl: <" . SMWExporter::getInstance()->expandURI( '&wikiurl;' ) . "> .\n";
		}
		$this->global_namespaces = [ 'rdf' => true, 'rdfs' => true, 'owl' => true, 'swivt' => true, 'wiki' => true, 'property' => true, 'category' => true ];
		$this->post_ns_buffer = "\n";
	}

	protected function serializeFooter() {
		if ( !$this->sparqlmode ) {
			$this->post_ns_buffer .= "\n# Created by Semantic MediaWiki, https://www.semantic-mediawiki.org/\n";
		}
	}

	public function serializeDeclaration( $uri, $typename ) {
		$this->post_ns_buffer .= "<" . SMWExporter::getInstance()->expandURI( $uri ) . "> rdf:type $typename .\n";
	}

	public function serializeExpData( SMWExpData $expData ) {

		$this->subExpData = [ $expData ];

		while ( count( $this->subExpData ) > 0 ) {
			$this->serializeNestedExpData( array_pop( $this->subExpData ), '' );
		}

		$this->serializeNamespaces();
	}

	protected function serializeNamespace( $shortname, $uri ) {
		$this->global_namespaces[$shortname] = true;
		if ( $this->sparqlmode ) {
			$this->sparql_namespaces[$shortname] = $uri;
		} else {
			$this->pre_ns_buffer .= "@prefix $shortname: <$uri> .\n";
		}
	}

	/**
	 * Serialize the given SMWExpData object, possibly recursively with
	 * increased indentation.
	 *
	 * @param $data SMWExpData containing the data to be serialised.
	 * @param $indent string specifying a prefix for indentation (usually a sequence of tabs)
	 */
	protected function serializeNestedExpData( SMWExpData $data, $indent ) {
		if ( count( $data->getProperties() ) == 0 ) {
			return; // nothing to export
		}

		// Avoid posting turtle property declarations already known for the
		// subject more than once
		if ( $data->getSubject()->getDataItem() !== null && $data->getSubject()->getDataItem()->getNamespace() === SMW_NS_PROPERTY ) {

			$hash = $data->getHash();
			$poolCache = InMemoryPoolCache::getInstance()->getPoolCacheById( 'turtle.serializer' );

			if ( $poolCache->contains( $hash ) && $poolCache->fetch( $hash ) ) {
				return;
			}

			$poolCache->save( $hash, true );
		}

		$this->recordDeclarationTypes( $data );

		$bnode = false;
		$this->post_ns_buffer .= $indent;
		if ( !$data->getSubject()->isBlankNode() ) {
			$this->serializeExpResource( $data->getSubject() );
		} else { // blank node
			$bnode = true;
			$this->post_ns_buffer .= "[";
		}

		if ( ( $indent !== '' ) && ( !$bnode ) ) { // called to generate a nested descripion; but Turtle cannot nest non-bnode descriptions, do this later
			$this->subexpdata[] = $data;
			return;
		} elseif ( !$bnode ) {
			$this->post_ns_buffer .= "\n ";
		}

		$firstproperty = true;
		foreach ( $data->getProperties() as $property ) {
			$this->post_ns_buffer .= $firstproperty ? "\t" : " ;\n $indent\t";
			$firstproperty = false;
			$prop_decl_queued = false;
			$class_type_prop = $this->isOWLClassTypeProperty( $property );
			$this->serializeExpResource( $property );
			$firstvalue = true;

			foreach ( $data->getValues( $property ) as $value ) {
				$this->post_ns_buffer .= $firstvalue ? '  ' : ' ,  ';
				$firstvalue = false;

				if ( $value instanceof SMWExpLiteral ) {
					$prop_decl_type = SMW_SERIALIZER_DECL_APROP;
					$this->serializeExpLiteral( $value );
				} elseif ( $value instanceof SMWExpResource ) {
					$prop_decl_type = SMW_SERIALIZER_DECL_OPROP;
					$this->serializeExpResource( $value );
				} elseif ( $value instanceof SMWExpData ) { // resource (maybe blank node), could have subdescriptions
					$prop_decl_type = SMW_SERIALIZER_DECL_OPROP;
					$collection = $value->getCollection();
					if ( $collection !== false ) { // RDF-style collection (list)
						$this->post_ns_buffer .= "( ";
						foreach ( $collection as $subvalue ) {
							$this->serializeNestedExpData( $subvalue, $indent . "\t\t" );
							if ( $class_type_prop ) {
								$this->requireDeclaration( $subvalue->getSubject(), SMW_SERIALIZER_DECL_CLASS );
							}
						}
						$this->post_ns_buffer .= " )";
					} else {
						if ( $class_type_prop ) {
							$this->requireDeclaration( $value->getSubject(), SMW_SERIALIZER_DECL_CLASS );
						}
						if ( count( $value->getProperties() ) > 0 ) { // resource with data: serialise
							$this->post_ns_buffer .= "\n";
							$this->serializeNestedExpData( $value, $indent . "\t\t" );
						} else { // resource without data: may need to be queued
							$this->serializeExpResource( $value->getSubject() );
						}
					}
				}

				if ( !$prop_decl_queued ) {
					$this->requireDeclaration( $property, $prop_decl_type );
					$prop_decl_queued = true;
				}
			}
		}
		$this->post_ns_buffer .= ( $bnode ? " ]" : " ." ) . ( $indent === '' ? "\n\n" : '' );
	}

	protected function serializeExpLiteral( SMWExpLiteral $element ) {
		$this->post_ns_buffer .= self::getTurtleNameForExpElement( $element );
	}

	protected function serializeExpResource( SMWExpResource $element ) {
		if ( $element instanceof SMWExpNsResource ) {
			$this->requireNamespace( $element->getNamespaceID(), $element->getNamespace() );
		}
		$this->post_ns_buffer .= self::getTurtleNameForExpElement( $element );
	}

	/**
	 * Get the Turtle serialization string for the given SMWExpElement. The
	 * method just computes a name, and does not serialize triples, so the
	 * parameter must be an SMWExpResource or SMWExpLiteral, no SMWExpData.
	 *
	 * @param $expElement SMWExpElement being SMWExpLiteral or SMWExpResource
	 * @return string
	 */
	public static function getTurtleNameForExpElement( SMWExpElement $expElement ) {
		if ( $expElement instanceof SMWExpResource ) {
			if ( $expElement->isBlankNode() ) {
				return '[]';
			} elseif ( ( $expElement instanceof SMWExpNsResource ) && ( $expElement->hasAllowedLocalName() ) ) {
				return $expElement->getQName();
			} else {
				return '<' . str_replace( '>', '\>', SMWExporter::getInstance()->expandURI( $expElement->getUri() ) ) . '>';
			}
		} elseif ( $expElement instanceof SMWExpLiteral ) {
			$dataType = $expElement->getDatatype();
			$lexicalForm = self::getCorrectLexicalForm( $expElement );

			if ( ( $dataType !== '' ) && ( $dataType != 'http://www.w3.org/2001/XMLSchema#string' ) ) {
				$count = 0;
				$newdt = str_replace( 'http://www.w3.org/2001/XMLSchema#', 'xsd:', $dataType, $count );
				return ( $count == 1 ) ? "$lexicalForm^^$newdt" : "$lexicalForm^^<$dataType>";
			} else {
				return $lexicalForm;
			}
		} else {
			throw new InvalidArgumentException( 'The method can only serialize atomic elements of type SMWExpResource or SMWExpLiteral.' );
		}
	}

	private static function getCorrectLexicalForm( $expElement ) {

		$lexicalForm = str_replace( [ '\\', "\n", '"' ], [ '\\\\', "\\n", '\"' ], $expElement->getLexicalForm() );

		if ( $expElement->getLang() !== '' && ( $expElement->getDatatype() === 'http://www.w3.org/1999/02/22-rdf-syntax-ns#langString' ) ) {
			$lexicalForm = '"' . $lexicalForm . '@' . $expElement->getLang() . '"';
		} elseif ( $expElement->getLang() !== '' ) {
			$lexicalForm = '"' . $lexicalForm . '"'. '@' . $expElement->getLang();
		} else {
			$lexicalForm = '"' . $lexicalForm . '"';
		}

		return $lexicalForm;
	}

}