52 $this->factory =
$factory ?: MediaWikiServices::getInstance()->getMagicWordFactory();
60 public function add( $name ) {
61 $this->names[] = $name;
62 $this->hash = $this->baseRegex = $this->regex =
null;
71 $this->names = array_merge( $this->names, array_values(
$names ) );
72 $this->hash = $this->baseRegex = $this->regex =
null;
80 if ( is_null( $this->hash ) ) {
81 $this->hash = [ 0 => [], 1 => [] ];
82 foreach ( $this->names as $name ) {
83 $magic = $this->factory->get( $name );
84 $case = intval( $magic->isCaseSensitive() );
85 foreach ( $magic->getSynonyms() as $syn ) {
87 $syn = $this->factory->getContentLanguage()->lc( $syn );
89 $this->hash[$case][$syn] = $name;
101 if ( is_null( $this->baseRegex ) ) {
102 $this->baseRegex = [ 0 =>
'', 1 =>
'' ];
104 foreach ( $this->names as $name ) {
105 $magic = $this->factory->get( $name );
106 $case = intval( $magic->isCaseSensitive() );
107 foreach ( $magic->getSynonyms() as $i => $syn ) {
109 $it = strtr( $i,
'0123456789',
'abcdefghij' );
110 $groupName = $it .
'_' . $name;
111 $group =
'(?P<' . $groupName .
'>' . preg_quote( $syn,
'/' ) .
')';
113 if ( isset( $allGroups[$groupName] ) ) {
115 __METHOD__ .
': duplicate internal name in magic word array: ' . $name
118 $allGroups[$groupName] =
true;
119 if ( $this->baseRegex[$case] ===
'' ) {
120 $this->baseRegex[$case] = $group;
122 $this->baseRegex[$case] .=
'|' . $group;
135 if ( is_null( $this->regex ) ) {
137 $this->regex = [
'',
'' ];
138 if ( $this->baseRegex[0] !==
'' ) {
139 $this->regex[0] =
"/{$base[0]}/iuS";
141 if ( $this->baseRegex[1] !==
'' ) {
142 $this->regex[1] =
"/{$base[1]}/S";
154 return str_replace(
"\\$1",
"(.*?)", $this->
getRegex() );
164 $newRegex = [
'',
'' ];
165 if (
$base[0] !==
'' ) {
166 $newRegex[0] =
"/^(?:{$base[0]})/iuS";
168 if (
$base[1] !==
'' ) {
169 $newRegex[1] =
"/^(?:{$base[1]})/S";
181 $newRegex = [
'',
'' ];
182 if (
$base[0] !==
'' ) {
183 $newRegex[0] = str_replace(
"\\$1",
"(.*?)",
"/^(?:{$base[0]})$/iuS" );
185 if (
$base[1] !==
'' ) {
186 $newRegex[1] = str_replace(
"\\$1",
"(.*?)",
"/^(?:{$base[1]})$/S" );
211 while ( ( $key = key( $m ) ) !==
null ) {
212 $value = current( $m );
214 if ( $key === 0 || $value ===
'' ) {
217 $parts = explode(
'_', $key, 2 );
218 if ( count( $parts ) != 2 ) {
221 throw new MWException( __METHOD__ .
': bad parameter name' );
223 list( , $magicName ) = $parts;
224 $paramValue = next( $m );
225 return [ $magicName, $paramValue ];
228 throw new MWException( __METHOD__ .
': parameter not found' );
243 foreach ( $regexes as
$regex ) {
246 if ( preg_match(
$regex, $text, $m ) ) {
251 return [
false, false ];
264 if ( isset(
$hash[1][$text] ) ) {
265 return $hash[1][$text];
267 $lc = $this->factory->getContentLanguage()->lc( $text );
268 return $hash[0][$lc] ??
false;
282 foreach ( $regexes as
$regex ) {
288 if (
$res ===
false ) {
289 LoggerFactory::getInstance(
'parser' )->warning(
'preg_match_all returned false', [
290 'code' => preg_last_error(),
296 list( $name, $param ) = $this->
parseMatch( $m );
297 $found[$name] = $param;
301 if (
$res ===
null ) {
302 LoggerFactory::getInstance(
'parser' )->warning(
'preg_replace returned null', [
303 'code' => preg_last_error(),
325 foreach ( $regexes as
$regex ) {
329 if ( preg_match(
$regex, $text, $m ) ) {
331 if ( strlen( $m[0] ) >= strlen( $text ) ) {
334 $text = substr( $text, strlen( $m[0] ) );