1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SESP; |
4
|
|
|
|
5
|
|
|
use Onoi\Cache\Cache; |
6
|
|
|
use Onoi\Cache\NullCache; |
7
|
|
|
use SMW\Message; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @ingroup SESP |
11
|
|
|
* |
12
|
|
|
* @license GNU GPL v2+ |
13
|
|
|
* @since 2.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class LabelFetcher { |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Namespace of the cache instance |
21
|
|
|
*/ |
22
|
|
|
const LABEL_CACHE_NAMESPACE = 'sesp:labels'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var Cache |
26
|
|
|
*/ |
27
|
|
|
private $cache; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var string |
31
|
|
|
*/ |
32
|
|
|
private $languageCode = 'en'; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
private $labelCacheVersion = 0; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @since 2.0 |
41
|
|
|
* |
42
|
|
|
* @param Cache|null $cache |
43
|
|
|
* @param string $languageCode |
44
|
|
|
*/ |
45
|
5 |
|
public function __construct( Cache $cache = null, $languageCode = 'en' ) { |
46
|
5 |
|
$this->cache = $cache; |
47
|
5 |
|
$this->languageCode = $languageCode; |
48
|
|
|
|
49
|
5 |
|
if ( $this->cache === null ) { |
50
|
|
|
$this->cache = new NullCache(); |
51
|
|
|
} |
52
|
5 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @since 2.0 |
56
|
|
|
* |
57
|
|
|
* @param integer|string $labelCacheVersion |
58
|
|
|
*/ |
59
|
1 |
|
public function setLabelCacheVersion( $labelCacheVersion ) { |
60
|
1 |
|
$this->labelCacheVersion = $labelCacheVersion; |
|
|
|
|
61
|
1 |
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* @since 2.0 |
65
|
|
|
* |
66
|
|
|
* @param string $key |
67
|
|
|
* |
68
|
|
|
* @return string |
69
|
|
|
*/ |
70
|
1 |
|
public function getLabel( $key ) { |
71
|
1 |
|
return Message::get( $key, null, Message::USER_LANGUAGE ); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @since 2.0 |
76
|
|
|
* |
77
|
|
|
* @param PropertyDefinitions $propertyDefinitions |
78
|
|
|
* |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
3 |
|
public function getLabelsFrom( PropertyDefinitions $propertyDefinitions ) { |
82
|
|
|
|
83
|
3 |
|
$hash = smwfCacheKey( |
84
|
3 |
|
self::LABEL_CACHE_NAMESPACE, |
85
|
|
|
array( |
|
|
|
|
86
|
3 |
|
$propertyDefinitions, |
87
|
3 |
|
$this->languageCode, |
88
|
3 |
|
$this->labelCacheVersion |
89
|
3 |
|
) |
90
|
3 |
|
); |
91
|
|
|
|
92
|
3 |
|
if ( $this->labelCacheVersion !== false && ( $labels = $this->cache->fetch( $hash ) ) !== false ) { |
93
|
2 |
|
return $labels; |
94
|
|
|
} |
95
|
|
|
|
96
|
1 |
|
$labels = array(); |
97
|
1 |
|
$exifDefinitions = array(); |
|
|
|
|
98
|
|
|
|
99
|
1 |
|
foreach ( $propertyDefinitions as $key => $definition ) { |
100
|
1 |
|
$this->matchLabel( $labels, $definition ); |
101
|
1 |
|
} |
102
|
|
|
|
103
|
1 |
|
foreach ( $propertyDefinitions->safeGet( '_EXIF', array() ) as $key => $definition ) { |
104
|
1 |
|
$this->matchLabel( $labels, $definition ); |
105
|
1 |
|
} |
106
|
|
|
|
107
|
1 |
|
if ( $labels !== array() ) { |
108
|
1 |
|
$this->cache->save( $hash, $labels, 3600 * 24 ); |
109
|
1 |
|
} |
110
|
|
|
|
111
|
1 |
|
return $labels; |
112
|
|
|
} |
113
|
|
|
|
114
|
1 |
|
private function matchLabel( &$labels, $definition ) { |
115
|
|
|
|
116
|
1 |
|
if ( !isset( $definition['id'] ) ) { |
117
|
1 |
|
return; |
118
|
|
|
} |
119
|
|
|
|
120
|
1 |
|
$alias = 'sesp-property-unknown-label'; |
121
|
|
|
|
122
|
1 |
|
if ( isset( $definition['alias'] ) ) { |
123
|
1 |
|
$alias = $definition['alias']; |
124
|
1 |
|
} |
125
|
|
|
|
126
|
1 |
|
$labels[$definition['id']] = Message::get( $alias, null, $this->languageCode ); |
127
|
1 |
|
} |
128
|
|
|
|
129
|
|
|
} |
130
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.