MonolingualTextHandler::getInsertValues()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 8
nc 2
nop 1
1
<?php
2
3
namespace Wikibase\QueryEngine\SQLStore\DVHandler;
4
5
use DataValues\DataValue;
6
use DataValues\MonolingualTextValue;
7
use Doctrine\DBAL\Schema\Table;
8
use Doctrine\DBAL\Types\Type;
9
use InvalidArgumentException;
10
use Wikibase\QueryEngine\SQLStore\DataValueHandler;
11
use Wikibase\QueryEngine\StringHasher;
12
13
/**
14
 * Represents the mapping between MonolingualTextValue and
15
 * the corresponding table in the store.
16
 *
17
 * @since 0.1
18
 *
19
 * @licence GNU GPL v2+
20
 * @author Jeroen De Dauw < [email protected] >
21
 * @author Thiemo Kreuz
22
 */
23
class MonolingualTextHandler extends DataValueHandler {
24
25
	/**
26
	 * @var StringHasher
27
	 */
28
	private $stringHasher;
29
30
	/**
31
	 * @see DataValueHandler::getBaseTableName
32
	 *
33
	 * @return string
34
	 */
35
	protected function getBaseTableName() {
36
		return 'mono_text';
37
	}
38
39
	/**
40
	 * @see DataValueHandler::completeTable
41
	 */
42
	protected function completeTable( Table $table ) {
43
		$table->addColumn( 'value_text', Type::TEXT );
44
		// See http://tools.ietf.org/html/rfc5646#section-4.4.1
45
		$table->addColumn( 'value_language', Type::STRING, array( 'length' => 35 ) );
46
		$table->addColumn( 'hash', Type::STRING, array( 'length' => StringHasher::LENGTH ) );
47
48
		// TODO: Is an index on the first 255 bytes/chars of each BLOB/CLOB column possible?
49
		$table->addIndex( array( 'value_language' ) );
50
		$table->addIndex( array( 'hash' ) );
51
	}
52
53
	/**
54
	 * @see DataValueHandler::getInsertValues
55
	 *
56
	 * @param DataValue $value
57
	 *
58
	 * @throws InvalidArgumentException
59
	 * @return string[]
60
	 */
61
	public function getInsertValues( DataValue $value ) {
62
		if ( !( $value instanceof MonolingualTextValue ) ) {
63
			throw new InvalidArgumentException( 'Value is not a MonolingualTextValue.' );
64
		}
65
66
		$values = array(
67
			'value_text' => $value->getText(),
68
			'value_language' => $value->getLanguageCode(),
69
70
			'hash' => $this->getEqualityFieldValue( $value )
71
		);
72
73
		return $values;
74
	}
75
76
	/**
77
	 * @see DataValueHandler::getEqualityFieldValue
78
	 *
79
	 * @param DataValue $value
80
	 *
81
	 * @throws InvalidArgumentException
82
	 * @return string
83
	 */
84
	public function getEqualityFieldValue( DataValue $value ) {
85
		if ( !( $value instanceof MonolingualTextValue ) ) {
86
			throw new InvalidArgumentException( 'Value is not a MonolingualTextValue.' );
87
		}
88
89
		return $this->hash( addcslashes( $value->getText(), '\\|' )
90
			. '|' . addcslashes( $value->getLanguageCode(), '\\|' ) );
91
	}
92
93
	private function hash( $string ) {
94
		if ( $this->stringHasher === null ) {
95
			$this->stringHasher = new StringHasher();
96
		}
97
98
		return $this->stringHasher->hash( $string );
99
	}
100
101
}
102