Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
TranslationTargetUrlCreator
0.00% covered (danger)
0.00%
0 / 15
0.00% covered (danger)
0.00%
0 / 3
20
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 createUrlForSXRedirection
0.00% covered (danger)
0.00%
0 / 8
0.00% covered (danger)
0.00%
0 / 1
2
 createTargetUrl
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
1<?php
2declare( strict_types = 1 );
3
4namespace ContentTranslation\Service;
5
6use ContentTranslation\SiteMapper;
7use MediaWiki\Config\ServiceOptions;
8use MediaWiki\Title\TitleFactory;
9
10/**
11 * @author Nik Gkountas
12 * @license GPL-2.0-or-later
13 *
14 * The TranslationTargetUrlCreator service creates the target URL for a translation,
15 * to be used inside "cx_translations" DB table or to be returned to the UI for proper
16 * redirection after successful publishing.
17 */
18class TranslationTargetUrlCreator {
19    private TitleFactory $titleFactory;
20    /** @var bool */
21    private $contentTranslationTranslateInTarget;
22
23    /**
24     * @internal For use by ServiceWiring
25     */
26    public const CONSTRUCTOR_OPTIONS = [
27        'ContentTranslationTranslateInTarget'
28    ];
29
30    public function __construct( TitleFactory $titleFactory, ServiceOptions $options ) {
31        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
32
33        $this->titleFactory = $titleFactory;
34        $this->contentTranslationTranslateInTarget = $options->get( 'ContentTranslationTranslateInTarget' );
35    }
36
37    public function createUrlForSXRedirection(
38        string $targetTitle,
39        string $targetLanguage,
40        string $sourceLanguage,
41        string $sourceTitle,
42        string $targetSectionTitle
43    ): string {
44        $url = $this->createTargetUrl( $targetTitle, $targetLanguage );
45        $params = [
46            "sx-source-page-title" => $sourceTitle,
47            "sx-published-section" => $targetSectionTitle,
48            "sx-source-language" => $sourceLanguage,
49            "sx-target-language" => $targetLanguage
50        ];
51
52        return wfAppendQuery( $url, $params );
53    }
54
55    /**
56     * Given the user, the target page title as a string and the target language of the translation,
57     * this method returns a string containing the URL of the target page.
58     *
59     * @param string $targetTitleRaw
60     * @param string $targetLanguage
61     * @return string the translation target URL
62     */
63    public function createTargetUrl( string $targetTitleRaw, string $targetLanguage ): string {
64        if ( $this->contentTranslationTranslateInTarget ) {
65            return SiteMapper::getPageURL( $targetLanguage, $targetTitleRaw );
66        }
67        $targetTitle = $this->titleFactory->newFromText( $targetTitleRaw );
68        return $targetTitle->getCanonicalURL();
69    }
70}