summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/src/Deserializers/DVDescriptionDeserializer/SomeValueDescriptionDeserializer.php
blob: 596245b08ea2936b6fe8180bd8bc40ae42aa1f8d (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
<?php

namespace SMW\Deserializers\DVDescriptionDeserializer;

use InvalidArgumentException;
use SMW\DIWikiPage;
use SMWDataValue as DataValue;

/**
 * @private
 *
 * @license GNU GPL v2+
 * @since 2.3
 *
 * @author mwjames
 */
class SomeValueDescriptionDeserializer extends DescriptionDeserializer {

	/**
	 * @since 2.3
	 *
	 * {@inheritDoc}
	 */
	public function isDeserializerFor( $serialization ) {
		return $serialization instanceof DataValue;
	}

	/**
	 * @since 2.3
	 *
	 * @param string $value
	 *
	 * @return Description
	 * @throws InvalidArgumentException
	 */
	public function deserialize( $value ) {

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

		// https://www.w3.org/TR/html4/charset.html
		// Internally encode something like [[Help:>Foo*]] since &lt; and &gt;
		// would throw off the Title validator; apply only in combination with
		// a NS such as [[Help:>...]]
		$value = str_replace( [ ':<', ':>' ], [ ':-3C', ':-3E' ], $value );

		$comparator = SMW_CMP_EQ;
		$this->prepareValue( $value, $comparator );

		$this->dataValue->setOption(
			DataValue::OPT_QUERY_COMP_CONTEXT,
			( $comparator !== SMW_CMP_EQ && $comparator !== SMW_CMP_NEQ )
		);

		$this->dataValue->setUserValue( $value );

		if ( !$this->dataValue->isValid() ) {
			return $this->descriptionFactory->newThingDescription();
		}

		$dataItem = $this->dataValue->getDataItem();

		$description = $this->descriptionFactory->newValueDescription(
			$dataItem,
			$this->dataValue->getProperty(),
			$comparator
		);

		// Ensure [[>Help:Foo]] === [[Help:>Foo]] / [[Help:~Foo*]] === [[~Help:Foo*]]
		if ( $dataItem instanceof DIWikiPage && $dataItem->getNamespace() !== NS_MAIN ) {
			$description = $this->findApproriateDescription( $comparator, $dataItem, $description );
		}

		return $description;
	}

	private function findApproriateDescription( $comparator, $dataItem, $description ) {

		$value = $dataItem->getDBKey();

		// Normalize a possible earlier encoded string part in order for the
		// QueryComparator::extractComparatorFromString to work its magic
		if ( $comparator === SMW_CMP_EQ || $comparator === SMW_CMP_NEQ ) {
			$value = str_replace( [ '-3C', '-3E' ], [ '<', '>' ], $value );
			$this->prepareValue( $value, $comparator );
		}

		// No approximate, use the normal ValueDescription
		if ( $comparator === SMW_CMP_EQ || $comparator === SMW_CMP_NEQ ) {
			return $description;
		}

		// The NS has been stripped, use a normal value clause in the MAIN namespace
		$valueDescription = $this->descriptionFactory->newValueDescription(
			$this->dataItemFactory->newDIWikiPage( $value, NS_MAIN ),
			null,
			$comparator
		);

		// #1652
		// Use [[Help:~Foo*]] as conjunctive description since the comparator
		// is only applied on the sortkey that contains the DBKey part
		$description = $this->descriptionFactory->newConjunction( [
			$this->descriptionFactory->newNamespaceDescription( $dataItem->getNamespace() ),
			$valueDescription
		] );

		return $description;
	}

}