Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
62.50% covered (warning)
62.50%
5 / 8
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
SandboxTitleMaker
62.50% covered (warning)
62.50%
5 / 8
50.00% covered (danger)
50.00%
1 / 2
4.84
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 makeSandboxTitle
50.00% covered (danger)
50.00%
3 / 6
0.00% covered (danger)
0.00%
0 / 1
4.12
1<?php
2declare( strict_types = 1 );
3
4namespace ContentTranslation\Service;
5
6use MediaWiki\Title\Title;
7use MediaWiki\Title\TitleFactory;
8use User;
9
10class SandboxTitleMaker {
11    private TitleFactory $titleFactory;
12    private bool $isSandboxLinkLoaded;
13
14    public function __construct( TitleFactory $titleFactory, bool $isSandboxLinkLoaded ) {
15        $this->titleFactory = $titleFactory;
16        $this->isSandboxLinkLoaded = $isSandboxLinkLoaded;
17    }
18
19    /**
20     * The logic for this function is copied by the "getSandboxTitle" private function,
21     * which lives inside the Hooks of the SandboxLink extension.
22     * Reference: https://github.com/wikimedia/mediawiki-extensions-SandboxLink/blob/master/includes/Hooks.php#L39
23     *
24     * @param User $user
25     * @param string $targetTitle
26     * @return Title|null
27     */
28    public function makeSandboxTitle( User $user, string $targetTitle ): ?Title {
29        $targetTitle = $user->getName() . '/' . $targetTitle;
30        if ( $this->isSandboxLinkLoaded ) {
31            $subpageMsg = wfMessage( 'sandboxlink-subpage-name' )->inContentLanguage();
32            if ( !$subpageMsg->isDisabled() ) {
33                $targetTitle = $user->getName() . '/' . $subpageMsg->plain();
34            }
35        }
36
37        return $this->titleFactory->makeTitleSafe( NS_USER, $targetTitle );
38    }
39}