Movatterモバイル変換


[0]ホーム

URL:


MediaWiki master
MagicWordArray.php
Go to the documentation of this file.
1<?php
21namespaceMediaWiki\Parser;
22
23use LogicException;
24useMediaWiki\Logger\LoggerFactory;
25useMediaWiki\MediaWikiServices;
26
35classMagicWordArray {
36
38public$names = [];
39privateMagicWordFactory $factory;
40
42private $hash;
43
45private $baseRegex;
46
48private $regex;
49
54publicfunction__construct($names = [], ?MagicWordFactory $factory =null ) {
55 $this->names =$names;
56 $this->factory = $factory ?:MediaWikiServices::getInstance()->getMagicWordFactory();
57 }
58
64publicfunctionadd( $name ): void {
65 $this->names[] = $name;
66 $this->hash = $this->baseRegex = $this->regex =null;
67 }
68
74publicfunctiongetHash(): array {
75if ( $this->hash === null ) {
76 $this->hash = [ 0 => [], 1 => [] ];
77foreach ( $this->names as $name ) {
78 $magic = $this->factory->get( $name );
79 $case = intval( $magic->isCaseSensitive() );
80foreach ( $magic->getSynonyms() as $syn ) {
81if ( !$case ) {
82 $syn = $this->factory->getContentLanguage()->lc( $syn );
83 }
84 $this->hash[$case][$syn] = $name;
85 }
86 }
87 }
88return $this->hash;
89 }
90
102publicfunctiongetBaseRegex(bool $capture =true,string $delimiter ='/' ): array {
103if ( $capture && $delimiter ==='/' && $this->baseRegex !== null ) {
104return $this->baseRegex;
105 }
106 $regex = [ 0 => [], 1 => [] ];
107foreach ( $this->names as $name ) {
108 $magic = $this->factory->get( $name );
109 $case = $magic->isCaseSensitive() ? 1 : 0;
110foreach ( $magic->getSynonyms() as $i => $syn ) {
111if ( $capture ) {
112// Group name must start with a non-digit in PCRE 8.34+
113 $it = strtr( $i,'0123456789','abcdefghij' );
114 $groupName = $it .'_' . $name;
115 $group ='(?P<' . $groupName .'>' . preg_quote( $syn, $delimiter ) .')';
116 $regex[$case][] = $group;
117 }else {
118 $regex[$case][] = preg_quote( $syn, $delimiter );
119 }
120 }
121 }
122'@phan-var array<int,string[]> $regex';
123foreach ( $regex as $case => &$re ) {
124 $re = count( $re ) ? implode('|', $re ) :'(?!)';
125if ( !$case ) {
126 $re ="(?i:{$re})";
127 }
128 }
129'@phan-var array<int,string> $regex';
130
131if ( $capture && $delimiter ==='/' ) {
132 $this->baseRegex = $regex;
133 }
134return $regex;
135 }
136
142privatefunction getRegex(): array {
143if ( $this->regex === null ) {
144 $this->regex = [];
145 $base = $this->getBaseRegex(true,'/' );
146foreach ( $base as $case => $re ) {
147 $this->regex[$case] ="/$re/JS";
148 }
149// As a performance optimization, turn on unicode mode only for
150// case-insensitive matching.
151 $this->regex[0] .='u';
152 }
153return $this->regex;
154 }
155
161privatefunction getRegexStart(): array {
162 $newRegex = [];
163 $base = $this->getBaseRegex(true,'/' );
164foreach ( $base as $case => $re ) {
165 $newRegex[$case] ="/^(?:$re)/JS";
166 }
167// As a performance optimization, turn on unicode mode only for
168// case-insensitive matching.
169 $newRegex[0] .='u';
170return $newRegex;
171 }
172
178privatefunction getVariableStartToEndRegex(): array {
179 $newRegex = [];
180 $base = $this->getBaseRegex(true,'/' );
181foreach ( $base as $case => $re ) {
182 $newRegex[$case] = str_replace('\$1','(.*?)',"/^(?:$re)$/JS" );
183 }
184// As a performance optimization, turn on unicode mode only for
185// case-insensitive matching.
186 $newRegex[0] .='u';
187return $newRegex;
188 }
189
194publicfunctiongetNames() {
195return $this->names;
196 }
197
205privatefunction parseMatch( array$matches ): array {
206 $magicName = null;
207foreach ($matches as $key => $match ) {
208if ( $magicName !==null ) {
209// The structure we found at this point is [ …,
210// 'a_magicWordName' => 'matchedSynonym',
211// n => 'matchedSynonym (again)',
212// n + 1 => 'parameterValue',
213// … ]
214return [ $magicName,$matches[$key + 1] ?? false ];
215 }
216// Skip the initial full match and any non-matching group
217if ( $match !=='' && $key !== 0 ) {
218 $parts = explode('_', $key, 2 );
219if ( !isset( $parts[1] ) ) {
220thrownew LogicException('Unexpected group name' );
221 }
222 $magicName = $parts[1];
223 }
224 }
225thrownew LogicException('Unexpected $m array with no match' );
226 }
227
235publicfunctionmatchVariableStartToEnd( $text ): array {
236 $regexes = $this->getVariableStartToEndRegex();
237foreach ( $regexes as $regex ) {
238 $m = [];
239if ( preg_match( $regex, $text, $m ) ) {
240return $this->parseMatch( $m );
241 }
242 }
243return [false, false ];
244 }
245
253publicfunctionmatchStartToEnd( $text ) {
254 $hash = $this->getHash();
255if ( isset( $hash[1][$text] ) ) {
256return $hash[1][$text];
257 }
258 $lc = $this->factory->getContentLanguage()->lc( $text );
259return $hash[0][$lc] ??false;
260 }
261
272publicfunctionmatchAndRemove( &$text ): array {
273 $found = [];
274 $regexes = $this->getRegex();
275 $res = preg_replace_callback( $regexes,function ( $m ) use ( &$found ) {
276 [ $name, $param ] = $this->parseMatch( $m );
277 $found[$name] = $param;
278return'';
279 }, $text );
280// T321234: Don't try to fix old revisions with broken UTF-8, just return $text as is
281if ( $res ===null ) {
282 $error = preg_last_error();
283 $errorText = preg_last_error_msg();
284 LoggerFactory::getInstance('parser' )->warning('preg_match_all error: {code} {errorText}', [
285'code' => $error,
286'regex' => $regexes,
287'text' => $text,
288'errorText' => $errorText
289 ] );
290if ( $error !== PREG_BAD_UTF8_ERROR ) {
291thrownew LogicException("preg_match_all error $error: $errorText" );
292 }
293 }else {
294 $text = $res;
295 }
296return $found;
297 }
298
309publicfunctionmatchStartAndRemove( &$text ) {
310 $regexes = $this->getRegexStart();
311foreach ( $regexes as $regex ) {
312if ( preg_match( $regex, $text, $m ) ) {
313 [ $id, ] = $this->parseMatch( $m );
314if ( strlen( $m[0] ) >= strlen( $text ) ) {
315 $text ='';
316 }else {
317 $text = substr( $text, strlen( $m[0] ) );
318 }
319return $id;
320 }
321 }
322returnfalse;
323 }
324}
$matches
$matches
DefinitionNoLocalSettings.php:27
if
if(!defined('MW_SETUP_CALLBACK'))
DefinitionWebStart.php:82
MediaWiki\Logger\LoggerFactory
Create PSR-3 logger objects.
DefinitionLoggerFactory.php:46
MediaWiki\MediaWikiServices
Service locator for MediaWiki core services.
DefinitionMediaWikiServices.php:250
MediaWiki\MediaWikiServices\getInstance
static getInstance()
Returns the global default instance of the top level service locator.
DefinitionMediaWikiServices.php:338
MediaWiki\Parser\MagicWordArray
Class for handling an array of magic words.
DefinitionMagicWordArray.php:35
MediaWiki\Parser\MagicWordArray\getNames
getNames()
DefinitionMagicWordArray.php:194
MediaWiki\Parser\MagicWordArray\matchVariableStartToEnd
matchVariableStartToEnd( $text)
Match some text, with parameter capture.
DefinitionMagicWordArray.php:235
MediaWiki\Parser\MagicWordArray\__construct
__construct( $names=[], ?MagicWordFactory $factory=null)
DefinitionMagicWordArray.php:54
MediaWiki\Parser\MagicWordArray\matchAndRemove
matchAndRemove(&$text)
Return an associative array for all items that match.
DefinitionMagicWordArray.php:272
MediaWiki\Parser\MagicWordArray\matchStartAndRemove
matchStartAndRemove(&$text)
Return the ID of the magic word at the start of $text, and remove the prefix from $text.
DefinitionMagicWordArray.php:309
MediaWiki\Parser\MagicWordArray\getBaseRegex
getBaseRegex(bool $capture=true, string $delimiter='/')
Get the base regex.
DefinitionMagicWordArray.php:102
MediaWiki\Parser\MagicWordArray\matchStartToEnd
matchStartToEnd( $text)
Match some text, without parameter capture.
DefinitionMagicWordArray.php:253
MediaWiki\Parser\MagicWordArray\$names
string[] $names
DefinitionMagicWordArray.php:38
MediaWiki\Parser\MagicWordArray\add
add( $name)
Add a magic word by name.
DefinitionMagicWordArray.php:64
MediaWiki\Parser\MagicWordArray\getHash
getHash()
Get a 2-d hashtable for this array.
DefinitionMagicWordArray.php:74
MediaWiki\Parser\MagicWordFactory
Store information about magic words, and create/cache MagicWord objects.
DefinitionMagicWordFactory.php:39
MediaWiki\Parser
DefinitionBlockLevelPass.php:27

[8]ページ先頭

©2009-2025 Movatter.jp