Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
RenderTranslationPageJob.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\PageTranslation;
5
6use JobQueueGroup;
7use MediaWiki\Category\Category;
8use MediaWiki\CommentStore\CommentStoreComment;
13use MediaWiki\MediaWikiServices;
14use MediaWiki\Revision\RevisionStore;
15use MediaWiki\Revision\SlotRecord;
16use MediaWiki\Title\Title;
17use MediaWiki\User\User;
18use MediaWiki\User\UserIdentity;
19use MediaWiki\User\UserRigorOptions;
20use RecentChange;
21
29 public const ACTION_DELETE = 'delete';
30 public const ACTION_CATEGORIZATION = 'categorization';
31
32 public static function newJob(
33 Title $target,
34 ?string $triggerAction = null,
35 ?string $unitTitleText = null
36 ): self {
37 $job = new self( $target, [ 'triggerAction' => $triggerAction, 'unitTitle' => $unitTitleText ] );
38 $job->setUser( FuzzyBot::getUser() );
39 $job->setFlags( EDIT_FORCE_BOT );
40 $job->setSummary( wfMessage( 'tpt-render-summary' )->inContentLanguage()->text() );
41
42 return $job;
43 }
44
45 public static function newNonPrioritizedJob(
46 Title $target,
47 ?string $triggerAction = null,
48 ?string $unitTitleText = null
49 ): self {
50 $job = self::newJob( $target, $triggerAction, $unitTitleText );
51 $job->command = 'NonPrioritizedRenderTranslationPageJob';
52 return $job;
53 }
54
55 public function __construct( Title $title, array $params = [] ) {
56 parent::__construct( 'RenderTranslationPageJob', $title, $params );
57 $this->removeDuplicates = true;
58 }
59
60 public function run(): bool {
61 $this->logJobStart();
62 $mwServices = MediaWikiServices::getInstance();
63 // We may be doing double wait here if this job was spawned by TranslationUpdateJob
64 $lb = $mwServices->getDBLoadBalancerFactory();
65 if ( !$lb->waitForReplication() ) {
66 $this->logWarning( 'Continuing despite replication lag' );
67 }
68
69 // Initialization
70 $translationPageTitle = $this->title;
71
72 $tpPage = TranslatablePage::getTranslationPageFromTitle( $translationPageTitle );
73 if ( !$tpPage ) {
74 $this->logError( 'Cannot render translation page!' );
75 return false;
76 }
77
78 // Other stuff
79 $user = $this->getUser();
80 $summary = $this->getSummary();
81 $flags = $this->getFlags();
82
83 // We should not re-create the translation page if a translation unit is being deleted
84 // because it is possible that the translation page may also be queued for deletion.
85 // Hence, set the flag to EDIT_UPDATE and remove EDIT_NEW if its added
86 if ( $this->isDeleteTrigger() ) {
87 $flags = ( $flags | EDIT_UPDATE ) & ~EDIT_NEW;
88 }
89
90 // @todo FuzzyBot hack
91 Hooks::$allowTargetEdit = true;
92
93 $commentStoreComment = CommentStoreComment::newUnsavedComment( $summary );
94 // $percentageTranslated is modified by reference
95 $content = $tpPage->getPageContent( $mwServices->getParser(), $percentageTranslated );
96 $translationPageTitleExists = $translationPageTitle->exists();
97 if ( $this->isCategoryTrigger() ) {
98 $isNonEmptyCategory = true;
99 } elseif ( $translationPageTitle->inNamespace( NS_CATEGORY ) ) {
100 $cat = Category::newFromTitle( $translationPageTitle );
101 $isNonEmptyCategory = $cat->getMemberCount() > 0;
102 } else {
103 $isNonEmptyCategory = false;
104 }
105 if ( $percentageTranslated === 0 && !$translationPageTitleExists && !$isNonEmptyCategory ) {
106 Hooks::$allowTargetEdit = false;
107 $this->logInfo( 'No translations found and translation page does not exist. Nothing to do.' );
108 return true;
109 }
110
111 if (
112 $percentageTranslated === 0 &&
113 $translationPageTitleExists &&
114 $this->hasOnlyFuzzyBotAsAuthor( $mwServices->getRevisionStore(), $translationPageTitle ) &&
115 !$isNonEmptyCategory
116 ) {
117 $this->logInfo( 'Deleting translation page having no translations and modified only by Fuzzybot' );
118 // Page is not translated at all but the translation page exists and has been only edited by FuzzyBot
119 $this->deleteTranslationPage( $mwServices->getJobQueueGroup(), $translationPageTitle, FuzzyBot::getUser() );
120 } else {
121 $pageUpdater = $mwServices->getWikiPageFactory()
122 ->newFromTitle( $translationPageTitle )
123 ->newPageUpdater( $user );
124 $pageUpdater->setContent( SlotRecord::MAIN, $content );
125
126 if ( $user->authorizeWrite( 'autopatrol', $translationPageTitle ) ) {
127 $pageUpdater->setRcPatrolStatus( RecentChange::PRC_AUTOPATROLLED );
128 }
129
130 $pageUpdater->addTag( 'translate-translation-pages' );
131 $pageUpdater->saveRevision( $commentStoreComment, $flags );
132 $status = $pageUpdater->getStatus();
133
134 if ( !$status->isOK() ) {
135 if ( $this->isDeleteTrigger() && $status->hasMessage( 'edit-gone-missing' ) ) {
136 $this->logInfo( 'Translation page missing with delete trigger' );
137 } else {
138 $this->logError(
139 'Error while editing content in page.',
140 [
141 'content' => $content->getTextForSummary(),
142 'errors' => $status->getErrors()
143 ]
144 );
145 }
146 }
147 }
148
149 Hooks::$allowTargetEdit = false;
150
151 $this->logInfo( 'Finished TranslateRenderJob' );
152 return true;
153 }
154
155 public function setFlags( int $flags ): void {
156 $this->params['flags'] = $flags;
157 }
158
159 private function getFlags(): int {
160 return $this->params['flags'];
161 }
162
163 public function setSummary( string $summary ): void {
164 $this->params['summary'] = $summary;
165 }
166
168 public function getDeduplicationInfo(): array {
169 $info = parent::getDeduplicationInfo();
170 // Unit title is only passed for logging and should not be used for de-duplication
171 unset( $info['params']['unitTitle'] );
172 return $info;
173 }
174
175 private function getSummary(): string {
176 return $this->params['summary'];
177 }
178
180 public function setUser( $user ): void {
181 if ( $user instanceof UserIdentity ) {
182 $this->params['user'] = $user->getName();
183 } else {
184 $this->params['user'] = $user;
185 }
186 }
187
189 private function getUser(): User {
190 $userFactory = MediaWikiServices::getInstance()->getUserFactory();
191 return $userFactory->newFromName( $this->params['user'], UserRigorOptions::RIGOR_NONE );
192 }
193
194 private function isDeleteTrigger(): bool {
195 return ( $this->params['triggerAction'] ?? null ) === self::ACTION_DELETE;
196 }
197
198 private function isCategoryTrigger(): bool {
199 return ( $this->params['triggerAction'] ?? null ) === self::ACTION_CATEGORIZATION;
200 }
201
202 private function logJobStart(): void {
203 $unitTitleText = $this->params['unitTitle'] ?? null;
204 $logMessage = 'Starting TranslateRenderJob ';
205 if ( $unitTitleText ) {
206 $logMessage .= "trigged by $unitTitleText ";
207 }
208
209 if ( $this->isDeleteTrigger() ) {
210 $logMessage .= '- [deletion] ';
211 }
212
213 $this->logInfo( trim( $logMessage ) );
214 }
215
216 private function deleteTranslationPage(
217 JobQueueGroup $jobQueueGroup,
218 Title $translationPageTitle,
219 UserIdentity $performer
220 ): void {
221 $translatablePageTitle = ( new MessageHandle( $translationPageTitle ) )->getTitleForBase();
222 $isTranslationPage = true;
223
224 $job = DeleteTranslatableBundleJob::newJob(
225 $translationPageTitle,
226 $translatablePageTitle->getPrefixedText(),
227 TranslatablePage::class,
228 $isTranslationPage,
229 $performer,
230 wfMessage( 'pt-deletepage-lang-outdated-logreason' )->inContentLanguage()->text()
231 );
232
233 $jobQueueGroup->push( $job );
234 }
235
236 private function hasOnlyFuzzyBotAsAuthor( RevisionStore $revisionStore, Title $title ): bool {
237 $fuzzyBot = FuzzyBot::getUser();
238 $pageAuthors = $revisionStore->getAuthorsBetween( $title->getId() );
239 foreach ( $pageAuthors as $author ) {
240 if ( !$author->equals( $fuzzyBot ) ) {
241 return false;
242 }
243 }
244 return true;
245 }
246}
return[ 'Translate:AggregateGroupManager'=> static function(MediaWikiServices $services):AggregateGroupManager { return new AggregateGroupManager($services->getTitleFactory(), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:AggregateGroupMessageGroupFactory'=> static function(MediaWikiServices $services):AggregateGroupMessageGroupFactory { return new AggregateGroupMessageGroupFactory($services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateComparator'=> static function(MediaWikiServices $services):ExternalMessageSourceStateComparator { return new ExternalMessageSourceStateComparator(new SimpleStringComparator(), $services->getRevisionLookup(), $services->getPageStore());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance(LogNames::GROUP_SYNCHRONIZATION), $services->get( 'Translate:MessageIndex'), $services->getTitleFactory(), $services->get( 'Translate:MessageGroupSubscription'), new ServiceOptions(ExternalMessageSourceStateImporter::CONSTRUCTOR_OPTIONS, $services->getMainConfig()));}, 'Translate:FileBasedMessageGroupFactory'=> static function(MediaWikiServices $services):FileBasedMessageGroupFactory { return new FileBasedMessageGroupFactory(new MessageGroupConfigurationParser(), new ServiceOptions(FileBasedMessageGroupFactory::SERVICE_OPTIONS, $services->getMainConfig()),);}, 'Translate:FileFormatFactory'=> static function(MediaWikiServices $services):FileFormatFactory { return new FileFormatFactory( $services->getObjectFactory());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:HookDefinedMessageGroupFactory'=> static function(MediaWikiServices $services):HookDefinedMessageGroupFactory { return new HookDefinedMessageGroupFactory( $services->get( 'Translate:HookRunner'));}, 'Translate:HookRunner'=> static function(MediaWikiServices $services):HookRunner { return new HookRunner( $services->getHookContainer());}, 'Translate:MessageBundleDependencyPurger'=> static function(MediaWikiServices $services):MessageBundleDependencyPurger { return new MessageBundleDependencyPurger( $services->get( 'Translate:TranslatableBundleFactory'));}, 'Translate:MessageBundleMessageGroupFactory'=> static function(MediaWikiServices $services):MessageBundleMessageGroupFactory { return new MessageBundleMessageGroupFactory($services->get( 'Translate:MessageGroupMetadata'), new ServiceOptions(MessageBundleMessageGroupFactory::SERVICE_OPTIONS, $services->getMainConfig()),);}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore($services->get( 'Translate:RevTagStore'), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:MessageBundleTranslationLoader'=> static function(MediaWikiServices $services):MessageBundleTranslationLoader { return new MessageBundleTranslationLoader( $services->getLanguageFallback());}, 'Translate:MessageGroupMetadata'=> static function(MediaWikiServices $services):MessageGroupMetadata { return new MessageGroupMetadata( $services->getConnectionProvider());}, 'Translate:MessageGroupReviewStore'=> static function(MediaWikiServices $services):MessageGroupReviewStore { return new MessageGroupReviewStore($services->getConnectionProvider(), $services->get( 'Translate:HookRunner'));}, 'Translate:MessageGroupStatsTableFactory'=> static function(MediaWikiServices $services):MessageGroupStatsTableFactory { return new MessageGroupStatsTableFactory($services->get( 'Translate:ProgressStatsTableFactory'), $services->getLinkRenderer(), $services->get( 'Translate:MessageGroupReviewStore'), $services->get( 'Translate:MessageGroupMetadata'), $services->getMainConfig() ->get( 'TranslateWorkflowStates') !==false);}, 'Translate:MessageGroupSubscription'=> static function(MediaWikiServices $services):MessageGroupSubscription { return new MessageGroupSubscription($services->get( 'Translate:MessageGroupSubscriptionStore'), $services->getJobQueueGroup(), $services->getUserIdentityLookup(), LoggerFactory::getInstance(LogNames::GROUP_SUBSCRIPTION), new ServiceOptions(MessageGroupSubscription::CONSTRUCTOR_OPTIONS, $services->getMainConfig()));}, 'Translate:MessageGroupSubscriptionHookHandler'=> static function(MediaWikiServices $services):MessageGroupSubscriptionHookHandler { return new MessageGroupSubscriptionHookHandler($services->get( 'Translate:MessageGroupSubscription'), $services->getUserFactory());}, 'Translate:MessageGroupSubscriptionStore'=> static function(MediaWikiServices $services):MessageGroupSubscriptionStore { return new MessageGroupSubscriptionStore( $services->getConnectionProvider());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=(array) $services->getMainConfig() ->get( 'TranslateMessageIndex');$class=array_shift( $params);$implementationMap=['HashMessageIndex'=> HashMessageIndex::class, 'CDBMessageIndex'=> CDBMessageIndex::class, 'DatabaseMessageIndex'=> DatabaseMessageIndex::class, 'hash'=> HashMessageIndex::class, 'cdb'=> CDBMessageIndex::class, 'database'=> DatabaseMessageIndex::class,];$messageIndexStoreClass=$implementationMap[$class] ?? $implementationMap['database'];return new MessageIndex(new $messageIndexStoreClass, $services->getMainWANObjectCache(), $services->getJobQueueGroup(), $services->get( 'Translate:HookRunner'), LoggerFactory::getInstance(LogNames::MAIN), $services->getMainObjectStash(), $services->getConnectionProvider(), new ServiceOptions(MessageIndex::SERVICE_OPTIONS, $services->getMainConfig()),);}, 'Translate:MessagePrefixStats'=> static function(MediaWikiServices $services):MessagePrefixStats { return new MessagePrefixStats( $services->getTitleParser());}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getConnectionProvider(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:RevTagStore'=> static function(MediaWikiServices $services):RevTagStore { return new RevTagStore( $services->getConnectionProvider());}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleDeleter'=> static function(MediaWikiServices $services):TranslatableBundleDeleter { return new TranslatableBundleDeleter($services->getMainObjectStash(), $services->getJobQueueGroup(), $services->get( 'Translate:SubpageListBuilder'), $services->get( 'Translate:TranslatableBundleFactory'));}, 'Translate:TranslatableBundleExporter'=> static function(MediaWikiServices $services):TranslatableBundleExporter { return new TranslatableBundleExporter($services->get( 'Translate:SubpageListBuilder'), $services->getWikiExporterFactory(), $services->getConnectionProvider());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleImporter'=> static function(MediaWikiServices $services):TranslatableBundleImporter { return new TranslatableBundleImporter($services->getWikiImporterFactory(), $services->get( 'Translate:TranslatablePageParser'), $services->getRevisionLookup(), $services->getNamespaceInfo(), $services->getTitleFactory(), $services->getFormatterFactory());}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getConnectionProvider(), $services->getObjectCacheFactory(), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatableBundleStatusStore'=> static function(MediaWikiServices $services):TranslatableBundleStatusStore { return new TranslatableBundleStatusStore($services->getConnectionProvider() ->getPrimaryDatabase(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), $services->getDBLoadBalancer() ->getMaintenanceConnectionRef(DB_PRIMARY));}, 'Translate:TranslatablePageMarker'=> static function(MediaWikiServices $services):TranslatablePageMarker { return new TranslatablePageMarker($services->getConnectionProvider(), $services->getJobQueueGroup(), $services->getLinkRenderer(), MessageGroups::singleton(), $services->get( 'Translate:MessageIndex'), $services->getTitleFormatter(), $services->getTitleParser(), $services->get( 'Translate:TranslatablePageParser'), $services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:TranslatablePageStateStore'), $services->get( 'Translate:TranslationUnitStoreFactory'), $services->get( 'Translate:MessageGroupMetadata'), $services->getWikiPageFactory(), $services->get( 'Translate:TranslatablePageView'), $services->get( 'Translate:MessageGroupSubscription'), $services->getFormatterFactory());}, 'Translate:TranslatablePageMessageGroupFactory'=> static function(MediaWikiServices $services):TranslatablePageMessageGroupFactory { return new TranslatablePageMessageGroupFactory(new ServiceOptions(TranslatablePageMessageGroupFactory::SERVICE_OPTIONS, $services->getMainConfig()),);}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStateStore'=> static function(MediaWikiServices $services):TranslatablePageStateStore { return new TranslatablePageStateStore($services->get( 'Translate:PersistentCache'), $services->getPageStore());}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), $services->get( 'Translate:RevTagStore'), $services->getConnectionProvider(), $services->get( 'Translate:TranslatableBundleStatusStore'), $services->get( 'Translate:TranslatablePageParser'), $services->get( 'Translate:MessageGroupMetadata'));}, 'Translate:TranslatablePageView'=> static function(MediaWikiServices $services):TranslatablePageView { return new TranslatablePageView($services->getConnectionProvider(), $services->get( 'Translate:TranslatablePageStateStore'), new ServiceOptions(TranslatablePageView::SERVICE_OPTIONS, $services->getMainConfig()));}, 'Translate:TranslateSandbox'=> static function(MediaWikiServices $services):TranslateSandbox { return new TranslateSandbox($services->getUserFactory(), $services->getConnectionProvider(), $services->getPermissionManager(), $services->getAuthManager(), $services->getUserGroupManager(), $services->getActorStore(), $services->getUserOptionsManager(), $services->getJobQueueGroup(), $services->get( 'Translate:HookRunner'), new ServiceOptions(TranslateSandbox::CONSTRUCTOR_OPTIONS, $services->getMainConfig()));}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { return new TranslationStashStorage( $services->getConnectionProvider() ->getPrimaryDatabase());}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory(), $services->getConnectionProvider());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
Class for pointing to messages, like Title class is for titles.
Job for updating translation pages when translation or template changes.
FuzzyBot - the misunderstood workhorse.
Definition FuzzyBot.php:15