MediaWiki REL1_34
TextLibrary.php
Go to the documentation of this file.
1<?php
2
4 // Matches Lua mw.text constants
6 const JSON_TRY_FIXING = 2;
7 const JSON_PRETTY = 4;
8
9 public function register() {
10 global $wgUrlProtocols;
11
12 $lib = [
13 'unstrip' => [ $this, 'textUnstrip' ],
14 'unstripNoWiki' => [ $this, 'textUnstripNoWiki' ],
15 'killMarkers' => [ $this, 'killMarkers' ],
16 'getEntityTable' => [ $this, 'getEntityTable' ],
17 'jsonEncode' => [ $this, 'jsonEncode' ],
18 'jsonDecode' => [ $this, 'jsonDecode' ],
19 ];
20 $opts = [
21 'comma' => wfMessage( 'comma-separator' )->inContentLanguage()->text(),
22 'and' => wfMessage( 'and' )->inContentLanguage()->text() .
23 wfMessage( 'word-separator' )->inContentLanguage()->text(),
24 'ellipsis' => wfMessage( 'ellipsis' )->inContentLanguage()->text(),
25 'nowiki_protocols' => [],
26 ];
27
28 foreach ( $wgUrlProtocols as $prot ) {
29 if ( substr( $prot, -1 ) === ':' ) {
30 // To convert the protocol into a case-insensitive Lua pattern,
31 // we need to replace letters with a character class like [Xx]
32 // and insert a '%' before various punctuation.
33 $prot = preg_replace_callback( '/([a-zA-Z])|([()^$%.\[\]*+?-])/', function ( $m ) {
34 if ( $m[1] ) {
35 return '[' . strtoupper( $m[1] ) . strtolower( $m[1] ) . ']';
36 } else {
37 return '%' . $m[2];
38 }
39 }, substr( $prot, 0, -1 ) );
40 $opts['nowiki_protocols']["($prot):"] = '%1&#58;';
41 }
42 }
43
44 return $this->getEngine()->registerInterface( 'mw.text.lua', $lib, $opts );
45 }
46
53 public function textUnstrip( $s ) {
54 $this->checkType( 'unstrip', 1, $s, 'string' );
55 $stripState = $this->getParser()->mStripState;
56 return [ $stripState->killMarkers( $stripState->unstripNoWiki( $s ) ) ];
57 }
58
65 public function textUnstripNoWiki( $s ) {
66 $this->checkType( 'unstripNoWiki', 1, $s, 'string' );
67 return [ $this->getParser()->mStripState->unstripNoWiki( $s ) ];
68 }
69
76 public function killMarkers( $s ) {
77 $this->checkType( 'killMarkers', 1, $s, 'string' );
78 return [ $this->getParser()->mStripState->killMarkers( $s ) ];
79 }
80
86 public function getEntityTable() {
87 $table = array_flip(
88 get_html_translation_table( HTML_ENTITIES, ENT_QUOTES | ENT_HTML5, "UTF-8" )
89 );
90 return [ $table ];
91 }
92
100 public function jsonEncode( $value, $flags ) {
101 $this->checkTypeOptional( 'mw.text.jsonEncode', 2, $flags, 'number', 0 );
102 $flags = (int)$flags;
103 if ( !( $flags & self::JSON_PRESERVE_KEYS ) && is_array( $value ) ) {
104 $value = self::reindexArrays( $value, true );
105 }
106 $ret = FormatJson::encode( $value, (bool)( $flags & self::JSON_PRETTY ), FormatJson::ALL_OK );
107 if ( $ret === false ) {
108 throw new Scribunto_LuaError( 'mw.text.jsonEncode: Unable to encode value' );
109 }
110 return [ $ret ];
111 }
112
120 public function jsonDecode( $s, $flags ) {
121 $this->checkType( 'mw.text.jsonDecode', 1, $s, 'string' );
122 $this->checkTypeOptional( 'mw.text.jsonDecode', 2, $flags, 'number', 0 );
123 $flags = (int)$flags;
124 $opts = FormatJson::FORCE_ASSOC;
125 if ( $flags & self::JSON_TRY_FIXING ) {
126 $opts |= FormatJson::TRY_FIXING;
127 }
128 $status = FormatJson::parse( $s, $opts );
129 if ( !$status->isOk() ) {
130 throw new Scribunto_LuaError( 'mw.text.jsonDecode: ' . $status->getMessage()->text() );
131 }
132 $val = $status->getValue();
133 if ( !( $flags & self::JSON_PRESERVE_KEYS ) && is_array( $val ) ) {
134 $val = self::reindexArrays( $val, false );
135 }
136 return [ $val ];
137 }
138
144 private static function reindexArrays( array $arr, $isEncoding ) {
145 if ( $isEncoding ) {
146 ksort( $arr, SORT_NUMERIC );
147 $next = 1;
148 } else {
149 $next = 0;
150 }
151 $isSequence = true;
152 foreach ( $arr as $k => &$v ) {
153 if ( is_array( $v ) ) {
154 $v = self::reindexArrays( $v, $isEncoding );
155 }
156
157 if ( $isSequence ) {
158 if ( is_int( $k ) ) {
159 $isSequence = $next++ === $k;
160 } elseif ( $isEncoding && ctype_digit( $k ) ) {
161 // json_decode currently doesn't return integer keys for {}
162 $isSequence = $next++ === (int)$k;
163 } else {
164 $isSequence = false;
165 }
166 }
167 }
168 if ( $isSequence ) {
169 if ( $isEncoding ) {
170 return array_values( $arr );
171 } else {
172 return $arr ? array_combine( range( 1, count( $arr ) ), $arr ) : $arr;
173 }
174 }
175 return $arr;
176 }
177
178}
$wgUrlProtocols
URL schemes that should be recognized as valid by wfParseUrl().
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
This class provides some basic services that Lua libraries will probably need.
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
getEngine()
Get the engine.
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
getParser()
Get the parser.
jsonDecode( $s, $flags)
Handler for jsonDecode.
getEntityTable()
Handler for getEntityTable.
textUnstrip( $s)
Handler for textUnstrip.
textUnstripNoWiki( $s)
Handler for textUnstripNoWiki.
killMarkers( $s)
Handler for killMarkers.
static reindexArrays(array $arr, $isEncoding)
Recursively reindex array with integer keys to 0-based or 1-based.
jsonEncode( $value, $flags)
Handler for jsonEncode.