diff --git a/includes/Hanzi2Pinyin.php b/ChineseConvertor/ChineseConvertor.php similarity index 88% rename from includes/Hanzi2Pinyin.php rename to ChineseConvertor/ChineseConvertor.php index 5f9aa03..a8f24bc 100644 --- a/includes/Hanzi2Pinyin.php +++ b/ChineseConvertor/ChineseConvertor.php @@ -9,20 +9,35 @@ use Fukuball\Jieba\Finalseg; use Fukuball\Jieba\Posseg; use Overtrue\Pinyin\Pinyin; -class Hanzi2Pinyin { +class ChineseConvertor extends BaseConvertor { private $config; + private static $standalone = null; private static $libLoaded = false; private static $jiebaLoaded = false; private static $pinyinParser = null; + public static function standalone(){ + if(!self::$standalone){ + global $wgLatinizeUrlChineseConvertorConfig; + self::$standalone = new self($wgLatinizeUrlChineseConvertorConfig); + } + return self::$standalone; + } + + public static function onGetConvertor($langCode, &$convertor){ + if(in_array($langCode, ['zh-cn', 'zh-hans'])){ + $convertor = self::standalone(); + } + return true; + } + public function __construct($config){ $this->config = $config; } - public function parse($hanzi, $method = false){ - if(!$method){ - $method = $this->config['parser'] . 'Parse'; - } + public function parse($hanzi){ + $method = $this->config['parser'] . 'Parse'; + if(is_callable([$this, $method])){ return call_user_func([$this, $method], $hanzi); } else { diff --git a/ChineseConvertor/extension.json b/ChineseConvertor/extension.json new file mode 100644 index 0000000..e290c40 --- /dev/null +++ b/ChineseConvertor/extension.json @@ -0,0 +1,28 @@ +{ + "name": "ChineseConvertor", + "author": "hyperzlib", + "url": "https://github.com/Isekai-Project/mediawiki-extension-LatinizeUrl", + "descriptionmsg": "latinizeurl-chinese-convertor-desc", + "version": "1.0.1", + "license-name": "MIT", + "type": "other", + "MessagesDirs": { + "ChineseConvertor": [ + "i18n" + ] + }, + "AutoloadClasses": { + "LatinizeUrl\\ChineseConvertor": "ChineseConvertor.php" + }, + "Hooks": { + "LatinizeUrlGetConvertor": [ + "LatinizeUrl\\ChineseConvertor::onGetConvertor" + ] + }, + "config": { + "LatinizeUrlChineseConvertorConfig": { + "parser": "inner" + } + }, + "manifest_version": 1 +} diff --git a/ChineseConvertor/i18n/zh-hans.json b/ChineseConvertor/i18n/zh-hans.json new file mode 100644 index 0000000..89ce00a --- /dev/null +++ b/ChineseConvertor/i18n/zh-hans.json @@ -0,0 +1,3 @@ +{ + "latinizeurl-chinese-convertor-desc": "LatinizeUrl用,汉字转拼音转换器。" +} \ No newline at end of file diff --git a/JapaneseConvertor/JapaneseConvertor.php b/JapaneseConvertor/JapaneseConvertor.php new file mode 100644 index 0000000..9a78e33 --- /dev/null +++ b/JapaneseConvertor/JapaneseConvertor.php @@ -0,0 +1,55 @@ +config = $config; + } + + public function parse($kanji){ + if(!isset($this->config['url'])){ + throw new Exception('LatinizeUrl remote api url not set.'); + } + $factory = new HttpRequestFactory(); + $req = $factory->create($this->config['url'], [ + 'method' => 'POST', + 'postData' => [ + 'sentence' => $kanji + ], + ], __METHOD__); + $status = \Status::wrap($req->execute()); + if(!$status->isOK()){ + throw new Exception('Cannot use LatinizeUrl remote api.'); + } + $json = \FormatJson::decode($req->getContent(), true); + if(isset($json["error"])){ + throw new Exception('LatinizeUrl remote api error: ' . $json["error"]); + } + if(!isset($json["status"]) || $json["status"] !== 1){ + throw new Exception('Cannot use LatinizeUrl remote api.'); + } + return $json["data"]; + } +} \ No newline at end of file diff --git a/JapaneseConvertor/extension.json b/JapaneseConvertor/extension.json new file mode 100644 index 0000000..bae4a20 --- /dev/null +++ b/JapaneseConvertor/extension.json @@ -0,0 +1,28 @@ +{ + "name": "JapaneseConvertor", + "author": "hyperzlib", + "url": "https://github.com/Isekai-Project/mediawiki-extension-LatinizeUrl", + "descriptionmsg": "latinizeurl-japanese-convertor-desc", + "version": "1.0.1", + "license-name": "MIT", + "type": "other", + "MessagesDirs": { + "JapaneseConvertor": [ + "i18n" + ] + }, + "AutoloadClasses": { + "LatinizeUrl\\JapaneseConvertor": "JapaneseConvertor.php" + }, + "Hooks": { + "LatinizeUrlGetConvertor": [ + "LatinizeUrl\\JapaneseConvertor::onGetConvertor" + ] + }, + "config": { + "LatinizeUrlJapaneseonvertorConfig": { + "parser": "remote" + } + }, + "manifest_version": 1 +} diff --git a/JapaneseConvertor/i18n/ja.json b/JapaneseConvertor/i18n/ja.json new file mode 100644 index 0000000..d7e018f --- /dev/null +++ b/JapaneseConvertor/i18n/ja.json @@ -0,0 +1,3 @@ +{ + "latinizeurl-japanese-convertor-desc": "LatinizeUrlの専用,漢字をローマ字に変換するためのツール。" +} \ No newline at end of file diff --git a/LatinizeUrl.alias.php b/LatinizeUrl.alias.php index 8f14925..cfdc678 100644 --- a/LatinizeUrl.alias.php +++ b/LatinizeUrl.alias.php @@ -11,4 +11,8 @@ $specialPageAliases['zh-hans'] = [ $specialPageAliases['zh-hant'] = [ 'CustomUrl' => '客制化URL' +]; + +$specialPageAliases['ja'] = [ + 'CustomUrl' => 'URLを編集' ]; \ No newline at end of file diff --git a/README.md b/README.md index 1b84fbd..7f9d564 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,7 @@ 添加 ```php wfLoadExtension('LatinizeUrl'); +wfLoadExtension('LatinizeUrl/ChineseConvertor'); ``` 到LocalSettings.php 执行maintenance/update.php或者网页版更新器 @@ -19,24 +20,26 @@ Hooks::run( 'InitializeParseTitle', [ &$ret, $request ] ); ## 配置 ### 使用PHP内置的解析器(较慢) ```php -$wgLatinizeUrlConfig['parser'] = 'inner'; +$LatinizeUrlChineseConvertorConfig['parser'] = 'inner'; ``` 开启分词功能 ```php -$wgLatinizeUrlConfig['cutWord'] = true; +$LatinizeUrlChineseConvertorConfig['cutWord'] = true; ``` 开启分词后会很慢,建议使用daemon解析 ### 使用daemon api解析 项目地址:[Isekai-LatinizeUrl-Backend](https://github.com/Isekai-Project/Isekai-LatinizeUrl-Backend) ```php -$wgLatinizeUrlConfig['parser'] = 'api'; -$wgLatinizeUrlConfig['url'] = '指向daemon的url,默认的path是网址:端口/asciiurl/hanzi2pinyin'; -$wgLatinizeUrlConfig['fallback'] = false; +$LatinizeUrlChineseConvertorConfig['parser'] = 'api'; +$LatinizeUrlChineseConvertorConfig['url'] = '指向daemon的url,默认的path是网址:端口/asciiurl/hanzi2pinyin'; +$LatinizeUrlChineseConvertorConfig['fallback'] = false; +//日语转换 +$LatinizeUrlJapaneseConvertorConfig['url'] = '指向daemon的url,默认的path是网址:端口/asciiurl/kanji2romaji'; ``` 也可以配置在daemon离线时自动退回php解析 ```php -$wgLatinizeUrlConfig['fallback'] = 'inner'; +$LatinizeUrlChineseConvertorConfig['fallback'] = 'inner'; ``` 另:虚拟主机可以使用异世界百科的开放api ``` diff --git a/extension.json b/extension.json index 441424b..fd6ec78 100644 --- a/extension.json +++ b/extension.json @@ -2,7 +2,7 @@ "name": "LatinizeUrl", "author": "hyperzlib", "url": "https://github.com/Isekai-Project/mediawiki-extension-LatinizeUrl", - "descriptionmsg": "latinizeurl_desc", + "descriptionmsg": "latinizeurl-desc", "version": "1.0.1", "license-name": "MIT", "type": "other", @@ -16,7 +16,7 @@ }, "AutoloadClasses": { "LatinizeUrl\\Hooks": "includes/Hooks.php", - "LatinizeUrl\\Hanzi2Pinyin": "includes/Hanzi2Pinyin.php", + "LatinizeUrl\\BaseConvertor": "includes/BaseConvertor.php", "LatinizeUrl\\Utils": "includes/Utils.php", "LatinizeUrl\\LatinizeCollation": "includes/LatinizeCollation.php", "LatinizeUrl\\Patcher": "includes/Patcher.php", @@ -67,9 +67,6 @@ "remoteExtPath": "LatinizeUrl" }, "config": { - "LatinizeUrlConfig": { - "parser": "inner" - }, "LatinizeUrlForceRedirect": true }, "manifest_version": 1 diff --git a/i18n/en.json b/i18n/en.json new file mode 100644 index 0000000..92d5377 --- /dev/null +++ b/i18n/en.json @@ -0,0 +1,9 @@ +{ + "latinizeurl-desc": "Latinize none-ASCII chars in url.", + "latinizeurl-customurl": "Custom URL", + "customurl-legend": "Custom URL", + "customurl-url-field-label": "The URL you want to use:", + "customurl-url-field-help": "Recommand using ASCII chars. If empty, will convert automatically to ASCII.", + "customurl-set-success": "The URL of this page has been set to [[$1|$2]].", + "customurl-set-failed": "This URL is invalid." +} \ No newline at end of file diff --git a/i18n/ja.json b/i18n/ja.json new file mode 100644 index 0000000..411e3f7 --- /dev/null +++ b/i18n/ja.json @@ -0,0 +1,9 @@ +{ + "latinizeurl-desc": "漢字をローマ字に変換する", + "latinizeurl-customurl": "URLを編集", + "customurl-legend": "URLを編集", + "customurl-url-field-label": "使用したいURL:", + "customurl-url-field-help": "ASCII文字をお勧めします。入力しない場合は、漢字に対応するローマ字が自動的に生成されます。", + "customurl-set-success": "このページの新しいURLは [[$1|$2]].", + "customurl-set-failed": "このURLは無効です。" +} \ No newline at end of file diff --git a/i18n/kor.json b/i18n/kor.json new file mode 100644 index 0000000..efe74b8 --- /dev/null +++ b/i18n/kor.json @@ -0,0 +1,9 @@ +{ + "latinizeurl-desc": "한자를 로마자 변환하는 도구.", + "latinizeurl-customurl": "URL을 편집", + "customurl-legend": "URL을 편집", + "customurl-url-field-label": "사용하고자하는 URL :", + "customurl-url-field-help": "ASCII 문자를 권장합니다. 입력하지 않으면, 한자에 대응하는 로마자 자동으로 생성됩니다.", + "customurl-set-success": "이 페이지의 새로운 URL은 [[$1|$2]].", + "customurl-set-failed": "이 URL이 잘못되었습니다." +} \ No newline at end of file diff --git a/i18n/zh-hans.json b/i18n/zh-hans.json index eb951b6..1314cd4 100644 --- a/i18n/zh-hans.json +++ b/i18n/zh-hans.json @@ -1,9 +1,9 @@ { - "latinizeurl_desc": "把url中的汉字转换成拼音。", + "latinizeurl-desc": "把url中的汉字转换成拼音。", "latinizeurl-customurl": "自定义URL", "customurl-legend": "自定义URL", "customurl-url-field-label": "你想要设置的URL:", "customurl-url-field-help": "建议使用纯英语字符,留空时会自动根据标题里的汉字生成对应的拼音URL。", - "customurl-set-success": "页面的URL已经设置为[[$1|$2]]", + "customurl-set-success": "页面的URL已经设置为:[[$1|$2]]。", "customurl-set-failed": "页面URL不可用。" } \ No newline at end of file diff --git a/includes/BaseConvertor.php b/includes/BaseConvertor.php new file mode 100644 index 0000000..e21ac70 --- /dev/null +++ b/includes/BaseConvertor.php @@ -0,0 +1,6 @@ +getTitle()->getNamespace(), self::$allowedNS)){ //不是普通页面就跳过 return; } - global $wgLatinizeUrlConfig; + $titleText = $wikiPage->getTitle()->getText(); - $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); + $convertor = Utils::getConvertor($wikiPage->getTitle()->getPageLanguage()); $latinize = $convertor->parse($titleText); $slug = Utils::wordListToUrl($latinize); Utils::addTitleSlugMap($titleText, $slug, $latinize); @@ -86,9 +86,9 @@ class Hooks { if(!in_array($newTitle->getNamespace(), self::$allowedNS)){ //不是普通页面就跳过 return; } - global $wgLatinizeUrlConfig; + $titleText = $newTitle->getText(); - $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); + $convertor = Utils::getConvertor($newTitle->getPageLanguage()); $latinize = $convertor->parse($titleText); $slug = Utils::wordListToUrl($latinize); Utils::addTitleSlugMap($titleText, $slug, $latinize); diff --git a/includes/LatinizeCollation.php b/includes/LatinizeCollation.php index 52f85e7..fe22160 100644 --- a/includes/LatinizeCollation.php +++ b/includes/LatinizeCollation.php @@ -12,13 +12,11 @@ class LatinizeCollation extends Collation { } 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); + function() use($string){ + $convertor = Utils::getConvertor(); $latinize = $convertor->parse($string); $slug = Utils::wordListToUrl($latinize); return $slug; diff --git a/includes/Utils.php b/includes/Utils.php index 7a7d250..3c1cbe7 100644 --- a/includes/Utils.php +++ b/includes/Utils.php @@ -5,6 +5,8 @@ use Exception; use ExtensionRegistry; use Title; use User; +use Hooks as MWHooks; +use Language; use MediaWiki\MediaWikiServices; class Utils { @@ -306,6 +308,29 @@ class Utils { return str_replace('+', '_', implode("/", array_map("urlencode", explode("/", $str)))); } + /** + * @param Language|string $language - 语言 + * @return BaseConvertor 转换器 + */ + public static function getConvertor($language = null){ + if($language == null){ + $language = MediaWikiServices::getInstance()->getContentLanguage(); + } + + if($language instanceof Language){ + $language = $language->getCode(); + } + + $convertor = null; + + MWHooks::run('LatinizeUrlGetConvertor', [ + $language, + &$convertor, + ]); + + return $convertor; + } + public static function getVersion(){ return ExtensionRegistry::getInstance()->getAllThings()['LatinizeUrl']['version']; } diff --git a/specials/SpecialCustomUrl.php b/specials/SpecialCustomUrl.php index c511be1..40ed9eb 100644 --- a/specials/SpecialCustomUrl.php +++ b/specials/SpecialCustomUrl.php @@ -83,11 +83,10 @@ class SpecialCustomUrl extends FormSpecialPage } public function onSubmit(array $data, \HTMLForm $form = null ) { - global $wgLatinizeUrlConfig; $slug = $data['slug']; if(empty($slug)){ //自动生成 $titleText = $this->title->getText(); - $convertor = new Hanzi2Pinyin($wgLatinizeUrlConfig); + $convertor = Utils::getConvertor($this->title->getPageLanguage()); $latinize = $convertor->parse($titleText); $slug = Utils::wordListToUrl($latinize); } else {