mediawiki-extensions-Scribunto/engines/LuaCommon/UriLibrary.php
Brad Jorsch df38a296bf Allow for dynamically-loaded PHP libraries
Scribunto currently supports libraries with PHP callbacks that are
loaded on startup, and pure-Lua libraries that may be loaded from the
module with require().

This change allows for libraries with PHP callbacks to also be loaded
with require().

Change-Id: Ibdc1f4ef51b1c8644c3d4c98d57755b5c06447a5
2014-10-03 09:27:23 -04:00

56 lines
1.4 KiB
PHP

<?php
class Scribunto_LuaUriLibrary extends Scribunto_LuaLibraryBase {
function register() {
$lib = array(
'anchorEncode' => array( $this, 'anchorEncode' ),
'localUrl' => array( $this, 'localUrl' ),
'fullUrl' => array( $this, 'fullUrl' ),
'canonicalUrl' => array( $this, 'canonicalUrl' ),
);
return $this->getEngine()->registerInterface( 'mw.uri.lua', $lib, array(
'defaultUrl' => $this->getTitle()->getFullUrl(),
) );
}
public function anchorEncode( $s ) {
return array( CoreParserFunctions::anchorencode(
$this->getParser(), $s
) );
}
private function getUrl( $func, $page, $query ) {
$title = Title::newFromText( $page );
if( !$title ) {
$title = Title::newFromURL( urldecode( $page ) );
}
if( $title ) {
# Convert NS_MEDIA -> NS_FILE
if( $title->getNamespace() == NS_MEDIA ) {
$title = Title::makeTitle( NS_FILE, $title->getDBkey() );
}
if( $query !== null ) {
$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 );
}
}