Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
95.00% |
19 / 20 |
|
50.00% |
1 / 2 |
CRAP | |
0.00% |
0 / 1 |
RemoveRedLinks | |
95.00% |
19 / 20 |
|
50.00% |
1 / 2 |
10 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
run | |
100.00% |
19 / 19 |
|
100.00% |
1 / 1 |
9 |
1 | <?php |
2 | declare( strict_types = 1 ); |
3 | |
4 | namespace Wikimedia\Parsoid\Html2Wt; |
5 | |
6 | use Wikimedia\Parsoid\Config\Env; |
7 | use Wikimedia\Parsoid\DOM\Element; |
8 | use Wikimedia\Parsoid\DOM\Node; |
9 | use Wikimedia\Parsoid\Utils\DOMCompat; |
10 | use Wikimedia\Parsoid\Utils\UrlUtils; |
11 | |
12 | class RemoveRedLinks { |
13 | |
14 | /** @suppress PhanEmptyPublicMethod */ |
15 | public function __construct( Env $env ) { |
16 | } |
17 | |
18 | /** |
19 | * Remove redlinks from a document |
20 | * @param Element $root |
21 | */ |
22 | public function run( Node $root ): void { |
23 | '@phan-var Element|DocumentFragment $root'; // @var Element|DocumentFragment $root |
24 | $wikilinks = DOMCompat::querySelectorAll( $root, 'a[rel~="mw:WikiLink"].new' ); |
25 | foreach ( $wikilinks as $a ) { |
26 | $href = DOMCompat::getAttribute( $a, 'href' ); |
27 | $qmPos = strpos( $href ?? '', '?' ); |
28 | if ( $qmPos !== false ) { |
29 | $parsedURL = UrlUtils::parseUrl( $href ); |
30 | if ( isset( $parsedURL['query'] ) ) { |
31 | $queryParams = $parsedURL['query']; |
32 | $queryElts = []; |
33 | parse_str( $queryParams, $queryElts ); |
34 | if ( isset( $queryElts['action'] ) && $queryElts['action'] === 'edit' ) { |
35 | unset( $queryElts['action'] ); |
36 | } |
37 | if ( isset( $queryElts['redlink'] ) && $queryElts['redlink'] === '1' ) { |
38 | unset( $queryElts['redlink'] ); |
39 | } |
40 | |
41 | // My understanding of this method and of PHP array handling makes me |
42 | // believe that the order of the parameters should not be modified here. |
43 | // There is however no guarantee whatsoever in the documentation or spec |
44 | // of these methods. |
45 | |
46 | if ( count( $queryElts ) === 0 ) { |
47 | // avoids the insertion of ? on empty query string |
48 | $parsedURL['query'] = null; |
49 | } else { |
50 | $parsedURL['query'] = http_build_query( $queryElts ); |
51 | } |
52 | $href = UrlUtils::assembleUrl( $parsedURL ); |
53 | } |
54 | $a->setAttribute( 'href', $href ); |
55 | } |
56 | } |
57 | } |
58 | } |