/
mediawiki-zhconverter.inc.php
212 lines (176 loc) · 7.27 KB
/
mediawiki-zhconverter.inc.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
<?php
# MediaWiki-ZhConverter
# Copyright (C) 2008 tszming (tszming@gmail.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
# http://www.gnu.org/copyleft/gpl.html
/**
* MediaWiki-ZhConverter
*
* A very simple helper class for calling MediaWiki's internal converter to do the
* conversion for us. This helper work with both PHP4/PHP5 version of MediaWiki
* and should work under both Windows and Linux.
*
* Please note that this simple library is not optimized for speed, but designed
* for the ease of maintenance, I didn't modify a single line of codes in the original
* MediaWiki, you have been warned for this.
*
* Some information about Chinese conversion you might be interested:
*
* For the Chinese conversion table used in MediaWiki, you can refer to:
* MEDIAWIKI_PATH/includes/ZhConversion.php
*
* It should be automatically build by a script:
* MEDIAWIKI_PATH/includes/zhtable/Makefile
*
* Latest Chinese mappings can be found online, depending on your needs, you might
* want to merge these mappings back to the ZhConversion.php:
*
* 1. http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-hans
* 2. http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-hant
* 3. http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-cn
* 4. http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-sg
* 5. http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-tw
* 6. http://zh.wikipedia.org/w/index.php?title=MediaWiki:Conversiontable/zh-hk
*
* @author tszming (tszming@gmail.com)
* @version 1.0.1
*
*/
if (!defined("MEDIAWIKI_PATH")) {
echo "Constant '<b>MEDIAWIKI_PATH</b>' must be defined!";
die();
}
if (!file_exists(MEDIAWIKI_PATH)) {
echo "MediaWiki not found on : " . MEDIAWIKI_PATH;
die();
}
/* Add to the include path */
set_include_path(get_include_path() . PATH_SEPARATOR . MEDIAWIKI_PATH);
$IP = MEDIAWIKI_PATH;
/**
* Needed some dummy classes/functions/variables in order to cheat the MediaWiki.
**/
define('MEDIAWIKI', true);
class MagicWord {
function &get( $id ) {
static $instance;
if (!isset($instance)) {
$instance = new MagicWord();
}
return $instance;
}
function matchAndRemove() { return false; }
function matchStart() { return false;}
}
function wfProfileIn() { return false; }
function wfProfileOut() { return false; }
class WebRequest {
function getText() { return false; }
}
class FakeMemCachedClient {
function add ($key, $val, $exp = 0) { return true; }
function decr ($key, $amt=1) { return null; }
function delete ($key, $time = 0) { return false; }
function disconnect_all () { }
function enable_compress ($enable) { }
function forget_dead_hosts () { }
function get ($key) { return null; }
function get_multi ($keys) { return array_pad(array(), count($keys), null); }
function incr ($key, $amt=1) { return null; }
function replace ($key, $value, $exp=0) { return false; }
function run_command ($sock, $cmd) { return null; }
function set ($key, $value, $exp=0){ return true; }
function set_compress_threshold ($thresh){ }
function set_debug ($dbg) { }
function set_servers ($list) { }
}
$wgRequest; $wgMemc;
class MediaWikiZhConverter {
var $_language;
var $_converter;
/**
* Singleton to make sure only ONE object is initialized at anytime.
*/
static function &getConverter() {
static $instance;
if (! isset($instance) ) {
/* Initialize some global variables needed */
global $wgRequest, $wgMemc;
global $wgLocalisationCacheConf, $wgDisabledVariants, $wgExtraLanguageNames;
global $wgLangConvMemc, $wgMessageCacheType, $wgObjectCaches;
global $wgLanguageConverterCacheType, $wgMWLoggerDefaultSpi, $wgMainWANCache, $wgWANObjectCaches;
$wgRequest = new WebRequest();
$wgMemc = new FakeMemCachedClient;
$wgLocalisationCacheConf['class'] = 'FakeMemCachedClient';
$wgLocalisationCacheConf['storeClass'] = 'LCStore_Null';
$wgDisabledVariants = array();
$wgExtraLanguageNames = array();
$wgLangConvMemc = new FakeMemCachedClient;
define('CACHE_NONE', 'FAKE');
$wgObjectCaches = array(
'FAKE' => array( 'class' => 'FakeMemCachedClient' ),
);
$wgMessageCacheType = 'FAKE';
$wgLanguageConverterCacheType = 'FAKE';
$wgMainWANCache = 'FAKE';
$wgWANObjectCaches = array(
'FAKE' => array(
'class' => 'WANObjectCache',
'cacheId' => 'FAKE',
'pool' => 'mediawiki-main-none',
'relayerConfig' => array( 'class' => 'EventRelayerNull' )
)
);
$wgMWLoggerDefaultSpi = array( 'class' => '\\MediaWiki\\Logger\\NullSpi' );
require_once "includes/GlobalFunctions.php";
require_once "includes/AutoLoader.php";
require_once "vendor/autoload.php";
/* Switch for PHP4 and PHP5 version of MediaWiki */
if (file_exists( MEDIAWIKI_PATH . "languages/LanguageZh.php")) {
require_once "languages/LanguageZh.php";
} else {
require_once "languages/classes/LanguageZh.php";
require_once "includes/libs/StringUtils.php";
}
$instance = new MediaWikiZhConverter();
$instance->_language = new LanguageZh();
$instance->_converter = $instance->_language->mConverter;
}
return $instance;
}
/**
* Convert in action.
*
* @param string $str text to be converted
* @param string $variant target language code, e.g. zh, zh-cn, zh-tw, zh-sg & zh-hk
*
* @return string the converted text
*/
static function convert($str, $variant) {
$converter =& MediaWikiZhConverter::getConverter();
return $converter->_converter->translate( $str, $variant );
}
/**
* Get all variants available.
*
* @return string All available variants, e.g. zh, zh-cn, zh-tw, zh-sg & zh-hk
*/
function getVariants() {
$converter =& MediaWikiZhConverter::getConverter();
return $converter->_converter->getVariants();
}
}
?>