1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SUC; |
4
|
|
|
|
5
|
|
|
use ObjectCache; |
6
|
|
|
use Onoi\Cache\CacheFactory as OnoiCacheFactory; |
7
|
|
|
use Onoi\BlobStore\BlobStore; |
8
|
|
|
use ApiParse; |
9
|
|
|
use ApiBase; |
10
|
|
|
use Title; |
11
|
|
|
use Revision; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* @license GNU GPL v2+ |
15
|
|
|
* @since 1.0 |
16
|
|
|
* |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class BackendCache { |
20
|
|
|
|
21
|
|
|
const VERSION = '0.1'; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var BackendCache |
25
|
|
|
*/ |
26
|
|
|
private static $instance = null; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var BlobStore |
30
|
|
|
*/ |
31
|
|
|
private $blobStore; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var Options |
35
|
|
|
*/ |
36
|
|
|
private $options; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @since 1.0 |
40
|
|
|
* |
41
|
|
|
* @param BlobStore $blobStore |
42
|
|
|
* @param Options $options |
43
|
|
|
*/ |
44
|
5 |
|
public function __construct( BlobStore $blobStore, Options $options ) { |
45
|
5 |
|
$this->blobStore = $blobStore; |
46
|
5 |
|
$this->options = $options; |
47
|
5 |
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @since 1.0 |
51
|
|
|
* |
52
|
|
|
* @param BackendCache|null $instance |
53
|
|
|
* |
54
|
|
|
* @return BackendCache |
55
|
|
|
*/ |
56
|
2 |
|
public static function getInstance( BackendCache $instance = null ) { |
57
|
|
|
|
58
|
2 |
|
if ( $instance !== null ) { |
59
|
1 |
|
return $instance; |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
if ( self::$instance !== null ) { |
63
|
|
|
return self::$instance; |
64
|
|
|
} |
65
|
|
|
|
66
|
1 |
|
$options = Options::newFromGlobals(); |
67
|
|
|
|
68
|
1 |
|
$cache = OnoiCacheFactory::getInstance()->newMediaWikiCache( |
69
|
1 |
|
ObjectCache::getInstance( $options->get( 'backendParserCacheType' ) ) |
70
|
1 |
|
); |
71
|
|
|
|
72
|
1 |
|
$blobStore = new BlobStore( |
73
|
1 |
|
'suc:store', |
74
|
|
|
$cache |
75
|
1 |
|
); |
76
|
|
|
|
77
|
1 |
|
$blobStore->setNamespacePrefix( |
78
|
1 |
|
$options->get( 'cachePrefix' ) |
79
|
1 |
|
); |
80
|
|
|
|
81
|
1 |
|
$blobStore->setExpiryInSeconds( |
82
|
1 |
|
$options->get( 'backendParserCacheLifetime' ) |
83
|
1 |
|
); |
84
|
|
|
|
85
|
1 |
|
return self::$instance = new self( $blobStore, $options ); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @since 1.0 |
90
|
|
|
*/ |
91
|
1 |
|
public static function clear() { |
92
|
1 |
|
self::$instance = null; |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @since 1.0 |
97
|
|
|
* |
98
|
|
|
* @param Title $title |
99
|
|
|
* |
100
|
|
|
* @return string |
101
|
|
|
*/ |
102
|
3 |
|
public function getHashFrom( Title $title = null ) { |
103
|
|
|
|
104
|
3 |
|
$enabledNamespaceWithTemplate = $this->options->get( 'enabledNamespaceWithTemplate' ); |
105
|
|
|
|
106
|
3 |
|
if ( $title === null || !isset( $enabledNamespaceWithTemplate[$title->getNamespace()] ) ) { |
107
|
2 |
|
return ''; |
108
|
|
|
} |
109
|
|
|
|
110
|
1 |
|
return md5( |
111
|
1 |
|
self::VERSION . |
112
|
1 |
|
$title->getPrefixedDBKey() . |
113
|
1 |
|
$enabledNamespaceWithTemplate[$title->getNamespace()] . |
114
|
1 |
|
$this->options->get( 'backendParserCacheLifetime' ) |
115
|
1 |
|
); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* @since 1.0 |
120
|
|
|
* |
121
|
|
|
* @param string $title |
122
|
|
|
* |
123
|
|
|
* @return Title|null |
124
|
|
|
*/ |
125
|
1 |
|
public function getTargetFrom( $title ) { |
126
|
|
|
|
127
|
1 |
|
$title = Title::newFromText( $title ); |
128
|
|
|
|
129
|
1 |
|
if ( $title === null || $title->hasFragment() ) { |
130
|
|
|
return $title; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
# Get the article text |
134
|
1 |
|
$rev = Revision::newFromTitle( $title, false, Revision::READ_LATEST ); |
135
|
|
|
|
136
|
1 |
|
if ( !is_object( $rev ) ) { |
137
|
1 |
|
return $title; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$content = $rev->getContent(); |
141
|
|
|
# Does the redirect point to the source? |
142
|
|
|
# Or is it a broken self-redirect, usually caused by namespace collisions? |
143
|
|
|
return $content && $content->getRedirectTarget() !== null ? $content->getRedirectTarget() : $title; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* @since 1.0 |
148
|
|
|
* |
149
|
|
|
* @return BlobStore |
150
|
|
|
*/ |
151
|
1 |
|
public function getBlobStore() { |
152
|
1 |
|
return $this->blobStore; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* @since 1.0 |
157
|
|
|
* |
158
|
|
|
* @param Title $title |
159
|
|
|
*/ |
160
|
1 |
|
public function invalidateCache( Title $title ) { |
161
|
1 |
|
$this->blobStore->delete( $this->getHashFrom( $title ) ); |
162
|
1 |
|
return true; |
163
|
|
|
} |
164
|
|
|
|
165
|
|
|
} |
166
|
|
|
|