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 MediaWiki\Export\DumpStringOutput;
8use MediaWiki\Export\WikiExporter;
9use MediaWiki\Export\WikiExporterFactory;
10use MediaWiki\Title\Title;
11use Wikimedia\Rdbms\IConnectionProvider;
12
20 private WikiExporter $wikiExporter;
22 private ?Closure $exportPageCallback;
23
24 public function __construct(
25 private readonly SubpageListBuilder $subpageListBuilder,
26 WikiExporterFactory $wikiExporterFactory,
27 IConnectionProvider $dbProvider
28 ) {
29 $this->wikiExporter = $wikiExporterFactory->getWikiExporter(
30 $dbProvider->getReplicaDatabase(),
31 WikiExporter::FULL
32 );
33 }
34
35 public function export( TranslatableBundle $bundle, bool $includeTalkPages, bool $includeSubPages ): string {
36 $classifiedSubpages = $this->subpageListBuilder->getSubpagesPerType( $bundle, $includeTalkPages );
37
38 $sink = new DumpStringOutput();
39 $this->wikiExporter->setOutputSink( $sink );
40 $this->wikiExporter->openStream();
41
42 // Add all the pages to be exported
43 $this->exportPages( [ $bundle->getTitle() ], 'translatable bundle' );
44 $this->exportPages( $classifiedSubpages[ 'translationPages' ], 'translation' );
45 $this->exportPages( $classifiedSubpages[ 'translationUnitPages' ], 'translation unit' );
46
47 // Filter out null values. Null values mean that there is no corresponding talk page
48 $talkPages = $includeTalkPages ? $classifiedSubpages[ 'talkPages' ] : [];
49 $talkPages = array_filter( $talkPages, static function ( $val ) {
50 return $val !== null;
51 } );
52 $this->exportPages( $talkPages, 'talk pages' );
53
54 $this->exportPages(
55 $includeTalkPages ? $classifiedSubpages[ 'translatableTalkPages' ] : [],
56 'translatable talk',
57 );
58 $this->exportPages(
59 $includeSubPages ? $classifiedSubpages[ 'normalSubpages' ] : [],
60 'subpage'
61 );
62
63 $this->wikiExporter->closeStream();
64
65 return (string)$sink;
66 }
67
69 public function setExportPageCallback( callable $callable ) {
70 $this->exportPageCallback = Closure::fromCallable( $callable );
71 }
72
77 private function exportPages( array $pagesForExport, string $pageType ): void {
78 if ( $this->exportPageCallback ) {
79 ( $this->exportPageCallback )( $pagesForExport, $pageType );
80 }
81
82 foreach ( $pagesForExport as $page ) {
83 $this->wikiExporter->pageByTitle( $page );
84 }
85 }
86}
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.