summaryrefslogtreecommitdiff
blob: ea97a7ed743a7798e689329b0e7ecba44cab8040 (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
<?php
/**
 * This file contains an unmanaged message group implementation.
 *
 * @file
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @copyright Copyright © 2008-2013, Niklas Laxström, Siebrand Mazeland
 * @license GPL-2.0-or-later
 */

/**
 * Group for messages that can be controlled via a page in %MediaWiki namespace.
 *
 * In the page comments start with # and continue till the end of the line.
 * The page should contain list of page names in %MediaWiki namespace, without
 * the namespace prefix. Use underscores for spaces in page names, since
 * whitespace separates the page names from each other.
 * @ingroup MessageGroup
 */
class WikiMessageGroup extends MessageGroupOld {
	protected $source;

	/**
	 * @param string $id Unique id for this group.
	 * @param string $source Mediawiki message that contains list of message keys.
	 */
	public function __construct( $id, $source ) {
		$this->id = $id;
		$this->source = $source;
	}

	/**
	 * Defaults to wiki content language.
	 * @return string Language code
	 */
	public function getSourceLanguage() {
		global $wgLanguageCode;

		return $wgLanguageCode;
	}

	/**
	 * Fetch definitions from database.
	 * @return array Array of messages keys with definitions.
	 */
	public function getDefinitions() {
		$definitions = [];

		// In theory the page could have templates that are substitued
		$source = wfMessage( $this->source );
		if ( $source->isDisabled() ) {
			return [];
		}

		$contents = $source->text();
		$contents = preg_replace( '~^\s*#.*$~m', '', $contents );
		$messages = preg_split( '/\s+/', $contents );

		foreach ( $messages as $message ) {
			if ( !$message ) {
				continue;
			}

			$definitions[$message] = wfMessage( $message )->inContentLanguage()->plain();
		}

		return $definitions;
	}

	/**
	 * Returns of stored translation of message specified by the $key in language
	 * code $code.
	 *
	 * @param string $key Key of the message.
	 * @param string $code Language code.
	 * @return string|null The translation or null if it doesn't exist.
	 */
	public function getMessage( $key, $code ) {
		if ( $code && $this->getSourceLanguage() !== $code ) {
			return TranslateUtils::getMessageContent( $key, $code );
		} else {
			return TranslateUtils::getMessageContent( $key, false );
		}
	}
}