summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/translationaids/TranslationAid.php
blob: 9c0f26a971f9273e281dfb1d76a49ffaaf11255b (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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
<?php
/**
 * Translation aid code.
 *
 * @file
 * @author Niklas Laxström
 * @copyright Copyright © 2013, Niklas Laxström
 * @license GPL-2.0+
 */

/**
 * Multipurpose class for translation aids:
 *  - interface for translation aid classes
 *  - listing of available translation aids
 *  - some utility functions for translation aid classes
 *
 * @defgroup TranslationAids Translation Aids
 * @since 2013-01-01
 */
abstract class TranslationAid {
	/**
	 * @var MessageGroup
	 */
	protected $group;

	/**
	 * @var MessageHandle
	 */
	protected $handle;

	/**
	 * @var IContextSource
	 */
	protected $context;

	public function __construct( MessageGroup $group, MessageHandle $handle,
		IContextSource $context
	) {
		$this->group = $group;
		$this->handle = $handle;
		$this->context = $context;
	}

	/**
	 * Translation aid class should implement this function. Return value should
	 * be an array with keys and values. Because these are used in the MediaWiki
	 * API, lists (numeric keys) should have key '**' set to element name that
	 * describes the list values. For example if the translation aid provides
	 * translation suggestions, it would return an array which has key '**' set
	 * to 'suggestion' and then list of arrays, each containing fields for the
	 * information of the suggestions. See InOtherLanguagesAid for example.
	 *
	 * @throw TranslationHelperException Used to signal unexpected errors to aid
	 *  debugging
	 * @return array
	 */
	abstract public function getData();

	/**
	 * Get the message definition. Cached for performance.
	 *
	 * @return string
	 */
	public function getDefinition() {
		static $cache = array();

		$key = $this->handle->getTitle()->getPrefixedText();

		if ( array_key_exists( $key, $cache ) ) {
			return $cache[$key];
		}

		if ( method_exists( $this->group, 'getMessageContent' ) ) {
			$cache[$key] = $this->group->getMessageContent( $this->handle );
		} else {
			$cache[$key] = $this->group->getMessage(
				$this->handle->getKey(),
				$this->group->getSourceLanguage()
			);
		}

		return $cache[$key];
	}

	/**
	 * @return Content
	 */
	protected function getDefinitionContent() {
		$text = $this->getDefinition();

		return ContentHandler::makeContent( $text, $this->handle->getTitle() );
	}

	/**
	 * Get the translations in all languages. Cached for performance.
	 * Fuzzy translation are not included.
	 *
	 * @return array Language code => Translation
	 */
	public function getTranslations() {
		static $cache = array();

		$key = $this->handle->getTitle()->getPrefixedText();

		if ( array_key_exists( $key, $cache ) ) {
			return $cache[$key];
		}

		$data = ApiQueryMessageTranslations::getTranslations( $this->handle );
		$namespace = $this->handle->getTitle()->getNamespace();

		$cache[$key] = array();

		foreach ( $data as $page => $info ) {
			$tTitle = Title::makeTitle( $namespace, $page );
			$tHandle = new MessageHandle( $tTitle );

			$fuzzy = MessageHandle::hasFuzzyString( $info[0] ) || $tHandle->isFuzzy();
			if ( $fuzzy ) {
				continue;
			}

			$code = $tHandle->getCode();
			$cache[$key][$code] = $info[0];
		}

		return $cache[$key];
	}

	/**
	 * List of available message types mapped to the classes
	 * implementing them.
	 *
	 * @return array
	 */
	public static function getTypes() {
		$types = array(
			'definition' => 'MessageDefinitionAid',
			'translation' => 'CurrentTranslationAid',
			'inotherlanguages' => 'InOtherLanguagesAid',
			'documentation' => 'DocumentationAid',
			'mt' => 'MachineTranslationAid',
			'definitiondiff' => 'UpdatedDefinitionAid',
			'ttmserver' => 'TTMServerAid',
			'support' => 'SupportAid',
			'gettext' => 'GettextDocumentationAid',
			'insertables' => 'InsertablesAid',
		);

		return $types;
	}
}