Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
MessageBundleMessageGroupLoader.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
10use Title;
12use Wikimedia\Rdbms\IDatabase;
13
20 private const CACHE_KEY = 'messageBundle';
21 private const CACHE_VERSION = 1;
22
24 protected $cache;
26 protected $db;
28 protected $groups;
29
30 public function __construct( IDatabase $db, MessageGroupWANCache $cache ) {
31 $this->db = $db;
32 $this->cache = $cache;
33 $this->cache->configure(
34 [
35 'key' => self::CACHE_KEY,
36 'version' => self::CACHE_VERSION,
37 'regenerator' => function () {
38 return $this->getCacheData();
39 }
40 ]
41 );
42 }
43
45 public function getGroups(): array {
46 if ( $this->groups === null ) {
47 $cacheData = $this->cache->getValue();
48 $this->groups = $this->initGroupsFromConf( $cacheData );
49 }
50
51 return $this->groups;
52 }
53
54 public function getCacheData(): array {
55 $cacheData = [];
56 $tables = [ 'page', 'revtag' ];
57 $vars = [ 'page_id', 'page_namespace', 'page_title', 'rt_revision' => 'MAX(rt_revision)' ];
58 $conds = [ 'page_id=rt_page', 'rt_type' => RevTagStore::MB_VALID_TAG ];
59 $options = [
60 'GROUP BY' => 'page_id,page_namespace,page_title'
61 ];
62 $res = $this->db->select( $tables, $vars, $conds, __METHOD__, $options );
63
64 foreach ( $res as $r ) {
65 $title = Title::newFromRow( $r );
66 $cacheData[] = [
67 $title->getPrefixedText(),
68 (int)$r->page_id,
69 (int)$r->rt_revision,
70 ];
71 }
72
73 return $cacheData;
74 }
75
77 private function initGroupsFromConf( array $cacheData ): array {
78 $groups = [];
79 $groupIds = [];
80
81 // First get all the group ids
82 foreach ( $cacheData as $conf ) {
83 $groupIds[] = MessageBundleMessageGroup::getGroupId( $conf[0] );
84 }
85
86 // Preload all the metadata
87 TranslateMetadata::preloadGroups( $groupIds, __METHOD__ );
88
89 // Loop over all the group ids and create the MessageBundleMessageGroup
90 foreach ( $groupIds as $index => $groupId ) {
91 $conf = $cacheData[$index];
92 $description = TranslateMetadata::get( $groupId, 'description' );
93 $description = $description !== false ? $description : null;
94 $groups[$groupId] = new MessageBundleMessageGroup( $groupId, $conf[0], $conf[1], $conf[2], $description );
95 }
96
97 return $groups;
98 }
99
101 public function recache(): void {
102 $this->groups = null;
103 $this->cache->touchKey();
104
105 $cacheData = $this->cache->getValue( 'recache' );
106 $this->groups = $this->initGroupsFromConf( $cacheData );
107 }
108
110 public function clearCache(): void {
111 $this->groups = null;
112 $this->cache->delete();
113 }
114}
Class to manage revision tags for translatable bundles.
An abstract class to be implemented by group loaders / stores.
Wrapper around WANObjectCache providing a simpler interface for MessageGroups to use the cache.
configure(array $config)
Configure the message group.
To be implemented by MessageGroupLoaders that use the MessageGroupWANCache.