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 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( $dbLoadBalancer->getConnection( DB_REPLICA ) );
31 }
32
33 public function export( TranslatableBundle $bundle, bool $includeTalkPages, bool $includeSubPages ): string {
34 $classifiedSubpages = $this->subpageListBuilder->getSubpagesPerType( $bundle, $includeTalkPages );
35
36 $sink = new DumpStringOutput();
37 $this->wikiExporter->setOutputSink( $sink );
38 $this->wikiExporter->openStream();
39
40 // Add all the pages to be exported
41 $this->exportPages( [ $bundle->getTitle() ], 'translatable bundle' );
42 $this->exportPages( $classifiedSubpages[ 'translationPages' ], 'translation' );
43 $this->exportPages( $classifiedSubpages[ 'translationUnitPages' ], 'translation unit' );
44
45 // Filter out null values. Null values mean that there is no corresponding talk page
46 $talkPages = $includeTalkPages ? $classifiedSubpages[ 'talkPages' ] : [];
47 $talkPages = array_filter( $talkPages, static function ( $val ) {
48 return $val !== null;
49 } );
50 $this->exportPages( $talkPages, 'talk pages' );
51
52 $this->exportPages(
53 $includeTalkPages ? $classifiedSubpages[ 'translatableTalkPages' ] : [],
54 'translatable talk',
55 );
56 $this->exportPages(
57 $includeSubPages ? $classifiedSubpages[ 'normalSubpages' ] : [],
58 'subpage'
59 );
60
61 $this->wikiExporter->closeStream();
62
63 return (string)$sink;
64 }
65
66 public function setExportPageCallback( callable $callable ) {
67 $this->exportPageCallback = Closure::fromCallable( $callable );
68 }
69
74 private function exportPages( array $pagesForExport, string $pageType ): void {
75 if ( $this->exportPageCallback ) {
76 call_user_func( $this->exportPageCallback, $pagesForExport, $pageType );
77 }
78
79 foreach ( $pagesForExport as $page ) {
80 $this->wikiExporter->pageByTitle( $page );
81 }
82 }
83}
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.