Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
83.33% |
15 / 18 |
|
0.00% |
0 / 2 |
CRAP | |
0.00% |
0 / 1 |
UrlHelper | |
83.33% |
15 / 18 |
|
0.00% |
0 / 2 |
7.23 | |
0.00% |
0 / 1 |
followRedirect | |
85.71% |
6 / 7 |
|
0.00% |
0 / 1 |
3.03 | |||
getBaseUrl | |
81.82% |
9 / 11 |
|
0.00% |
0 / 1 |
4.10 |
1 | <?php |
2 | |
3 | namespace MediaWiki\MassMessage; |
4 | |
5 | use MediaWiki\MediaWikiServices; |
6 | use MediaWiki\Title\Title; |
7 | |
8 | class UrlHelper { |
9 | |
10 | /** |
11 | * Function to follow redirects. |
12 | * |
13 | * @param Title $title |
14 | * @return Title|null null if the page is an interwiki redirect |
15 | */ |
16 | public static function followRedirect( Title $title ) { |
17 | if ( !$title->isRedirect() ) { |
18 | return $title; |
19 | } |
20 | $wikipage = MediaWikiServices::getInstance()->getWikiPageFactory()->newFromTitle( $title ); |
21 | |
22 | $target = $wikipage->followRedirect(); |
23 | if ( $target instanceof Title ) { |
24 | return $target; |
25 | } |
26 | |
27 | // This must be an interwiki redirect |
28 | return null; |
29 | } |
30 | |
31 | /** |
32 | * Returns the basic hostname and port using UrlUtils::parse. |
33 | * |
34 | * @param string $url |
35 | * @return string|null |
36 | */ |
37 | public static function getBaseUrl( $url ) { |
38 | static $mapping = []; |
39 | |
40 | if ( isset( $mapping[$url] ) ) { |
41 | return $mapping[$url]; |
42 | } |
43 | |
44 | $urlUtils = MediaWikiServices::getInstance()->getUrlUtils(); |
45 | $parse = $urlUtils->parse( $url ); |
46 | if ( $parse === null ) { |
47 | return null; |
48 | } |
49 | $mapping[$url] = $parse['host']; |
50 | if ( isset( $parse['port'] ) ) { |
51 | $mapping[$url] .= ':' . $parse['port']; |
52 | } |
53 | return $mapping[$url]; |
54 | } |
55 | } |