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