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

namespace SMW;

use Title;

/**
 * Utility class to create unified hash keys for a variety of objects
 *
 * @license GNU GPL v2+
 * @since 2.1
 *
 * @author mwjames
 */
class HashBuilder {

	/**
	 * @since 2.4
	 *
	 * @param SemanticData $semanticData
	 *
	 * @return string
	 */
	public static function createFromSemanticData( SemanticData $semanticData ) {

		$hash = [];
		$hash[] = $semanticData->getSubject()->getSerialization();

		foreach ( $semanticData->getProperties() as $property ) {
			$hash[] = $property->getKey();

			foreach ( $semanticData->getPropertyValues( $property ) as $di ) {
				$hash[] = $di->getSerialization();
			}
		}

		foreach ( $semanticData->getSubSemanticData() as $data ) {
			$hash[] = $data->getHash();
		}

		sort( $hash );

		return md5( implode( '#', $hash ) );
	}

	/**
	 * @since 2.1
	 *
	 * @param string|array $hashableContent
	 * @param string $prefix
	 *
	 * @return string
	 */
	public static function createFromContent( $hashableContent, $prefix = '' ) {

		if ( is_string( $hashableContent ) ) {
			$hashableContent = [ $hashableContent ];
		}

		return $prefix . md5( json_encode( $hashableContent ) );
	}

	/**
	 * @since 2.5
	 *
	 * @param array $hashableContent
	 * @param string $prefix
	 *
	 * @return string
	 */
	public static function createFromArray( array $hashableContent, $prefix = '' ) {
		return $prefix . md5( json_encode( $hashableContent ) );
	}

	/**
	 * @since 2.4
	 *
	 * @return string
	 */
	public static function createFromSegments( /* args */ ) {
		return implode( '#', func_get_args() );
	}

	/**
	 * @deprecated since 2.4, use Hash::createFromSegments
	 * @since 2.1
	 *
	 * @param string $title
	 * @param string $namespace
	 * @param string $interwiki
	 * @param string $fragment
	 *
	 * @return string
	 */
	public static function createHashIdFromSegments( $title, $namespace, $interwiki = '', $fragment = '' ) {
		return self::createFromSegments( $title, $namespace, $interwiki, $fragment );
	}

	/**
	 * @since 2.1
	 *
	 * @param Title $title
	 *
	 * @return string
	 */
	public static function getHashIdForTitle( Title $title ) {
		return self::createFromSegments(
			$title->getDBKey(),
			$title->getNamespace(),
			$title->getInterwiki(),
			$title->getFragment()
		);
	}

	/**
	 * @since 2.1
	 *
	 * @param DIWikiPage $dataItem
	 *
	 * @return string
	 */
	public static function getHashIdForDiWikiPage( DIWikiPage $dataItem ) {
		return self::createFromSegments(
			$dataItem->getDBKey(),
			$dataItem->getNamespace(),
			$dataItem->getInterwiki(),
			$dataItem->getSubobjectName()
		);
	}

	/**
	 * @since 2.1
	 *
	 * @param string $hash
	 *
	 * @return Title|null
	 */
	public static function newTitleFromHash( $hash ) {
		list( $title, $namespace, $interwiki, $fragement ) = explode( '#', $hash, 4 );
		return Title::makeTitle( $namespace, $title, $fragement, $interwiki );
	}

	/**
	 * @note This method does not make additional checks therefore it is assumed
	 * that the input hash is derived or generated from HashBuilder::getSegmentedHashId
	 *
	 * @since 2.1
	 *
	 * @param string
	 *
	 * @return DIWikiPage|null
	 */
	public static function newDiWikiPageFromHash( $hash ) {

		list( $title, $namespace, $interwiki, $subobjectName ) = explode( '#', $hash, 4 );

		// A leading underscore is an internal SMW convention to describe predefined
		// properties and as such need to be transformed into a valid representation
		if ( $title{0} === '_' ) {
			$title = str_replace( ' ', '_', PropertyRegistry::getInstance()->findPropertyLabelById( $title ) );
		}

		return new DIWikiPage( $title, $namespace, $interwiki, $subobjectName );
	}

}