| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
namespace MediaWiki\Parser; |
| 22 |
|
| 23 |
use LogicException; |
| 24 |
use MediaWiki\Logger\LoggerFactory; |
| 25 |
use MediaWiki\MediaWikiServices; |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
class MagicWordArray { |
| 36 |
|
| 37 |
|
| 38 |
public $names = []; |
| 39 |
private MagicWordFactory $factory; |
| 40 |
|
| 41 |
|
| 42 |
private $hash; |
| 43 |
|
| 44 |
|
| 45 |
private $baseRegex; |
| 46 |
|
| 47 |
|
| 48 |
private $regex; |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public function __construct( $names = [], ?MagicWordFactory $factory = null ) { |
| 55 |
$this->names = $names; |
| 56 |
$this->factory = $factory ?: MediaWikiServices::getInstance()->getMagicWordFactory(); |
| 57 |
} |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
public function add( $name ): void { |
| 65 |
$this->names[] = $name; |
| 66 |
$this->hash = $this->baseRegex = $this->regex = null; |
| 67 |
} |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
public function getHash(): array { |
| 75 |
if ( $this->hash === null ) { |
| 76 |
$this->hash = [ 0 => [], 1 => [] ]; |
| 77 |
foreach ( $this->names as $name ) { |
| 78 |
$magic = $this->factory->get( $name ); |
| 79 |
$case = intval( $magic->isCaseSensitive() ); |
| 80 |
foreach ( $magic->getSynonyms() as $syn ) { |
| 81 |
if ( !$case ) { |
| 82 |
$syn = $this->factory->getContentLanguage()->lc( $syn ); |
| 83 |
} |
| 84 |
$this->hash[$case][$syn] = $name; |
| 85 |
} |
| 86 |
} |
| 87 |
} |
| 88 |
return $this->hash; |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public function getBaseRegex( bool $capture = true, string $delimiter = '/' ): array { |
| 103 |
if ( $capture && $delimiter === '/' && $this->baseRegex !== null ) { |
| 104 |
return $this->baseRegex; |
| 105 |
} |
| 106 |
$regex = [ 0 => [], 1 => [] ]; |
| 107 |
foreach ( $this->names as $name ) { |
| 108 |
$magic = $this->factory->get( $name ); |
| 109 |
$case = $magic->isCaseSensitive() ? 1 : 0; |
| 110 |
foreach ( $magic->getSynonyms() as $i => $syn ) { |
| 111 |
if ( $capture ) { |
| 112 |
|
| 113 |
$it = strtr( $i, '0123456789', 'abcdefghij' ); |
| 114 |
$groupName = $it . '_' . $name; |
| 115 |
$group = '(?P<' . $groupName . '>' . preg_quote( $syn, $delimiter ) . ')'; |
| 116 |
$regex[$case][] = $group; |
| 117 |
} else { |
| 118 |
$regex[$case][] = preg_quote( $syn, $delimiter ); |
| 119 |
} |
| 120 |
} |
| 121 |
} |
| 122 |
'@phan-var array<int,string[]> $regex'; |
| 123 |
foreach ( $regex as $case => &$re ) { |
| 124 |
$re = count( $re ) ? implode( '|', $re ) : '(?!)'; |
| 125 |
if ( !$case ) { |
| 126 |
$re = "(?i:{$re})"; |
| 127 |
} |
| 128 |
} |
| 129 |
'@phan-var array<int,string> $regex'; |
| 130 |
|
| 131 |
if ( $capture && $delimiter === '/' ) { |
| 132 |
$this->baseRegex = $regex; |
| 133 |
} |
| 134 |
return $regex; |
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
private function getRegex(): array { |
| 143 |
if ( $this->regex === null ) { |
| 144 |
$this->regex = []; |
| 145 |
$base = $this->getBaseRegex( true, '/' ); |
| 146 |
foreach ( $base as $case => $re ) { |
| 147 |
$this->regex[$case] = "/$re/JS"; |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
$this->regex[0] .= 'u'; |
| 152 |
} |
| 153 |
return $this->regex; |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
private function getRegexStart(): array { |
| 162 |
$newRegex = []; |
| 163 |
$base = $this->getBaseRegex( true, '/' ); |
| 164 |
foreach ( $base as $case => $re ) { |
| 165 |
$newRegex[$case] = "/^(?:$re)/JS"; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
$newRegex[0] .= 'u'; |
| 170 |
return $newRegex; |
| 171 |
} |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
private function getVariableStartToEndRegex(): array { |
| 179 |
$newRegex = []; |
| 180 |
$base = $this->getBaseRegex( true, '/' ); |
| 181 |
foreach ( $base as $case => $re ) { |
| 182 |
$newRegex[$case] = str_replace( '\$1', '(.*?)', "/^(?:$re)$/JS" ); |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
$newRegex[0] .= 'u'; |
| 187 |
return $newRegex; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public function getNames() { |
| 195 |
return $this->names; |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private function parseMatch( array $matches ): array { |
| 206 |
$magicName = null; |
| 207 |
foreach ( $matches as $key => $match ) { |
| 208 |
if ( $magicName !== null ) { |
| 209 |
…, |
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
] |
| 214 |
return [ $magicName, $match, $matches[$key + 1] ?? false ]; |
| 215 |
} |
| 216 |
|
| 217 |
if ( $match !== '' && $key !== 0 ) { |
| 218 |
$parts = explode( '_', $key, 2 ); |
| 219 |
if ( !isset( $parts[1] ) ) { |
| 220 |
throw new LogicException( 'Unexpected group name' ); |
| 221 |
} |
| 222 |
$magicName = $parts[1]; |
| 223 |
} |
| 224 |
} |
| 225 |
throw new LogicException( 'Unexpected $m array with no match' ); |
| 226 |
} |
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
public function matchVariableStartToEnd( $text ): array { |
| 236 |
$regexes = $this->getVariableStartToEndRegex(); |
| 237 |
foreach ( $regexes as $regex ) { |
| 238 |
$m = []; |
| 239 |
if ( preg_match( $regex, $text, $m ) ) { |
| 240 |
[ $id, $alias, $param ] = $this->parseMatch( $m ); |
| 241 |
return [ $id, $param ]; |
| 242 |
} |
| 243 |
} |
| 244 |
return [ false, false ]; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
public function matchStartToEnd( $text ) { |
| 255 |
$hash = $this->getHash(); |
| 256 |
if ( isset( $hash[1][$text] ) ) { |
| 257 |
return $hash[1][$text]; |
| 258 |
} |
| 259 |
$lc = $this->factory->getContentLanguage()->lc( $text ); |
| 260 |
return $hash[0][$lc] ?? false; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
public function matchAndRemove( &$text, bool $returnAlias = false ): array { |
| 277 |
$found = []; |
| 278 |
$regexes = $this->getRegex(); |
| 279 |
$res = preg_replace_callback( $regexes, function ( $m ) use ( &$found, $returnAlias ) { |
| 280 |
[ $name, $alias, $param ] = $this->parseMatch( $m ); |
| 281 |
$found[$name] = $returnAlias ? $alias : $param; |
| 282 |
return ''; |
| 283 |
}, $text ); |
| 284 |
|
| 285 |
if ( $res === null ) { |
| 286 |
$error = preg_last_error(); |
| 287 |
$errorText = preg_last_error_msg(); |
| 288 |
LoggerFactory::getInstance( 'parser' )->warning( 'preg_match_all error: {code} {errorText}', [ |
| 289 |
'code' => $error, |
| 290 |
'regex' => $regexes, |
| 291 |
'text' => $text, |
| 292 |
'errorText' => $errorText |
| 293 |
] ); |
| 294 |
if ( $error !== PREG_BAD_UTF8_ERROR ) { |
| 295 |
throw new LogicException( "preg_match_all error $error: $errorText" ); |
| 296 |
} |
| 297 |
} else { |
| 298 |
$text = $res; |
| 299 |
} |
| 300 |
return $found; |
| 301 |
} |
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
public function matchStartAndRemove( &$text ) { |
| 314 |
$regexes = $this->getRegexStart(); |
| 315 |
foreach ( $regexes as $regex ) { |
| 316 |
if ( preg_match( $regex, $text, $m ) ) { |
| 317 |
[ $id, ] = $this->parseMatch( $m ); |
| 318 |
if ( strlen( $m[0] ) >= strlen( $text ) ) { |
| 319 |
$text = ''; |
| 320 |
} else { |
| 321 |
$text = substr( $text, strlen( $m[0] ) ); |
| 322 |
} |
| 323 |
return $id; |
| 324 |
} |
| 325 |
} |
| 326 |
return false; |
| 327 |
} |
| 328 |
} |
| 329 |
|
| 330 |
|
| 331 |
class_alias( MagicWordArray::class, 'MagicWordArray' ); |