Completed
Push — master ( 335380...3837d1 )
by mw
132:18 queued 97:45
created

doCheckInternalPropertyIndices()   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 27
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 18
nc 2
nop 1
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\SQLStore;
4
5
use Onoi\MessageReporter\NullMessageReporter;
6
use Onoi\MessageReporter\MessageReporter;
7
use Onoi\MessageReporter\MessageReporterAware;
8
use SMWDataItem as DataItem;
9
use SMW\DIProperty;
10
use SMWSql3SmwIds;
11
12
/**
13
 * @private
14
 *
15
 *
16
 * Allows to execute SQLStore or table specific tasks that are expected to be part
17
 * of the installation or removal routine.
18
 *
19
 * @license GNU GPL v2+
20
 * @since 2.5
21
 *
22
 * @author mwjames
23
 */
24
class TableIntegrityChecker implements MessageReporterAware {
25
26
	/**
27
	 * @var SQLStore
28
	 */
29
	private $store;
30
31
	/**
32
	 * @var MessageReporter
33
	 */
34
	private $messageReporter;
35
36
	/**
37
	 * @since 2.5
38
	 *
39
	 * @param SQLStore $store
40
	 */
41
	public function __construct( SQLStore $store ) {
42
		$this->store = $store;
43
		$this->messageReporter = new NullMessageReporter();
44
	}
45
46
	/**
47
	 * @see MessageReporterAware::setMessageReporter
48
	 *
49
	 * @since 2.5
50
	 *
51
	 * @param MessageReporter $messageReporter
52
	 */
53
	public function setMessageReporter( MessageReporter $messageReporter ) {
54
		$this->messageReporter = $messageReporter;
55
	}
56
57
	/**
58
	 * @since 2.5
59
	 *
60
	 * @param TableBuilder $tableBuilder
61
	 */
62
	public function checkOnPostCreation( TableBuilder $tableBuilder ) {
63
64
		$this->doCheckInternalPropertyIndices(
65
			$this->store->getConnection( DB_MASTER )
66
		);
67
68
		$tableBuilder->checkOn( TableBuilder::POST_CREATION );
69
	}
70
71
	/**
72
	 * Create some initial DB entries for important built-in properties. Having the DB contents predefined
73
	 * allows us to safe DB calls when certain data is needed. At the same time, the entries in the DB
74
	 * make sure that DB-based functions work as with all other properties.
75
	 */
76
	private function doCheckInternalPropertyIndices( $connection ) {
77
78
		$this->messageReporter->reportMessage( "\nSetting up internal property indices ...\n" );
79
		$this->doCheckPredefinedPropertyBorder( $connection );
80
81
		// now write actual properties; do that each time, it is cheap enough and we can update sortkeys by current language
82
		$this->messageReporter->reportMessage( "   ... writing entries for internal properties ...\n" );
83
84
		foreach ( SMWSql3SmwIds::$special_ids as $prop => $id ) {
85
			$property = new DIProperty( $prop );
86
			$connection->replace(
87
				SQLStore::ID_TABLE,
88
				array( 'smw_id' ),
89
				array(
90
					'smw_id' => $id,
91
					'smw_title' => $property->getKey(),
92
					'smw_namespace' => SMW_NS_PROPERTY,
93
					'smw_iw' => $this->store->getObjectIds()->getPropertyInterwiki( $property ),
94
					'smw_subobject' => '',
95
					'smw_sortkey' => $property->getCanonicalLabel()
96
				),
97
				__METHOD__
98
			);
99
		}
100
101
		$this->messageReporter->reportMessage( "   ... done.\n" );
102
	}
103
104
	private function doCheckPredefinedPropertyBorder( $connection ) {
105
106
		// Check if we already have this structure
107
		$expectedID = SQLStore::FIXED_PROPERTY_ID_UPPERBOUND;
108
109
		$currentID = $connection->selectRow(
110
			SQLStore::ID_TABLE,
111
			'smw_id',
112
			'smw_iw=' . $connection->addQuotes( SMW_SQL3_SMWBORDERIW )
113
		);
114
115
		if ( $currentID !== false && $currentID->smw_id == $expectedID ) {
116
			return $this->messageReporter->reportMessage( "   ... space for internal properties already allocated.\n" );
117
		}
118
119
		// Legacy bound
120
		$currentID = $currentID === false ? 50 : $currentID->smw_id;
121
122
		$this->messageReporter->reportMessage( "   ... allocating space for internal properties ...\n" );
123
		$this->store->getObjectIds()->moveSMWPageID( $expectedID );
124
125
		$connection->insert(
126
			SQLStore::ID_TABLE,
127
			array(
128
				'smw_id' => $expectedID,
129
				'smw_title' => '',
130
				'smw_namespace' => 0,
131
				'smw_iw' => SMW_SQL3_SMWBORDERIW,
132
				'smw_subobject' => '',
133
				'smw_sortkey' => ''
134
			),
135
			__METHOD__
136
		);
137
138
		$this->messageReporter->reportMessage( "   ... moving from $currentID to $expectedID " );
139
140
		// make way for built-in ids
141
		for ( $i = $currentID; $i < $expectedID; $i++ ) {
142
			$this->store->getObjectIds()->moveSMWPageID( $i );
143
			$this->messageReporter->reportMessage( '.' );
144
		}
145
146
		$this->messageReporter->reportMessage( "\n   ... done.\n" );
147
	}
148
149
}
150