Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
80.00% |
8 / 10 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| BaseHrefFixer | |
80.00% |
8 / 10 |
|
66.67% |
2 / 3 |
5.20 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| getXPath | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| apply | |
71.43% |
5 / 7 |
|
0.00% |
0 / 1 |
3.21 | |||
| 1 | <?php |
| 2 | |
| 3 | namespace Flow\Parsoid\Fixer; |
| 4 | |
| 5 | use Flow\Parsoid\Fixer; |
| 6 | use MediaWiki\Title\Title; |
| 7 | use MediaWiki\Utils\UrlUtils; |
| 8 | |
| 9 | /** |
| 10 | * Parsoid markup expects a <base href> of //domain/wiki/ . |
| 11 | * However, this would have to be added in the <head> and apply |
| 12 | * to the whole page, which could affect other content. |
| 13 | * |
| 14 | * For now, we just apply this transformation to our own user |
| 15 | * Parsoid content. It does not need to be done for WikiLink, since |
| 16 | * that is handled by WikiLinkFixer in another way. |
| 17 | */ |
| 18 | class BaseHrefFixer implements Fixer { |
| 19 | /** |
| 20 | * @var string |
| 21 | */ |
| 22 | protected $baseHref; |
| 23 | |
| 24 | /** |
| 25 | * @param string $articlePath path setting for wiki |
| 26 | * @param UrlUtils $urlUtils injected url utilities |
| 27 | */ |
| 28 | public function __construct( string $articlePath, UrlUtils $urlUtils ) { |
| 29 | $replacedArticlePath = str_replace( '$1', '', $articlePath ); |
| 30 | $this->baseHref = $urlUtils->expand( $replacedArticlePath, PROTO_RELATIVE ) ?? ''; |
| 31 | } |
| 32 | |
| 33 | /** |
| 34 | * Returns XPath matching elements that need to be transformed |
| 35 | * |
| 36 | * @return string XPath of elements this acts on |
| 37 | */ |
| 38 | public function getXPath() { |
| 39 | // WikiLinkFixer handles mw:WikiLink |
| 40 | return '//a[@href and not(contains(concat(" ",normalize-space(@rel)," ")," mw:WikiLink "))]'; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Prefixes the href with base href. |
| 45 | * |
| 46 | * @param \DOMNode $node Link |
| 47 | * @param Title $title |
| 48 | */ |
| 49 | public function apply( \DOMNode $node, Title $title ) { |
| 50 | if ( !$node instanceof \DOMElement ) { |
| 51 | return; |
| 52 | } |
| 53 | |
| 54 | $href = $node->getAttribute( 'href' ); |
| 55 | if ( !str_starts_with( $href, './' ) ) { |
| 56 | // If we need to handle more complex cases, we should resolve it |
| 57 | // with a library like Net_URL2. This check will then be |
| 58 | // unnecessary. |
| 59 | return; |
| 60 | } |
| 61 | |
| 62 | $href = $this->baseHref . $href; |
| 63 | $node->setAttribute( 'href', $href ); |
| 64 | } |
| 65 | } |