2013-03-04 20:00:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Scribunto_LuaTextLibrary extends Scribunto_LuaLibraryBase {
|
|
|
|
function register() {
|
2013-09-03 20:04:58 +00:00
|
|
|
global $wgUrlProtocols;
|
|
|
|
|
2013-03-04 20:00:59 +00:00
|
|
|
$lib = array(
|
|
|
|
'unstrip' => array( $this, 'textUnstrip' ),
|
|
|
|
'getEntityTable' => array( $this, 'getEntityTable' ),
|
|
|
|
);
|
2013-09-03 20:04:58 +00:00
|
|
|
$opts = array(
|
2013-03-04 20:00:59 +00:00
|
|
|
'comma' => wfMessage( 'comma-separator' )->inContentLanguage()->text(),
|
|
|
|
'and' => wfMessage( 'and' )->inContentLanguage()->text() .
|
|
|
|
wfMessage( 'word-separator' )->inContentLanguage()->text(),
|
|
|
|
'ellipsis' => wfMessage( 'ellipsis' )->inContentLanguage()->text(),
|
2013-09-03 20:04:58 +00:00
|
|
|
'nowiki_protocols' => array(),
|
|
|
|
);
|
|
|
|
|
|
|
|
foreach ( $wgUrlProtocols as $prot ) {
|
|
|
|
if ( substr( $prot, -1 ) === ':' ) {
|
|
|
|
// To convert the protocol into a case-insensitive Lua pattern,
|
|
|
|
// we need to replace letters with a character class like [Xx]
|
|
|
|
// and insert a '%' before various punctuation.
|
|
|
|
$prot = preg_replace_callback( '/([a-zA-Z])|([()^$%.\[\]*+?-])/', function ( $m ) {
|
|
|
|
if ( $m[1] ) {
|
|
|
|
return '[' . strtoupper( $m[1] ) . strtolower( $m[1] ) . ']';
|
|
|
|
} else {
|
|
|
|
return '%' . $m[2];
|
|
|
|
}
|
|
|
|
}, substr( $prot, 0, -1 ) );
|
|
|
|
$opts['nowiki_protocols']["($prot):"] = '%1:';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-09-26 18:11:21 +00:00
|
|
|
return $this->getEngine()->registerInterface( 'mw.text.lua', $lib, $opts );
|
2013-03-04 20:00:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function textUnstrip( $s ) {
|
|
|
|
$this->checkType( 'unstrip', 1, $s, 'string' );
|
|
|
|
return array( $this->getParser()->mStripState->unstripBoth( $s ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
function getEntityTable() {
|
|
|
|
$flags = ENT_QUOTES;
|
|
|
|
// PHP 5.3 compat
|
|
|
|
if ( defined( "ENT_HTML5" ) ) {
|
|
|
|
$flags |= constant( "ENT_HTML5" );
|
|
|
|
}
|
|
|
|
$table = array_flip( get_html_translation_table( HTML_ENTITIES, $flags, "UTF-8" ) );
|
|
|
|
return array( $table );
|
|
|
|
}
|
|
|
|
}
|