Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
2 / 2 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| WikitextParser | |
100.00% |
2 / 2 |
|
100.00% |
2 / 2 |
4 | |
100.00% |
1 / 1 |
| parseWikitext | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| parse | n/a |
0 / 0 |
n/a |
0 / 0 |
0 | |||||
| needsParsing | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Kartographer; |
| 4 | |
| 5 | /** |
| 6 | * @license MIT |
| 7 | */ |
| 8 | abstract class WikitextParser { |
| 9 | |
| 10 | /** |
| 11 | * @param string $wikiText |
| 12 | * @return string Fully parsed HTML |
| 13 | */ |
| 14 | final public function parseWikitext( string $wikiText ): string { |
| 15 | return $this->needsParsing( $wikiText ) ? $this->parse( $wikiText ) : $wikiText; |
| 16 | } |
| 17 | |
| 18 | abstract protected function parse( string $wikiText ): string; |
| 19 | |
| 20 | private function needsParsing( string $wikiText ): bool { |
| 21 | // Skip expensive parser calls when there is no wikitext syntax to parse. This is not |
| 22 | // uncommon in this context. wfEscapeWikiText() is ~400 times faster than the core parser, |
| 23 | // which means 1 non-wikitext string in 400 is already worth the extra check. Still escaping |
| 24 | // becomes expensive the longer the string is. Assume long strings are wikitext. |
| 25 | return ( strlen( $wikiText ) >= 65536 || wfEscapeWikiText( $wikiText ) !== $wikiText ); |
| 26 | } |
| 27 | } |