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    private $contentTranslationTranslateInTarget;
21
22    /**
23     * @internal For use by ServiceWiring
24     */
25    public const CONSTRUCTOR_OPTIONS = [
26        'ContentTranslationTranslateInTarget'
27    ];
28
29    public function __construct( TitleFactory $titleFactory, ServiceOptions $options ) {
30        $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS );
31
32        $this->titleFactory = $titleFactory;
33        $this->contentTranslationTranslateInTarget = $options->get( 'ContentTranslationTranslateInTarget' );
34    }
35
36    public function createUrlForSXRedirection(
37        string $targetTitle,
38        string $targetLanguage,
39        string $sourceLanguage,
40        string $sourceTitle,
41        string $targetSectionTitle
42    ): string {
43        $url = $this->createTargetUrl( $targetTitle, $targetLanguage );
44        $params = [
45            "sx-source-page-title" => $sourceTitle,
46            "sx-published-section" => $targetSectionTitle,
47            "sx-source-language" => $sourceLanguage,
48            "sx-target-language" => $targetLanguage
49        ];
50
51        return wfAppendQuery( $url, $params );
52    }
53
54    /**
55     * Given the user, the target page title as a string and the target language of the translation,
56     * this method returns a string containing the URL of the target page.
57     *
58     * @param string $targetTitleRaw
59     * @param string $targetLanguage
60     * @return string the translation target URL
61     */
62    public function createTargetUrl( string $targetTitleRaw, string $targetLanguage ): string {
63        if ( $this->contentTranslationTranslateInTarget ) {
64            return SiteMapper::getPageURL( $targetLanguage, $targetTitleRaw );
65        }
66        $targetTitle = $this->titleFactory->newFromText( $targetTitleRaw );
67        return $targetTitle->getCanonicalURL();
68    }
69}