2013-03-04 20:00:59 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
class Scribunto_LuaTextLibrary extends Scribunto_LuaLibraryBase {
|
2015-01-26 22:42:19 +00:00
|
|
|
// Matches Lua mw.text constants
|
2020-05-30 00:32:16 +00:00
|
|
|
private const JSON_PRESERVE_KEYS = 1;
|
|
|
|
private const JSON_TRY_FIXING = 2;
|
|
|
|
private const JSON_PRETTY = 4;
|
2015-01-26 22:42:19 +00:00
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
public function register() {
|
2013-09-03 20:04:58 +00:00
|
|
|
global $wgUrlProtocols;
|
|
|
|
|
2017-06-15 17:19:00 +00:00
|
|
|
$lib = [
|
|
|
|
'unstrip' => [ $this, 'textUnstrip' ],
|
|
|
|
'unstripNoWiki' => [ $this, 'textUnstripNoWiki' ],
|
|
|
|
'killMarkers' => [ $this, 'killMarkers' ],
|
|
|
|
'getEntityTable' => [ $this, 'getEntityTable' ],
|
|
|
|
'jsonEncode' => [ $this, 'jsonEncode' ],
|
|
|
|
'jsonDecode' => [ $this, 'jsonDecode' ],
|
|
|
|
];
|
|
|
|
$opts = [
|
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(),
|
2017-06-15 17:19:00 +00:00
|
|
|
'nowiki_protocols' => [],
|
|
|
|
];
|
2013-09-03 20:04:58 +00:00
|
|
|
|
|
|
|
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.
|
2021-05-13 08:16:18 +00:00
|
|
|
$prot = preg_replace_callback( '/([a-zA-Z])|([()^$%.\[\]*+?-])/', static function ( $m ) {
|
2013-09-03 20:04:58 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for textUnstrip
|
|
|
|
* @internal
|
|
|
|
* @param string $s
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function textUnstrip( $s ) {
|
2013-03-04 20:00:59 +00:00
|
|
|
$this->checkType( 'unstrip', 1, $s, 'string' );
|
2021-02-19 21:54:37 +00:00
|
|
|
$stripState = $this->getParser()->getStripState();
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $stripState->killMarkers( $stripState->unstripNoWiki( $s ) ) ];
|
2014-11-05 17:18:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for textUnstripNoWiki
|
|
|
|
* @internal
|
|
|
|
* @param string $s
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function textUnstripNoWiki( $s ) {
|
2014-11-05 17:18:22 +00:00
|
|
|
$this->checkType( 'unstripNoWiki', 1, $s, 'string' );
|
2021-02-19 21:54:37 +00:00
|
|
|
return [ $this->getParser()->getStripState()->unstripNoWiki( $s ) ];
|
2014-11-05 17:18:22 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for killMarkers
|
|
|
|
* @internal
|
|
|
|
* @param string $s
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
public function killMarkers( $s ) {
|
2014-11-05 17:18:22 +00:00
|
|
|
$this->checkType( 'killMarkers', 1, $s, 'string' );
|
2021-02-19 21:54:37 +00:00
|
|
|
return [ $this->getParser()->getStripState()->killMarkers( $s ) ];
|
2013-03-04 20:00:59 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for getEntityTable
|
|
|
|
* @internal
|
|
|
|
* @return array[]
|
|
|
|
*/
|
|
|
|
public function getEntityTable() {
|
2017-08-21 21:16:32 +00:00
|
|
|
$table = array_flip(
|
|
|
|
get_html_translation_table( HTML_ENTITIES, ENT_QUOTES | ENT_HTML5, "UTF-8" )
|
|
|
|
);
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $table ];
|
2013-03-04 20:00:59 +00:00
|
|
|
}
|
2015-01-26 22:42:19 +00:00
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for jsonEncode
|
|
|
|
* @internal
|
|
|
|
* @param mixed $value
|
|
|
|
* @param string|int $flags
|
|
|
|
* @return string[]
|
|
|
|
*/
|
2015-01-26 22:42:19 +00:00
|
|
|
public function jsonEncode( $value, $flags ) {
|
|
|
|
$this->checkTypeOptional( 'mw.text.jsonEncode', 2, $flags, 'number', 0 );
|
|
|
|
$flags = (int)$flags;
|
|
|
|
if ( !( $flags & self::JSON_PRESERVE_KEYS ) && is_array( $value ) ) {
|
|
|
|
$value = self::reindexArrays( $value, true );
|
|
|
|
}
|
|
|
|
$ret = FormatJson::encode( $value, (bool)( $flags & self::JSON_PRETTY ), FormatJson::ALL_OK );
|
|
|
|
if ( $ret === false ) {
|
|
|
|
throw new Scribunto_LuaError( 'mw.text.jsonEncode: Unable to encode value' );
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $ret ];
|
2015-01-26 22:42:19 +00:00
|
|
|
}
|
|
|
|
|
2018-11-09 19:31:08 +00:00
|
|
|
/**
|
|
|
|
* Handler for jsonDecode
|
|
|
|
* @internal
|
|
|
|
* @param string $s
|
|
|
|
* @param string|int $flags
|
|
|
|
* @return array
|
|
|
|
*/
|
2015-01-26 22:42:19 +00:00
|
|
|
public function jsonDecode( $s, $flags ) {
|
|
|
|
$this->checkType( 'mw.text.jsonDecode', 1, $s, 'string' );
|
|
|
|
$this->checkTypeOptional( 'mw.text.jsonDecode', 2, $flags, 'number', 0 );
|
|
|
|
$flags = (int)$flags;
|
|
|
|
$opts = FormatJson::FORCE_ASSOC;
|
|
|
|
if ( $flags & self::JSON_TRY_FIXING ) {
|
|
|
|
$opts |= FormatJson::TRY_FIXING;
|
|
|
|
}
|
|
|
|
$status = FormatJson::parse( $s, $opts );
|
|
|
|
if ( !$status->isOk() ) {
|
|
|
|
throw new Scribunto_LuaError( 'mw.text.jsonDecode: ' . $status->getMessage()->text() );
|
|
|
|
}
|
|
|
|
$val = $status->getValue();
|
|
|
|
if ( !( $flags & self::JSON_PRESERVE_KEYS ) && is_array( $val ) ) {
|
|
|
|
$val = self::reindexArrays( $val, false );
|
|
|
|
}
|
2017-06-15 17:19:00 +00:00
|
|
|
return [ $val ];
|
2015-01-26 22:42:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/** Recursively reindex array with integer keys to 0-based or 1-based
|
|
|
|
* @param array $arr
|
|
|
|
* @param bool $isEncoding
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
private static function reindexArrays( array $arr, $isEncoding ) {
|
|
|
|
if ( $isEncoding ) {
|
|
|
|
ksort( $arr, SORT_NUMERIC );
|
|
|
|
$next = 1;
|
|
|
|
} else {
|
|
|
|
$next = 0;
|
|
|
|
}
|
|
|
|
$isSequence = true;
|
|
|
|
foreach ( $arr as $k => &$v ) {
|
|
|
|
if ( is_array( $v ) ) {
|
|
|
|
$v = self::reindexArrays( $v, $isEncoding );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $isSequence ) {
|
|
|
|
if ( is_int( $k ) ) {
|
|
|
|
$isSequence = $next++ === $k;
|
|
|
|
} elseif ( $isEncoding && ctype_digit( $k ) ) {
|
|
|
|
// json_decode currently doesn't return integer keys for {}
|
|
|
|
$isSequence = $next++ === (int)$k;
|
|
|
|
} else {
|
|
|
|
$isSequence = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( $isSequence ) {
|
|
|
|
if ( $isEncoding ) {
|
|
|
|
return array_values( $arr );
|
|
|
|
} else {
|
|
|
|
return $arr ? array_combine( range( 1, count( $arr ) ), $arr ) : $arr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $arr;
|
|
|
|
}
|
|
|
|
|
2013-03-04 20:00:59 +00:00
|
|
|
}
|