summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Exporter/Element/ExpResource.php
blob: f991b7eae636c034d80b39a6009d2f2263050835 (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
<?php

namespace SMW\Exporter\Element;

use InvalidArgumentException;
use RuntimeException;
use SMWDataItem as DataItem;

/**
 * A single resource (individual) for export, as defined by a URI.
 * This class can also be used to represent blank nodes: It is assumed that all
 * objects of class ExpElement or any of its subclasses represent a blank
 * node if their name is empty or of the form "_id" where "id" is any
 * identifier string. IDs are local to the current context, such as a list of
 * triples or an SMWExpData container.
 *
 * @license GNU GPL v2+
 * @since 2.2
 *
 * @author Markus Krötzsch
 * @author mwjames
 */
class ExpResource extends ExpElement {

	/**
	 * @var string
	 */
	private $uri;

	/**
	 * @var boolean
	 */
	public $isImported = false;

	/**
	 * @note The given URI must not contain serialization-specific
	 * abbreviations or escapings, such as XML entities.
	 *
	 * @param string $uri The full URI
	 * @param DataItem|null $dataItem
	 *
	 * @throws InvalidArgumentException
	 */
	public function __construct( $uri, DataItem $dataItem = null ) {

		if ( !is_string( $uri ) ) {
			throw new InvalidArgumentException( '$uri needs to be a string' );
		}

		parent::__construct( $dataItem );

		// https://www.w3.org/2011/rdf-wg/wiki/IRIs/RDFConceptsProposal
		// "... characters “<”, “>”, “{”, “}”, “|”, “\”, “^”, “`”, ‘“’ (double quote),
		// and “ ” (space) were allowed ... are not allowed in IRIs, Data
		// containing these characters in %-encoded form is fine ..."
		$this->uri = str_replace( [ '"' ], [ '%22' ], $uri );
	}

	/**
	 * Return true if this resource represents a blank node.
	 *
	 * @return boolean
	 */
	public function isBlankNode() {
		return $this->uri === '' || $this->uri{0} == '_';
	}

	/**
	 * @since 2.5
	 *
	 * @return boolean
	 */
	public function isImported() {
		return $this->isImported;
	}

	/**
	 * Get the URI of this resource. The result is a UTF-8 encoded URI (or
	 * IRI) without any escaping.
	 *
	 * @return string
	 */
	public function getUri() {
		return $this->uri;
	}

	/**
	 * @since  2.2
	 *
	 * @return array
	 */
	public function getSerialization() {

		$serialization = [
			'type' => self::TYPE_RESOURCE,
			'uri'  => $this->getUri()
		];

		return $serialization + parent::getSerialization();
	}

	/**
	 * @see ExpElement::newFromSerialization
	 */
	protected static function deserialize( $serialization ) {

		if ( !isset( $serialization['uri'] ) ) {
			throw new RuntimeException( "Invalid serialization format" );
		}

		return new self(
			$serialization['uri'],
			$serialization['dataitem']
		);
	}

}