Completed
Pull Request — master (#59)
by Mark A.
20:07
created

CacheInvalidator::purgeCache()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
ccs 4
cts 4
cp 1
crap 1
1
<?php
2
3
namespace SG\Cache;
4
5
use SG\SemanticDataComparator;
6
use SG\PropertyRegistrationHelper;
7
8
use SMW\Store;
9
use SMW\SemanticData;
10
use SMW\DIWikiPage;
11
use SMW\DIProperty;
12
13
use Lingo\LingoParser;
14
15
use MediaWiki\Linker\LinkTarget;
16
17
/**
18
 * @ingroup SG
19
 * @ingroup SemanticGlossary
20
 *
21
 * @license GNU GPL v2+
22
 * @since 1.0
23
 *
24
 * @author Stephan Gambke
25
 * @author mwjames
26
 */
27
class CacheInvalidator {
28
29
	/**
30
	 * @var CacheInvalidator
31
	 */
32
	private static $instance = null;
33
34
	/**
35
	 * @var GlossaryCache
36
	 */
37
	private $cache = null;
38
39
	/**
40
	 * @since 1.0
41
	 *
42
	 * @return CacheInvalidator
43
	 */
44 4
	public static function getInstance() {
45
46 4
		if ( self::$instance === null ) {
47
48 1
			$instance = new self();
49 1
			$instance->setCache( new GlossaryCache() );
50
51 1
			self::$instance = $instance;
52
		}
53
54 4
		return self::$instance;
55
	}
56
57
	/**
58
	 * @since 1.0
59
	 */
60 1
	public static function clear() {
61 1
		self::$instance = null;
62 1
	}
63
64
	/**
65
	 * @since 1.0
66
	 *
67
	 * @param GlossaryCache $glossaryCache
68
	 */
69 7
	public function setCache( GlossaryCache $glossaryCache ) {
70 7
		$this->glossaryCache = $glossaryCache;
71 7
	}
72
73
	/**
74
	 * @since 1.0
75
	 *
76
	 * @param Store $store
77
	 * @param SemanticData $semanticData
78
	 *
79
	 * @return boolean
80
	 */
81 6
	public function invalidateCacheOnStoreUpdate( Store $store, SemanticData $semanticData = null ) {
82
83 6
		if ( $semanticData === null ) {
84 1
			return false;
85
		}
86
87 5
		$this->matchAllSubobjects( $store, $semanticData );
88
89 5
		if ( $this->hasSemanticDataDeviation( $store, $semanticData ) ) {
90 3
			$this->purgeCache( $semanticData->getSubject() );
91 3
			LingoParser::purgeCache();
92
		}
93
94 5
		return true;
95
	}
96
97
	/**
98
	 * @since 1.0
99
	 *
100
	 * @param Store $store
101
	 * @param DIWikiPage $subject
102
	 * @param boolean|true $purgeLingo
103
	 *
104
	 * @return boolean
105
	 */
106 3
	public function invalidateCacheOnPageDelete( Store $store, DIWikiPage $subject, $purgeLingo = true ) {
107
108 3
		$this->matchSubobjectsToSubject( $store, $subject );
109 3
		$this->purgeCache( $subject );
110
111 3
		if ( $purgeLingo ) {
112 3
			LingoParser::purgeCache();
113
		}
114
115 3
		return true;
116
	}
117
118
	/**
119
	 * @since 1.0
120
	 *
121
	 * @param LinkTarget $title
122
	 *
123
	 * @return boolean
124
	 */
125 1
	public function invalidateCacheOnPageMove( LinkTarget $title ) {
126 1
		$this->purgeCache( DIWikiPage::newFromText( $title->getDBkey(), $title->getNamespace() );
0 ignored issues
show
Bug introduced by
This code did not parse for me. Apparently, there is an error somewhere around this line:

Syntax error, unexpected ';', expecting ',' or ')'
Loading history...
127 1
		return true;
128
	}
129
130 5
	private function matchAllSubobjects( Store $store, SemanticData $semanticData ) {
131
132 5
		$properties = $semanticData->getProperties();
133
134 5
		if ( array_key_exists( DIProperty::TYPE_SUBOBJECT, $properties ) ) {
135 1
			foreach ( $semanticData->getPropertyValues( $properties[ DIProperty::TYPE_SUBOBJECT ] ) as $subobject ) {
136 1
				$this->invalidateCacheOnStoreUpdate(
137 1
					$store,
138 1
					$semanticData->findSubSemanticData( $subobject->getSubobjectName() ),
139 1
					false
140
				);
141
			}
142
		}
143 5
	}
144
145 3
	private function matchSubobjectsToSubject( Store $store, DIWikiPage $subject ) {
146
147 3
		$properties = $store->getProperties( $subject );
148
149 3
		if ( array_key_exists( DIProperty::TYPE_SUBOBJECT, $properties ) ) {
150 1
			foreach ( $store->getPropertyValues( $subject, $properties[ DIProperty::TYPE_SUBOBJECT ] ) as $subobject ) {
151
				$this->invalidateCacheOnPageDelete(
152
					$store,
153
					$subobject->getSubject(),
154
					false
155
				);
156
			}
157
		}
158 3
	}
159
160 5
	private function hasSemanticDataDeviation( Store $store, SemanticData $semanticData ) {
161
162 5
		$dataComparator = new SemanticDataComparator( $store, $semanticData );
163
164 5
		return $dataComparator->compareForProperty( PropertyRegistrationHelper::SG_TERM ) ||
165 5
			$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_DEFINITION ) ||
166 5
			$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_LINK ) ||
167 5
			$dataComparator->compareForProperty( PropertyRegistrationHelper::SG_STYLE );
168
	}
169
170 6
	private function purgeCache( DIWikiPage $subject ) {
171
172 6
		$this->glossaryCache->getCache()->delete(
173 6
			$this->glossaryCache->getKeyForSubject( $subject )
174
		);
175
176 6
		return true;
177
	}
178
179
}
180