summaryrefslogtreecommitdiff
path: root/extensions/LocalisationUpdate/QuickArrayReader.php
blob: 214d5a61342cd3ec3c8131fa446ed7f27c5e6441 (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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php

/**
 * Quickie parser class that can happily read the subset of PHP we need
 * for our localization arrays safely.
 *
 * About an order of magnitude faster than ConfEditor(), but still an
 * order of magnitude slower than eval().
 */
class QuickArrayReader {
	var $vars = array();

	/**
	 * @param $string string
	 */
	function __construct( $string ) {
		$scalarTypes = array(
			T_LNUMBER => true,
			T_DNUMBER => true,
			T_STRING => true,
			T_CONSTANT_ENCAPSED_STRING => true,
		);
		$skipTypes = array(
			T_WHITESPACE => true,
			T_COMMENT => true,
			T_DOC_COMMENT => true,
		);
		$tokens = token_get_all( $string );
		$count = count( $tokens );
		for( $i = 0; $i < $count; ) {
			while( isset($skipTypes[$tokens[$i][0]] ) ) {
				$i++;
			}
			switch( $tokens[$i][0] ) {
			case T_OPEN_TAG:
				$i++;
				continue;
			case T_VARIABLE:
				// '$messages' -> 'messages'
				$varname = trim( substr( $tokens[$i][1], 1 ) );
				$varindex = null;

				while( isset($skipTypes[$tokens[++$i][0]] ) );

				if( $tokens[$i] === '[' ) {
					while( isset($skipTypes[$tokens[++$i][0]] ) );

					if( isset($scalarTypes[$tokens[$i][0]] ) ) {
						$varindex = $this->parseScalar( $tokens[$i] );
					} else {
						throw $this->except( $tokens[$i], 'scalar index' );
					}
					while( isset($skipTypes[$tokens[++$i][0]] ) );

					if( $tokens[$i] !== ']' ) {
						throw $this->except( $tokens[$i], ']' );
					}
					while( isset($skipTypes[$tokens[++$i][0]] ) );
				}

				if( $tokens[$i] !== '=' ) {
					throw $this->except( $tokens[$i], '=' );
				}
				while( isset($skipTypes[$tokens[++$i][0]] ) );

				if( isset($scalarTypes[$tokens[$i][0]] ) ) {
					$buildval = $this->parseScalar( $tokens[$i] );
				} elseif( $tokens[$i][0] === T_ARRAY ) {
					while( isset($skipTypes[$tokens[++$i][0]] ) );
					if( $tokens[$i] !== '(' ) {
						throw $this->except( $tokens[$i], '(' );
					}
					$buildval = array();
					do {
						while( isset($skipTypes[$tokens[++$i][0]] ) );

						if( $tokens[$i] === ')' ) {
							break;
						}
						if( isset($scalarTypes[$tokens[$i][0]] ) ) {
							$key = $this->parseScalar( $tokens[$i] );
						}
						while( isset($skipTypes[$tokens[++$i][0]] ) );

						if( $tokens[$i][0] !== T_DOUBLE_ARROW ) {
							throw $this->except( $tokens[$i], '=>' );
						}
						while( isset($skipTypes[$tokens[++$i][0]] ) );

						if( isset($scalarTypes[$tokens[$i][0]] ) ) {
							$val = $this->parseScalar( $tokens[$i] );
						}
						@$buildval[$key] = $val;
						while( isset($skipTypes[$tokens[++$i][0]] ) );

						if( $tokens[$i] === ',' ) {
							continue;
						} elseif( $tokens[$i] === ')' ) {
							break;
						} else {
							throw $this->except( $tokens[$i], ', or )' );
						}
					} while(true);
				} else {
					throw $this->except( $tokens[$i], 'scalar or array' );
				}
				if( is_null( $varindex ) ) {
					$this->vars[$varname] = $buildval;
				} else {
					@$this->vars[$varname][$varindex] = $buildval;
				}
				while( isset($skipTypes[$tokens[++$i][0]] ) );
				if( $tokens[$i] !== ';' ) {
					throw $this->except($tokens[$i], ';');
				}
				$i++;
				break;
			default:
				throw $this->except($tokens[$i], 'open tag, whitespace, or variable.');
			}
		}
	}

	/**
	 * @param $got string
	 * @param $expected string
	 * @return Exception
	 */
	private function except( $got, $expected ) {
		if( is_array( $got ) ) {
			$got = token_name( $got[0] ) . " ('" . $got[1] . "')";
		} else {
			$got = "'" . $got . "'";
		}
		return new Exception( "Expected $expected, got $got" );
	}

	/**
	 * Parse a scalar value in PHP
	 *
	 * @param $token string
	 *
	 * @return mixed Parsed value
	 */
	function parseScalar( $token ) {
		if( is_array( $token ) ) {
			$str = $token[1];
		} else {
			$str = $token;
		}
		if ( $str !== '' && $str[0] == '\'' )
			// Single-quoted string
			// @fixme trim() call is due to mystery bug where whitespace gets
			// appended to the token; without it we ended up reading in the
			// extra quote on the end!
			return strtr( substr( trim( $str ), 1, -1 ),
				array( '\\\'' => '\'', '\\\\' => '\\' ) );
		if ( $str !== '' && @$str[0] == '"' )
			// Double-quoted string
			// @fixme trim() call is due to mystery bug where whitespace gets
			// appended to the token; without it we ended up reading in the
			// extra quote on the end!
			return stripcslashes( substr( trim( $str ), 1, -1 ) );
		if ( substr( $str, 0, 4 ) === 'true' )
			return true;
		if ( substr( $str, 0, 5 ) === 'false' )
			return false;
		if ( substr( $str, 0, 4 ) === 'null' )
			return null;
		// Must be some kind of numeric value, so let PHP's weak typing
		// be useful for a change
		return $str;
	}

	/**
	 * @param $varname string
	 * @return null|string
	 */
	function getVar( $varname ) {
		if( isset( $this->vars[$varname] ) ) {
			return $this->vars[$varname];
		} else {
			return null;
		}
	}
}