From dd54bbff45cad5518ea1293ff45288319e3a7b23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=87=8F=E5=AD=90=E5=A4=8D=E5=90=88=E6=80=81?= Date: Wed, 29 Jul 2020 22:53:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=88=86=E7=B1=BB=E4=B8=AD?= =?UTF-8?q?=E7=9A=84=E5=88=86=E7=B1=BB=E4=BB=8D=E7=84=B6=E6=98=BE=E7=A4=BA?= =?UTF-8?q?=E4=B8=BA=E6=B1=89=E5=AD=97=E7=9A=84=E9=97=AE=E9=A2=98=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 同时更改结构,为以后的分离拼音化库做准备。 --- includes/Hanzi2Pinyin.php | 30 ------------------------------ includes/Hooks.php | 12 ++++++------ includes/LatinizeCollation.php | 26 ++++++++++++++++++++++++-- includes/Utils.php | 26 ++++++++++++++++++++++++++ specials/SpecialCustomUrl.php | 4 ++-- 5 files changed, 58 insertions(+), 40 deletions(-) diff --git a/includes/Hanzi2Pinyin.php b/includes/Hanzi2Pinyin.php index 27d0116..5f9aa03 100644 --- a/includes/Hanzi2Pinyin.php +++ b/includes/Hanzi2Pinyin.php @@ -147,34 +147,4 @@ class Hanzi2Pinyin { } 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); - } } \ No newline at end of file diff --git a/includes/Hooks.php b/includes/Hooks.php index b04bf5f..0d6cfdb 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -77,9 +77,9 @@ class Hooks { global $wgLatinizeUrlConfig; $titleText = $wikiPage->getTitle()->getText(); $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); - $pinyin = $convertor->parse($titleText); - $slug = $convertor->pinyin2String($pinyin); - Utils::addTitleSlugMap($titleText, $slug, $pinyin); + $latinize = $convertor->parse($titleText); + $slug = Utils::wordListToUrl($latinize); + Utils::addTitleSlugMap($titleText, $slug, $latinize); } public static function onTitleMoveComplete(Title &$title, Title &$newTitle, User $user, $oldid, $newid, $reason, $revision){ @@ -89,9 +89,9 @@ class Hooks { global $wgLatinizeUrlConfig; $titleText = $newTitle->getText(); $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); - $pinyin = $convertor->parse($titleText); - $slug = $convertor->pinyin2String($pinyin); - Utils::addTitleSlugMap($titleText, $slug, $pinyin); + $latinize = $convertor->parse($titleText); + $slug = Utils::wordListToUrl($latinize); + Utils::addTitleSlugMap($titleText, $slug, $latinize); } public static function onApiBeforeMain(\ApiBase &$processor){ diff --git a/includes/LatinizeCollation.php b/includes/LatinizeCollation.php index 3619358..52f85e7 100644 --- a/includes/LatinizeCollation.php +++ b/includes/LatinizeCollation.php @@ -2,14 +2,36 @@ namespace LatinizeUrl; use Collation; +use MediaWiki\MediaWikiServices; 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){ $slug = Utils::getSlugByTitle($string); if($slug){ return ucfirst($slug); } else { - return $string; + return $this->getLatinize($string); } } @@ -18,7 +40,7 @@ class LatinizeCollation extends Collation { if($slug){ return strtoupper($slug[0]); } else { - return mb_substr($string, 0, 1, 'UTF-8'); + return strtoupper(mb_substr($this->getLatinize($string), 0, 1, 'UTF-8')); } } } \ No newline at end of file diff --git a/includes/Utils.php b/includes/Utils.php index 298a87a..7a7d250 100644 --- a/includes/Utils.php +++ b/includes/Utils.php @@ -309,4 +309,30 @@ class Utils { public static function getVersion(){ 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; + } } \ No newline at end of file diff --git a/specials/SpecialCustomUrl.php b/specials/SpecialCustomUrl.php index ba90138..c511be1 100644 --- a/specials/SpecialCustomUrl.php +++ b/specials/SpecialCustomUrl.php @@ -88,8 +88,8 @@ class SpecialCustomUrl extends FormSpecialPage if(empty($slug)){ //自动生成 $titleText = $this->title->getText(); $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); - $pinyin = $convertor->parse($titleText); - $slug = $convertor->pinyin2String($pinyin); + $latinize = $convertor->parse($titleText); + $slug = Utils::wordListToUrl($latinize); } else { $slug = str_replace('_', ' ', $slug); }