Completed
Push — master ( 373b7d...c92b89 )
by Razon
11:43 queued 10:06
created

ZhConverter   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 106
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 8
eloc 39
c 1
b 0
f 0
dl 0
loc 106
ccs 0
cts 54
cp 0
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A toTW() 0 3 1
A toHK() 0 3 1
A toMO() 0 3 1
A toCN() 0 3 1
A getConverter() 0 37 3
A to() 0 3 1
1
<?php
2
namespace RazonYang\MediaWiki\ZhConverter;
3
4
use MediaWiki\Logger\NullSpi;
5
6
defined('MEDIAWIKI') || define('MEDIAWIKI', true);
7
defined('MEDIAWIKI_PATH') || define('MEDIAWIKI_PATH', ComposerHelper::getVendorPath() . '/mediawiki/core');
8
9
require_once __DIR__ . '/functions.php';
10
require_once MEDIAWIKI_PATH . '/includes/GlobalFunctions.php';
11
require_once MEDIAWIKI_PATH . '/includes/AutoLoader.php';
12
13
set_include_path(get_include_path() . PATH_SEPARATOR . MEDIAWIKI_PATH);
14
15
global $IP;
16
$IP = MEDIAWIKI_PATH;
17
18
class ZhConverter
19
{
20
    const ZH_CN = 'zh-cn'; // 中文简体
21
    const ZH_TW = 'zh-tw'; // 台湾繁体
22
    const ZH_HK = 'zh-hk'; // 香港繁体
23
    const ZH_MO = 'zh-mo'; // 澳门繁体
24
25
    private static $converter;
26
27
    /**
28
     * @return \LanguageConverter
29
     */
30
    public static function getConverter()
31
    {
32
        if (static::$converter === null) {
0 ignored issues
show
Bug introduced by
Since $converter is declared private, accessing it with static will lead to errors in possible sub-classes; you can either use self, or increase the visibility of $converter to at least protected.
Loading history...
33
            /* Initialize some global variables needed */
34
            global $wgRequest, $wgMemc;
35
            global $wgLocalisationCacheConf, $wgDisabledVariants, $wgExtraLanguageNames;
36
            global $wgLangConvMemc, $wgMessageCacheType, $wgObjectCaches;
37
            global $wgLanguageConverterCacheType, $wgMWLoggerDefaultSpi, $wgMainWANCache, $wgWANObjectCaches;
38
            $wgRequest = new WebRequest();
39
            $wgMemc = new FakeMemCachedClient();
40
            $wgLocalisationCacheConf['class'] = FakeMemCachedClient::class;
41
            $wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
42
            $wgDisabledVariants = [];
43
            $wgExtraLanguageNames = [];
44
            $wgLangConvMemc = new FakeMemCachedClient;
45
            defined('CACHE_NONE') || define('CACHE_NONE', 'FAKE');
46
            $wgObjectCaches = [
47
                'FAKE' => ['class' => FakeMemCachedClient::class],
48
            ];
49
            $wgMessageCacheType = 'FAKE';
50
            $wgLanguageConverterCacheType = 'FAKE';
51
            $wgMainWANCache = 'FAKE';
52
            $wgWANObjectCaches = [
53
                'FAKE' => [
54
                    'class' => 'WANObjectCache',
55
                    'cacheId' => 'FAKE',
56
                    'pool' => 'mediawiki-main-none',
57
                    'relayerConfig' => ['class' => 'EventRelayerNull']
58
                ]
59
            ];
60
            $wgMWLoggerDefaultSpi = ['class' => NullSpi::class];
61
62
            $lang = new \LanguageZh();
63
            static::$converter = $lang->mConverter;
64
        }
65
66
        return static::$converter;
67
    }
68
69
    /**
70
     * Converts text to zh-*.
71
     *
72
     * @param string $variant
73
     * @param string $text
74
     *
75
     * @return string
76
     */
77
    public static function to($variant, $text)
78
    {
79
        return static::getConverter()->translate($text, $variant);
80
    }
81
82
    /**
83
     * Converts text to zh-cn.
84
     *
85
     * @param string $text
86
     * @return string
87
     */
88
    public static function toCN($text)
89
    {
90
        return self::to(self::ZH_CN, $text);
91
    }
92
93
    /**
94
     * Converts text to zh-tw.
95
     *
96
     * @param string $text
97
     * @return string
98
     */
99
    public static function toTW($text)
100
    {
101
        return self::to(self::ZH_TW, $text);
102
    }
103
104
    /**
105
     * Converts text to zh-hk.
106
     *
107
     * @param string $text
108
     * @return string
109
     */
110
    public static function toHK($text)
111
    {
112
        return self::to(self::ZH_HK, $text);
113
    }
114
115
    /**
116
     * Converts text to zh-mo.
117
     *
118
     * @param string $text
119
     * @return string
120
     */
121
    public static function toMO($text)
122
    {
123
        return self::to(self::ZH_MO, $text);
124
    }
125
}
126