2 require_once( "LanguageZh_cn.php");
3 require_once( "LanguageZh_tw.php");
4 require_once( "LanguageZh_sg.php");
5 require_once( "LanguageZh_hk.php");
8 hook to refresh the cache of conversion tables when
9 MediaWiki:zhconversiontable* is updated
11 function zhOnArticleSaveComplete($article, $user, $text, $summary, $isminor, $iswatch, $section) {
12 $titleobj = $article->getTitle();
13 if($titleobj->getNamespace() == NS_MEDIAWIKI
) {
14 global $wgContLang; // should be an LanguageZh.
15 if(get_class($wgContLang) != 'languagezh')
18 $title = $titleobj->getDBkey();
19 $t = explode('/', $title, 2);
20 if( $t[0] == 'Zhconversiontable' ) {
21 if(!in_array($t[1], array('zh-cn', 'zh-tw', 'zh-sg', 'zh-hk')))
23 $wgContLang->reloadTables();
28 $wgHooks['ArticleSaveComplete'][] = 'zhOnArticleSaveComplete';
30 /* class that handles both Traditional and Simplified Chinese
31 right now it only distinguish zh_cn and zh_tw (actuall, zh_cn and
32 non-zh_cn), will add support for zh_sg, zh_hk, etc, later.
34 class LanguageZh
extends LanguageZh_cn
{
36 var $mZhLanguageCode=false;
37 var $mTables=false; //the mapping tables
38 var $mTablesLoaded = false;
40 var $mDoTitleConvert = true;
41 function LanguageZh() {
43 $this->mCacheKey
= $wgDBname . ":zhtables";
46 function reloadTables() {
48 $wgMemc->delete($this->mCacheKey
);
49 $this->mTablesLoaded
=false;
53 // load conversion tables either from the cache or the disk
54 function loadTables() {
56 if( $this->mTablesLoaded
)
58 $this->mTablesLoaded
= true;
59 $this->mTables
= $wgMemc->get( $this->mCacheKey
);
60 if( empty( $this->mTables
) ) {
61 global $wgMessageCache;
62 require( "includes/ZhConversion.php" );
63 $this->mTables
= array();
64 $this->mTables
['zh-cn'] = $zh2CN;
65 $this->mTables
['zh-tw'] = $zh2TW;
66 $this->mTables
['zh-sg'] = $zh2SG;
67 $this->mTables
['zh-hk'] = $zh2HK;
68 if( is_object( $wgMessageCache ) ){
69 $cached = $this->parseCachedTable( $wgMessageCache->get( 'zhconversiontable/zh-cn', true, true, true ) );
70 $this->mTables
['zh-cn'] = array_merge($this->mTables
['zh-cn'], $cached);
72 $cached = $this->parseCachedTable( $wgMessageCache->get( 'zhconversiontable/zh-tw', true, true, true ) );
73 $this->mTables
['zh-tw'] = array_merge($this->mTables
['zh-tw'], $cached);
75 $cached = $this->parseCachedTable( $wgMessageCache->get( 'zhconversiontable/zh-sg', true, true, true ) );
76 $this->mTables
['zh-sg'] = array_merge($this->mTables
['zh-sg'], $cached);
78 $cached = $this->parseCachedTable( $wgMessageCache->get( 'zhconversiontable/zh-hk', true, true, true ) );
79 $this->mTables
['zh-hk'] = array_merge($this->mTables
['zh-hk'], $cached);
82 $wgMemc->set($this->mCacheKey
, $this->mTables
, 43200);
87 parse the conversion table stored in the cache
89 the table should be in the following format:
97 function parseCachedTable($txt) {
98 /* $txt should be enclosed by -{ and }- */
99 $a = explode( '-{', $txt);
103 $b = explode( '}-', $a[0]);
105 $stripped = str_replace(array('*','#'), '', $b[0]);
106 $table = explode( ';', $stripped );
108 foreach( $table as $t ) {
109 $m = explode( '=>', $t );
110 if( count( $m ) != 2)
112 $ret[trim($m[0])] = trim($m[1]);
118 get preferred language variants.
120 function getPreferredVariant() {
121 global $wgUser, $wgRequest;
123 if($this->mZhLanguageCode
)
124 return $this->mZhLanguageCode
;
126 // see if the preference is set in the request
127 $zhreq = $wgRequest->getText( 'variant' );
128 if( in_array( $zhreq, $this->getVariants() ) ) {
129 $this->mZhLanguageCode
= $zhreq;
133 // get language variant preference from logged in users
134 if($wgUser->getID()!=0) {
135 $this->mZhLanguageCode
= $wgUser->getOption('variant');
138 if( !$this->mZhLanguageCode
) {
139 // see if some zh- variant is set in the http header,
140 $this->mZhLanguageCode
="zh";
141 $header = str_replace( '_', '-', strtolower($_SERVER["HTTP_ACCEPT_LANGUAGE"]));
142 $zh = strstr($header, 'zh-');
144 $this->mZhLanguageCode
= substr($zh,0,5);
147 return $this->mZhLanguageCode
;
150 # this should give much better diff info
151 function segmentForDiff( $text ) {
153 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
154 "' ' .\"$1\"", $text);
157 function unsegmentForDiff( $text ) {
159 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
163 function autoConvert($text, $toVariant=false) {
164 $fname="LanguageZh::autoConvert";
165 wfProfileIn( $fname );
167 if(!$this->mTablesLoaded
)
171 $toVariant = $this->getPreferredVariant();
173 switch( $toVariant ) {
174 case 'zh-cn': $ret = strtr($text, $this->mTables
['zh-cn']);break;
175 case 'zh-tw': $ret = strtr($text, $this->mTables
['zh-tw']);break;
176 case 'zh-sg': $ret = strtr(strtr($text, $this->mTables
['zh-cn']), $this->mTables
['zh-sg']);break;
177 case 'zh-hk': $ret = strtr(strtr($text, $this->mTables
['zh-tw']), $this->mTables
['zh-hk']);break;
178 default: $ret = $text;
180 wfProfileOut( $fname );
184 function autoConvertToAllVariants($text) {
185 $fname="LanguageZh::autoConvertToAllVariants";
186 wfProfileIn( $fname );
187 if( !$this->mTablesLoaded
)
191 $ret['zh-cn'] = strtr($text, $this->mTables
['zh-cn']);
192 $ret['zh-tw'] = strtr($text, $this->mTables
['zh-tw']);
193 $ret['zh-sg'] = strtr(strtr($text, $this->mTables
['zh-cn']), $this->mTables
['zh-sg']);
194 $ret['zh-hk'] = strtr(strtr($text, $this->mTables
['zh-tw']), $this->mTables
['zh-hk']);
195 wfProfileOut( $fname );
199 # convert text to different variants of a language. the automatic
200 # conversion is done in autoConvert(). here we parse the text
201 # marked with -{}-, which specifies special conversions of the
202 # text that can not be accomplished in autoConvert()
204 # syntax of the markup:
205 # -{code1:text1;code2:text2;...}- or
206 # -{text}- in which case no conversion should take place for text
207 function convert( $text , $isTitle=false) {
208 global $wgDisableLangConversion;
209 if($wgDisableLangConversion)
212 $mw =& MagicWord
::get( MAG_NOTITLECONVERT
);
213 if( $mw->matchAndRemove( $text ) )
214 $this->mDoTitleConvert
= false;
216 // no conversion if redirecting
217 $mw =& MagicWord
::get( MAG_REDIRECT
);
218 if( $mw->matchStart( $text ))
222 if( !$this->mDoTitleConvert
)
226 $isredir = $wgRequest->getText( 'redirect', 'yes' );
227 $action = $wgRequest->getText( 'action' );
228 if ( $isredir == 'no' ||
$action == 'edit' ) {
232 return $this->autoConvert($text);
236 $plang = $this->getPreferredVariant();
237 $fallback = $this->getVariantFallback($plang);
239 $tarray = explode("-{", $text);
240 $tfirst = array_shift($tarray);
241 $text = $this->autoConvert($tfirst);
242 foreach($tarray as $txt) {
243 $marked = explode("}-", $txt);
245 $choice = explode(";", $marked{0});
246 if(!array_key_exists(1, $choice)) {
247 /* a single choice */
252 foreach($choice as $c) {
253 $v = explode(":", $c);
254 if(!array_key_exists(1, $v)) {
255 //syntax error in the markup, give up
259 $content = trim($v{1});
260 if($code == $plang) {
264 if($code == $fallback)
274 if(array_key_exists(1, $marked))
275 $text .= $this->autoConvert($marked{1});
282 function getVariants() {
283 return array("zh", "zh-cn", "zh-tw", "zh-sg", "zh-hk");
286 function getVariantFallback($v) {
288 case 'zh': return 'zh-cn'; break;
289 case 'zh-cn': return 'zh-sg'; break;
290 case 'zh-sg': return 'zh-cn'; break;
291 case 'zh-tw': return 'zh-hk'; break;
292 case 'zh-hk': return 'zh-tw'; break;
298 function stripForSearch( $string ) {
299 $fname="LanguageZh::stripForSearch";
300 wfProfileIn( $fname );
302 // eventually this should be a word segmentation
303 // for now just treat each character as a word
305 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
306 "' ' .\"$1\"", $string);
308 //always convert to zh-cn before indexing. it should be
309 //better to use zh-cn for search, since conversion from
310 //Traditional to Simplified is less ambiguous than the
313 $t = $this->autoConvert($t, 'zh-cn');
314 $t = LanguageUtf8
::stripForSearch( $t );
315 wfProfileOut( $fname );
320 function convertForSearchResult( $termsArray ) {
321 $terms = implode( '|', $termsArray );
322 $terms = implode( '|', $this->autoConvertToAllVariants( $terms ) );
323 $ret = array_unique( explode('|', $terms) );
327 function findVariantLink( &$link, &$nt ) {
328 static $count=0; //used to limit this operation
329 static $cache=array();
330 global $wgDisableLangConversion;
331 $pref = $this->getPreferredVariant();
332 if( $wgDisableLangConversion ||
$pref == 'zh' ||
$count > 50)
335 $variants = $this->autoConvertToAllVariants($link);
336 if($variants == false) //give up
338 foreach( $variants as $v ) {
339 if(isset($cache[$v]))
342 $varnt = Title
::newFromText( $v );
343 if( $varnt && $varnt->getArticleID() > 0 ) {
351 function getExtraHashOptions() {
353 $variant = $this->getPreferredVariant();
354 return '!' . $variant ;