2013-01-26 00:09:50 +00:00
|
|
|
<?php
|
|
|
|
|
2022-08-03 12:31:15 +00:00
|
|
|
namespace MediaWiki\Extension\Scribunto\Engines\LuaCommon;
|
|
|
|
|
|
|
|
use CoreParserFunctions;
|
2023-08-19 04:19:07 +00:00
|
|
|
use MediaWiki\Title\Title;
|
2022-08-03 12:31:15 +00:00
|
|
|
|
|
|
|
class UriLibrary extends LibraryBase {
|
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
|
|
|
|
2020-02-21 20:49:51 +00:00
|
|
|
$title = $this->getTitle();
|
2017-06-15 17:19:00 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, [
|
2020-02-21 20:49:51 +00:00
|
|
|
'defaultUrl' => $title ? $title->getFullUrl() : null,
|
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 );
|
|
|
|
}
|
|
|
|
}
|