Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleImporter.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use Closure;
7use Exception;
8use ImportStreamSource;
9use ManualLogEntry;
12use MediaWiki\Page\PageIdentity;
13use MediaWiki\Revision\RevisionLookup;
14use MediaWiki\Revision\SlotRecord;
15use MediaWiki\User\UserIdentity;
16use TextContent;
17use Title;
18use WikiImporterFactory;
19
27 private WikiImporterFactory $wikiImporterFactory;
28 private TranslatablePageParser $translatablePageParser;
29 private RevisionLookup $revisionLookup;
30 private ?Title $bundleTitle;
31 private ?Closure $importCompleteCallback;
32
33 public function __construct(
34 WikiImporterFactory $wikiImporterFactory,
35 TranslatablePageParser $translatablePageParser,
36 RevisionLookup $revisionLookup
37 ) {
38 $this->wikiImporterFactory = $wikiImporterFactory;
39 $this->translatablePageParser = $translatablePageParser;
40 $this->revisionLookup = $revisionLookup;
41 }
42
43 public function import(
44 string $importFilePath,
45 string $interwikiPrefix,
46 bool $assignKnownUsers,
47 UserIdentity $user,
48 ?string $comment
49 ): Title {
50 $importSource = ImportStreamSource::newFromFile( $importFilePath );
51 if ( !$importSource->isOK() ) {
53 "Error while reading import file '$importFilePath': " . $importSource->getMessage()->text()
54 );
55 }
56
57 $wikiImporter = $this->wikiImporterFactory->getWikiImporter( $importSource->value );
58 $wikiImporter->setUsernamePrefix( $interwikiPrefix, $assignKnownUsers );
59 $wikiImporter->setPageOutCallback( [ $this, 'pageCallback' ] );
60
61 try {
62 // Reset the currently set title which might have been set during the previous import process
63 $this->bundleTitle = null;
64 $importResult = $wikiImporter->doImport();
65 } catch ( Exception $e ) {
67 $e->getMessage(),
68 $e->getCode(),
69 $e
70 );
71 }
72
73 if ( $importResult === false ) {
74 throw new TranslatableBundleImportException( 'Unknown error while importing translatable bundle.' );
75 }
76
77 if ( !$this->bundleTitle ) {
78 throw new TranslatableBundleImportException( 'Import done, but could not identify imported page.' );
79 }
80
81 if ( $this->importCompleteCallback ) {
82 // Import is complete
83 call_user_func( $this->importCompleteCallback, $this->bundleTitle );
84 }
85
86 // WikiImporter does not trigger hooks that run after a page is edited. Hence, manually add the ready
87 // tag to the imported page if it contains the markup
88 $this->addReadyTagForTranslatablePage( $this->bundleTitle );
89 $this->logImport( $user, $this->bundleTitle, $comment );
90
91 return $this->bundleTitle;
92 }
93
94 public function pageCallback( PageIdentity $pageIdentity ): void {
95 // We assume that the first PageIdentity that we receive is the translatable bundle being imported.
96 $this->bundleTitle ??= Title::newFromPageIdentity( $pageIdentity );
97 }
98
99 public function setImportCompleteCallback( callable $callable ): void {
100 $this->importCompleteCallback = Closure::fromCallable( $callable );
101 }
102
103 private function logImport( UserIdentity $user, Title $bundle, ?string $comment ): void {
104 $entry = new ManualLogEntry( 'import', 'translatable-bundle' );
105 $entry->setPerformer( $user );
106 $entry->setTarget( $bundle );
107 $logId = $entry->insert();
108 if ( $comment ) {
109 $entry->setComment( $comment );
110 }
111 $entry->publish( $logId );
112 }
113
115 private function addReadyTagForTranslatablePage( Title $translatablePageTitle ) {
116 $revisionRecord = $this->revisionLookup->getRevisionByTitle( $translatablePageTitle );
117 if ( !$revisionRecord ) {
119 "Revision record could not be found for imported page: {$translatablePageTitle}"
120 );
121 }
122
123 $content = $revisionRecord->getContent( SlotRecord::MAIN );
124 if ( !$content instanceof TextContent ) {
126 "Content in revision record for {$translatablePageTitle} is not of type TextContent"
127 );
128 }
129
130 if ( $this->translatablePageParser->containsMarkup( $content->getText() ) ) {
131 // Add the ready tag
132 $page = TranslatablePage::newFromTitle( Title::newFromLinkTarget( $translatablePageTitle ) );
133 $page->addReadyTag( $revisionRecord->getId() );
134 }
135 }
136}
Exception thrown when an error occurs when importing a translatable bundle via TranslatableBundleImpo...
Generates ParserOutput from text or removes all tags from a text.
Mixed bag of methods related to translatable pages.