Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
Hooks.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageBundleTranslation;
5
6use Content;
7use IContextSource;
8use MediaWiki\Hook\EditFilterMergedContentHook;
9use MediaWiki\Logger\LoggerFactory;
10use MediaWiki\MediaWikiServices;
11use MediaWiki\Revision\SlotRecord;
12use MediaWiki\Storage\Hook\PageSaveCompleteHook;
14use Psr\Log\LoggerInterface;
15use Status;
16use User;
17use WANObjectCache;
18use Wikimedia\Rdbms\ILoadBalancer;
19
25class Hooks implements EditFilterMergedContentHook, PageSaveCompleteHook {
26 public const CONSTRUCTOR_OPTIONS = [
27 'TranslateEnableMessageBundleIntegration',
28 ];
29
31 private static $instance;
33 private $logger;
35 private $loadBalancer;
37 private $messageBundleStore;
39 private $WANObjectCache;
41 private $enableIntegration;
42
43 public function __construct(
44 LoggerInterface $logger,
45 ILoadBalancer $loadBalancer,
46 WANObjectCache $WANObjectCache,
47 MessageBundleStore $messageBundleStore,
48 bool $enableIntegration
49 ) {
50 $this->logger = $logger;
51 $this->loadBalancer = $loadBalancer;
52 $this->WANObjectCache = $WANObjectCache;
53 $this->messageBundleStore = $messageBundleStore;
54 $this->enableIntegration = $enableIntegration;
55 }
56
57 public static function getInstance(): self {
58 $services = MediaWikiServices::getInstance();
59 self::$instance = self::$instance ??
60 new self(
61 LoggerFactory::getInstance( 'Translate.MessageBundle' ),
62 $services->getDBLoadBalancer(),
63 $services->getMainWANObjectCache(),
64 $services->get( 'Translate:MessageBundleStore' ),
65 $services->getMainConfig()->get( 'TranslateEnableMessageBundleIntegration' )
66 );
67 return self::$instance;
68 }
69
72 IContextSource $context,
73 Content $content,
74 Status $status,
75 $summary,
76 User $user,
77 $minoredit
78 ): void {
79 if ( $content instanceof MessageBundleContent ) {
80 try {
81 // Validation is performed in the store because injecting services into the
82 // Content class is not straightforward
83 $this->messageBundleStore->validate( $context->getTitle(), $content );
84 } catch ( MalformedBundle $e ) {
85 // MalformedBundle implements MessageSpecifier, but for unknown reason it gets
86 // cast to a string if we don't convert it to a proper message.
87 $status->fatal( 'translate-messagebundle-validation-error', $context->msg( $e ) );
88 }
89 }
90 }
91
93 public function onPageSaveComplete(
94 $wikiPage,
95 $user,
96 $summary,
97 $flags,
98 $revisionRecord,
99 $editResult
100 ): void {
101 if ( !$this->enableIntegration ) {
102 return;
103 }
104
105 $method = __METHOD__;
106 $content = $revisionRecord->getContent( SlotRecord::MAIN );
107 $pageTitle = $wikiPage->getTitle();
108
109 if ( $content === null ) {
110 $this->logger->debug( "Unable to access content of page {pageName} in $method", [
111 'pageName' => $pageTitle->getPrefixedText()
112 ] );
113 return;
114 }
115
116 if ( !$content instanceof MessageBundleContent ) {
117 return;
118 }
119
120 try {
121 $this->messageBundleStore->save( $pageTitle, $revisionRecord, $content );
122 } catch ( MalformedBundle $e ) {
123 // This should not happen, as it should not be possible to save a page with invalid content
124 $this->logger->warning( "Page {pageName} is not a valid message bundle in $method", [
125 'pageName' => $pageTitle->getPrefixedText(),
126 'exception' => $e,
127 ] );
128 return;
129 }
130 }
131
133 public static function onTranslateInitGroupLoaders( array &$groupLoader ): void {
134 self::getInstance()->onTranslateInitGroupLoadersImpl( $groupLoader );
135 }
136
137 public function onTranslateInitGroupLoadersImpl( array &$groupLoader ): void {
138 if ( !$this->enableIntegration ) {
139 return;
140 }
141
142 $groupLoader[] = new MessageBundleMessageGroupLoader(
143 $this->loadBalancer->getConnectionRef( DB_REPLICA ),
144 new MessageGroupWANCache( $this->WANObjectCache )
145 );
146 }
147}
static onTranslateInitGroupLoaders(array &$groupLoader)
Hook: TranslateInitGroupLoaders.
Definition Hooks.php:133
onPageSaveComplete( $wikiPage, $user, $summary, $flags, $revisionRecord, $editResult)
@inheritDoc
Definition Hooks.php:93
onEditFilterMergedContent(IContextSource $context, Content $content, Status $status, $summary, User $user, $minoredit)
@inheritDoc
Definition Hooks.php:71
Wrapper around WANObjectCache providing a simpler interface for MessageGroups to use the cache.