Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
| TranslationTargetUrlCreator | |
0.00% |
0 / 14 |
|
0.00% |
0 / 3 |
20 | |
0.00% |
0 / 1 |
| __construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
| createUrlForSXRedirection | |
0.00% |
0 / 8 |
|
0.00% |
0 / 1 |
2 | |||
| createTargetUrl | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
6 | |||
| 1 | <?php |
| 2 | declare( strict_types = 1 ); |
| 3 | |
| 4 | namespace ContentTranslation\Service; |
| 5 | |
| 6 | use ContentTranslation\SiteMapper; |
| 7 | use MediaWiki\Config\ServiceOptions; |
| 8 | use 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 | */ |
| 18 | class TranslationTargetUrlCreator { |
| 19 | private bool $contentTranslationTranslateInTarget; |
| 20 | |
| 21 | /** |
| 22 | * @internal For use by ServiceWiring |
| 23 | */ |
| 24 | public const CONSTRUCTOR_OPTIONS = [ |
| 25 | 'ContentTranslationTranslateInTarget' |
| 26 | ]; |
| 27 | |
| 28 | public function __construct( private readonly TitleFactory $titleFactory, ServiceOptions $options ) { |
| 29 | $options->assertRequiredOptions( self::CONSTRUCTOR_OPTIONS ); |
| 30 | |
| 31 | $this->contentTranslationTranslateInTarget = $options->get( 'ContentTranslationTranslateInTarget' ); |
| 32 | } |
| 33 | |
| 34 | public function createUrlForSXRedirection( |
| 35 | string $targetTitle, |
| 36 | string $targetLanguage, |
| 37 | string $sourceLanguage, |
| 38 | string $sourceTitle, |
| 39 | string $targetSectionTitle |
| 40 | ): string { |
| 41 | $url = $this->createTargetUrl( $targetTitle, $targetLanguage ); |
| 42 | $params = [ |
| 43 | "sx-source-page-title" => $sourceTitle, |
| 44 | "sx-published-section" => $targetSectionTitle, |
| 45 | "sx-source-language" => $sourceLanguage, |
| 46 | "sx-target-language" => $targetLanguage |
| 47 | ]; |
| 48 | |
| 49 | return wfAppendQuery( $url, $params ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Given the user, the target page title as a string and the target language of the translation, |
| 54 | * this method returns a string containing the URL of the target page. |
| 55 | * |
| 56 | * @param string $targetTitleRaw |
| 57 | * @param string $targetLanguage |
| 58 | * @return string the translation target URL |
| 59 | */ |
| 60 | public function createTargetUrl( string $targetTitleRaw, string $targetLanguage ): string { |
| 61 | if ( $this->contentTranslationTranslateInTarget ) { |
| 62 | return SiteMapper::getPageURL( $targetLanguage, $targetTitleRaw ); |
| 63 | } |
| 64 | $targetTitle = $this->titleFactory->newFromText( $targetTitleRaw ); |
| 65 | return $targetTitle->getCanonicalURL(); |
| 66 | } |
| 67 | } |