修复分类中的分类仍然显示为汉字的问题。

同时更改结构,为以后的分离拼音化库做准备。
master
落雨楓 5 years ago
parent c7aec2832b
commit dd54bbff45

@ -147,34 +147,4 @@ class Hanzi2Pinyin {
} }
return $json["data"]; return $json["data"];
} }
public function pinyin2String($sentenceList){
$strBuilder = [];
foreach($sentenceList as $pinyinList){
if(is_array($pinyinList)){
$segStrBuilder = [];
foreach($pinyinList as $pinyinGroup){
if(is_array($pinyinGroup)){
$groupStrBuilder = [];
foreach($pinyinGroup as $pinyin){
$groupStrBuilder[] = $this->initialCapital($pinyin);
}
$segStrBuilder[] = implode('', $groupStrBuilder);
} else {
$segStrBuilder[] = $pinyinGroup;
}
}
$strBuilder[] = implode('-', $segStrBuilder);
} else {
$strBuilder[] = $pinyinList;
}
}
$str = implode('-', $strBuilder);
$str = preg_replace('/-([\x20-\x2f\x3a-\x40\x5b-\x60\x7a-\x7f])-/', '$1', $str);
return $str;
}
public function initialCapital($text){
return strtoupper(substr($text, 0, 1)) . substr($text, 1);
}
} }

@ -77,9 +77,9 @@ class Hooks {
global $wgLatinizeUrlConfig; global $wgLatinizeUrlConfig;
$titleText = $wikiPage->getTitle()->getText(); $titleText = $wikiPage->getTitle()->getText();
$convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig);
$pinyin = $convertor->parse($titleText); $latinize = $convertor->parse($titleText);
$slug = $convertor->pinyin2String($pinyin); $slug = Utils::wordListToUrl($latinize);
Utils::addTitleSlugMap($titleText, $slug, $pinyin); Utils::addTitleSlugMap($titleText, $slug, $latinize);
} }
public static function onTitleMoveComplete(Title &$title, Title &$newTitle, User $user, $oldid, $newid, $reason, $revision){ public static function onTitleMoveComplete(Title &$title, Title &$newTitle, User $user, $oldid, $newid, $reason, $revision){
@ -89,9 +89,9 @@ class Hooks {
global $wgLatinizeUrlConfig; global $wgLatinizeUrlConfig;
$titleText = $newTitle->getText(); $titleText = $newTitle->getText();
$convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig);
$pinyin = $convertor->parse($titleText); $latinize = $convertor->parse($titleText);
$slug = $convertor->pinyin2String($pinyin); $slug = Utils::wordListToUrl($latinize);
Utils::addTitleSlugMap($titleText, $slug, $pinyin); Utils::addTitleSlugMap($titleText, $slug, $latinize);
} }
public static function onApiBeforeMain(\ApiBase &$processor){ public static function onApiBeforeMain(\ApiBase &$processor){

@ -2,14 +2,36 @@
namespace LatinizeUrl; namespace LatinizeUrl;
use Collation; use Collation;
use MediaWiki\MediaWikiServices;
class LatinizeCollation extends Collation { class LatinizeCollation extends Collation {
private $cache = null;
public function __construct(){
$this->cache = MediaWikiServices::getInstance()->getMainWANObjectCache();
}
private function getLatinize($string){
global $wgLatinizeUrlConfig;
return $this->cache->getWithSetCallback(
$this->cache->makeKey('latinizeConvert', $string),
$this->cache::TTL_MINUTE * 10,
function() use($string, $wgLatinizeUrlConfig){
$convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig);
$latinize = $convertor->parse($string);
$slug = Utils::wordListToUrl($latinize);
return $slug;
}
);
}
public function getSortKey($string){ public function getSortKey($string){
$slug = Utils::getSlugByTitle($string); $slug = Utils::getSlugByTitle($string);
if($slug){ if($slug){
return ucfirst($slug); return ucfirst($slug);
} else { } else {
return $string; return $this->getLatinize($string);
} }
} }
@ -18,7 +40,7 @@ class LatinizeCollation extends Collation {
if($slug){ if($slug){
return strtoupper($slug[0]); return strtoupper($slug[0]);
} else { } else {
return mb_substr($string, 0, 1, 'UTF-8'); return strtoupper(mb_substr($this->getLatinize($string), 0, 1, 'UTF-8'));
} }
} }
} }

@ -309,4 +309,30 @@ class Utils {
public static function getVersion(){ public static function getVersion(){
return ExtensionRegistry::getInstance()->getAllThings()['LatinizeUrl']['version']; return ExtensionRegistry::getInstance()->getAllThings()['LatinizeUrl']['version'];
} }
public static function wordListToUrl($sentenceList){
$strBuilder = [];
foreach($sentenceList as $pinyinList){
if(is_array($pinyinList)){
$segStrBuilder = [];
foreach($pinyinList as $pinyinGroup){
if(is_array($pinyinGroup)){
$groupStrBuilder = [];
foreach($pinyinGroup as $pinyin){
$groupStrBuilder[] = ucfirst($pinyin);
}
$segStrBuilder[] = implode('', $groupStrBuilder);
} else {
$segStrBuilder[] = $pinyinGroup;
}
}
$strBuilder[] = implode('-', $segStrBuilder);
} else {
$strBuilder[] = $pinyinList;
}
}
$str = implode('-', $strBuilder);
$str = preg_replace('/-([\x20-\x2f\x3a-\x40\x5b-\x60\x7a-\x7f])-/', '$1', $str);
return $str;
}
} }

@ -88,8 +88,8 @@ class SpecialCustomUrl extends FormSpecialPage
if(empty($slug)){ //自动生成 if(empty($slug)){ //自动生成
$titleText = $this->title->getText(); $titleText = $this->title->getText();
$convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig);
$pinyin = $convertor->parse($titleText); $latinize = $convertor->parse($titleText);
$slug = $convertor->pinyin2String($pinyin); $slug = Utils::wordListToUrl($latinize);
} else { } else {
$slug = str_replace('_', ' ', $slug); $slug = str_replace('_', ' ', $slug);
} }

Loading…
Cancel
Save