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\ILoadBalancer;
12
20 private WikiExporter $wikiExporter;
21 private SubpageListBuilder $subpageListBuilder;
22 private ?Closure $exportPageCallback;
23
24 public function __construct(
25 SubpageListBuilder $subpageListBuilder,
26 WikiExporterFactory $wikiExporterFactory,
27 ILoadBalancer $dbLoadBalancer
28 ) {
29 $this->subpageListBuilder = $subpageListBuilder;
30 $this->wikiExporter = $wikiExporterFactory->getWikiExporter(
31 $dbLoadBalancer->getConnection( DB_REPLICA ),
32 WikiExporter::FULL
33 );
34 }
35
36 public function export( TranslatableBundle $bundle, bool $includeTalkPages, bool $includeSubPages ): string {
37 $classifiedSubpages = $this->subpageListBuilder->getSubpagesPerType( $bundle, $includeTalkPages );
38
39 $sink = new DumpStringOutput();
40 $this->wikiExporter->setOutputSink( $sink );
41 $this->wikiExporter->openStream();
42
43 // Add all the pages to be exported
44 $this->exportPages( [ $bundle->getTitle() ], 'translatable bundle' );
45 $this->exportPages( $classifiedSubpages[ 'translationPages' ], 'translation' );
46 $this->exportPages( $classifiedSubpages[ 'translationUnitPages' ], 'translation unit' );
47
48 // Filter out null values. Null values mean that there is no corresponding talk page
49 $talkPages = $includeTalkPages ? $classifiedSubpages[ 'talkPages' ] : [];
50 $talkPages = array_filter( $talkPages, static function ( $val ) {
51 return $val !== null;
52 } );
53 $this->exportPages( $talkPages, 'talk pages' );
54
55 $this->exportPages(
56 $includeTalkPages ? $classifiedSubpages[ 'translatableTalkPages' ] : [],
57 'translatable talk',
58 );
59 $this->exportPages(
60 $includeSubPages ? $classifiedSubpages[ 'normalSubpages' ] : [],
61 'subpage'
62 );
63
64 $this->wikiExporter->closeStream();
65
66 return (string)$sink;
67 }
68
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 call_user_func( $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.