Completed
Push — master ( fef851...52af6f )
by Stephan
04:43
created

ElementsCacheBuilder::buildElements()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 18
ccs 10
cts 10
cp 1
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 11
nc 2
nop 5
crap 2
1
<?php
2
3
namespace SG\Cache;
4
5
use SG\PropertyRegistry;
6
use SMW\DataValueFactory;
7
use SMW\Store;
8
use SMW\DIProperty;
9
use SMWStringValue as StringValue;
10
use SMWPrintRequest as PrintRequest;
11
use SMWPropertyValue as PropertyValue;
12
use SMWThingDescription as ThingDescription;
13
use SMWSomeProperty as SomeProperty;
14
use SMWQuery as Query;
15
use Lingo\Element;
16
17
/**
18
 * @ingroup SG
19
 * @ingroup SemanticGlossary
20
 *
21
 * @license GNU GPL v2+
22
 * @since 1.1
23
 *
24
 * @author Stephan Gambke
25
 * @author mwjames
26
 */
27
class ElementsCacheBuilder {
28
29
	/* @var Store */
30
	private $store;
31
32
	/* @var GlossaryCache */
33
	private $glossaryCache;
34
35
	private $mDiTerm;
36
	private $mDiDefinition;
37
	private $mDiLink;
38
	private $mDiStyle;
39
40
	private $mDvTerm;
41
	private $mDvDefinition;
42
	private $mDvLink;
43
	private $mDvStyle;
44
45
	private $queryResults;
46
47
	/**
48
	 * @since  1.1
49
	 *
50
	 * @param SMWStore $store
51
	 * @param GlossaryCache $cache
0 ignored issues
show
Bug introduced by
There is no parameter named $cache. Was it maybe removed?

This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.

Consider the following example. The parameter $italy is not defined by the method finale(...).

/**
 * @param array $germany
 * @param array $island
 * @param array $italy
 */
function finale($germany, $island) {
    return "2:1";
}

The most likely cause is that the parameter was removed, but the annotation was not.

Loading history...
52
	 */
53 2
	public function __construct( Store $store, GlossaryCache $glossaryCache ) {
54 2
		$this->store = $store;
55 2
		$this->glossaryCache = $glossaryCache;
56 2
	}
57
58
	/**
59
	 * @since 1.1
60
	 *
61
	 * @return array
62
	 */
63 1
	public function getElements() {
64 1
		wfProfileIn( __METHOD__ );
65
66 1
		$ret = array();
67
68 1
		if ( $this->queryResults === null ) {
69 1
			$this->queryResults = $this->store->getQueryResult( $this->buildQuery() )->getResults();
70
		}
71
72
		// find next line
73 1
		$page = current( $this->queryResults );
74
75 1
		if ( $page && count( $ret ) == 0 ) {
76
77 1
			next( $this->queryResults );
78
79 1
			$cachekey = $this->glossaryCache->getKeyForSubject( $page );
80 1
			$cachedResult = $this->glossaryCache->getCache()->get( $cachekey );
81
82
			// cache hit?
83 1
			if ( $cachedResult !== false && $cachedResult !== null ) {
84
85
				wfDebug( "Cache hit: Got glossary entry $cachekey from cache.\n" );
86
				$ret = &$cachedResult;
87
			} else {
88
89 1
				wfDebug( "Cache miss: Glossary entry $cachekey not found in cache.\n" );
90
91 1
				$ret = $this->buildElements(
92 1
					$this->getTerms( $page ),
93 1
					$this->getDefinitionValue( $page ),
94 1
					$this->getLinkValue( $page ),
95 1
					$this->getStyleValue( $page ),
96 1
					$page
97
				);
98
99 1
				wfDebug( "Cached glossary entry $cachekey.\n" );
100 1
				$this->glossaryCache->getCache()->set( $cachekey, $ret );
101
			}
102
		}
103
104 1
		wfProfileOut( __METHOD__ );
105 1
		return $ret;
106
	}
107
108 1
	private function buildElements( $terms, $definition, $link, $style, $page ) {
109
110 1
		$ret = array();
111
112 1
		foreach ( $terms as $term ) {
113
			$tmp_ret = array(
114 1
				Element::ELEMENT_TERM => $term,
115 1
				Element::ELEMENT_DEFINITION => $definition,
116 1
				Element::ELEMENT_LINK => $link,
117 1
				Element::ELEMENT_STYLE => $style,
118 1
				Element::ELEMENT_SOURCE => $page
119
			);
120
121 1
			$ret[] = $tmp_ret;
122
		}
123
124 1
		return $ret;
125
	}
126
127 1
	private function buildQuery() {
128
129 1
		$dataValueFactory = DataValueFactory::getInstance();
130
131
		// build term data item and data value for later use
132 1
		$this->mDiTerm = new DIProperty( PropertyRegistry::SG_TERM );
133 1
		$this->mDvTerm = $dataValueFactory->newDataValueByType( '_txt' );
134 1
		$this->mDvTerm->setProperty( $this->mDiTerm );
135
136 1
		$pvTerm = $dataValueFactory->newDataValueByType( '__pro' );
137 1
		$pvTerm->setDataItem( $this->mDiTerm );
138 1
		$prTerm = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvTerm );
139
140
		// build definition data item and data value for later use
141 1
		$this->mDiDefinition = new DIProperty( PropertyRegistry::SG_DEFINITION );
142 1
		$this->mDvDefinition = $dataValueFactory->newDataValueByType( '_txt' );
143 1
		$this->mDvDefinition->setProperty( $this->mDiDefinition );
144
145 1
		$pvDefinition = $dataValueFactory->newDataValueByType( '__pro' );
146 1
		$pvDefinition->setDataItem( $this->mDiDefinition );
147 1
		$prDefinition = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvDefinition );
