Translate extension for MediaWiki
 
Loading...
Searching...
No Matches
StringMatcher.php
1<?php
2declare( strict_types = 1 );
3
4namespace MediaWiki\Extension\Translate\MessageProcessing;
5
7use Title;
8
19 protected $sPrefix = '';
21 protected $aExact = [];
23 protected $aPrefix = [];
25 protected $aRegex = [];
26
27 public function __construct( string $prefix = '', array $patterns = [] ) {
28 $this->sPrefix = $prefix;
29 $this->init( $patterns );
30 }
31
40 protected function init( array $strings ): void {
41 foreach ( $strings as $string ) {
42 $pos = strpos( $string, '*' );
43 if ( $pos === false ) {
44 $this->aExact[] = $string;
45 } elseif ( $pos + 1 === strlen( $string ) ) {
46 $prefix = substr( $string, 0, -1 );
47 $this->aPrefix[$prefix] = strlen( $prefix );
48 } else {
49 $string = str_replace( '\\*', '.+', preg_quote( $string, '/' ) );
50 $this->aRegex[] = "/^$string$/";
51 }
52 }
53 }
54
55 protected static function getValidKeyChars(): string {
56 static $valid = null;
57 if ( $valid === null ) {
58 $valid = strtr(
59 Title::legalChars(),
60 [
61 '=' => '', // equals sign, which is itself usef for escaping
62 '&' => '', // ampersand, for entities
63 '%' => '', // percent sign, which is used in URL encoding
64 ]
65 );
66 }
67
68 return $valid;
69 }
70
72 public function setConf( array $conf ): void {
73 $this->sPrefix = $conf['prefix'];
74 $this->init( $conf['patterns'] );
75 }
76
78 public function matches( string $key ): bool {
79 if ( in_array( $key, $this->aExact ) ) {
80 return true;
81 }
82
83 foreach ( $this->aPrefix as $prefix => $len ) {
84 if ( strncmp( $key, $prefix, $len ) === 0 ) {
85 return true;
86 }
87 }
88
89 foreach ( $this->aRegex as $regex ) {
90 if ( preg_match( $regex, $key ) ) {
91 return true;
92 }
93 }
94
95 return false;
96 }
97
99 public function mangle( string $key ): string {
100 if ( $this->matches( $key ) ) {
101 $key = $this->sPrefix . $key;
102 }
103
104 $escaper = static function ( $match ) {
105 $esc = '';
106 foreach ( str_split( $match[0] ) as $c ) {
107 $esc .= '=' . sprintf( '%02X', ord( $c ) );
108 }
109 return $esc;
110 };
111
112 // Apply a "quoted-printable"-like escaping
113 $valid = self::getValidKeyChars();
114 $key = preg_replace_callback( "/[^$valid]/", $escaper, $key );
115 // Additional limitations in MediaWiki, see MediaWikiTitleCodec::splitTitleString
116 $key = preg_replace_callback( '/(~~~|^[ _]|[ _]$|[ _]{2,}|^:)/', $escaper, $key );
117 // TODO: length check + truncation
118 // TODO: forbid path travels
119
120 return $key;
121 }
122
124 public function mangleList( array $list ): array {
125 return array_map( [ $this, 'mangle' ], $list );
126 }
127
129 public function mangleArray( array $array ): array {
130 $out = [];
131 foreach ( $array as $key => $value ) {
132 $out[$this->mangle( (string)$key )] = $value;
133 }
134
135 return $out;
136 }
137
139 public function unmangle( string $key ): string {
140 // Unescape the "quoted-printable"-like escaping,
141 // which is applied in mangle
142 $unescapedString = preg_replace_callback(
143 '/=([A-F0-9]{2})/',
144 static function ( $match ) {
145 return chr( hexdec( $match[1] ) );
146 },
147 $key
148 );
149
150 if ( strncmp( $unescapedString, $this->sPrefix, strlen( $this->sPrefix ) ) === 0 ) {
151 $unmangled = substr( $unescapedString, strlen( $this->sPrefix ) );
152
153 // Check if this string should be mangled / un-mangled to begin with
154 if ( $this->matches( $unmangled ) ) {
155 return $unmangled;
156 }
157 }
158 return $unescapedString;
159 }
160
162 public function unmangleList( array $list ): array {
163 foreach ( $list as $index => $key ) {
164 $list[$index] = $this->unmangle( $key );
165 }
166
167 return $list;
168 }
169
171 public function unmangleArray( array $array ): array {
172 $out = [];
173 foreach ( $array as $key => $value ) {
174 $out[$this->unmangle( $key )] = $value;
175 }
176
177 return $out;
178 }
179
181 public static function getExtraSchema(): array {
182 $schema = [
183 'root' => [
184 '_type' => 'array',
185 '_children' => [
186 'MANGLER' => [
187 '_type' => 'array',
188 '_children' => [
189 'prefix' => [
190 '_type' => 'text',
191 '_not_empty' => true,
192 ],
193 'patterns' => [
194 '_type' => 'array',
195 '_required' => true,
196 '_ignore_extra_keys' => true,
197 '_children' => [],
198 ],
199 ],
200 ],
201 ],
202 ],
203 ];
204
205 return $schema;
206 }
207}
return[ 'Translate:ConfigHelper'=> static function():ConfigHelper { return new ConfigHelper();}, 'Translate:CsvTranslationImporter'=> static function(MediaWikiServices $services):CsvTranslationImporter { return new CsvTranslationImporter( $services->getWikiPageFactory());}, 'Translate:EntitySearch'=> static function(MediaWikiServices $services):EntitySearch { return new EntitySearch($services->getMainWANObjectCache(), $services->getCollationFactory() ->makeCollation( 'uca-default-u-kn'), MessageGroups::singleton(), $services->getNamespaceInfo(), $services->get( 'Translate:MessageIndex'), $services->getTitleParser(), $services->getTitleFormatter());}, 'Translate:ExternalMessageSourceStateImporter'=> static function(MediaWikiServices $services):ExternalMessageSourceStateImporter { return new ExternalMessageSourceStateImporter($services->getMainConfig(), $services->get( 'Translate:GroupSynchronizationCache'), $services->getJobQueueGroup(), LoggerFactory::getInstance( 'Translate.GroupSynchronization'), MessageIndex::singleton());}, 'Translate:GroupSynchronizationCache'=> static function(MediaWikiServices $services):GroupSynchronizationCache { return new GroupSynchronizationCache( $services->get( 'Translate:PersistentCache'));}, 'Translate:MessageBundleStore'=> static function(MediaWikiServices $services):MessageBundleStore { return new MessageBundleStore(new RevTagStore(), $services->getJobQueueGroup(), $services->getLanguageNameUtils(), $services->get( 'Translate:MessageIndex'));}, 'Translate:MessageGroupReview'=> static function(MediaWikiServices $services):MessageGroupReview { return new MessageGroupReview($services->getDBLoadBalancer(), $services->getHookContainer());}, 'Translate:MessageIndex'=> static function(MediaWikiServices $services):MessageIndex { $params=$services->getMainConfig() ->get( 'TranslateMessageIndex');if(is_string( $params)) { $params=(array) $params;} $class=array_shift( $params);return new $class( $params);}, 'Translate:ParsingPlaceholderFactory'=> static function():ParsingPlaceholderFactory { return new ParsingPlaceholderFactory();}, 'Translate:PersistentCache'=> static function(MediaWikiServices $services):PersistentCache { return new PersistentDatabaseCache($services->getDBLoadBalancer(), $services->getJsonCodec());}, 'Translate:ProgressStatsTableFactory'=> static function(MediaWikiServices $services):ProgressStatsTableFactory { return new ProgressStatsTableFactory($services->getLinkRenderer(), $services->get( 'Translate:ConfigHelper'));}, 'Translate:SubpageListBuilder'=> static function(MediaWikiServices $services):SubpageListBuilder { return new SubpageListBuilder($services->get( 'Translate:TranslatableBundleFactory'), $services->getLinkBatchFactory());}, 'Translate:TranslatableBundleFactory'=> static function(MediaWikiServices $services):TranslatableBundleFactory { return new TranslatableBundleFactory($services->get( 'Translate:TranslatablePageStore'), $services->get( 'Translate:MessageBundleStore'));}, 'Translate:TranslatableBundleMover'=> static function(MediaWikiServices $services):TranslatableBundleMover { return new TranslatableBundleMover($services->getMovePageFactory(), $services->getJobQueueGroup(), $services->getLinkBatchFactory(), $services->get( 'Translate:TranslatableBundleFactory'), $services->get( 'Translate:SubpageListBuilder'), $services->getMainConfig() ->get( 'TranslatePageMoveLimit'));}, 'Translate:TranslatablePageParser'=> static function(MediaWikiServices $services):TranslatablePageParser { return new TranslatablePageParser($services->get( 'Translate:ParsingPlaceholderFactory'));}, 'Translate:TranslatablePageStore'=> static function(MediaWikiServices $services):TranslatablePageStore { return new TranslatablePageStore($services->get( 'Translate:MessageIndex'), $services->getJobQueueGroup(), new RevTagStore(), $services->getDBLoadBalancer());}, 'Translate:TranslationStashReader'=> static function(MediaWikiServices $services):TranslationStashReader { $db=$services->getDBLoadBalancer() ->getConnectionRef(DB_REPLICA);return new TranslationStashStorage( $db);}, 'Translate:TranslationStatsDataProvider'=> static function(MediaWikiServices $services):TranslationStatsDataProvider { return new TranslationStatsDataProvider(new ServiceOptions(TranslationStatsDataProvider::CONSTRUCTOR_OPTIONS, $services->getMainConfig()), $services->getObjectFactory());}, 'Translate:TranslationUnitStoreFactory'=> static function(MediaWikiServices $services):TranslationUnitStoreFactory { return new TranslationUnitStoreFactory( $services->getDBLoadBalancer());}, 'Translate:TranslatorActivity'=> static function(MediaWikiServices $services):TranslatorActivity { $query=new TranslatorActivityQuery($services->getMainConfig(), $services->getDBLoadBalancer());return new TranslatorActivity($services->getMainObjectStash(), $query, $services->getJobQueueGroup());}, 'Translate:TtmServerFactory'=> static function(MediaWikiServices $services):TtmServerFactory { $config=$services->getMainConfig();$default=$config->get( 'TranslateTranslationDefaultService');if( $default===false) { $default=null;} return new TtmServerFactory( $config->get( 'TranslateTranslationServices'), $default);}]
@phpcs-require-sorted-array
The versatile default implementation of StringMangler interface.
Interface that key-mangling classes must implement.
Message groups are usually configured in YAML, though the actual storage format does not matter,...