| 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 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
public function anchorEncode( $s ) { |
| 30 |
return [ CoreParserFunctions::anchorencode( |
| 31 |
$this->getParser(), $s |
| 32 |
) ]; |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 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 |
|
| 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 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
public function localUrl( $page, $query ) { |
| 71 |
return $this->getUrl( 'getLocalURL', $page, $query ); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
public function fullUrl( $page, $query ) { |
| 82 |
return $this->getUrl( 'getFullURL', $page, $query ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public function canonicalUrl( $page, $query ) { |
| 93 |
return $this->getUrl( 'getCanonicalURL', $page, $query ); |
| 94 |
} |
| 95 |
} |