Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatablePageMessageGroupStore.php
Go to the documentation of this file.
1<?php
12use MediaWiki\MediaWikiServices;
13use Wikimedia\Rdbms\IDatabase;
14
21{
22
23 private const CACHE_KEY = 'wikipage';
24 private const CACHE_VERSION = 2;
25
27 protected $db;
29 protected $cache;
34 protected $groups;
35
36 public function __construct( IDatabase $db, MessageGroupWANCache $cache ) {
37 $this->db = $db;
38 $this->cache = $cache;
39 $this->cache->configure(
40 [
41 'key' => self::CACHE_KEY,
42 'version' => self::CACHE_VERSION,
43 'regenerator' => [ $this, 'getCacheData' ],
44 'touchedCallback' => [ $this, 'isExpired' ]
45 ]
46 );
47 }
48
55 public function getGroups() {
56 if ( $this->groups === null ) {
58 $wrapper = $this->cache->getValue();
59 $this->groups = $this->initGroupsFromTitle( $wrapper->getValue() );
60 }
61
62 return $this->groups;
63 }
64
68 public function recache() {
69 $this->clearProcessCache();
70 $this->cache->touchKey();
71
73 $wrapper = $this->cache->getValue( 'recache' );
74 $this->groups = $this->initGroupsFromTitle( $wrapper->getValue() );
75 }
76
80 public function clearCache() {
81 $this->clearProcessCache();
82 $this->cache->delete();
83 }
84
88 protected function clearProcessCache() {
89 $this->groups = null;
90 }
91
97 public function getCacheData() {
98 global $wgEnablePageTranslation;
99
100 $groupTitles = $deps = [];
101 $deps = new GlobalDependency( 'wgEnablePageTranslation' );
102
103 if ( $wgEnablePageTranslation ) {
104 $groupTitles = $this->getTranslatablePageTitles();
105 }
106
107 $wrapper = new DependencyWrapper( $groupTitles, $deps );
108 $wrapper->initialiseDeps();
109 return $wrapper;
110 }
111
118 public static function registerLoader( array &$groupLoader, array $deps ) {
119 $groupLoader[] = self::getInstance(
120 $deps['database'],
121 $deps['cache']
122 );
123 }
124
133 public static function getInstance( IDatabase $db = null, WANObjectCache $cache = null ) {
134 return new self(
135 $db ?? TranslateUtils::getSafeReadDB(),
137 $cache ?? MediaWikiServices::getInstance()->getMainWANObjectCache()
138 )
139 );
140 }
141
148 protected function getTranslatablePageTitles() {
149 $groupTitles = [];
150 $tables = [ 'page', 'revtag' ];
151 $vars = [ 'page_id', 'page_namespace', 'page_title' ];
152 $conds = [ 'page_id=rt_page', 'rt_type' => RevTagStore::TP_MARK_TAG ];
153 $options = [ 'GROUP BY' => 'rt_page,page_id,page_namespace,page_title' ];
154 $res = $this->db->select( $tables, $vars, $conds, __METHOD__, $options );
155
156 foreach ( $res as $r ) {
157 $title = Title::newFromRow( $r );
158 $groupTitles[] = $title->getPrefixedText();
159 }
160
161 return $groupTitles;
162 }
163
171 protected function initGroupsFromTitle( $titles ) {
172 $groups = [];
173 foreach ( $titles as $title ) {
174 $title = Title::newFromText( $title );
175 $id = TranslatablePage::getMessageGroupIdFromTitle( $title );
176 $groups[$id] = new WikiPageMessageGroup( $id, $title );
177 }
178
179 return $groups;
180 }
181}
Class to manage revision tags for translatable bundles.
Mixed bag of methods related to translatable pages.
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.