Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
TranslatableBundleExporter.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageGroupProcessing;
5
6use Closure;
7use DumpStringOutput;
8use MediaWiki\Export\WikiExporterFactory;
9use MediaWiki\Title\Title;
10use WikiExporter;
11use Wikimedia\Rdbms\IConnectionProvider;
12
20 private WikiExporter $wikiExporter;
21 private ?Closure $exportPageCallback;
22
23 public function __construct(
24 private readonly SubpageListBuilder $subpageListBuilder,
25 WikiExporterFactory $wikiExporterFactory,
26 IConnectionProvider $dbProvider
27 ) {
28 $this->wikiExporter = $wikiExporterFactory->getWikiExporter(
29 $dbProvider->getReplicaDatabase(),
30 WikiExporter::FULL
31 );
32 }
33
34 public function export( TranslatableBundle $bundle, bool $includeTalkPages, bool $includeSubPages ): string {
35 $classifiedSubpages = $this->subpageListBuilder->getSubpagesPerType( $bundle, $includeTalkPages );
36
37 $sink = new DumpStringOutput();
38 $this->wikiExporter->setOutputSink( $sink );
39 $this->wikiExporter->openStream();
40
41 // Add all the pages to be exported
42 $this->exportPages( [ $bundle->getTitle() ], 'translatable bundle' );
43 $this->exportPages( $classifiedSubpages[ 'translationPages' ], 'translation' );
44 $this->exportPages( $classifiedSubpages[ 'translationUnitPages' ], 'translation unit' );
45
46 // Filter out null values. Null values mean that there is no corresponding talk page
47 $talkPages = $includeTalkPages ? $classifiedSubpages[ 'talkPages' ] : [];
48 $talkPages = array_filter( $talkPages, static function ( $val ) {
49 return $val !== null;
50 } );
51 $this->exportPages( $talkPages, 'talk pages' );
52
53 $this->exportPages(
54 $includeTalkPages ? $classifiedSubpages[ 'translatableTalkPages' ] : [],
55 'translatable talk',
56 );
57 $this->exportPages(
58 $includeSubPages ? $classifiedSubpages[ 'normalSubpages' ] : [],
59 'subpage'
60 );
61
62 $this->wikiExporter->closeStream();
63
64 return (string)$sink;
65 }
66
67 public function setExportPageCallback( callable $callable ) {
68 $this->exportPageCallback = Closure::fromCallable( $callable );
69 }
70
75 private function exportPages( array $pagesForExport, string $pageType ): void {
76 if ( $this->exportPageCallback ) {
77 ( $this->exportPageCallback )( $pagesForExport, $pageType );
78 }
79
80 foreach ( $pagesForExport as $page ) {
81 $this->wikiExporter->pageByTitle( $page );
82 }
83 }
84}
Generates list of subpages for the translatable bundle that can be moved or deleted.
Translatable bundle represents a message group where its translatable content is defined on a wiki pa...
getTitle()
Return the title of the page where the translatable bundle is defined.