Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
60.47% covered (warning)
60.47%
26 / 43
0.00% covered (danger)
0.00%
0 / 1
CRAP
0.00% covered (danger)
0.00%
0 / 1
AddLinkAttributes
60.47% covered (warning)
60.47%
26 / 43
0.00% covered (danger)
0.00%
0 / 1
41.31
0.00% covered (danger)
0.00%
0 / 1
 handler
60.47% covered (warning)
60.47%
26 / 43
0.00% covered (danger)
0.00%
0 / 1
41.31
1<?php
2declare( strict_types = 1 );
3
4namespace Wikimedia\Parsoid\Wt2Html\DOM\Handlers;
5
6use Wikimedia\Parsoid\Core\DOMCompat;
7use Wikimedia\Parsoid\DOM\Element;
8use Wikimedia\Parsoid\Utils\DOMDataUtils;
9use Wikimedia\Parsoid\Utils\DOMUtils;
10use Wikimedia\Parsoid\Utils\DTState;
11use Wikimedia\Parsoid\Utils\WTUtils;
12
13class AddLinkAttributes {
14    /**
15     * Adds classes to external links and interwiki links
16     */
17    public static function handler( Element $a, DTState $state ): bool {
18        if ( DOMUtils::hasRel( $a, "mw:ExtLink" ) ) {
19            // NOTE: This pass is running before UnpackDOMFragments pass.
20            // So, this first child can be a mw:DOMFragment that hides
21            // another a-link. So, when the child fragment is unpacked,
22            // $a can split into two nodes with the first node going empty.
23            // So, this link attribute assignment is more a reflection of
24            // wikitext intention, and less the final output. This assignment
25            // matches legacy output, and it is debatable which is "correct".
26            // Since link-in-link is any a linter error, we are okay sticking
27            // with whatever falls out of the implementation.
28            if ( $a->firstChild ) {
29                // The "external free" class is reserved for links which
30                // are syntactically unbracketed; see commit
31                // 65fcb7a94528ea56d461b3c7b9cb4d4fe4e99211 in core.
32                if ( WTUtils::isATagFromURLLinkSyntax( $a ) ) {
33                    $classInfoText = 'external free';
34                } elseif ( WTUtils::isATagFromMagicLinkSyntax( $a ) ) {
35                    // Handled below
36                    $classInfoText = null;
37                } else {
38                    $classInfoText = 'external text';
39                }
40            } else {
41                $classInfoText = 'external autonumber';
42            }
43            if ( $classInfoText !== null ) {
44                $a->setAttribute( 'class', $classInfoText );
45            }
46            $href = DOMCompat::getAttribute( $a, 'href' ) ?? '';
47            $attribs = $state->env->getExternalLinkAttribs( $href );
48            if ( ( $attribs['href'] ?? null ) === null ) {
49                // Blocked link: T420043
50                $a->removeAttribute( 'href' );
51            }
52            foreach ( $attribs as $key => $val ) {
53                if ( $key === 'rel' ) {
54                    foreach ( $val as $v ) {
55                        DOMUtils::addRel( $a, $v );
56                    }
57                } elseif ( $key === 'class' ) {
58                    DOMCompat::getClassList( $a )->add( ...$val );
59                } else {
60                    $a->setAttribute( $key, $val );
61                }
62            }
63            if ( $attribs['href'] ?? null ) {
64                $state->env->getMetadata()->addExternalLink( $attribs['href'] );
65            }
66        } elseif (
67            DOMUtils::hasRel( $a, 'mw:WikiLink' ) ||
68            DOMUtils::hasRel( $a, 'mw:WikiLink/Interwiki' )
69        ) {
70            if ( DOMUtils::hasRel( $a, 'mw:WikiLink/Interwiki' ) ) {
71                DOMCompat::getClassList( $a )->add( 'extiw' );
72            }
73            $titleAttr = DOMCompat::getAttribute( $a, 'title' );
74            if ( $titleAttr !== null ) {
75                $title = $state->env->makeTitleFromText( $titleAttr, null, true );
76                if ( $title !== null ) {
77                    // T357048: Always pass a page id to ::addLink so that it
78                    // doesn't kill performance by looking one up.
79                    $state->env->getMetadata()->addLink( $title, 0 );
80                }
81            }
82        }
83
84        if ( WTUtils::isATagFromMagicLinkSyntax( $a ) ) {
85            $ref = DOMDataUtils::getDataParsoid( $a )->getTemp()->ref;
86            $classInfoText = ( $ref === 'isbn' ) ? 'internal' : 'external';
87            // "mw-magiclink" here is for backwards compatibility
88            // with what Parsoid output prior to T329347
89            $classInfoText .= " mw-magiclink mw-magiclink-{$ref}";
90            $a->setAttribute( 'class', $classInfoText );
91            $state->env->getDataAccess()->addTrackingCategory(
92                $state->env->getPageConfig(),
93                $state->env->getMetadata(),
94                "magiclink-tracking-{$ref}"
95            );
96        }
97
98        return true;
99    }
100}