[ Index ] |
PHP Cross Reference of MediaWiki-1.24.0 |
[Summary view] [Print] [Text view]
1 <?php 2 3 /** 4 * Quickie parser class that can happily read the subset of PHP we need 5 * for our localization arrays safely. 6 * 7 * Still an order of magnitude slower than eval(). 8 */ 9 class QuickArrayReader { 10 private $vars = array(); 11 12 /** 13 * @param $string string 14 */ 15 function __construct( $string ) { 16 $scalarTypes = array( 17 T_LNUMBER => true, 18 T_DNUMBER => true, 19 T_STRING => true, 20 T_CONSTANT_ENCAPSED_STRING => true, 21 ); 22 $skipTypes = array( 23 T_WHITESPACE => true, 24 T_COMMENT => true, 25 T_DOC_COMMENT => true, 26 ); 27 $tokens = token_get_all( $string ); 28 $count = count( $tokens ); 29 for ( $i = 0; $i < $count; ) { 30 while ( isset( $skipTypes[$tokens[$i][0]] ) ) { 31 $i++; 32 } 33 switch ( $tokens[$i][0] ) { 34 case T_OPEN_TAG: 35 $i++; 36 continue; 37 case T_VARIABLE: 38 // '$messages' -> 'messages' 39 $varname = trim( substr( $tokens[$i][1], 1 ) ); 40 $varindex = null; 41 42 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 43 44 if ( $tokens[$i] === '[' ) { 45 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 46 47 if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { 48 $varindex = $this->parseScalar( $tokens[$i] ); 49 } else { 50 throw $this->except( $tokens[$i], 'scalar index' ); 51 } 52 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 53 54 if ( $tokens[$i] !== ']' ) { 55 throw $this->except( $tokens[$i], ']' ); 56 } 57 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 58 } 59 60 if ( $tokens[$i] !== '=' ) { 61 throw $this->except( $tokens[$i], '=' ); 62 } 63 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 64 65 if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { 66 $buildval = $this->parseScalar( $tokens[$i] ); 67 } elseif ( $tokens[$i][0] === T_ARRAY ) { 68 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 69 if ( $tokens[$i] !== '(' ) { 70 throw $this->except( $tokens[$i], '(' ); 71 } 72 $buildval = array(); 73 do { 74 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 75 76 if ( $tokens[$i] === ')' ) { 77 break; 78 } 79 if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { 80 $key = $this->parseScalar( $tokens[$i] ); 81 } 82 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 83 84 if ( $tokens[$i][0] !== T_DOUBLE_ARROW ) { 85 throw $this->except( $tokens[$i], '=>' ); 86 } 87 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 88 89 if ( isset( $scalarTypes[$tokens[$i][0]] ) ) { 90 $val = $this->parseScalar( $tokens[$i] ); 91 } 92 wfSuppressWarnings(); 93 $buildval[$key] = $val; 94 wfRestoreWarnings(); 95 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 96 97 if ( $tokens[$i] === ',' ) { 98 continue; 99 } elseif ( $tokens[$i] === ')' ) { 100 break; 101 } else { 102 throw $this->except( $tokens[$i], ', or )' ); 103 } 104 } while ( true ); 105 } else { 106 throw $this->except( $tokens[$i], 'scalar or array' ); 107 } 108 if ( is_null( $varindex ) ) { 109 $this->vars[$varname] = $buildval; 110 } else { 111 wfSuppressWarnings(); 112 $this->vars[$varname][$varindex] = $buildval; 113 wfRestoreWarnings(); 114 } 115 while ( isset( $skipTypes[$tokens[++$i][0]] ) ); 116 if ( $tokens[$i] !== ';' ) { 117 throw $this->except( $tokens[$i], ';' ); 118 } 119 $i++; 120 break; 121 default: 122 throw $this->except( $tokens[$i], 'open tag, whitespace, or variable.' ); 123 } 124 } 125 } 126 127 /** 128 * @param $got string 129 * @param $expected string 130 * @return Exception 131 */ 132 private function except( $got, $expected ) { 133 if ( is_array( $got ) ) { 134 $got = token_name( $got[0] ) . " ('" . $got[1] . "')"; 135 } else { 136 $got = "'" . $got . "'"; 137 } 138 139 return new Exception( "Expected $expected, got $got" ); 140 } 141 142 /** 143 * Parse a scalar value in PHP 144 * 145 * @param $token string 146 * 147 * @return mixed Parsed value 148 */ 149 function parseScalar( $token ) { 150 if ( is_array( $token ) ) { 151 $str = $token[1]; 152 } else { 153 $str = $token; 154 } 155 if ( $str !== '' && $str[0] == '\'' ) { 156 // Single-quoted string 157 // @fixme trim() call is due to mystery bug where whitespace gets 158 // appended to the token; without it we ended up reading in the 159 // extra quote on the end! 160 return strtr( substr( trim( $str ), 1, -1 ), 161 array( '\\\'' => '\'', '\\\\' => '\\' ) ); 162 } 163 164 wfSuppressWarnings(); 165 if ( $str !== '' && $str[0] == '"' ) { 166 // Double-quoted string 167 // @fixme trim() call is due to mystery bug where whitespace gets 168 // appended to the token; without it we ended up reading in the 169 // extra quote on the end! 170 wfRestoreWarnings(); 171 return stripcslashes( substr( trim( $str ), 1, -1 ) ); 172 } 173 wfRestoreWarnings(); 174 175 if ( substr( $str, 0, 4 ) === 'true' ) { 176 return true; 177 } 178 179 if ( substr( $str, 0, 5 ) === 'false' ) { 180 return false; 181 } 182 183 if ( substr( $str, 0, 4 ) === 'null' ) { 184 return null; 185 } 186 187 // Must be some kind of numeric value, so let PHP's weak typing 188 // be useful for a change 189 return $str; 190 } 191 192 /** 193 * @param $varname string 194 * @return null|string 195 */ 196 function getVar( $varname ) { 197 if ( isset( $this->vars[$varname] ) ) { 198 return $this->vars[$varname]; 199 } else { 200 return null; 201 } 202 } 203 } 204
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
Generated: Fri Nov 28 14:03:12 2014 | Cross-referenced by PHPXref 0.7.1 |