summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/ffs/DtdFFS.php
blob: aed82fca673484dc9f00c97c89d35faf9d84e428 (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
<?php
/**
 * Implements FFS for DTD file format.
 *
 * @file
 * @author Guillaume Duhamel
 * @author Niklas Laxström
 * @author Siebrand Mazeland
 * @copyright Copyright © 2009-2010, Guillaume Duhamel, Niklas Laxström, Siebrand Mazeland
 * @license GPL-2.0-or-later
 */

/**
 * File format support for DTD.
 *
 * @ingroup FFS
 */
class DtdFFS extends SimpleFFS {
	public function getFileExtensions() {
		return [ '.dtd' ];
	}

	/**
	 * @param string $data
	 * @return array Parsed data.
	 */
	public function readFromVariable( $data ) {
		preg_match_all( ',# Author: ([^\n]+)\n,', $data, $matches );
		$authors = [];

		$count = count( $matches[1] );
		for ( $i = 0; $i < $count; $i++ ) {
			$authors[] = $matches[1][$i];
		}

		preg_match_all( ',<!ENTITY[ ]+([^ ]+)\s+"([^"]+)"[^>]*>,', $data, $matches );

		$keys = $matches[1];
		$values = $matches[2];

		$messages = [];

		$count = count( $matches[1] );
		for ( $i = 0; $i < $count; $i++ ) {
			$messages[$keys[$i]] = str_replace(
				[ '&quot;', '&#34;', '&#39;' ],
				[ '"', '"', "'" ],
				$values[$i] );
		}

		$messages = $this->group->getMangler()->mangle( $messages );

		return [
			'AUTHORS' => $authors,
			'MESSAGES' => $messages,
		];
	}

	protected function writeReal( MessageCollection $collection ) {
		$collection->loadTranslations();

		$header = "<!--\n";
		$header .= $this->doHeader( $collection );
		$header .= $this->doAuthors( $collection );
		$header .= "-->\n";

		$output = '';
		$mangler = $this->group->getMangler();

		/**
		 * @var TMessage $m
		 */
		foreach ( $collection as $key => $m ) {
			$key = $mangler->unmangle( $key );
			$trans = $m->translation();
			$trans = str_replace( TRANSLATE_FUZZY, '', $trans );

			if ( $trans === '' ) {
				continue;
			}

			$trans = str_replace( '"', '&quot;', $trans );
			$output .= "<!ENTITY $key \"$trans\">\n";
		}

		return $output ? $header . $output : false;
	}

	protected function doHeader( MessageCollection $collection ) {
		global $wgSitename;

		$code = $collection->code;
		$name = TranslateUtils::getLanguageName( $code );
		$native = TranslateUtils::getLanguageName( $code, $code );

		$output = "# Messages for $name ($native)\n";
		$output .= "# Exported from $wgSitename\n\n";

		return $output;
	}

	protected function doAuthors( MessageCollection $collection ) {
		$output = '';
		$authors = $collection->getAuthors();
		$authors = $this->filterAuthors( $authors, $collection->code );

		foreach ( $authors as $author ) {
			$output .= "# Author: $author\n";
		}

		return $output;
	}
}