25class Hooks implements EditFilterMergedContentHook, PageSaveCompleteHook {
26 public const CONSTRUCTOR_OPTIONS = [
27 'TranslateEnableMessageBundleIntegration',
31 private static $instance;
35 private $loadBalancer;
37 private $messageBundleStore;
39 private $WANObjectCache;
41 private $enableIntegration;
43 public function __construct(
44 LoggerInterface $logger,
45 ILoadBalancer $loadBalancer,
46 WANObjectCache $WANObjectCache,
48 bool $enableIntegration
50 $this->logger = $logger;
51 $this->loadBalancer = $loadBalancer;
52 $this->WANObjectCache = $WANObjectCache;
53 $this->messageBundleStore = $messageBundleStore;
54 $this->enableIntegration = $enableIntegration;
57 public static function getInstance():
self {
58 $services = MediaWikiServices::getInstance();
59 self::$instance = self::$instance ??
61 LoggerFactory::getInstance(
'Translate.MessageBundle' ),
62 $services->getDBLoadBalancer(),
63 $services->getMainWANObjectCache(),
64 $services->get(
'Translate:MessageBundleStore' ),
65 $services->getMainConfig()->get(
'TranslateEnableMessageBundleIntegration' )
67 return self::$instance;
72 IContextSource $context,
83 $this->messageBundleStore->validate( $context->getTitle(), $content );
87 $status->fatal(
'translate-messagebundle-validation-error', $context->msg( $e ) );
101 if ( !$this->enableIntegration ) {
105 $method = __METHOD__;
106 $content = $revisionRecord->getContent( SlotRecord::MAIN );
107 $pageTitle = $wikiPage->getTitle();
109 if ( $content ===
null ) {
110 $this->logger->debug(
"Unable to access content of page {pageName} in $method", [
111 'pageName' => $pageTitle->getPrefixedText()
116 if ( !$content instanceof MessageBundleContent ) {
121 $this->messageBundleStore->save( $pageTitle, $revisionRecord, $content );
122 }
catch ( MalformedBundle $e ) {
124 $this->logger->warning(
"Page {pageName} is not a valid message bundle in $method", [
125 'pageName' => $pageTitle->getPrefixedText(),
134 self::getInstance()->onTranslateInitGroupLoadersImpl( $groupLoader );
137 public function onTranslateInitGroupLoadersImpl( array &$groupLoader ): void {
138 if ( !$this->enableIntegration ) {
142 $groupLoader[] =
new MessageBundleMessageGroupLoader(
143 $this->loadBalancer->getConnectionRef( DB_REPLICA ),