18 private TitleFactory $titleFactory;
22 public function __construct() {
23 parent::__construct();
24 $this->addDescription(
25 'Export a translatable bundle into a file that can then be imported ' .
26 'into another wiki using the importTranslatableBundle.php script'
29 'translatable-bundle',
30 'Name of the translatable bundle to be exported',
36 'Path to save the export file including the file name',
42 'Include subpages in the export file',
47 'Include talk pages in the export file',
51 $this->requireExtension(
'Translate' );
56 $this->setupServices();
58 $bundle = $this->getBundleToExport();
59 $includeTalkPages = $this->hasOption(
'include-talk-pages' );
60 $includeSubPages = $this->hasOption(
'include-subpages' );
61 $exportFilename = $this->getExportFilename();
63 $this->translatableBundleExporter->setExportPageCallback( [ $this,
'exportPageCallback' ] );
64 $output = $this->translatableBundleExporter->export(
70 file_put_contents( $exportFilename, $output );
71 $this->output(
"Done! Exported bundle '{$bundle->getTitle()->getPrefixedText()}' to '$exportFilename'.\n" );
74 private function setupServices(): void {
75 $serviceInstance =
Services::getInstance();
77 $this->titleFactory = MediaWikiServices::getInstance()->getTitleFactory();
79 $this->translatableBundleFactory = $serviceInstance->getTranslatableBundleFactory();
82 private function getBundleToExport(): TranslatableBundle {
83 $titleString = $this->getOption(
'translatable-bundle' );
84 $translatableBundleTitle = $this->titleFactory->newFromText( $titleString );
85 if ( !$translatableBundleTitle ) {
86 $this->fatalError(
"'$titleString' is not a valid title" );
89 $bundle = $this->translatableBundleFactory->getBundle( $translatableBundleTitle );
91 $this->fatalError(
"Page $titleString is not a translatable bundle" );
97 private function getExportFilename(): string {
98 $filename = $this->getOption(
'filename' );
99 if ( !is_writable( dirname( $filename ) ) ) {
100 $this->fatalError(
"Unable to create file '$filename'" );
106 public function exportPageCallback( array $pages,
string $pageType ): void {
107 $this->output(
'Exporting ' . count( $pages ) .
" page(s) of type $pageType.\n" );