1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
|
<?php
/**
* This file contains a class to load aggregate message groups and handle
* the related cache.
*
* @file
* @author Abijeet Patro
* @license GPL-2.0-or-later
*/
use MediaWiki\MediaWikiServices;
use Wikimedia\Rdbms\IDatabase;
/**
* Loads AggregateMessageGroup, and handles the related cache.
* @since 2019.05
*/
class AggregateMessageGroupLoader extends MessageGroupLoader
implements CachedMessageGroupLoader {
private const CACHE_KEY = 'aggregate';
private const CACHE_VERSION = 2;
/** @var MessageGroupWANCache */
protected $cache;
/** @var IDatabase */
protected $db;
/**
* List of groups
* @var array|null
*/
protected $groups;
public function __construct( IDatabase $db, MessageGroupWANCache $cache ) {
$this->db = $db;
$this->cache = $cache;
$this->cache->configure(
[
'key' => self::CACHE_KEY,
'version' => self::CACHE_VERSION,
'regenerator' => [ $this, 'getCacheData' ],
]
);
}
/**
* Fetches the AggregateMessageGroups
*
* @return AggregateMessageGroup[] Key is the MessageGroup ID and value is the corresponding
* AggregateMessageGroup
*/
public function getGroups() {
if ( $this->groups === null ) {
$cacheData = $this->cache->getValue();
$this->groups = $this->initGroupsFromConf( $cacheData );
}
return $this->groups;
}
/**
* Hook: TranslateInitGroupLoaders
*
* @param array &$groupLoader
* @param array $deps Dependencies
*/
public static function registerLoader( array &$groupLoader, array $deps ) {
$groupLoader[] = self::getInstance(
$deps['database'],
$deps['cache']
);
}
/**
* Return an instance of this class using the parameters, if passed,
* else initialize the necessary dependencies and return an instance.
*
* @param IDatabase|null $db
* @param WANObjectCache|null $cache
* @return self
*/
public static function getInstance( IDatabase $db = null, WANObjectCache $cache = null ) {
return new self(
$db ?? TranslateUtils::getSafeReadDB(),
new MessageGroupWANCache(
$cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
)
);
}
/**
* Get all the aggregate messages groups defined in translate_metadata table
* and return their configuration
* @return array
*/
public function getCacheData() {
$tables = [ 'translate_metadata' ];
$field = 'tmd_group';
$conds = [ 'tmd_key' => 'subgroups' ];
$groupIds = $this->db->selectFieldValues( $tables, $field, $conds, __METHOD__ );
TranslateMetadata::preloadGroups( $groupIds );
$groups = [];
foreach ( $groupIds as $id ) {
$conf = [];
$conf['BASIC'] = [
'id' => $id,
'label' => TranslateMetadata::get( $id, 'name' ),
'description' => TranslateMetadata::get( $id, 'description' ),
'meta' => 1,
'class' => AggregateMessageGroup::class,
'namespace' => NS_TRANSLATIONS,
];
$conf['GROUPS'] = TranslateMetadata::getSubgroups( $id );
$groups[$id] = $conf;
}
return $groups;
}
/**
* Loads AggregateMessageGroup from the database
*
* @return AggregateMessageGroup[]
*/
public function loadAggregateGroups() {
// get the data from the database everytime
$groupConf = $this->getCacheData();
return $this->initGroupsFromConf( $groupConf );
}
protected function initGroupsFromConf( $groups ) {
foreach ( $groups as $id => $conf ) {
$groups[ $id ] = MessageGroupBase::factory( $conf );
}
return $groups;
}
/**
* Clear and refill the cache with the latest values
*/
public function recache() {
$this->clearProcessCache();
$this->cache->touchKey();
$cacheData = $this->cache->getValue( 'recache' );
$this->groups = $this->initGroupsFromConf( $cacheData );
}
/**
* Clear values from the cache
*/
public function clearCache() {
$this->clearProcessCache();
$this->cache->delete();
}
/**
* Clears the process cache, mainly the cached groups property.
*/
protected function clearProcessCache() {
$this->groups = null;
}
}
|