summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Translate/MessageGroupConfigurationParser.php
blob: acb452375946522354cba46aa823c811e924454a (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
153
154
155
156
157
158
<?php
/**
 *
 * @file
 * @author Niklas Laxström
 * @license GPL-2.0-or-later
 */

/**
 * Utility class to parse and validate message group configurations.
 * @since 2014.01
 */
class MessageGroupConfigurationParser {
	protected $baseSchema;

	public function __construct() {
		// Don't perform validations if library not available
		if ( class_exists( RomaricDrigon\MetaYaml\MetaYaml::class ) ) {
			$this->baseSchema = $this->getBaseSchema();
		}
	}

	/**
	 * Easy to use function to get valid group configurations from YAML. Those not matching
	 * schema will be ignored, if schema validation is enabled.
	 *
	 * @param string $data Yaml
	 * @param callable|null $callback Optional callback which is called on errors. Parameters are
	 *   document index, processed configuration and error message.
	 * @return array Group configurations indexed by message group id.
	 */
	public function getHopefullyValidConfigurations( $data, $callback = null ) {
		if ( !is_callable( $callback ) ) {
			$callback = function () {
				/*noop*/
			};
		}

		$documents = self::getDocumentsFromYaml( $data );
		$configurations = self::parseDocuments( $documents );
		$groups = [];

		if ( is_array( $this->baseSchema ) ) {
			foreach ( $configurations as $index => $config ) {
				try {
					$this->validate( $config );
					$groups[$config['BASIC']['id']] = $config;
				} catch ( Exception $e ) {
					$callback( $index, $config, $e->getMessage() );
				}
			}
		} else {
			foreach ( $configurations as $index => $config ) {
				if ( isset( $config['BASIC']['id'] ) ) {
					$groups[$config['BASIC']['id']] = $config;
				} else {
					$callback( $index, $config, 'id is missing' );
				}
			}
		}

		return $groups;
	}

	/**
	 * Given a Yaml string, returns the non-empty documents as an array.
	 *
	 * @param string $data
	 * @return string[]
	 */
	public function getDocumentsFromYaml( $data ) {
		return preg_split( "/^---$/m", $data, -1, PREG_SPLIT_NO_EMPTY );
	}

	/**
	 * Returns group configurations from YAML documents. If there is document containing template,
	 * it will be merged with other configurations.
	 *
	 * @param array $documents
	 * @return array Unvalidated group configurations
	 */
	public function parseDocuments( array $documents ) {
		$groups = [];
		$template = [];

		foreach ( $documents as $document ) {
			$document = TranslateYaml::loadString( $document );

			if ( isset( $document['TEMPLATE'] ) ) {
				$template = $document['TEMPLATE'];
			} else {
				$groups[] = $document;
			}
		}

		if ( $template ) {
			foreach ( $groups as $i => $group ) {
				$groups[$i] = self::mergeTemplate( $template, $group );
				// Little hack to allow aggregate groups to be defined in same file with other groups.
				if ( $groups[$i]['BASIC']['class'] === 'AggregateMessageGroup' ) {
					unset( $groups[$i]['FILES'] );
				}
			}
		}

		return $groups;
	}

	public function getBaseSchema() {
		return TranslateYaml::load( __DIR__ . '/data/group-yaml-schema.yaml' );
	}

	/**
	 * Validates group configuration against schema.
	 *
	 * @param array $config
	 * @throws Exception If configuration is not valid.
	 */
	public function validate( array $config ) {
		$schema = $this->baseSchema;

		foreach ( $config as $sectionName => $section ) {
			if ( !isset( $section['class'] ) ) {
				continue;
			}

			$class = $section['class'];
			// There is no sane way to check whether *class* implements interface in PHP
			if ( !method_exists( $class, 'getExtraSchema' ) ) {
				continue;
			}

			$extra = call_user_func( [ $class, 'getExtraSchema' ] );
			$schema = array_replace_recursive( $schema, $extra );
		}

		$schema = new RomaricDrigon\MetaYaml\MetaYaml( $schema );
		$schema->validate( $config );
	}

	/**
	 * Merges a document template (base) to actual definition (specific)
	 * @param array $base
	 * @param array $specific
	 * @return array
	 */
	public static function mergeTemplate( array $base, array $specific ) {
		foreach ( $specific as $key => $value ) {
			if ( is_array( $value ) && isset( $base[$key] ) && is_array( $base[$key] ) ) {
				$base[$key] = self::mergeTemplate( $base[$key], $value );
			} else {
				$base[$key] = $value;
			}
		}

		return $base;
	}
}