summaryrefslogtreecommitdiff
blob: b7cd68cf82622d91b19fc9b0b65b7b27786c17a5 (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
<?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 Gettext documentation.
 *
 * @ingroup TranslationAids
 * @since 2013-01-01
 */
class GettextDocumentationAid extends TranslationAid {
	public function getData() {
		// We need to get the primary group to get the correct file
		// So $group can be different from $this->group
		$group = $this->handle->getGroup();
		if ( !$group instanceof FileBasedMessageGroup ) {
			throw new TranslationHelperException( 'Not a Gettext group' );
		}

		$ffs = $group->getFFS();
		if ( !$ffs instanceof GettextFFS ) {
			throw new TranslationHelperException( 'Not a Gettext group' );
		}

		global $wgContLang;
		$mykey = $wgContLang->lcfirst( $this->handle->getKey() );
		$mykey = str_replace( ' ', '_', $mykey );
		$data = $ffs->read( $group->getSourceLanguage() );
		$help = $data['TEMPLATE'][$mykey]['comments'];

		$conf = $group->getConfiguration();
		if ( isset( $conf['BASIC']['codeBrowser'] ) ) {
			$pattern = $conf['BASIC']['codeBrowser'];
			$pattern = str_replace( '%FILE%', '\1', $pattern );
			$pattern = str_replace( '%LINE%', '\2', $pattern );
			$pattern = "[$pattern \\1:\\2]";
		} else {
			$pattern = "\\1:\\2";
		}

		$out = '';
		foreach ( $help as $type => $lines ) {
			if ( $type === ':' ) {
				$files = '';
				foreach ( $lines as $line ) {
					$files .= ' ' . preg_replace( '/([^ :]+):(\d+)/', $pattern, $line );
				}
				$out .= "<nowiki>#:</nowiki> $files<br />";
			} else {
				foreach ( $lines as $line ) {
					$out .= "<nowiki>#$type</nowiki> $line<br />";
				}
			}
		}

		return [
			'language' => $wgContLang->getCode(),
			// @todo Provide raw data when possible
			// 'value' => $help,
			'html' => $this->context->getOutput()->parse( $out ),
		];
	}
}