适配MediaWiki 1.39

master
落雨楓 2 years ago
parent 1e68aa33da
commit 475a3d294a

@ -4,11 +4,11 @@
"author": "Hyperzlib", "author": "Hyperzlib",
"url": "https://github.com/Isekai-Project/mediawiki-extension-IsekaiAIReview", "url": "https://github.com/Isekai-Project/mediawiki-extension-IsekaiAIReview",
"descriptionmsg": "isekai-aireview-desc", "descriptionmsg": "isekai-aireview-desc",
"version": "1.0.1", "version": "1.0.2",
"license-name": "MIT", "license-name": "MIT",
"type": "other", "type": "other",
"requires": { "requires": {
"MediaWiki": ">= 1.35.0", "MediaWiki": ">= 1.38.0",
"extensions": { "extensions": {
"Moderation": ">= 1.5.0" "Moderation": ">= 1.5.0"
} }

@ -6,7 +6,7 @@
"logentry-aireview-approve": "AI已{{GENDER:$2|通过}}$4对页面$3的$5", "logentry-aireview-approve": "AI已{{GENDER:$2|通过}}$4对页面$3的$5",
"logentry-aireview-reject": "AI已{{GENDER:$2|阻止}}$4对页面$3的$5原因$6", "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-spam": "垃圾信息",
"isekai-aireview-aliyun-reason-ad": "广告", "isekai-aireview-aliyun-reason-ad": "广告",
"isekai-aireview-aliyun-reason-politics": "政治敏感", "isekai-aireview-aliyun-reason-politics": "政治敏感",

@ -11,13 +11,14 @@ class AliyunAIReview {
public function __construct(){ public function __construct(){
global $wgAIReviewEndpoint, $wgAIReviewAccessKeyId, $wgAIReviewAccessKeySecret; global $wgAIReviewEndpoint, $wgAIReviewAccessKeyId, $wgAIReviewAccessKeySecret;
AlibabaCloud::accessKeyClient($wgAIReviewAccessKeyId, $wgAIReviewAccessKeySecret) AlibabaCloud::accessKeyClient($wgAIReviewAccessKeyId, $wgAIReviewAccessKeySecret)
->connectTimeout(3)
->regionId($wgAIReviewEndpoint) ->regionId($wgAIReviewEndpoint)
->asDefaultClient(); ->asDefaultClient();
} }
public function reviewText($text){ public function reviewText($text, $rawResponse = false) {
$reqData = $this->buildRequestData($text); $reqData = $this->buildRequestData($text);
$response = $this->doRequest($reqData); $response = $this->doRequest($reqData, $rawResponse);
return $response; return $response;
} }
@ -52,16 +53,20 @@ class AliyunAIReview {
* @throws \AlibabaCloud\Client\Exception\ServerException * @throws \AlibabaCloud\Client\Exception\ServerException
* @throws \AlibabaCloud\Client\Exception\ClientException * @throws \AlibabaCloud\Client\Exception\ClientException
*/ */
public function doRequest($requestData){ public function doRequest($requestData, $rawResponse = false) {
$textScan = Green::v20180509()->textScan(); $textScan = Green::v20180509()->textScan();
$response = $textScan->method('POST')->accept('JSON')->body(json_encode($requestData))->request(); $response = $textScan->body(json_encode($requestData))->request();
if ($rawResponse) {
return $response;
} else {
if ($response->getReasonPhrase() === 'OK') { if ($response->getReasonPhrase() === 'OK') {
return $this->parseResponse($response->toArray()); return $this->parseResponse($response->toArray());
} else { } else {
return ['pass' => false, 'reason' => wfMessage('isekai-aireview-aliyun-server-error', $response->getStatusCode())->escaped()]; return ['pass' => false, 'reason' => wfMessage('isekai-aireview-aliyun-server-error', $response->getStatusCode())->escaped()];
} }
} }
}
public function parseResponse($response){ public function parseResponse($response){
if($response['code'] !== 200) if($response['code'] !== 200)

@ -0,0 +1,83 @@
<?php
namespace Isekai\AIReview\Maintenance;
use Isekai\AIReview\AliyunAIReview;
use Maintenance;
/**
* Make sure the index for the wiki is sane.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
* http://www.gnu.org/copyleft/gpl.html
*/
$IP = getenv( 'MW_INSTALL_PATH' );
if ( $IP === false ) {
$IP = __DIR__ . '/../../..';
}
require_once "$IP/maintenance/Maintenance.php";
class TryAIReview extends Maintenance {
public function __construct() {
parent::__construct();
$this->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;
Loading…
Cancel
Save