重构项目,增加多语言支持。

master
落雨楓 5 years ago
parent dd54bbff45
commit d14fb19829

@ -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 {

@ -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
}

@ -0,0 +1,3 @@
{
"latinizeurl-chinese-convertor-desc": "LatinizeUrl用汉字转拼音转换器。"
}

@ -0,0 +1,55 @@
<?php
/* Only support api parse yet */
namespace LatinizeUrl;
use Exception;
use MediaWiki\Http\HttpRequestFactory;
class JapaneseConvertor extends BaseConvertor {
private $config;
private static $standalone = null;
public static function standalone(){
if(!self::$standalone){
global $wgLatinizeUrlJapaneseConvertorConfig;
self::$standalone = new self($wgLatinizeUrlJapaneseConvertorConfig);
}
return self::$standalone;
}
public static function onGetConvertor($langCode, &$convertor){
if(in_array($langCode, ['ja', 'ja-jp'])){
$convertor = self::standalone();
}
return true;
}
public function __construct($config){
$this->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"];
}
}

@ -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
}

@ -0,0 +1,3 @@
{
"latinizeurl-japanese-convertor-desc": "LatinizeUrlの専用漢字をローマ字に変換するためのツール。"
}

@ -12,3 +12,7 @@ $specialPageAliases['zh-hans'] = [
$specialPageAliases['zh-hant'] = [
'CustomUrl' => '客制化URL'
];
$specialPageAliases['ja'] = [
'CustomUrl' => 'URLを編集'
];

@ -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
```

@ -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

@ -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."
}

@ -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は無効です。"
}

@ -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이 잘못되었습니다."
}

@ -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不可用。"
}

@ -0,0 +1,6 @@
<?php
namespace LatinizeUrl;
abstract class BaseConvertor {
abstract public function parse($words);
}

@ -74,9 +74,9 @@ class Hooks {
if(!in_array($wikiPage->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);

@ -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;

@ -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'];
}

@ -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 {

Loading…
Cancel
Save