Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExtLinkFixer
0.00% covered (danger)
0.00%
0 / 11
0.00% covered (danger)
0.00%
0 / 2
90
0.00% covered (danger)
0.00%
0 / 1
 getXPath
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 apply
0.00% covered (danger)
0.00%
0 / 10
0.00% covered (danger)
0.00%
0 / 1
72
1<?php
2
3namespace Flow\Parsoid\Fixer;
4
5use Flow\Parsoid\Fixer;
6use MediaWiki\Title\Title;
7
8/**
9 * Parsoid markup didn't always contain class="external" and rel="nofollow" where appropriate.
10 * This is needed for correct styling and to ensure proper indexing,
11 * so we add them here if they are missing.
12 */
13class ExtLinkFixer implements Fixer {
14    /**
15     * Returns XPath matching elements that need to be transformed
16     *
17     * @return string XPath of elements this acts on
18     */
19    public function getXPath() {
20        return '//a[contains(concat(" ",normalize-space(@rel)," ")," mw:ExtLink ")]';
21    }
22
23    /**
24     * Adds class="external" & rel="nofollow" to external links.
25     *
26     * @param \DOMNode $node Link
27     * @param Title $title
28     */
29    public function apply( \DOMNode $node, Title $title ) {
30        if ( !$node instanceof \DOMElement ) {
31            return;
32        }
33        $nodeClass = $node->getAttribute( 'class' );
34        if ( strpos( ' ' . $nodeClass . ' ', ' external ' ) === false ) {
35            $node->setAttribute( 'class', 'external' .
36                ( $nodeClass !== '' ? ' ' . $nodeClass : '' ) );
37        }
38
39        global $wgNoFollowLinks, $wgNoFollowDomainExceptions;
40        if ( $wgNoFollowLinks && !wfMatchesDomainList( $node->getAttribute( 'href' ), $wgNoFollowDomainExceptions ) ) {
41            $oldRel = $node->getAttribute( 'rel' );
42            if ( strpos( ' ' . $oldRel . ' ', ' nofollow ' ) === false ) {
43                $node->setAttribute( 'rel', 'nofollow' . ( $oldRel !== '' ? ' ' . $oldRel : '' ) );
44            }
45        }
46    }
47}