MediaWiki  1.34.0
TextLibrary.php
Go to the documentation of this file.
1 <?php
2 
4  // Matches Lua mw.text constants
5  const JSON_PRESERVE_KEYS = 1;
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 }
Scribunto_LuaTextLibrary\textUnstripNoWiki
textUnstripNoWiki( $s)
Handler for textUnstripNoWiki.
Definition: TextLibrary.php:65
Scribunto_LuaError
Definition: LuaCommon.php:992
wfMessage
wfMessage( $key,... $params)
This is the function for getting translated interface messages.
Definition: GlobalFunctions.php:1264
$s
$s
Definition: mergeMessageFileList.php:185
Scribunto_LuaLibraryBase\getEngine
getEngine()
Get the engine.
Definition: LibraryBase.php:56
FormatJson\ALL_OK
const ALL_OK
Skip escaping as many characters as reasonably possible.
Definition: FormatJson.php:55
Scribunto_LuaTextLibrary\jsonEncode
jsonEncode( $value, $flags)
Handler for jsonEncode.
Definition: TextLibrary.php:100
Scribunto_LuaLibraryBase\getParser
getParser()
Get the parser.
Definition: LibraryBase.php:74
Scribunto_LuaLibraryBase\checkType
checkType( $name, $argIdx, $arg, $expectType)
Check the type of a variable.
Definition: LibraryBase.php:141
FormatJson\encode
static encode( $value, $pretty=false, $escaping=0)
Returns the JSON representation of a value.
Definition: FormatJson.php:115
Scribunto_LuaTextLibrary
Definition: TextLibrary.php:3
FormatJson\TRY_FIXING
const TRY_FIXING
If set, attempt to fix invalid JSON.
Definition: FormatJson.php:70
FormatJson\FORCE_ASSOC
const FORCE_ASSOC
If set, treat JSON objects '{...}' as associative arrays.
Definition: FormatJson.php:63
Scribunto_LuaTextLibrary\reindexArrays
static reindexArrays(array $arr, $isEncoding)
Recursively reindex array with integer keys to 0-based or 1-based.
Definition: TextLibrary.php:144
Scribunto_LuaLibraryBase\checkTypeOptional
checkTypeOptional( $name, $argIdx, &$arg, $expectType, $default)
Check the type of a variable, with default if null.
Definition: LibraryBase.php:164
Scribunto_LuaTextLibrary\getEntityTable
getEntityTable()
Handler for getEntityTable.
Definition: TextLibrary.php:86
Scribunto_LuaTextLibrary\JSON_TRY_FIXING
const JSON_TRY_FIXING
Definition: TextLibrary.php:6
Scribunto_LuaLibraryBase
This class provides some basic services that Lua libraries will probably need.
Definition: LibraryBase.php:27
Scribunto_LuaTextLibrary\JSON_PRETTY
const JSON_PRETTY
Definition: TextLibrary.php:7
$wgUrlProtocols
$wgUrlProtocols
URL schemes that should be recognized as valid by wfParseUrl().
Definition: DefaultSettings.php:4185
Scribunto_LuaTextLibrary\killMarkers
killMarkers( $s)
Handler for killMarkers.
Definition: TextLibrary.php:76
FormatJson\parse
static parse( $value, $options=0)
Decodes a JSON string.
Definition: FormatJson.php:188
$status
return $status
Definition: SyntaxHighlight.php:347
Scribunto_LuaTextLibrary\jsonDecode
jsonDecode( $s, $flags)
Handler for jsonDecode.
Definition: TextLibrary.php:120
Scribunto_LuaTextLibrary\JSON_PRESERVE_KEYS
const JSON_PRESERVE_KEYS
Definition: TextLibrary.php:5
Scribunto_LuaTextLibrary\textUnstrip
textUnstrip( $s)
Handler for textUnstrip.
Definition: TextLibrary.php:53