summaryrefslogtreecommitdiff
blob: 98c72b93440303eb6442c2cad983a171f02e88a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
<?php
/**
 * Translation aid provider.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2013, Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Translation aid which gives an url where users can ask for help
 *
 * @ingroup TranslationAids
 * @since 2013-01-02
 */
class SupportAid extends TranslationAid {
	public function getData() {
		return [
			'url' => self::getSupportUrl( $this->handle ),
		];
	}

	/**
	 * Target URL for a link provided by a support button/aid.
	 *
	 * @param MessageHandle $handle MessageHandle object for the translation message.
	 * @since 2015.09
	 * @return string
	 * @throws TranslationHelperException
	 */
	public static function getSupportUrl( MessageHandle $handle ) {
		$title = $handle->getTitle();
		$config = self::getConfig( $handle );

		// Preprocess params
		$params = [];
		if ( isset( $config['params'] ) ) {
			foreach ( $config['params'] as $key => $value ) {
				$params[$key] = str_replace( '%MESSAGE%', $title->getPrefixedText(), $value );
			}
		}

		// Return the URL or make one from the page
		if ( isset( $config['url'] ) ) {
			return wfAppendQuery( $config['url'], $params );
		} elseif ( isset( $config['page'] ) ) {
			$page = Title::newFromText( $config['page'] );
			if ( !$page ) {
				throw new TranslationHelperException( 'Support page not configured properly' );
			}
			return $page->getFullURL( $params );
		} else {
			throw new TranslationHelperException( 'Support page not configured properly' );
		}
	}

	/**
	 * Fetches Support URL config
	 * @param MessageHandle $handle
	 * @return array
	 * @throws TranslationHelperException
	 */
	private static function getConfig( MessageHandle $handle ): array {
		global $wgTranslateSupportUrl, $wgTranslateSupportUrlNamespace;

		if ( !$handle->isValid() ) {
			throw new TranslationHelperException( 'Invalid MessageHandle' );
		}

		// Fetch group level configuration if possible, fallback to namespace based, or default
		$group = $handle->getGroup();
		$namespace = $handle->getTitle()->getNamespace();
		$config = $group->getSupportConfig()
			?? $wgTranslateSupportUrlNamespace[$namespace]
			?? $wgTranslateSupportUrl;

		if ( !$config ) {
			throw new TranslationHelperException( 'Support page not configured' );
		}

		return $config;
	}
}