2013-01-26 00:09:50 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Scribunto_LuaUriLibrary extends Scribunto_LuaLibraryBase {
|
2018-11-09 19:31:08 +00:00
|
|
|
public function register() {
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib = [
|
|
|
|
'anchorEncode' => [ $this, 'anchorEncode' ],
|
|
|
|
'localUrl' => [ $this, 'localUrl' ],
|
|
|
|
'fullUrl' => [ $this, 'fullUrl' ],
|
|
|
|
'canonicalUrl' => [ $this, 'canonicalUrl' ],
|
|
|
|
];
|
2013-02-07 18:51:41 +00:00
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, [
|
2013-02-07 18:51:41 +00:00
|
|
|
'defaultUrl' => $this->getTitle()->getFullUrl(),
|
2017-06-15 17:19:00 +00:00
|
|
|
] );
|
2013-01-26 00:09:50 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for anchorEncode
|
|
|
|
* @internal
|
|
|
|
* @param string $s
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2013-01-26 00:09:50 +00:00
|
|
|
public function anchorEncode( $s ) {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ CoreParserFunctions::anchorencode(
|
2013-01-26 00:09:50 +00:00
|
|
|
$this->getParser(), $s
|
2017-06-15 17:19:00 +00:00
|
|
|
) ];
|
2013-01-26 00:09:50 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 18:50:34 +00:00
|
|
|
/**
|
|
|
|
* Get a URL (helper for handlers)
|
|
|
|
* @param string $func Title class method to call
|
|
|
|
* @param string $page Page title
|
|
|
|
* @param array $query Query string
|
|
|
|
* @return string[]|null[]
|
|
|
|
*/
|
2013-01-26 00:09:50 +00:00
|
|
|
private function getUrl( $func, $page, $query ) {
|
|
|
|
$title = Title::newFromText( $page );
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( !$title ) {
|
2013-01-26 00:09:50 +00:00
|
|
|
$title = Title::newFromURL( urldecode( $page ) );
|
|
|
|
}
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( $title ) {
|
2013-01-26 00:09:50 +00:00
|
|
|
# Convert NS_MEDIA -> NS_FILE
|
2021-01-01 16:44:40 +00:00
|
|
|
if ( $title->getNamespace() === NS_MEDIA ) {
|
2013-01-26 00:09:50 +00:00
|
|
|
$title = Title::makeTitle( NS_FILE, $title->getDBkey() );
|
|
|
|
}
|
2015-06-21 04:38:39 +00:00
|
|
|
if ( $query !== null ) {
|
2013-01-26 00:09:50 +00:00
|
|
|
$text = $title->$func( $query );
|
|
|
|
} else {
|
|
|
|
$text = $title->$func();
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $text ];
|
2013-01-26 00:09:50 +00:00
|
|
|
} else {
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ null ];
|
2013-01-26 00:09:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for localUrl
|
|
|
|
* @internal
|
|
|
|
* @param string $page
|
|
|
|
* @param array $query
|
|
|
|
* @return string[]|null[]
|
|
|
|
*/
|
2013-01-26 00:09:50 +00:00
|
|
|
public function localUrl( $page, $query ) {
|
|
|
|
return $this->getUrl( 'getLocalURL', $page, $query );
|
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for fullUrl
|
|
|
|
* @internal
|
|
|
|
* @param string $page
|
|
|
|
* @param array $query
|
|
|
|
* @return string[]|null[]
|
|
|
|
*/
|
2013-01-26 00:09:50 +00:00
|
|
|
public function fullUrl( $page, $query ) {
|
|
|
|
return $this->getUrl( 'getFullURL', $page, $query );
|
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for canonicalUrl
|
|
|
|
* @internal
|
|
|
|
* @param string $page
|
|
|
|
* @param array $query
|
|
|
|
* @return string[]|null[]
|
|
|
|
*/
|
2013-01-26 00:09:50 +00:00
|
|
|
public function canonicalUrl( $page, $query ) {
|
|
|
|
return $this->getUrl( 'getCanonicalURL', $page, $query );
|
|
|
|
}
|
|
|
|
}
|