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
23 protected MessageGroupWANCache $cache;
24 protected IDatabase $db;
26 protected ?array $groups = null;
27
28 public function __construct( IDatabase $db, MessageGroupWANCache $cache ) {
29 $this->db = $db;
30 $this->cache = $cache;
31 $this->cache->configure(
32 [
33 'key' => self::CACHE_KEY,
34 'version' => self::CACHE_VERSION,
35 'regenerator' => function () {
36 return $this->getCacheData();
37 }
38 ]
39 );
40 }
41
43 public function getGroups(): array {
44 if ( !isset( $this->groups ) ) {
45 $cacheData = $this->cache->getValue();
46 $this->groups = $this->initGroupsFromConf( $cacheData );
47 }
48
49 return $this->groups;
50 }
51
52 public function getCacheData(): array {
53 $cacheData = [];
54 $tables = [ 'page', 'revtag' ];
55 $vars = [ 'page_id', 'page_namespace', 'page_title', 'rt_revision' => 'MAX(rt_revision)' ];
56 $conds = [ 'page_id=rt_page', 'rt_type' => RevTagStore::MB_VALID_TAG ];
57 $options = [
58 'GROUP BY' => 'page_id,page_namespace,page_title'
59 ];
60 $res = $this->db->select( $tables, $vars, $conds, __METHOD__, $options );
61
62 foreach ( $res as $r ) {
63 $title = Title::newFromRow( $r );
64 $cacheData[] = [
65 $title->getPrefixedText(),
66 (int)$r->page_id,
67 (int)$r->rt_revision,
68 ];
69 }
70
71 return $cacheData;
72 }
73
75 private function initGroupsFromConf( array $cacheData ): array {
76 $groups = [];
77 $groupIds = [];
78
79 // First get all the group ids
80 foreach ( $cacheData as $conf ) {
81 $groupIds[] = MessageBundleMessageGroup::getGroupId( $conf[0] );
82 }
83
84 // Preload all the metadata
85 TranslateMetadata::preloadGroups( $groupIds, __METHOD__ );
86
87 // Loop over all the group ids and create the MessageBundleMessageGroup
88 foreach ( $groupIds as $index => $groupId ) {
89 $conf = $cacheData[$index];
90 $description = $this->getMetadata( $groupId, 'description' );
91 $label = $this->getMetadata( $groupId, 'label' );
92 $groups[$groupId] = new MessageBundleMessageGroup(
93 $groupId,
94 $conf[0],
95 $conf[1],
96 $conf[2],
97 $description,
98 $label
99 );
100 }
101
102 return $groups;
103 }
104
106 public function recache(): void {
107 $this->groups = null;
108 $this->cache->touchKey();
109
110 $cacheData = $this->cache->getValue( 'recache' );
111 $this->groups = $this->initGroupsFromConf( $cacheData );
112 }
113
115 public function clearCache(): void {
116 $this->groups = null;
117 $this->cache->delete();
118 }
119
120 private function getMetadata( string $groupId, string $key ): ?string {
121 $metadata = TranslateMetadata::get( $groupId, $key );
122 return $metadata !== false ? $metadata : null;
123 }
124}
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.