2013-01-26 00:09:50 +00:00
|
|
|
<?php
|
|
|
|
|
2016-05-17 14:52:05 +00:00
|
|
|
// @codingStandardsIgnoreLine Squiz.Classes.ValidClassName.NotCamelCaps
|
2013-01-26 00:09:50 +00:00
|
|
|
class Scribunto_LuaUriLibrary extends Scribunto_LuaLibraryBase {
|
2013-03-04 19:55:46 +00:00
|
|
|
function register() {
|
2013-01-26 00:09:50 +00:00
|
|
|
$lib = array(
|
|
|
|
'anchorEncode' => array( $this, 'anchorEncode' ),
|
|
|
|
'localUrl' => array( $this, 'localUrl' ),
|
|
|
|
'fullUrl' => array( $this, 'fullUrl' ),
|
|
|
|
'canonicalUrl' => array( $this, 'canonicalUrl' ),
|
|
|
|
);
|
2013-02-07 18:51:41 +00:00
|
|
|
|
2014-09-26 18:11:21 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, array(
|
2013-02-07 18:51:41 +00:00
|
|
|
'defaultUrl' => $this->getTitle()->getFullUrl(),
|
2013-01-26 00:09:50 +00:00
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function anchorEncode( $s ) {
|
|
|
|
return array( CoreParserFunctions::anchorencode(
|
|
|
|
$this->getParser(), $s
|
|
|
|
) );
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-06-21 04:38:39 +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();
|
|
|
|
}
|
|
|
|
return array( $text );
|
|
|
|
} else {
|
|
|
|
return array( null );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function localUrl( $page, $query ) {
|
|
|
|
return $this->getUrl( 'getLocalURL', $page, $query );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function fullUrl( $page, $query ) {
|
|
|
|
return $this->getUrl( 'getFullURL', $page, $query );
|
|
|
|
}
|
|
|
|
|
|
|
|
public function canonicalUrl( $page, $query ) {
|
|
|
|
return $this->getUrl( 'getCanonicalURL', $page, $query );
|
|
|
|
}
|
|
|
|
}
|