summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/SQLStore/PropertyTypeFinder.php
blob: f65c8e7b0286cd135e6a1b39b86313bf89ae61d0 (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
<?php

namespace SMW\SQLStore;

use RuntimeException;
use SMW\DIProperty;
use SMW\MediaWiki\Database;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class PropertyTypeFinder {

	/**
	 * @var Database
	 */
	private $connection;

	/**
	 * @var string
	 */
	private $typeTableName = '';

	/**
	* @since 2.5
	*
	* @param Database $connection
	*/
	public function __construct( Database $connection ) {
		$this->connection = $connection;
	}

	/**
	* @since 2.5
	*
	* @param string $typeTableName
	*/
	public function setTypeTableName( $typeTableName ) {
		$this->typeTableName = $typeTableName;
	}

	/**
	* @since 2.5
	*
	* @param DIProperty $property
	*
	* @return string
	* @throws RuntimeException
	*/
	public function findTypeID( DIProperty $property ) {

		try {
			$row = $this->connection->selectRow(
				SQLStore::ID_TABLE,
				[
					'smw_id'
				],
				[
					'smw_namespace' => SMW_NS_PROPERTY,
					'smw_title' => $property->getKey(),
					'smw_iw' => '',
					'smw_subobject' => ''
				],
				__METHOD__
			);
		} catch ( \Exception $e ) {
			$row = false;
		}

		if ( !isset( $row->smw_id ) ) {
			return $GLOBALS['smwgPDefaultType'];
		}

		if ( $this->typeTableName === '' ) {
			throw new RuntimeException( "Missing a table name" );
		}

		// The Finder is executed before tables are initialized with a corresponding
		// and matchable DIHandler therefore using Store::getPropertyValue cannot
		// be used at this point as it would create a circular reference during
		// the table initialization.
		//
		// We expect it to be a URI table with `o_serialized` containing the
		// type string
		$row = $this->connection->selectRow(
			$this->typeTableName,
			[
				'o_serialized'
			],
			[
				's_id' => $row->smw_id
			],
			__METHOD__
		);

		if ( $row === false ) {
			return $GLOBALS['smwgPDefaultType'];
		}

		// e.g. http://semantic-mediawiki.org/swivt/1.0#_num
		list( $url, $fragment ) = explode( "#", $row->o_serialized );

		return $fragment;
	}

}