You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
55 lines
1.7 KiB
PHTML
55 lines
1.7 KiB
PHTML
5 years ago
|
<?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"];
|
||
|
}
|
||
|
}
|