Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
CRAP | |
0.00% |
0 / 1 |
SupportAid | |
0.00% |
0 / 31 |
|
0.00% |
0 / 3 |
110 | |
0.00% |
0 / 1 |
getData | |
0.00% |
0 / 3 |
|
0.00% |
0 / 1 |
2 | |||
getSupportUrl | |
0.00% |
0 / 17 |
|
0.00% |
0 / 1 |
42 | |||
getConfig | |
0.00% |
0 / 11 |
|
0.00% |
0 / 1 |
12 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace MediaWiki\Extension\Translate\TranslatorInterface\Aid; |
5 | |
6 | use MediaWiki\Extension\Translate\MessageLoading\MessageHandle; |
7 | use MediaWiki\Extension\Translate\TranslatorInterface\TranslationHelperException; |
8 | use MediaWiki\Extension\Translate\Utilities\Utilities; |
9 | use MediaWiki\Title\Title; |
10 | |
11 | /** |
12 | * Translation aid that provides an url where users can ask for help |
13 | * @author Niklas Laxström |
14 | * @license GPL-2.0-or-later |
15 | * @since 2013-01-02 |
16 | * @ingroup TranslationAids |
17 | */ |
18 | class SupportAid extends TranslationAid { |
19 | public function getData(): array { |
20 | return [ |
21 | 'url' => self::getSupportUrl( $this->handle ), |
22 | ]; |
23 | } |
24 | |
25 | /** |
26 | * Target URL for a link provided by a support button/aid. |
27 | * @param MessageHandle $handle MessageHandle object for the translation message. |
28 | * @return string |
29 | * @throws TranslationHelperException |
30 | */ |
31 | public static function getSupportUrl( MessageHandle $handle ): string { |
32 | $title = $handle->getTitle(); |
33 | $config = self::getConfig( $handle ); |
34 | |
35 | $placeholders = [ |
36 | '%MESSAGE%' => $title->getPrefixedText(), |
37 | '%MESSAGE_URL%' => Utilities::getEditorUrl( $handle ) |
38 | ]; |
39 | |
40 | // Preprocess params |
41 | $params = []; |
42 | if ( isset( $config['params'] ) ) { |
43 | foreach ( $config['params'] as $key => $value ) { |
44 | $params[$key] = strtr( $value, $placeholders ); |
45 | } |
46 | } |
47 | |
48 | // Return the URL or make one from the page |
49 | if ( isset( $config['url'] ) ) { |
50 | return wfAppendQuery( $config['url'], $params ); |
51 | } elseif ( isset( $config['page'] ) ) { |
52 | $page = Title::newFromText( $config['page'] ); |
53 | if ( $page ) { |
54 | return $page->getFullURL( $params ); |
55 | } |
56 | } |
57 | |
58 | throw new TranslationHelperException( 'Support page not configured properly' ); |
59 | } |
60 | |
61 | /** |
62 | * Fetches Support URL config |
63 | * @param MessageHandle $handle |
64 | * @return array |
65 | * @throws TranslationHelperException |
66 | */ |
67 | private static function getConfig( MessageHandle $handle ): array { |
68 | global $wgTranslateSupportUrl, $wgTranslateSupportUrlNamespace; |
69 | |
70 | if ( !$handle->isValid() ) { |
71 | throw new TranslationHelperException( 'Invalid MessageHandle' ); |
72 | } |
73 | |
74 | // Fetch group level configuration if possible, fallback to namespace based, or default |
75 | $group = $handle->getGroup(); |
76 | $namespace = $handle->getTitle()->getNamespace(); |
77 | $config = $group->getSupportConfig() |
78 | ?? $wgTranslateSupportUrlNamespace[$namespace] |
79 | ?? $wgTranslateSupportUrl; |
80 | |
81 | if ( !$config ) { |
82 | throw new TranslationHelperException( 'Support page not configured' ); |
83 | } |
84 | |
85 | return $config; |
86 | } |
87 | } |