1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SESP\Cache; |
4
|
|
|
|
5
|
|
|
use ObjectCache; |
6
|
|
|
use Language; |
7
|
|
|
use BagOStuff; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* @ingroup SESP |
11
|
|
|
* |
12
|
|
|
* @licence GNU GPL v2+ |
13
|
|
|
* @since 1.2.0 |
14
|
|
|
* |
15
|
|
|
* @author mwjames |
16
|
|
|
*/ |
17
|
|
|
class MessageCache { |
18
|
|
|
|
19
|
|
|
/** @var MessageCache[] */ |
20
|
|
|
private static $instance = array(); |
21
|
|
|
|
22
|
|
|
/** @var Language */ |
23
|
|
|
protected $language = null; |
24
|
|
|
|
25
|
|
|
protected $touched = null; |
26
|
|
|
protected $cacheTimeOffset = null; |
27
|
|
|
protected $messages = null; |
28
|
|
|
protected $cache = null; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @since 1.2.0 |
32
|
|
|
* |
33
|
|
|
* @param Language|null $language |
34
|
|
|
* @param integer|null $cacheTimeOffset |
35
|
|
|
*/ |
36
|
14 |
|
public function __construct( Language $language = null, $cacheTimeOffset = null ) { |
37
|
14 |
|
$this->language = $language; |
38
|
14 |
|
$this->cacheTimeOffset = $cacheTimeOffset; |
39
|
14 |
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @since 1.4 |
43
|
|
|
* |
44
|
|
|
* @param Language $language |
45
|
|
|
*/ |
46
|
1 |
|
public function setLanguage( Language $language ) { |
47
|
1 |
|
$this->language = $language; |
48
|
1 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @since 1.4 |
52
|
|
|
* |
53
|
|
|
* @return MessageCache |
54
|
|
|
*/ |
55
|
1 |
|
public function inUserLanguage() { |
56
|
1 |
|
$this->language = $GLOBALS['wgLang']; |
57
|
1 |
|
return $this; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* @since 1.2.0 |
62
|
|
|
*/ |
63
|
1 |
|
public static function clear() { |
64
|
1 |
|
self::$instance = array(); |
65
|
1 |
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* @since 1.2.0 |
69
|
|
|
* |
70
|
|
|
* MessageCache::ByLanguage( Language::factory( 'en' ) )->purge() |
71
|
|
|
* |
72
|
|
|
* @return MessageCache |
73
|
|
|
*/ |
74
|
1 |
|
public function purge() { |
75
|
1 |
|
$this->getCache()->delete( $this->getCacheId() ); |
76
|
1 |
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @since 1.2.0 |
81
|
|
|
* |
82
|
|
|
* @param integer $cacheTimeOffset |
83
|
|
|
* |
84
|
|
|
* @return MessageCache |
85
|
|
|
*/ |
86
|
9 |
|
public function setCacheTimeOffset( $cacheTimeOffset ) { |
87
|
9 |
|
$this->cacheTimeOffset = $cacheTimeOffset; |
88
|
9 |
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @since 1.2.0 |
93
|
|
|
* |
94
|
|
|
* @param BagOStuff $cache |
95
|
|
|
*/ |
96
|
4 |
|
public function setCache( BagOStuff $cache ) { |
97
|
4 |
|
$this->cache = $cache; |
98
|
4 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @since 1.2.0 |
103
|
|
|
* |
104
|
|
|
* @return string |
105
|
|
|
*/ |
106
|
3 |
|
public function getCacheId() { |
107
|
3 |
|
return $this->getCachePrefix() . ':sesp:mcache:' . $this->language->getCode(); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* @since 1.2.0 |
112
|
|
|
* |
113
|
|
|
* @param string $key |
114
|
|
|
* |
115
|
|
|
* @return string |
116
|
|
|
*/ |
117
|
4 |
|
public function get( $key /* arguments */ ) { |
|
|
|
|
118
|
|
|
|
119
|
4 |
|
$arguments = func_get_args(); |
120
|
|
|
|
121
|
4 |
|
if ( $this->messages === null ) { |
122
|
4 |
|
$this->messages = $this->fetchMessagesFromCache(); |
123
|
4 |
|
} |
124
|
|
|
|
125
|
4 |
|
$key = implode( '#', $arguments ); |
126
|
|
|
|
127
|
4 |
|
if ( isset( $this->messages[ $key ] ) ) { |
128
|
1 |
|
return $this->messages[ $key ]; |
129
|
|
|
} |
130
|
|
|
|
131
|
3 |
|
return $this->getTextMessage( $key, $arguments ); |
132
|
|
|
} |
133
|
|
|
|
134
|
3 |
|
protected function getTextMessage( $key, $arguments ) { |
135
|
|
|
|
136
|
3 |
|
$this->messages[ $key ] = wfMessage( $arguments )->inLanguage( $this->language )->text(); |
137
|
3 |
|
$this->updateMessagesToCache(); |
138
|
|
|
|
139
|
3 |
|
return $this->messages[ $key ]; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
protected function updateMessagesToCache() { |
143
|
|
|
|
144
|
|
|
$messagesToBeCached = array( |
145
|
3 |
|
'touched' => $this->getTouched(), |
146
|
3 |
|
'messages' => $this->messages |
147
|
3 |
|
); |
148
|
|
|
|
149
|
3 |
|
return $this->getCache()->set( $this->getCacheId(), $messagesToBeCached ); |
150
|
|
|
} |
151
|
|
|
|
152
|
4 |
|
protected function fetchMessagesFromCache() { |
153
|
|
|
|
154
|
4 |
|
$cached = $this->getCache()->get( $this->getCacheId() ); |
155
|
|
|
|
156
|
4 |
|
if ( isset( $cached['touched'] ) && isset( $cached['messages'] ) && $cached['touched'] === $this->getTouched() ) { |
157
|
1 |
|
return $cached['messages']; |
158
|
|
|
} |
159
|
|
|
|
160
|
3 |
|
return null; |
161
|
|
|
} |
162
|
|
|
|
163
|
5 |
|
protected function getCache() { |
164
|
|
|
|
165
|
5 |
|
if ( !$this->cache instanceOf BagOStuff ) { |
|
|
|
|
166
|
1 |
|
$this->cache = ObjectCache::getInstance( $GLOBALS['sespCacheType'] ); |
167
|
1 |
|
} |
168
|
|
|
|
169
|
5 |
|
return $this->cache; |
170
|
|
|
} |
171
|
|
|
|
172
|
3 |
|
protected function getCachePrefix() { |
173
|
3 |
|
return $GLOBALS['wgCachePrefix'] === false ? wfWikiID() : $GLOBALS['wgCachePrefix']; |
174
|
|
|
} |
175
|
|
|
|
176
|
4 |
|
protected function getTouched() { |
177
|
|
|
|
178
|
4 |
|
if ( $this->touched === null ) { |
179
|
4 |
|
$this->touched = $this->getMessageFileModificationTime() . $this->cacheTimeOffset; |
180
|
4 |
|
} |
181
|
|
|
|
182
|
4 |
|
return $this->touched; |
183
|
|
|
} |
184
|
|
|
|
185
|
2 |
|
protected function getMessageFileModificationTime() { |
186
|
|
|
|
187
|
2 |
|
if ( method_exists( $this->language, 'getJsonMessagesFileName' ) ) { |
188
|
2 |
|
return filemtime( $this->language->getJsonMessagesFileName( $this->language->getCode() ) ); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return filemtime( $this->language->getMessagesFileName( $this->language->getCode() ) ); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
} |
195
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.