Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
88.89% |
24 / 27 |
|
83.33% |
5 / 6 |
CRAP | |
0.00% |
0 / 1 |
UriLibrary | |
88.89% |
24 / 27 |
|
83.33% |
5 / 6 |
11.17 | |
0.00% |
0 / 1 |
register | |
100.00% |
10 / 10 |
|
100.00% |
1 / 1 |
2 | |||
anchorEncode | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
getUrl | |
72.73% |
8 / 11 |
|
0.00% |
0 / 1 |
5.51 | |||
localUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
fullUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
canonicalUrl | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | |
3 | namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon; |
4 | |
5 | use MediaWiki\Parser\CoreParserFunctions; |
6 | use MediaWiki\Title\Title; |
7 | |
8 | class UriLibrary extends LibraryBase { |
9 | public function register() { |
10 | $lib = [ |
11 | 'anchorEncode' => [ $this, 'anchorEncode' ], |
12 | 'localUrl' => [ $this, 'localUrl' ], |
13 | 'fullUrl' => [ $this, 'fullUrl' ], |
14 | 'canonicalUrl' => [ $this, 'canonicalUrl' ], |
15 | ]; |
16 | |
17 | $title = $this->getTitle(); |
18 | return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, [ |
19 | 'defaultUrl' => $title ? $title->getFullUrl() : null, |
20 | ] ); |
21 | } |
22 | |
23 | /** |
24 | * Handler for anchorEncode |
25 | * @internal |
26 | * @param string $s |
27 | * @return string[] |
28 | */ |
29 | public function anchorEncode( $s ) { |
30 | return [ CoreParserFunctions::anchorencode( |
31 | $this->getParser(), $s |
32 | ) ]; |
33 | } |
34 | |
35 | /** |
36 | * Get a URL (helper for handlers) |
37 | * @param string $func Title class method to call |
38 | * @param string $page Page title |
39 | * @param array $query Query string |
40 | * @return string[]|null[] |
41 | */ |
42 | private function getUrl( $func, $page, $query ) { |
43 | $title = Title::newFromText( $page ); |
44 | if ( !$title ) { |
45 | $title = Title::newFromURL( urldecode( $page ) ); |
46 | } |
47 | if ( $title ) { |
48 | # Convert NS_MEDIA -> NS_FILE |
49 | if ( $title->getNamespace() === NS_MEDIA ) { |
50 | $title = Title::makeTitle( NS_FILE, $title->getDBkey() ); |
51 | } |
52 | if ( $query !== null ) { |
53 | $text = $title->$func( $query ); |
54 | } else { |
55 | $text = $title->$func(); |
56 | } |
57 | return [ $text ]; |
58 | } else { |
59 | return [ null ]; |
60 | } |
61 | } |
62 | |
63 | /** |
64 | * Handler for localUrl |
65 | * @internal |
66 | * @param string $page |
67 | * @param array $query |
68 | * @return string[]|null[] |
69 | */ |
70 | public function localUrl( $page, $query ) { |
71 | return $this->getUrl( 'getLocalURL', $page, $query ); |
72 | } |
73 | |
74 | /** |
75 | * Handler for fullUrl |
76 | * @internal |
77 | * @param string $page |
78 | * @param array $query |
79 | * @return string[]|null[] |
80 | */ |
81 | public function fullUrl( $page, $query ) { |
82 | return $this->getUrl( 'getFullURL', $page, $query ); |
83 | } |
84 | |
85 | /** |
86 | * Handler for canonicalUrl |
87 | * @internal |
88 | * @param string $page |
89 | * @param array $query |
90 | * @return string[]|null[] |
91 | */ |
92 | public function canonicalUrl( $page, $query ) { |
93 | return $this->getUrl( 'getCanonicalURL', $page, $query ); |
94 | } |
95 | } |