Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
CRAP
0.00% covered (danger)
0.00%
0 / 1
SpecialAutomaticTranslation
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 5
30
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 execute
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
2
 getInitialLanguagesData
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
2
 getHtml
0.00% covered (danger)
0.00%
0 / 13
0.00% covered (danger)
0.00%
0 / 1
2
 getDescription
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
1<?php
2
3declare( strict_types = 1 );
4
5namespace ContentTranslation\Special;
6
7use ContentTranslation\SiteMapper;
8use MediaWiki\Html\TemplateParser;
9use MediaWiki\Language\LanguageNameUtils;
10use MediaWiki\SpecialPage\SpecialPage;
11
12/**
13 * A special page for translating Wikipedia pages using MinT.
14 *
15 * This page provides the user the capability to search for an Wikipedia
16 * article in any language, and get the automatic (machine) translation for
17 * this article, by the MinT translation service.
18 *
19 * @author Nik Gkountas
20 * @license GPL-2.0-or-later
21 */
22class SpecialAutomaticTranslation extends SpecialPage {
23
24    public function __construct( private readonly LanguageNameUtils $languageNameUtils ) {
25        parent::__construct( 'AutomaticTranslation' );
26    }
27
28    /** @inheritDoc */
29    public function execute( $subPage ) {
30        parent::execute( $subPage );
31
32        $this->setHeaders();
33        $out = $this->getOutput();
34        $targetLanguageCode = SiteMapper::getCurrentLanguageCode();
35        $targetLanguage = $this->languageNameUtils->getLanguageName( $targetLanguageCode );
36        $out->addHTML( $this->getHtml( $targetLanguage ) );
37
38        $out->addModuleStyles( 'mint.styles' );
39        $out->addModules( 'mint.app' );
40
41        // TODO: is there a way to get URL params in "getInitialLanguagesData" hook?
42        $out->addJsConfigVars( 'mintUrlSourceLanguageCode', $out->getRequest()->getVal( 'from' ) );
43        $out->addJsConfigVars( 'mintUrlTargetLanguageCode', $out->getRequest()->getVal( 'to' ) );
44    }
45
46    /**
47     * Callback used to share MinT initial languages JS code
48     */
49    public static function getInitialLanguagesData(): array {
50        return [
51            'mintInitialSourceLanguageCode' => 'all',
52            'mintInitialTargetLanguageCode' => SiteMapper::getCurrentLanguageCode()
53        ];
54    }
55
56    private function getHtml( string $targetLanguage ): string {
57        $templateParser = new TemplateParser( __DIR__ . '/../templates/minT' );
58        $data = [
59            'headerTitle' => $this->msg( 'mint-home-header-title' )->text(),
60            'inputPlaceholder' => $this->msg( 'mint-home-input-placeholder' )->text(),
61            'sourceLanguage' => $this->msg( 'mint-translation-list-all-languages-option-label' )->text(),
62            'sourceLanguageCode' => 'all',
63            'targetLanguage' => $targetLanguage,
64            'infoPanelText' => $this->msg( 'mint-home-info-panel-text', $targetLanguage )->text(),
65            'randomTopicButtonLabel' => $this->msg( 'mint-home-random-topic-button-label' )->text(),
66            'experimentalNote' =>
67                $this->msg( 'mint-home-experimental-note' )->params( 'https://www.mediawiki.org/wiki/MinT' )->parse()
68        ];
69
70        return $templateParser->processTemplate( 'Home', $data );
71    }
72
73    /** @inheritDoc */
74    public function getDescription() {
75        return $this->msg( 'automatic-translation-special-page-description' );
76    }
77}