Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatablePageMessageGroupStore.php
Go to the documentation of this file.
1<?php
13use MediaWiki\MediaWikiServices;
14use Wikimedia\Rdbms\IDatabase;
15
22{
23
24 private const CACHE_KEY = 'wikipage';
25 private const CACHE_VERSION = 2;
26
28 protected $db;
30 protected $cache;
35 protected $groups;
36
37 public function __construct( IDatabase $db, MessageGroupWANCache $cache ) {
38 $this->db = $db;
39 $this->cache = $cache;
40 $this->cache->configure(
41 [
42 'key' => self::CACHE_KEY,
43 'version' => self::CACHE_VERSION,
44 'regenerator' => [ $this, 'getCacheData' ],
45 'touchedCallback' => [ $this, 'isExpired' ]
46 ]
47 );
48 }
49
56 public function getGroups() {
57 if ( $this->groups === null ) {
59 $wrapper = $this->cache->getValue();
60 $this->groups = $this->initGroupsFromTitle( $wrapper->getValue() );
61 }
62
63 return $this->groups;
64 }
65
69 public function recache() {
70 $this->clearProcessCache();
71 $this->cache->touchKey();
72
74 $wrapper = $this->cache->getValue( 'recache' );
75 $this->groups = $this->initGroupsFromTitle( $wrapper->getValue() );
76 }
77
81 public function clearCache() {
82 $this->clearProcessCache();
83 $this->cache->delete();
84 }
85
89 protected function clearProcessCache() {
90 $this->groups = null;
91 }
92
98 public function getCacheData() {
99 global $wgEnablePageTranslation;
100
101 $groupTitles = $deps = [];
102 $deps = new GlobalDependency( 'wgEnablePageTranslation' );
103
104 if ( $wgEnablePageTranslation ) {
105 $groupTitles = $this->getTranslatablePageTitles();
106 }
107
108 $wrapper = new DependencyWrapper( $groupTitles, $deps );
109 $wrapper->initialiseDeps();
110 return $wrapper;
111 }
112
119 public static function registerLoader( array &$groupLoader, array $deps ) {
120 $groupLoader[] = self::getInstance(
121 $deps['database'],
122 $deps['cache']
123 );
124 }
125
134 public static function getInstance( IDatabase $db = null, WANObjectCache $cache = null ) {
135 return new self(
136 $db ?? Utilities::getSafeReadDB(),
138 $cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
139 )
140 );
141 }
142
149 protected function getTranslatablePageTitles() {
150 $groupTitles = [];
151 $tables = [ 'page', 'revtag' ];
152 $vars = [ 'page_id', 'page_namespace', 'page_title' ];
153 $conds = [ 'page_id=rt_page', 'rt_type' => RevTagStore::TP_MARK_TAG ];
154 $options = [ 'GROUP BY' => 'rt_page,page_id,page_namespace,page_title' ];
155 $res = $this->db->select( $tables, $vars, $conds, __METHOD__, $options );
156
157 foreach ( $res as $r ) {
158 $title = Title::newFromRow( $r );
159 $groupTitles[] = $title->getPrefixedText();
160 }
161
162 return $groupTitles;
163 }
164
172 protected function initGroupsFromTitle( $titles ) {
173 $groups = [];
174 foreach ( $titles as $title ) {
175 $title = Title::newFromText( $title );
176 $id = TranslatablePage::getMessageGroupIdFromTitle( $title );
177 $groups[$id] = new WikiPageMessageGroup( $id, $title );
178 }
179
180 return $groups;
181 }
182}
Class to manage revision tags for translatable bundles.
Mixed bag of methods related to translatable pages.
Essentially random collection of helper functions, similar to GlobalFunctions.php.
Definition Utilities.php:31
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.
Handles DB operations for Translatable pages, and the related cache.
recache()
Clear and refill the cache with the latest values.
getGroups()
Return the WikiPageMessageGroups If local variable is set, use that otherwise fetch from the cache.
getCacheData()
Get the data that needs to be stored in the cache.
getTranslatablePageTitles()
Fetch page titles marked for translation from the database to store in the cache.
initGroupsFromTitle( $titles)
Convert page titles to WikiPageMessageGroup objects.
static getInstance(IDatabase $db=null, WANObjectCache $cache=null)
Return an instance of this class using the parameters, if passed, else initialize the necessary depen...
clearProcessCache()
Clears the process cache, mainly the cached groups property.
static registerLoader(array &$groupLoader, array $deps)
Hook: TranslateInitGroupLoaders.
Wraps the translatable page sections into a message group.
To be implemented by MessageGroupLoaders that use the MessageGroupWANCache.