From 475a3d294a2176f09e443caebae804d41b0adce4 Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Wed, 4 Jan 2023 22:17:38 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8DMediaWiki=201.39?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- extension.json | 4 +- i18n/zh-hans.json | 2 +- includes/AliyunAIReview.php | 19 +++++---- maintenance/TryAIReview.php | 83 +++++++++++++++++++++++++++++++++++++ maintenance/test.txt | 0 5 files changed, 98 insertions(+), 10 deletions(-) create mode 100644 maintenance/TryAIReview.php create mode 100644 maintenance/test.txt diff --git a/extension.json b/extension.json index 4026fae..095bf4a 100644 --- a/extension.json +++ b/extension.json @@ -4,11 +4,11 @@ "author": "Hyperzlib", "url": "https://github.com/Isekai-Project/mediawiki-extension-IsekaiAIReview", "descriptionmsg": "isekai-aireview-desc", - "version": "1.0.1", + "version": "1.0.2", "license-name": "MIT", "type": "other", "requires": { - "MediaWiki": ">= 1.35.0", + "MediaWiki": ">= 1.38.0", "extensions": { "Moderation": ">= 1.5.0" } diff --git a/i18n/zh-hans.json b/i18n/zh-hans.json index f22c3d8..5d0c7e9 100644 --- a/i18n/zh-hans.json +++ b/i18n/zh-hans.json @@ -6,7 +6,7 @@ "logentry-aireview-approve": "AI已{{GENDER:$2|通过}}$4对页面$3的$5", "logentry-aireview-reject": "AI已{{GENDER:$2|阻止}}$4对页面$3的$5,原因:$6", - "isekai-aireview-aliyun-reason-normal": "正常(不知道为什么会被拦截)", + "isekai-aireview-aliyun-reason-normal": "正常(不应该被拦截)", "isekai-aireview-aliyun-reason-spam": "垃圾信息", "isekai-aireview-aliyun-reason-ad": "广告", "isekai-aireview-aliyun-reason-politics": "政治敏感", diff --git a/includes/AliyunAIReview.php b/includes/AliyunAIReview.php index 35434a3..a645c06 100644 --- a/includes/AliyunAIReview.php +++ b/includes/AliyunAIReview.php @@ -11,13 +11,14 @@ class AliyunAIReview { public function __construct(){ global $wgAIReviewEndpoint, $wgAIReviewAccessKeyId, $wgAIReviewAccessKeySecret; AlibabaCloud::accessKeyClient($wgAIReviewAccessKeyId, $wgAIReviewAccessKeySecret) + ->connectTimeout(3) ->regionId($wgAIReviewEndpoint) ->asDefaultClient(); } - public function reviewText($text){ + public function reviewText($text, $rawResponse = false) { $reqData = $this->buildRequestData($text); - $response = $this->doRequest($reqData); + $response = $this->doRequest($reqData, $rawResponse); return $response; } @@ -52,14 +53,18 @@ class AliyunAIReview { * @throws \AlibabaCloud\Client\Exception\ServerException * @throws \AlibabaCloud\Client\Exception\ClientException */ - public function doRequest($requestData){ + public function doRequest($requestData, $rawResponse = false) { $textScan = Green::v20180509()->textScan(); - $response = $textScan->method('POST')->accept('JSON')->body(json_encode($requestData))->request(); + $response = $textScan->body(json_encode($requestData))->request(); - if($response->getReasonPhrase() === 'OK'){ - return $this->parseResponse($response->toArray()); + if ($rawResponse) { + return $response; } else { - return ['pass' => false, 'reason' => wfMessage('isekai-aireview-aliyun-server-error', $response->getStatusCode())->escaped()]; + if ($response->getReasonPhrase() === 'OK') { + return $this->parseResponse($response->toArray()); + } else { + return ['pass' => false, 'reason' => wfMessage('isekai-aireview-aliyun-server-error', $response->getStatusCode())->escaped()]; + } } } diff --git a/maintenance/TryAIReview.php b/maintenance/TryAIReview.php new file mode 100644 index 0000000..3c706c7 --- /dev/null +++ b/maintenance/TryAIReview.php @@ -0,0 +1,83 @@ +addDescription('Test AI Review'); + $this->addArg('text', 'Text to review', false); + $this->addOption('file', 'File to review', false, true); + } + + public function execute() { + $text = ''; + + if ($this->hasOption('file')) { + $file = $this->getOption('file'); + + if (!file_exists($file)) { + $this->error("File not found." . PHP_EOL); + return false; + } + + $text = file_get_contents($file); + } + + if ($this->hasArg(0)) { + $text = $this->getArg(0); + } + + if (empty($text)) { + $this->error("Text to review is empty." . PHP_EOL); + return false; + } + + $aiReview = new AliyunAIReview(); + + /** @var \AlibabaCloud\Client\Result\Result $response */ + $response = $aiReview->reviewText($text, true); + + $this->output("Response Status: " . $response->getReasonPhrase() . PHP_EOL); + $this->output("Response Body: "); + var_dump($response->toArray()); + $this->output(PHP_EOL); + + $parsedResponse = $aiReview->parseResponse($response->toArray()); + $this->output("Parsed response: "); + var_dump($parsedResponse); + + return true; + } +} + +$maintClass = TryAIReview::class; +require_once RUN_MAINTENANCE_IF_MAIN; diff --git a/maintenance/test.txt b/maintenance/test.txt new file mode 100644 index 0000000..e69de29