Completed
Push — master ( 68d370...07e827 )
by mw
33:19 queued 18:50
created

removeIDFromEntityReferenceTables()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 26
ccs 18
cts 18
cp 1
rs 8.8571
cc 1
eloc 17
nc 1
nop 1
crap 1
1
<?php
2
3
namespace SMW\SQLStore;
4
5
/**
6
 * Class responsible for the clean-up (aka disposal) of any outdated table entries
7
 * that are contained in either the ID_TABLE or related property tables with
8
 * reference to a matchable ID.
9
 *
10
 * @license GNU GPL v2+
11
 * @since 2.4
12
 *
13
 * @author mwjames
14
 */
15
class PropertyTableIdReferenceDisposer {
16
17
	/**
18
	 * @var SQLStore
19
	 */
20
	private $store = null;
21
22
	/**
23
	 * @var Database
24
	 */
25
	private $connection = null;
26
27
	/**
28
	 * @since 2.4
29
	 *
30
	 * @param SQLStore $store
31
	 */
32 10
	public function __construct( SQLStore $store ) {
33 10
		$this->store = $store;
34 10
		$this->connection = $this->store->getConnection( 'mw.db' );
35 10
	}
36
37
	/**
38
	 * Use case: After a property changed its type (_wpg -> _txt), object values in the
39
	 * ID table are not removed at the time of the conversion process.
40
	 *
41
	 * Before an attempt to remove the ID from entity tables, it is secured that no
42
	 * references exists for the ID.
43
	 *
44
	 * @note This method does not check for an ID being object or subject value
45
	 * and has to be done prior calling this routine.
46
	 *
47
	 * @since 2.4
48
	 *
49
	 * @param integer $id
50
	 */
51 3
	public function tryToRemoveOutdatedIDFromEntityTables( $id ) {
52
53 3
		if ( $this->store->getPropertyTableIdReferenceFinder()->hasResidualReferenceFor( $id ) ) {
54 2
			return null;
55
		}
56
57 3
		$this->removeIDFromEntityReferenceTables( $id );
58 3
	}
59
60
	/**
61
	 * @note This method does not make any assumption about the ID state and therefore
62
	 * has to be validated before this method is called.
63
	 *
64
	 * @since 2.4
65
	 *
66
	 * @param integer $id
67
	 */
68 2
	public function cleanUpTableEntriesFor( $id ) {
69
70 2
		foreach ( $this->store->getPropertyTables() as $proptable ) {
71 2
			if ( $proptable->usesIdSubject() ) {
72 2
				$this->connection->delete(
73 2
					$proptable->getName(),
74 2
					array( 's_id' => $id ),
75 2
					__METHOD__
76
				);
77
			}
78
79 2
			if ( !$proptable->isFixedPropertyTable() ) {
80 2
				$this->connection->delete(
81 2
					$proptable->getName(),
82 2
					array( 'p_id' => $id ),
83 2
					__METHOD__
84
				);
85
			}
86
87 2
			$fields = $proptable->getFields( $this->store );
88
89
			// Match tables (including ftp_redi) that contain an object reference
90 2
			if ( isset( $fields['o_id'] ) ) {
91 1
				$this->connection->delete(
92 1
					$proptable->getName(),
93 1
					array( 'o_id' => $id ),
94 2
					__METHOD__
95
				);
96
			}
97
		}
98
99 2
		$this->removeIDFromEntityReferenceTables( $id );
100 2
	}
101
102 4
	private function removeIDFromEntityReferenceTables( $id ) {
103
104 4
		$this->connection->delete(
105 4
			SQLStore::ID_TABLE,
106 4
			array( 'smw_id' => $id ),
107 4
			__METHOD__
108
		);
109
110 4
		$this->connection->delete(
111 4
			SQLStore::PROPERTY_STATISTICS_TABLE,
112 4
			array( 'p_id' => $id ),
113 4
			__METHOD__
114
		);
115
116 4
		$this->connection->delete(
117 4
			SQLStore::QUERY_LINKS_TABLE,
118 4
			array( 's_id' => $id ),
119 4
			__METHOD__
120
		);
121
122 4
		$this->connection->delete(
123 4
			SQLStore::QUERY_LINKS_TABLE,
124 4
			array( 'o_id' => $id ),
125 4
			__METHOD__
126
		);
127 4
	}
128
129
}
130