| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
namespace MediaWiki\Parser; |
| 25 |
|
| 26 |
use Wikimedia\ObjectCache\WANObjectCache; |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
class Preprocessor_Hash extends Preprocessor { |
| 48 |
|
| 49 |
protected const CACHE_VERSION = 5; |
| 50 |
|
| 51 |
|
| 52 |
protected $cacheThreshold; |
| 53 |
|
| 54 |
private ?string $configurationHash = null; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public function __construct( |
| 64 |
Parser $parser, |
| 65 |
?WANObjectCache $wanCache = null, |
| 66 |
array $options = [] |
| 67 |
) { |
| 68 |
parent::__construct( $parser, $wanCache, $options ); |
| 69 |
|
| 70 |
$this->cacheThreshold = $options['cacheThreshold'] ?? false; |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public function newFrame() { |
| 77 |
return new PPFrame_Hash( $this ); |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
public function newCustomFrame( $args ) { |
| 85 |
return new PPCustomFrame_Hash( $this, $args ); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function newPartNodeArray( $values ) { |
| 93 |
$list = []; |
| 94 |
|
| 95 |
foreach ( $values as $k => $val ) { |
| 96 |
if ( is_int( $k ) ) { |
| 97 |
$store = [ [ 'part', [ |
| 98 |
[ 'name', [ [ '@index', [ $k ] ] ] ], |
| 99 |
[ 'value', [ strval( $val ) ] ], |
| 100 |
] ] ]; |
| 101 |
} else { |
| 102 |
$store = [ [ 'part', [ |
| 103 |
[ 'name', [ strval( $k ) ] ], |
| 104 |
'=', |
| 105 |
[ 'value', [ strval( $val ) ] ], |
| 106 |
] ] ]; |
| 107 |
} |
| 108 |
|
| 109 |
$list[] = new PPNode_Hash_Tree( $store, 0 ); |
| 110 |
} |
| 111 |
|
| 112 |
return new PPNode_Hash_Array( $list ); |
| 113 |
} |
| 114 |
|
| 115 |
private function getConfigurationHash(): string { |
| 116 |
if ( $this->configurationHash ) { |
| 117 |
return $this->configurationHash; |
| 118 |
} |
| 119 |
|
| 120 |
$configs = [ |
| 121 |
'tags' => $this->parser->getTags(), |
| 122 |
]; |
| 123 |
sort( $configs['tags'] ); |
| 124 |
$this->configurationHash = sha1( json_encode( $configs ) ); |
| 125 |
|
| 126 |
return $this->configurationHash; |
| 127 |
} |
| 128 |
|
| 129 |
public function preprocessToObj( $text, $flags = 0 ) { |
| 130 |
if ( $this->disableLangConversion ) { |
| 131 |
|
| 132 |
$flags |= self::DOM_LANG_CONVERSION_DISABLED; |
| 133 |
} |
| 134 |
|
| 135 |
$domTreeArray = null; |
| 136 |
if ( |
| 137 |
$this->cacheThreshold !== false && |
| 138 |
strlen( $text ) >= $this->cacheThreshold && |
| 139 |
( $flags & self::DOM_UNCACHED ) != self::DOM_UNCACHED |
| 140 |
) { |
| 141 |
$domTreeJson = $this->wanCache->getWithSetCallback( |
| 142 |
$this->wanCache->makeKey( |
| 143 |
'preprocess-hash', $this->getConfigurationHash(), sha1( $text ), $flags ), |
| 144 |
$this->wanCache::TTL_DAY, |
| 145 |
function () use ( $text, $flags, &$domTreeArray ) { |
| 146 |
$domTreeArray = $this->buildDomTreeArrayFromText( $text, $flags ); |
| 147 |
|
| 148 |
return json_encode( |
| 149 |
$domTreeArray, |
| 150 |
JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE |
| 151 |
); |
| 152 |
}, |
| 153 |
[ 'version' => self::CACHE_VERSION, 'segmentable' => true ] |
| 154 |
); |
| 155 |
$domTreeArray ??= json_decode( $domTreeJson ); |
| 156 |
} |
| 157 |
|
| 158 |
$domTreeArray ??= $this->buildDomTreeArrayFromText( $text, $flags ); |
| 159 |
|
| 160 |
return new PPNode_Hash_Tree( $domTreeArray, 0 ); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
private function buildDomTreeArrayFromText( $text, $flags ) { |
| 169 |
$forInclusion = ( $flags & self::DOM_FOR_INCLUSION ); |
| 170 |
$langConversionDisabled = ( $flags & self::DOM_LANG_CONVERSION_DISABLED ); |
| 171 |
|
| 172 |
$xmlishElements = $this->parser->getStripList(); |
| 173 |
$xmlishAllowMissingEndTag = [ 'includeonly', 'noinclude', 'onlyinclude' ]; |
| 174 |
$enableOnlyinclude = false; |
| 175 |
if ( $forInclusion ) { |
| 176 |
$ignoredTags = [ 'includeonly', '/includeonly' ]; |
| 177 |
$ignoredElements = [ 'noinclude' ]; |
| 178 |
$xmlishElements[] = 'noinclude'; |
| 179 |
if ( str_contains( $text, '<onlyinclude>' ) |
| 180 |
&& str_contains( $text, '</onlyinclude>' ) |
| 181 |
) { |
| 182 |
$enableOnlyinclude = true; |
| 183 |
} |
| 184 |
} else { |
| 185 |
$ignoredTags = [ 'noinclude', '/noinclude', 'onlyinclude', '/onlyinclude' ]; |
| 186 |
$ignoredElements = [ 'includeonly' ]; |
| 187 |
$xmlishElements[] = 'includeonly'; |
| 188 |
} |
| 189 |
$xmlishRegex = implode( '|', array_merge( $xmlishElements, $ignoredTags ) ); |
| 190 |
|
| 191 |
|
| 192 |
$elementsRegex = "~(?:$xmlishRegex)(?=\s|\/>|>)|!--~iA"; |
| 193 |
|
| 194 |
$stack = new PPDStack_Hash; |
| 195 |
|
| 196 |
$searchBase = "[{<\n"; |
| 197 |
if ( !$langConversionDisabled ) { |
| 198 |
$searchBase .= '-'; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
$revText = strrev( $text ); |
| 203 |
$lengthText = strlen( $text ); |
| 204 |
|
| 205 |
|
| 206 |
$i = 0; |
| 207 |
|
| 208 |
$accum =& $stack->getAccum(); |
| 209 |
|
| 210 |
$findEquals = false; |
| 211 |
|
| 212 |
$findPipe = false; |
| 213 |
$headingIndex = 1; |
| 214 |
|
| 215 |
$inHeading = false; |
| 216 |
|
| 217 |
$noMoreGT = false; |
| 218 |
|
| 219 |
$noMoreClosingTag = []; |
| 220 |
|
| 221 |
$findOnlyinclude = $enableOnlyinclude; |
| 222 |
|
| 223 |
$fakeLineStart = true; |
| 224 |
|
| 225 |
while ( true ) { |
| 226 |
if ( $findOnlyinclude ) { |
| 227 |
|
| 228 |
$startPos = strpos( $text, '<onlyinclude>', $i ); |
| 229 |
if ( $startPos === false ) { |
| 230 |
|
| 231 |
$accum[] = [ 'ignore', [ substr( $text, $i ) ] ]; |
| 232 |
break; |
| 233 |
} |
| 234 |
$tagEndPos = $startPos + 13; |
| 235 |
$accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i ) ] ]; |
| 236 |
$i = $tagEndPos; |
| 237 |
$findOnlyinclude = false; |
| 238 |
} |
| 239 |
|
| 240 |
if ( $fakeLineStart ) { |
| 241 |
$found = 'line-start'; |
| 242 |
$curChar = ''; |
| 243 |
} else { |
| 244 |
|
| 245 |
$search = $searchBase; |
| 246 |
if ( $stack->top === false ) { |
| 247 |
$currentClosing = ''; |
| 248 |
} else { |
| 249 |
$currentClosing = $stack->top->close; |
| 250 |
$search .= $currentClosing; |
| 251 |
} |
| 252 |
if ( $findPipe ) { |
| 253 |
$search .= '|'; |
| 254 |
} |
| 255 |
if ( $findEquals ) { |
| 256 |
|
| 257 |
$search .= '='; |
| 258 |
} |
| 259 |
$rule = null; |
| 260 |
|
| 261 |
$literalLength = strcspn( $text, $search, $i ); |
| 262 |
if ( $literalLength > 0 ) { |
| 263 |
self::addLiteral( $accum, substr( $text, $i, $literalLength ) ); |
| 264 |
$i += $literalLength; |
| 265 |
} |
| 266 |
if ( $i >= $lengthText ) { |
| 267 |
if ( $currentClosing === "\n" ) { |
| 268 |
|
| 269 |
$curChar = ''; |
| 270 |
$found = 'line-end'; |
| 271 |
} else { |
| 272 |
|
| 273 |
break; |
| 274 |
} |
| 275 |
} else { |
| 276 |
$curChar = $curTwoChar = $text[$i]; |
| 277 |
if ( $i + 1 < $lengthText ) { |
| 278 |
$curTwoChar .= $text[$i + 1]; |
| 279 |
} |
| 280 |
if ( $curChar === '|' ) { |
| 281 |
$found = 'pipe'; |
| 282 |
} elseif ( $curChar === '=' ) { |
| 283 |
$found = 'equals'; |
| 284 |
} elseif ( $curChar === '<' ) { |
| 285 |
$found = 'angle'; |
| 286 |
} elseif ( $curChar === "\n" ) { |
| 287 |
if ( $inHeading ) { |
| 288 |
$found = 'line-end'; |
| 289 |
} else { |
| 290 |
$found = 'line-start'; |
| 291 |
} |
| 292 |
} elseif ( $curTwoChar === $currentClosing ) { |
| 293 |
$found = 'close'; |
| 294 |
$curChar = $curTwoChar; |
| 295 |
} elseif ( $curChar === $currentClosing ) { |
| 296 |
$found = 'close'; |
| 297 |
} elseif ( isset( $this->rules[$curTwoChar] ) ) { |
| 298 |
$curChar = $curTwoChar; |
| 299 |
$found = 'open'; |
| 300 |
$rule = $this->rules[$curChar]; |
| 301 |
} elseif ( isset( $this->rules[$curChar] ) ) { |
| 302 |
$found = 'open'; |
| 303 |
$rule = $this->rules[$curChar]; |
| 304 |
} else { |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
if ( $curChar === '-' || $curChar === '}' ) { |
| 311 |
self::addLiteral( $accum, $curChar ); |
| 312 |
} |
| 313 |
++$i; |
| 314 |
continue; |
| 315 |
} |
| 316 |
} |
| 317 |
} |
| 318 |
|
| 319 |
if ( $found === 'angle' ) { |
| 320 |
|
| 321 |
if ( $enableOnlyinclude |
| 322 |
&& substr_compare( $text, '</onlyinclude>', $i, 14 ) === 0 |
| 323 |
) { |
| 324 |
$findOnlyinclude = true; |
| 325 |
continue; |
| 326 |
} |
| 327 |
|
| 328 |
|
| 329 |
if ( !preg_match( $elementsRegex, $text, $matches, 0, $i + 1 ) ) { |
| 330 |
|
| 331 |
self::addLiteral( $accum, '<' ); |
| 332 |
++$i; |
| 333 |
continue; |
| 334 |
} |
| 335 |
$name = $matches[0]; |
| 336 |
|
| 337 |
if ( $name === '!--' ) { |
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
$endPos = strpos( $text, '-->', $i + 4 ); |
| 345 |
if ( $endPos === false ) { |
| 346 |
|
| 347 |
$inner = substr( $text, $i ); |
| 348 |
$accum[] = [ 'comment', [ $inner ] ]; |
| 349 |
$i = $lengthText; |
| 350 |
} else { |
| 351 |
|
| 352 |
|
| 353 |
$wsStart = $i ? ( $i - strspn( $revText, " \t", $lengthText - $i ) ) : 0; |
| 354 |
|
| 355 |
|
| 356 |
$wsEnd = $endPos + 3; |
| 357 |
|
| 358 |
$wsEnd += strspn( $text, " \t", $wsEnd ); |
| 359 |
|
| 360 |
|
| 361 |
$comments = [ [ $wsStart, $wsEnd ] ]; |
| 362 |
while ( substr_compare( $text, '<!--', $wsEnd, 4 ) === 0 ) { |
| 363 |
$c = strpos( $text, '-->', $wsEnd + 4 ); |
| 364 |
if ( $c === false ) { |
| 365 |
break; |
| 366 |
} |
| 367 |
$c += 3; |
| 368 |
|
| 369 |
$c += strspn( $text, " \t", $c ); |
| 370 |
$comments[] = [ $wsEnd, $c ]; |
| 371 |
$wsEnd = $c; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
if ( $wsStart > 0 && substr_compare( $text, "\n", $wsStart - 1, 1 ) === 0 |
| 379 |
&& substr_compare( $text, "\n", $wsEnd, 1 ) === 0 |
| 380 |
) { |
| 381 |
|
| 382 |
$wsLength = $i - $wsStart; |
| 383 |
$endIndex = count( $accum ) - 1; |
| 384 |
|
| 385 |
if ( $wsLength > 0 |
| 386 |
&& $endIndex >= 0 |
| 387 |
&& is_string( $accum[$endIndex] ) |
| 388 |
&& strspn( $accum[$endIndex], " \t", -$wsLength ) === $wsLength |
| 389 |
) { |
| 390 |
$accum[$endIndex] = substr( $accum[$endIndex], 0, -$wsLength ); |
| 391 |
} |
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
[ $startPos, $endPos ] = array_pop( $comments ); |
| 396 |
foreach ( $comments as [ $cStartPos, $cEndPos ] ) { |
| 397 |
|
| 398 |
$inner = substr( $text, $cStartPos, $cEndPos - $cStartPos ); |
| 399 |
$accum[] = [ 'comment', [ $inner ] ]; |
| 400 |
} |
| 401 |
|
| 402 |
|
| 403 |
$fakeLineStart = true; |
| 404 |
} else { |
| 405 |
|
| 406 |
$startPos = $i; |
| 407 |
$endPos += 2; |
| 408 |
} |
| 409 |
|
| 410 |
if ( $stack->top ) { |
| 411 |
$part = $stack->top->getCurrentPart(); |
| 412 |
if ( $part->commentEnd !== $wsStart - 1 ) { |
| 413 |
$part->visualEnd = $wsStart; |
| 414 |
} |
| 415 |
|
| 416 |
$part->commentEnd = $endPos; |
| 417 |
} |
| 418 |
$i = $endPos + 1; |
| 419 |
$inner = substr( $text, $startPos, $endPos - $startPos + 1 ); |
| 420 |
$accum[] = [ 'comment', [ $inner ] ]; |
| 421 |
} |
| 422 |
continue; |
| 423 |
} |
| 424 |
$attrStart = $i + strlen( $name ) + 1; |
| 425 |
|
| 426 |
|
| 427 |
$tagEndPos = $noMoreGT ? false : strpos( $text, '>', $attrStart ); |
| 428 |
if ( $tagEndPos === false ) { |
| 429 |
|
| 430 |
|
| 431 |
$noMoreGT = true; |
| 432 |
self::addLiteral( $accum, '<' ); |
| 433 |
++$i; |
| 434 |
continue; |
| 435 |
} |
| 436 |
|
| 437 |
$lowerName = strtolower( $name ); |
| 438 |
|
| 439 |
if ( in_array( $lowerName, $ignoredTags ) ) { |
| 440 |
$accum[] = [ 'ignore', [ substr( $text, $i, $tagEndPos - $i + 1 ) ] ]; |
| 441 |
$i = $tagEndPos + 1; |
| 442 |
continue; |
| 443 |
} |
| 444 |
|
| 445 |
$tagStartPos = $i; |
| 446 |
if ( $text[$tagEndPos - 1] === '/' ) { |
| 447 |
|
| 448 |
$attrEnd = $tagEndPos - 1; |
| 449 |
$inner = null; |
| 450 |
$i = $tagEndPos + 1; |
| 451 |
$close = null; |
| 452 |
} else { |
| 453 |
$attrEnd = $tagEndPos; |
| 454 |
|
| 455 |
if ( |
| 456 |
!isset( $noMoreClosingTag[$lowerName] ) && |
| 457 |
preg_match( "/<\/" . preg_quote( $name, '/' ) . "\s*>/i", |
| 458 |
$text, $matches, PREG_OFFSET_CAPTURE, $tagEndPos + 1 ) |
| 459 |
) { |
| 460 |
[ $close, $closeTagStartPos ] = $matches[0]; |
| 461 |
$inner = substr( $text, $tagEndPos + 1, $closeTagStartPos - $tagEndPos - 1 ); |
| 462 |
$i = $closeTagStartPos + strlen( $close ); |
| 463 |
} else { |
| 464 |
|
| 465 |
if ( in_array( $name, $xmlishAllowMissingEndTag ) ) { |
| 466 |
|
| 467 |
$inner = substr( $text, $tagEndPos + 1 ); |
| 468 |
$i = $lengthText; |
| 469 |
$close = null; |
| 470 |
} else { |
| 471 |
|
| 472 |
$i = $tagEndPos + 1; |
| 473 |
self::addLiteral( $accum, substr( $text, $tagStartPos, $tagEndPos + 1 - $tagStartPos ) ); |
| 474 |
|
| 475 |
$noMoreClosingTag[$lowerName] = true; |
| 476 |
continue; |
| 477 |
} |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
if ( in_array( $lowerName, $ignoredElements ) ) { |
| 482 |
$accum[] = [ 'ignore', [ substr( $text, $tagStartPos, $i - $tagStartPos ) ] ]; |
| 483 |
continue; |
| 484 |
} |
| 485 |
|
| 486 |
if ( $attrEnd <= $attrStart ) { |
| 487 |
$attr = ''; |
| 488 |
} else { |
| 489 |
|
| 490 |
|
| 491 |
$attr = substr( $text, $attrStart, $attrEnd - $attrStart ); |
| 492 |
} |
| 493 |
|
| 494 |
$children = [ |
| 495 |
[ 'name', [ $name ] ], |
| 496 |
[ 'attr', [ $attr ] ], |
| 497 |
]; |
| 498 |
if ( $inner !== null ) { |
| 499 |
$children[] = [ 'inner', [ $inner ] ]; |
| 500 |
} |
| 501 |
if ( $close !== null ) { |
| 502 |
$children[] = [ 'close', [ $close ] ]; |
| 503 |
} |
| 504 |
$accum[] = [ 'ext', $children ]; |
| 505 |
} elseif ( $found === 'line-start' ) { |
| 506 |
|
| 507 |
|
| 508 |
if ( $fakeLineStart ) { |
| 509 |
$fakeLineStart = false; |
| 510 |
} else { |
| 511 |
self::addLiteral( $accum, $curChar ); |
| 512 |
$i++; |
| 513 |
} |
| 514 |
|
| 515 |
|
| 516 |
$count = strspn( $text, '=', $i, min( $lengthText, 6 ) ); |
| 517 |
if ( $count === 1 && $findEquals ) { |
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
} elseif ( $count > 0 ) { |
| 523 |
$piece = [ |
| 524 |
'open' => "\n", |
| 525 |
'close' => "\n", |
| 526 |
'parts' => [ new PPDPart_Hash( str_repeat( '=', $count ) ) ], |
| 527 |
'startPos' => $i, |
| 528 |
'count' => $count, |
| 529 |
]; |
| 530 |
$stack->push( $piece ); |
| 531 |
$accum =& $stack->getAccum(); |
| 532 |
$stackFlags = $stack->getFlags(); |
| 533 |
if ( isset( $stackFlags['findEquals'] ) ) { |
| 534 |
$findEquals = $stackFlags['findEquals']; |
| 535 |
} |
| 536 |
if ( isset( $stackFlags['findPipe'] ) ) { |
| 537 |
$findPipe = $stackFlags['findPipe']; |
| 538 |
} |
| 539 |
if ( isset( $stackFlags['inHeading'] ) ) { |
| 540 |
$inHeading = $stackFlags['inHeading']; |
| 541 |
} |
| 542 |
$i += $count; |
| 543 |
} |
| 544 |
} elseif ( $found === 'line-end' ) { |
| 545 |
$piece = $stack->top; |
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
assert( $piece->open === "\n" ); |
| 550 |
$part = $piece->getCurrentPart(); |
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
$wsLength = strspn( $revText, " \t", $lengthText - $i ); |
| 555 |
$searchStart = $i - $wsLength; |
| 556 |
if ( $part->commentEnd === $searchStart - 1 ) { |
| 557 |
|
| 558 |
|
| 559 |
$searchStart = $part->visualEnd; |
| 560 |
$searchStart -= strspn( $revText, " \t", $lengthText - $searchStart ); |
| 561 |
} |
| 562 |
$equalsLength = strspn( $revText, '=', $lengthText - $searchStart ); |
| 563 |
if ( $equalsLength > 0 ) { |
| 564 |
if ( $searchStart - $equalsLength === $piece->startPos ) { |
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
if ( $equalsLength < 3 ) { |
| 569 |
$count = 0; |
| 570 |
} else { |
| 571 |
$count = min( 6, intval( ( $equalsLength - 1 ) / 2 ) ); |
| 572 |
} |
| 573 |
} else { |
| 574 |
$count = min( $equalsLength, $piece->count ); |
| 575 |
} |
| 576 |
if ( $count > 0 ) { |
| 577 |
|
| 578 |
$element = [ [ 'possible-h', |
| 579 |
array_merge( |
| 580 |
[ |
| 581 |
[ '@level', [ $count ] ], |
| 582 |
[ '@i', [ $headingIndex++ ] ] |
| 583 |
], |
| 584 |
$accum |
| 585 |
) |
| 586 |
] ]; |
| 587 |
} else { |
| 588 |
|
| 589 |
$element = $accum; |
| 590 |
} |
| 591 |
} else { |
| 592 |
|
| 593 |
$element = $accum; |
| 594 |
} |
| 595 |
|
| 596 |
$stack->pop(); |
| 597 |
$accum =& $stack->getAccum(); |
| 598 |
$stackFlags = $stack->getFlags(); |
| 599 |
if ( isset( $stackFlags['findEquals'] ) ) { |
| 600 |
$findEquals = $stackFlags['findEquals']; |
| 601 |
} |
| 602 |
if ( isset( $stackFlags['findPipe'] ) ) { |
| 603 |
$findPipe = $stackFlags['findPipe']; |
| 604 |
} |
| 605 |
if ( isset( $stackFlags['inHeading'] ) ) { |
| 606 |
$inHeading = $stackFlags['inHeading']; |
| 607 |
} |
| 608 |
|
| 609 |
|
| 610 |
array_splice( $accum, count( $accum ), 0, $element ); |
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
} elseif ( $found === 'open' ) { |
| 618 |
|
| 619 |
$curLen = strlen( $curChar ); |
| 620 |
$count = $curLen > 1 |
| 621 |
|
| 622 |
? strspn( $text, $curChar[$curLen - 1], $i + 1 ) + 1 |
| 623 |
: strspn( $text, $curChar, $i ); |
| 624 |
|
| 625 |
$savedPrefix = ''; |
| 626 |
$lineStart = $i > 0 && $text[$i - 1] === "\n"; |
| 627 |
|
| 628 |
if ( $curChar === "-{" && $count > $curLen ) { |
| 629 |
|
| 630 |
$savedPrefix = '-'; |
| 631 |
$i++; |
| 632 |
$curChar = '{'; |
| 633 |
$count--; |
| 634 |
$rule = $this->rules[$curChar]; |
| 635 |
} |
| 636 |
|
| 637 |
|
| 638 |
if ( $count >= $rule['min'] ) { |
| 639 |
|
| 640 |
$piece = [ |
| 641 |
'open' => $curChar, |
| 642 |
'close' => $rule['end'], |
| 643 |
'savedPrefix' => $savedPrefix, |
| 644 |
'count' => $count, |
| 645 |
'lineStart' => $lineStart, |
| 646 |
]; |
| 647 |
|
| 648 |
$stack->push( $piece ); |
| 649 |
$accum =& $stack->getAccum(); |
| 650 |
$stackFlags = $stack->getFlags(); |
| 651 |
if ( isset( $stackFlags['findEquals'] ) ) { |
| 652 |
$findEquals = $stackFlags['findEquals']; |
| 653 |
} |
| 654 |
if ( isset( $stackFlags['findPipe'] ) ) { |
| 655 |
$findPipe = $stackFlags['findPipe']; |
| 656 |
} |
| 657 |
if ( isset( $stackFlags['inHeading'] ) ) { |
| 658 |
$inHeading = $stackFlags['inHeading']; |
| 659 |
} |
| 660 |
} else { |
| 661 |
|
| 662 |
self::addLiteral( $accum, $savedPrefix . str_repeat( $curChar, $count ) ); |
| 663 |
} |
| 664 |
$i += $count; |
| 665 |
} elseif ( $found === 'close' ) { |
| 666 |
|
| 667 |
$piece = $stack->top; |
| 668 |
'@phan-var PPDStackElement_Hash $piece'; |
| 669 |
|
| 670 |
$maxCount = $piece->count; |
| 671 |
if ( $piece->close === '}-' && $curChar === '}' ) { |
| 672 |
$maxCount--; |
| 673 |
} |
| 674 |
$curLen = strlen( $curChar ); |
| 675 |
$count = $curLen > 1 |
| 676 |
? $curLen |
| 677 |
: strspn( $text, $curChar, $i, $maxCount ); |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
$rule = $this->rules[$piece->open]; |
| 682 |
if ( $count > $rule['max'] ) { |
| 683 |
|
| 684 |
|
| 685 |
$matchingCount = $rule['max']; |
| 686 |
} else { |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
$matchingCount = $count; |
| 691 |
while ( $matchingCount > 0 && !array_key_exists( $matchingCount, $rule['names'] ) ) { |
| 692 |
--$matchingCount; |
| 693 |
} |
| 694 |
} |
| 695 |
|
| 696 |
if ( $matchingCount <= 0 ) { |
| 697 |
|
| 698 |
|
| 699 |
$endText = substr( $text, $i, $count ); |
| 700 |
self::addLiteral( $accum, $endText ); |
| 701 |
$i += $count; |
| 702 |
continue; |
| 703 |
} |
| 704 |
|
| 705 |
$name = $rule['names'][$matchingCount]; |
| 706 |
if ( $name === null ) { |
| 707 |
|
| 708 |
$endText = substr( $text, $i, $matchingCount ); |
| 709 |
$element = $piece->breakSyntax( $matchingCount ); |
| 710 |
self::addLiteral( $element, $endText ); |
| 711 |
} else { |
| 712 |
|
| 713 |
$parts = $piece->parts; |
| 714 |
$titleAccum = $parts[0]->out; |
| 715 |
unset( $parts[0] ); |
| 716 |
|
| 717 |
$children = []; |
| 718 |
|
| 719 |
|
| 720 |
|
| 721 |
if ( $maxCount === $matchingCount && |
| 722 |
$piece->lineStart && |
| 723 |
$piece->savedPrefix === '' |
| 724 |
) { |
| 725 |
$children[] = [ '@lineStart', [ 1 ] ]; |
| 726 |
} |
| 727 |
$titleNode = [ 'title', $titleAccum ]; |
| 728 |
$children[] = $titleNode; |
| 729 |
$argIndex = 1; |
| 730 |
foreach ( $parts as $part ) { |
| 731 |
if ( isset( $part->eqpos ) ) { |
| 732 |
$equalsNode = $part->out[$part->eqpos]; |
| 733 |
$nameNode = [ 'name', array_slice( $part->out, 0, $part->eqpos ) ]; |
| 734 |
$valueNode = [ 'value', array_slice( $part->out, $part->eqpos + 1 ) ]; |
| 735 |
$partNode = [ 'part', [ $nameNode, $equalsNode, $valueNode ] ]; |
| 736 |
$children[] = $partNode; |
| 737 |
} else { |
| 738 |
$nameNode = [ 'name', [ [ '@index', [ $argIndex++ ] ] ] ]; |
| 739 |
$valueNode = [ 'value', $part->out ]; |
| 740 |
$partNode = [ 'part', [ $nameNode, $valueNode ] ]; |
| 741 |
$children[] = $partNode; |
| 742 |
} |
| 743 |
} |
| 744 |
$element = [ [ $name, $children ] ]; |
| 745 |
} |
| 746 |
|
| 747 |
|
| 748 |
$i += $matchingCount; |
| 749 |
|
| 750 |
|
| 751 |
$stack->pop(); |
| 752 |
$accum =& $stack->getAccum(); |
| 753 |
|
| 754 |
|
| 755 |
if ( $matchingCount < $piece->count ) { |
| 756 |
$piece->parts = [ new PPDPart_Hash ]; |
| 757 |
$piece->count -= $matchingCount; |
| 758 |
|
| 759 |
$min = $this->rules[$piece->open]['min']; |
| 760 |
if ( $piece->count >= $min ) { |
| 761 |
$stack->push( $piece ); |
| 762 |
$accum =& $stack->getAccum(); |
| 763 |
} elseif ( $piece->count === 1 && $piece->open === '{' && $piece->savedPrefix === '-' ) { |
| 764 |
$piece->savedPrefix = ''; |
| 765 |
$piece->open = '-{'; |
| 766 |
$piece->count = 2; |
| 767 |
$piece->close = $this->rules[$piece->open]['end']; |
| 768 |
$stack->push( $piece ); |
| 769 |
$accum =& $stack->getAccum(); |
| 770 |
} else { |
| 771 |
$s = substr( $piece->open, 0, -1 ); |
| 772 |
$s .= str_repeat( |
| 773 |
substr( $piece->open, -1 ), |
| 774 |
$piece->count - strlen( $s ) |
| 775 |
); |
| 776 |
self::addLiteral( $accum, $piece->savedPrefix . $s ); |
| 777 |
} |
| 778 |
} elseif ( $piece->savedPrefix !== '' ) { |
| 779 |
self::addLiteral( $accum, $piece->savedPrefix ); |
| 780 |
} |
| 781 |
|
| 782 |
$stackFlags = $stack->getFlags(); |
| 783 |
if ( isset( $stackFlags['findEquals'] ) ) { |
| 784 |
$findEquals = $stackFlags['findEquals']; |
| 785 |
} |
| 786 |
if ( isset( $stackFlags['findPipe'] ) ) { |
| 787 |
$findPipe = $stackFlags['findPipe']; |
| 788 |
} |
| 789 |
if ( isset( $stackFlags['inHeading'] ) ) { |
| 790 |
$inHeading = $stackFlags['inHeading']; |
| 791 |
} |
| 792 |
|
| 793 |
|
| 794 |
array_splice( $accum, count( $accum ), 0, $element ); |
| 795 |
} elseif ( $found === 'pipe' ) { |
| 796 |
$findEquals = true; |
| 797 |
$stack->addPart(); |
| 798 |
$accum =& $stack->getAccum(); |
| 799 |
++$i; |
| 800 |
} elseif ( $found === 'equals' ) { |
| 801 |
$findEquals = false; |
| 802 |
$accum[] = [ 'equals', [ '=' ] ]; |
| 803 |
$stack->getCurrentPart()->eqpos = count( $accum ) - 1; |
| 804 |
++$i; |
| 805 |
} |
| 806 |
} |
| 807 |
|
| 808 |
|
| 809 |
foreach ( $stack->stack as $piece ) { |
| 810 |
array_splice( $stack->rootAccum, count( $stack->rootAccum ), 0, $piece->breakSyntax() ); |
| 811 |
} |
| 812 |
|
| 813 |
|
| 814 |
foreach ( $stack->rootAccum as &$node ) { |
| 815 |
if ( is_array( $node ) && $node[PPNode_Hash_Tree::NAME] === 'possible-h' ) { |
| 816 |
$node[PPNode_Hash_Tree::NAME] = 'h'; |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
return [ [ 'root', $stack->rootAccum ] ]; |
| 821 |
} |
| 822 |
|
| 823 |
private static function addLiteral( array &$accum, $text ) { |
| 824 |
$n = count( $accum ); |
| 825 |
if ( $n && is_string( $accum[$n - 1] ) ) { |
| 826 |
$accum[$n - 1] .= $text; |
| 827 |
} else { |
| 828 |
$accum[] = $text; |
| 829 |
} |
| 830 |
} |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
class_alias( Preprocessor_Hash::class, 'Preprocessor_Hash' ); |