MediaWiki REL1_32
ConverterRule.php
Go to the documentation of this file.
1<?php
28 public $mText; // original text in -{text}-
29 public $mConverter; // LanguageConverter object
30 public $mRuleDisplay = '';
31 public $mRuleTitle = false;
32 public $mRules = '';// string : the text of the rules
33 public $mRulesAction = 'none';
34 public $mFlags = [];
35 public $mVariantFlags = [];
36 public $mConvTable = [];
37 public $mBidtable = [];// array of the translation in each variant
38 public $mUnidtable = [];// array of the translation in each variant
39
44 public function __construct( $text, $converter ) {
45 $this->mText = $text;
46 $this->mConverter = $converter;
47 }
48
55 public function getTextInBidtable( $variants ) {
56 $variants = (array)$variants;
57 if ( !$variants ) {
58 return false;
59 }
60 foreach ( $variants as $variant ) {
61 if ( isset( $this->mBidtable[$variant] ) ) {
62 return $this->mBidtable[$variant];
63 }
64 }
65 return false;
66 }
67
72 function parseFlags() {
73 $text = $this->mText;
74 $flags = [];
75 $variantFlags = [];
76
77 $sepPos = strpos( $text, '|' );
78 if ( $sepPos !== false ) {
79 $validFlags = $this->mConverter->mFlags;
80 $f = StringUtils::explode( ';', substr( $text, 0, $sepPos ) );
81 foreach ( $f as $ff ) {
82 $ff = trim( $ff );
83 if ( isset( $validFlags[$ff] ) ) {
84 $flags[$validFlags[$ff]] = true;
85 }
86 }
87 $text = strval( substr( $text, $sepPos + 1 ) );
88 }
89
90 if ( !$flags ) {
91 $flags['S'] = true;
92 } elseif ( isset( $flags['R'] ) ) {
93 $flags = [ 'R' => true ];// remove other flags
94 } elseif ( isset( $flags['N'] ) ) {
95 $flags = [ 'N' => true ];// remove other flags
96 } elseif ( isset( $flags['-'] ) ) {
97 $flags = [ '-' => true ];// remove other flags
98 } elseif ( count( $flags ) == 1 && isset( $flags['T'] ) ) {
99 $flags['H'] = true;
100 } elseif ( isset( $flags['H'] ) ) {
101 // replace A flag, and remove other flags except T
102 $temp = [ '+' => true, 'H' => true ];
103 if ( isset( $flags['T'] ) ) {
104 $temp['T'] = true;
105 }
106 if ( isset( $flags['D'] ) ) {
107 $temp['D'] = true;
108 }
109 $flags = $temp;
110 } else {
111 if ( isset( $flags['A'] ) ) {
112 $flags['+'] = true;
113 $flags['S'] = true;
114 }
115 if ( isset( $flags['D'] ) ) {
116 unset( $flags['S'] );
117 }
118 // try to find flags like "zh-hans", "zh-hant"
119 // allow syntaxes like "-{zh-hans;zh-hant|XXXX}-"
120 $variantFlags = array_intersect( array_keys( $flags ), $this->mConverter->mVariants );
121 if ( $variantFlags ) {
122 $variantFlags = array_flip( $variantFlags );
123 $flags = [];
124 }
125 }
126 $this->mVariantFlags = $variantFlags;
127 $this->mRules = $text;
128 $this->mFlags = $flags;
129 }
130
135 function parseRules() {
136 $rules = $this->mRules;
137 $bidtable = [];
138 $unidtable = [];
139 $variants = $this->mConverter->mVariants;
140 $varsep_pattern = $this->mConverter->getVarSeparatorPattern();
141
142 // Split according to $varsep_pattern, but ignore semicolons from HTML entities
143 $rules = preg_replace( '/(&[#a-zA-Z0-9]+);/', "$1\x01", $rules );
144 $choice = preg_split( $varsep_pattern, $rules );
145 $choice = str_replace( "\x01", ';', $choice );
146
147 foreach ( $choice as $c ) {
148 $v = explode( ':', $c, 2 );
149 if ( count( $v ) != 2 ) {
150 // syntax error, skip
151 continue;
152 }
153 $to = trim( $v[1] );
154 $v = trim( $v[0] );
155 $u = explode( '=>', $v, 2 );
156 // if $to is empty (which is also used as $from in bidtable),
157 // strtr() could return a wrong result.
158 if ( count( $u ) == 1 && $to !== '' && in_array( $v, $variants ) ) {
159 $bidtable[$v] = $to;
160 } elseif ( count( $u ) == 2 ) {
161 $from = trim( $u[0] );
162 $v = trim( $u[1] );
163 // if $from is empty, strtr() could return a wrong result.
164 if ( array_key_exists( $v, $unidtable )
165 && !is_array( $unidtable[$v] )
166 && $from !== ''
167 && in_array( $v, $variants ) ) {
168 $unidtable[$v] = [ $from => $to ];
169 } elseif ( $from !== '' && in_array( $v, $variants ) ) {
170 $unidtable[$v][$from] = $to;
171 }
172 }
173 // syntax error, pass
174 if ( !isset( $this->mConverter->mVariantNames[$v] ) ) {
175 $bidtable = [];
176 $unidtable = [];
177 break;
178 }
179 }
180 $this->mBidtable = $bidtable;
181 $this->mUnidtable = $unidtable;
182 }
183
189 function getRulesDesc() {
190 $codesep = $this->mConverter->mDescCodeSep;
191 $varsep = $this->mConverter->mDescVarSep;
192 $text = '';
193 foreach ( $this->mBidtable as $k => $v ) {
194 $text .= $this->mConverter->mVariantNames[$k] . "$codesep$v$varsep";
195 }
196 foreach ( $this->mUnidtable as $k => $a ) {
197 foreach ( $a as $from => $to ) {
198 $text .= $from . '⇒' . $this->mConverter->mVariantNames[$k] .
199 "$codesep$to$varsep";
200 }
201 }
202 return $text;
203 }
204
213 function getRuleConvertedStr( $variant ) {
214 $bidtable = $this->mBidtable;
215 $unidtable = $this->mUnidtable;
216
217 if ( count( $bidtable ) + count( $unidtable ) == 0 ) {
218 return $this->mRules;
219 } else {
220 // display current variant in bidirectional array
221 $disp = $this->getTextInBidtable( $variant );
222 // or display current variant in fallbacks
223 if ( $disp === false ) {
224 $disp = $this->getTextInBidtable(
225 $this->mConverter->getVariantFallbacks( $variant ) );
226 }
227 // or display current variant in unidirectional array
228 if ( $disp === false && array_key_exists( $variant, $unidtable ) ) {
229 $disp = array_values( $unidtable[$variant] )[0];
230 }
231 // or display first text under disable manual convert
232 if ( $disp === false && $this->mConverter->mManualLevel[$variant] == 'disable' ) {
233 if ( count( $bidtable ) > 0 ) {
234 $disp = array_values( $bidtable )[0];
235 } else {
236 $disp = array_values( array_values( $unidtable )[0] )[0];
237 }
238 }
239 return $disp;
240 }
241 }
242
253 function getRuleConvertedTitle( $variant ) {
254 if ( $variant === $this->mConverter->mMainLanguageCode ) {
255 // If a string targeting exactly this variant is set,
256 // use it. Otherwise, just return false, so the real
257 // page name can be shown (and because variant === main,
258 // there'll be no further automatic conversion).
259 $disp = $this->getTextInBidtable( $variant );
260 if ( $disp ) {
261 return $disp;
262 }
263 if ( array_key_exists( $variant, $this->mUnidtable ) ) {
264 $disp = array_values( $this->mUnidtable[$variant] )[0];
265 }
266 // Assigned above or still false.
267 return $disp;
268 } else {
269 return $this->getRuleConvertedStr( $variant );
270 }
271 }
272
277 function generateConvTable() {
278 // Special case optimisation
279 if ( !$this->mBidtable && !$this->mUnidtable ) {
280 $this->mConvTable = [];
281 return;
282 }
283
284 $bidtable = $this->mBidtable;
285 $unidtable = $this->mUnidtable;
286 $manLevel = $this->mConverter->mManualLevel;
287
288 $vmarked = [];
289 foreach ( $this->mConverter->mVariants as $v ) {
290 /* for bidirectional array
291 fill in the missing variants, if any,
292 with fallbacks */
293 if ( !isset( $bidtable[$v] ) ) {
294 $variantFallbacks =
295 $this->mConverter->getVariantFallbacks( $v );
296 $vf = $this->getTextInBidtable( $variantFallbacks );
297 if ( $vf ) {
298 $bidtable[$v] = $vf;
299 }
300 }
301
302 if ( isset( $bidtable[$v] ) ) {
303 foreach ( $vmarked as $vo ) {
304 // use syntax: -{A|zh:WordZh;zh-tw:WordTw}-
305 // or -{H|zh:WordZh;zh-tw:WordTw}-
306 // or -{-|zh:WordZh;zh-tw:WordTw}-
307 // to introduce a custom mapping between
308 // words WordZh and WordTw in the whole text
309 if ( $manLevel[$v] == 'bidirectional' ) {
310 $this->mConvTable[$v][$bidtable[$vo]] = $bidtable[$v];
311 }
312 if ( $manLevel[$vo] == 'bidirectional' ) {
313 $this->mConvTable[$vo][$bidtable[$v]] = $bidtable[$vo];
314 }
315 }
316 $vmarked[] = $v;
317 }
318 /* for unidirectional array fill to convert tables */
319 if ( ( $manLevel[$v] == 'bidirectional' || $manLevel[$v] == 'unidirectional' )
320 && isset( $unidtable[$v] )
321 ) {
322 if ( isset( $this->mConvTable[$v] ) ) {
323 $this->mConvTable[$v] = $unidtable[$v] + $this->mConvTable[$v];
324 } else {
325 $this->mConvTable[$v] = $unidtable[$v];
326 }
327 }
328 }
329 }
330
335 public function parse( $variant = null ) {
336 if ( !$variant ) {
337 $variant = $this->mConverter->getPreferredVariant();
338 }
339
340 $this->parseFlags();
341 $flags = $this->mFlags;
342
343 // convert to specified variant
344 // syntax: -{zh-hans;zh-hant[;...]|<text to convert>}-
345 if ( $this->mVariantFlags ) {
346 // check if current variant in flags
347 if ( isset( $this->mVariantFlags[$variant] ) ) {
348 // then convert <text to convert> to current language
349 $this->mRules = $this->mConverter->autoConvert( $this->mRules,
350 $variant );
351 } else {
352 // if current variant no in flags,
353 // then we check its fallback variants.
354 $variantFallbacks =
355 $this->mConverter->getVariantFallbacks( $variant );
356 if ( is_array( $variantFallbacks ) ) {
357 foreach ( $variantFallbacks as $variantFallback ) {
358 // if current variant's fallback exist in flags
359 if ( isset( $this->mVariantFlags[$variantFallback] ) ) {
360 // then convert <text to convert> to fallback language
361 $this->mRules =
362 $this->mConverter->autoConvert( $this->mRules,
363 $variantFallback );
364 break;
365 }
366 }
367 }
368 }
369 $this->mFlags = $flags = [ 'R' => true ];
370 }
371
372 if ( !isset( $flags['R'] ) && !isset( $flags['N'] ) ) {
373 // decode => HTML entities modified by Sanitizer::removeHTMLtags
374 $this->mRules = str_replace( '=&gt;', '=>', $this->mRules );
375 $this->parseRules();
376 }
377 $rules = $this->mRules;
378
379 if ( !$this->mBidtable && !$this->mUnidtable ) {
380 if ( isset( $flags['+'] ) || isset( $flags['-'] ) ) {
381 // fill all variants if text in -{A/H/-|text}- is non-empty but without rules
382 if ( $rules !== '' ) {
383 foreach ( $this->mConverter->mVariants as $v ) {
384 $this->mBidtable[$v] = $rules;
385 }
386 }
387 } elseif ( !isset( $flags['N'] ) && !isset( $flags['T'] ) ) {
388 $this->mFlags = $flags = [ 'R' => true ];
389 }
390 }
391
392 $this->mRuleDisplay = false;
393 foreach ( $flags as $flag => $unused ) {
394 switch ( $flag ) {
395 case 'R':
396 // if we don't do content convert, still strip the -{}- tags
397 $this->mRuleDisplay = $rules;
398 break;
399 case 'N':
400 // process N flag: output current variant name
401 $ruleVar = trim( $rules );
402 if ( isset( $this->mConverter->mVariantNames[$ruleVar] ) ) {
403 $this->mRuleDisplay = $this->mConverter->mVariantNames[$ruleVar];
404 } else {
405 $this->mRuleDisplay = '';
406 }
407 break;
408 case 'D':
409 // process D flag: output rules description
410 $this->mRuleDisplay = $this->getRulesDesc();
411 break;
412 case 'H':
413 // process H,- flag or T only: output nothing
414 $this->mRuleDisplay = '';
415 break;
416 case '-':
417 $this->mRulesAction = 'remove';
418 $this->mRuleDisplay = '';
419 break;
420 case '+':
421 $this->mRulesAction = 'add';
422 $this->mRuleDisplay = '';
423 break;
424 case 'S':
425 $this->mRuleDisplay = $this->getRuleConvertedStr( $variant );
426 break;
427 case 'T':
428 $this->mRuleTitle = $this->getRuleConvertedTitle( $variant );
429 $this->mRuleDisplay = '';
430 break;
431 default:
432 // ignore unknown flags (but see error case below)
433 }
434 }
435 if ( $this->mRuleDisplay === false ) {
436 $this->mRuleDisplay = '<span class="error">'
437 . wfMessage( 'converter-manual-rule-error' )->inContentLanguage()->escaped()
438 . '</span>';
439 }
440
441 $this->generateConvTable();
442 }
443
448 public function hasRules() {
449 return $this->mRules !== '';
450 }
451
456 public function getDisplay() {
457 return $this->mRuleDisplay;
458 }
459
464 public function getTitle() {
465 return $this->mRuleTitle;
466 }
467
472 public function getRulesAction() {
473 return $this->mRulesAction;
474 }
475
481 public function getConvTable() {
482 return $this->mConvTable;
483 }
484
489 public function getRules() {
490 return $this->mRules;
491 }
492
497 public function getFlags() {
498 return $this->mFlags;
499 }
500}
Parser for rules of language conversion , parse rules in -{ }- tag.
getRules()
Get conversion rules string.
parse( $variant=null)
Parse rules and flags.
getFlags()
Get conversion flags.
getTextInBidtable( $variants)
Check if variants array in convert array.
getRuleConvertedTitle( $variant)
Similar to getRuleConvertedStr(), but this prefers to use original page title if $variant === $this->...
getDisplay()
Get display text on markup -{...}-.
getRulesAction()
Return how deal with conversion rules.
parseFlags()
Parse flags with syntax -{FLAG| ... }-.
__construct( $text, $converter)
getRuleConvertedStr( $variant)
Parse rules conversion.
generateConvTable()
Generate conversion table for all text.
parseRules()
Generate conversion table.
hasRules()
Checks if there are conversion rules.
getConvTable()
Get conversion table.
getTitle()
Get converted title.
static explode( $separator, $subject)
Workalike for explode() with limited memory usage.
This document is intended to provide useful advice for parties seeking to redistribute MediaWiki to end users It s targeted particularly at maintainers for Linux since it s been observed that distribution packages of MediaWiki often break We ve consistently had to recommend that users seeking support use official tarballs instead of their distribution s and this often solves whatever problem the user is having It would be nice if this could such as
null means default in associative array with keys and values unescaped Should be merged with default with a value of false meaning to suppress the attribute in associative array with keys and values unescaped noclasses just before the function returns a value If you return true
Definition hooks.txt:2055
either a unescaped string or a HtmlArmor object after in associative array form externallinks including delete and has completed for all link tables whether this was an auto creation use $formDescriptor instead default is conds Array Extra conditions for the No matching items in log is displayed if loglist is empty msgKey Array If you want a nice box with a set this to the key of the message First element is the message additional optional elements are parameters for the key that are processed with wfMessage() -> params() ->parseAsBlock() - offset Set to overwrite offset parameter in $wgRequest set to '' to unset offset - wrap String Wrap the message in html(usually something like "&lt;div ...>$1&lt;/div>"). - flags Integer display flags(NO_ACTION_LINK, NO_EXTRA_USER_LINKS) 'LogException':Called before an exception(or PHP error) is logged. This is meant for integration with external error aggregation services
injection txt This is an overview of how MediaWiki makes use of dependency injection The design described here grew from the discussion of RFC T384 The term dependency this means that anything an object needs to operate should be injected from the the object itself should only know narrow no concrete implementation of the logic it relies on The requirement to inject everything typically results in an architecture that based on two main types of and essentially stateless service objects that use other service objects to operate on the value objects As of the beginning MediaWiki is only starting to use the DI approach Much of the code still relies on global state or direct resulting in a highly cyclical dependency which acts as the top level factory for services in MediaWiki which can be used to gain access to default instances of various services MediaWikiServices however also allows new services to be defined and default services to be redefined Services are defined or redefined by providing a callback the instantiator that will return a new instance of the service When it will create an instance of MediaWikiServices and populate it with the services defined in the files listed by thereby bootstrapping the DI framework Per $wgServiceWiringFiles lists includes ServiceWiring php
Definition injection.txt:37
The wiki should then use memcached to cache various data To use multiple just add more items to the array To increase the weight of a make its entry a array("192.168.0.1:11211", 2))