148
149
		// build link data item and data value for later use
150 1
		$this->mDiLink = new DIProperty( PropertyRegistry::SG_LINK );
151 1
		$this->mDvLink = $dataValueFactory->newDataValueByType( '_txt' );
152 1
		$this->mDvLink->setProperty( $this->mDiLink );
153
154 1
		$pvLink = $dataValueFactory->newDataValueByType( '__pro' );
155 1
		$pvLink->setDataItem( $this->mDiLink );
156 1
		$prLink = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvLink );
157
158
		// build style data item and data value for later use
159 1
		$this->mDiStyle = new DIProperty( PropertyRegistry::SG_STYLE );
160 1
		$this->mDvStyle = $dataValueFactory->newDataValueByType( '_txt' );
161 1
		$this->mDvStyle->setProperty( $this->mDiStyle );
162
163 1
		$pvStyle = $dataValueFactory->newDataValueByType( '__pro' );
164 1
		$pvStyle->setDataItem( $this->mDiStyle );
165 1
		$prStyle = new PrintRequest( PrintRequest::PRINT_PROP, null, $pvStyle );
166
167
		// Create query
168 1
		$desc = new SomeProperty( new DIProperty( '___glt' ), new ThingDescription() );
169 1
		$desc->addPrintRequest( $prTerm );
170 1
		$desc->addPrintRequest( $prDefinition );
171 1
		$desc->addPrintRequest( $prLink );
172 1
		$desc->addPrintRequest( $prStyle );
173
174 1
		$query = new Query( $desc, false, false );
0 ignored issues
show
Unused Code introduced by
The call to SMWQuery::__construct() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
175 1
		$query->sort = true;
176 1
		$query->sortkeys['___glt'] = 'ASC';
177
178 1
		if ( defined( 'SMWQuery::PROC_CONTEXT' ) ) {
179 1
			$query->setOption( Query::PROC_CONTEXT, 'SG.ElementsCacheBuilder' );
180
		}
181
182 1
		return $query;
183
	}
184
185 1
	private function getDefinitionValue( $page ) {
186
187 1
		$definition  = null;
188
189 1
		$definitions = $this->store->getPropertyValues(
190 1
			$page,
191 1
			$this->mDiDefinition
192
		);
193
194 1
		if ( !empty( $definitions ) ) {
195 1
			$this->mDvDefinition->setDataItem( $definitions[0] );
196 1
			$definition = trim( $this->mDvDefinition->getShortWikiText() );
197
		}
198
199 1
		return $definition;
200
	}
201
202 1
	private function getLinkValue( $page ) {
203
204 1
		$link  = null;
205
206 1
		$links = $this->store->getPropertyValues( $page, $this->mDiLink );;
207
208 1
		if ( !empty( $links ) ) {
209
			$this->mDvLink->setDataItem( $links[0] );
210
			$link = trim( $this->mDvLink->getShortWikiText() );
211
		}
212
213 1
		return $link;
214
	}
215
216 1
	private function getStyleValue( $page ) {
217
218 1
		$style  = null;
219
220 1
		$styles = $this->store->getPropertyValues( $page, $this->mDiStyle );;
221
222 1
		if ( !empty( $styles ) ) {
223
		  $this->mDvStyle->setDataItem( $styles[0] );
224
		  $style = trim( $this->mDvStyle->getShortWikiText() );
225
		}
226
227 1
		return $style;
228
	}
229
230 1
	private function getTerms( $page ) {
231
232 1
		$collectedTerms = array();
233
234 1
		$terms = $this->store->getPropertyValues( $page, $this->mDiTerm );
235
236 1
		if ( $terms !== array() ) {
237 1
			foreach ( $terms as $term ) {
238 1
				$this->mDvTerm->setDataItem( $term );
239 1
				$collectedTerms[] = trim( $this->mDvTerm->getShortWikiText() );
240
			}
241
		}
242
243 1
		return $collectedTerms;
244
	}
245
246
}
247