d4fbaf303f0f4b4a87c08354547cc8ef75f1cc71
[lhc/web/wiklou.git] / languages / classes / LanguageZh.php
1 <?php
2 /**
3 * @package MediaWiki
4 * @subpackage Language
5 */
6 require_once( dirname(__FILE__).'/../LanguageConverter.php' );
7 require_once( dirname(__FILE__).'/LanguageZh_cn.php' );
8
9 class ZhConverter extends LanguageConverter {
10 function loadDefaultTables() {
11 require( "includes/ZhConversion.php" );
12 $this->mTables = array(
13 'zh-cn' => new ReplacementArray( $zh2CN ),
14 'zh-tw' => new ReplacementArray( $zh2TW ),
15 'zh-sg' => new ReplacementArray( array_merge($zh2CN, $zh2SG) ),
16 'zh-hk' => new ReplacementArray( array_merge($zh2TW, $zh2HK) ),
17 'zh' => new ReplacementArray
18 );
19 }
20
21 function postLoadTables() {
22 $this->mTables['zh-sg']->merge( $this->mTables['zh-cn'] );
23 $this->mTables['zh-hk']->merge( $this->mTables['zh-tw'] );
24 }
25
26 /* there shouldn't be any latin text in Chinese conversion, so no need
27 to mark anything.
28 $noParse is there for compatibility with LanguageConvert::markNoConversion
29 */
30 function markNoConversion($text, $noParse = false) {
31 return $text;
32 }
33
34 function convertCategoryKey( $key ) {
35 return $this->autoConvert( $key, 'zh-cn' );
36 }
37 }
38
39
40 /* class that handles both Traditional and Simplified Chinese
41 right now it only distinguish zh_cn, zh_tw, zh_sg and zh_hk.
42 */
43 class LanguageZh extends LanguageZh_cn {
44
45 function __construct() {
46 global $wgHooks;
47 parent::__construct();
48 $this->mConverter = new ZhConverter($this, 'zh',
49 array('zh', 'zh-cn', 'zh-tw', 'zh-sg', 'zh-hk'),
50 array('zh'=>'zh-cn',
51 'zh-cn'=>'zh-sg',
52 'zh-sg'=>'zh-cn',
53 'zh-tw'=>'zh-hk',
54 'zh-hk'=>'zh-tw'));
55 $wgHooks['ArticleSaveComplete'][] = $this->mConverter;
56 }
57
58
59 # this should give much better diff info
60 function segmentForDiff( $text ) {
61 return preg_replace(
62 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
63 "' ' .\"$1\"", $text);
64 }
65
66 function unsegmentForDiff( $text ) {
67 return preg_replace(
68 "/ ([\\xc0-\\xff][\\x80-\\xbf]*)/e",
69 "\"$1\"", $text);
70 }
71
72 // word segmentation
73 function stripForSearch( $string ) {
74 $fname="LanguageZh::stripForSearch";
75 wfProfileIn( $fname );
76
77 // eventually this should be a word segmentation
78 // for now just treat each character as a word
79 $t = preg_replace(
80 "/([\\xc0-\\xff][\\x80-\\xbf]*)/e",
81 "' ' .\"$1\"", $string);
82
83 //always convert to zh-cn before indexing. it should be
84 //better to use zh-cn for search, since conversion from
85 //Traditional to Simplified is less ambiguous than the
86 //other way around
87
88 $t = $this->mConverter->autoConvert($t, 'zh-cn');
89 $t = parent::stripForSearch( $t );
90 wfProfileOut( $fname );
91 return $t;
92
93 }
94
95 function convertForSearchResult( $termsArray ) {
96 $terms = implode( '|', $termsArray );
97 $terms = implode( '|', $this->mConverter->autoConvertToAllVariants( $terms ) );
98 $ret = array_unique( explode('|', $terms) );
99 return $ret;
100 }
101
102 }
103 ?>