From 9d26636e8293abc9ec6b58e1bc6529ffa9067abb Mon Sep 17 00:00:00 2001 From: Lex Lim Date: Wed, 28 Dec 2022 08:57:25 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A01.39=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- composer.json | 2 +- extension.json | 8 ++--- i18n/zh-hans.json | 1 + includes/AIReviewJob.php | 24 +++++++++------ includes/AliyunAIReview.php | 6 +++- includes/Hooks.php | 8 +++-- includes/LogFormatter.php | 61 +++++++++++++++++++------------------ includes/Utils.php | 12 +++++++- 8 files changed, 72 insertions(+), 50 deletions(-) diff --git a/composer.json b/composer.json index a68717a..bb0f96e 100644 --- a/composer.json +++ b/composer.json @@ -7,7 +7,7 @@ }, "authors": [ { - "name": "量子复合态", + "name": "Hyperzlib", "email": "hyperzlib@outlook.com" } ] diff --git a/extension.json b/extension.json index 98f2b66..4026fae 100644 --- a/extension.json +++ b/extension.json @@ -1,13 +1,14 @@ { "name": "IsekaiAIReview", - "author": "hyperzlib", + "namemsg": "isekai-aireview-name", + "author": "Hyperzlib", "url": "https://github.com/Isekai-Project/mediawiki-extension-IsekaiAIReview", "descriptionmsg": "isekai-aireview-desc", - "version": "1.0.0", + "version": "1.0.1", "license-name": "MIT", "type": "other", "requires": { - "MediaWiki": ">= 1.31.0", + "MediaWiki": ">= 1.35.0", "extensions": { "Moderation": ">= 1.5.0" } @@ -52,6 +53,5 @@ "AIReviewBizType": null, "AIReviewRobotUID": 1 }, - "load_composer_autoloader": true, "manifest_version": 1 } \ No newline at end of file diff --git a/i18n/zh-hans.json b/i18n/zh-hans.json index cbdcb22..f22c3d8 100644 --- a/i18n/zh-hans.json +++ b/i18n/zh-hans.json @@ -1,4 +1,5 @@ { + "isekai-aireview-name": "异世界百科 AI自动审核", "isekai-aireview-desc": "通过AI实现自动审核", "log-name-aireview": "机器审核记录", "log-description-aireview": "这里会显示经过机器审核的记录", diff --git a/includes/AIReviewJob.php b/includes/AIReviewJob.php index 65b31d4..0c97040 100644 --- a/includes/AIReviewJob.php +++ b/includes/AIReviewJob.php @@ -3,7 +3,6 @@ namespace Isekai\AIReview; use Job; use MediaWiki\MediaWikiServices; -use MediaWiki\Moderation\AddLogEntryConsequence; use Title; use User; use RequestContext; @@ -20,19 +19,20 @@ class AIReviewJob extends Job { public function run(){ global $wgAIReviewRobotUID; - $dbr = wfGetDB(DB_REPLICA); + $services = MediaWikiServices::getInstance(); - $robotUser = User::newFromId($wgAIReviewRobotUID); + $dbr = $services->getDBLoadBalancer()->getMaintenanceConnectionRef(DB_REPLICA); - $modid = $this->params['mod_id']; - $modUser = $dbr->selectField('moderation', 'mod_user', ['mod_id' => $modid], __METHOD__); + $robotUser = $services->getUserFactory()->newFromId($wgAIReviewRobotUID); + + $mod_id = $this->params['mod_id']; + $modUser = $dbr->selectField('moderation', 'mod_user', ['mod_id' => $mod_id], __METHOD__); - $services = MediaWikiServices::getInstance(); $entryFactory = $services->getService('Moderation.EntryFactory'); $consequenceManager = $services->getService('Moderation.ConsequenceManager'); /** @var ModerationViewableEntry $contentEntry */ - $contentEntry = $entryFactory->findViewableEntry($modid); + $contentEntry = $entryFactory->findViewableEntry($mod_id); $title = $contentEntry->getTitle(); $context = RequestContext::getMain(); @@ -50,7 +50,9 @@ class AIReviewJob extends Job { 'isekai-aireview', 'Reject revision on: ' . $title->getText() . ', reason: ' . Utils::getReadableReason($result['reason']) ); - Utils::addAIReviewLog('reject', $robotUser, $modUser, $title, $modid, $result['reason']); + Utils::addAIReviewLog('reject', $robotUser, $modUser, $title, $mod_id, $result['reason']); + $services->getHookContainer()->run("IsekaiAIReviewResult", + [ false, $title, $mod_id, $modUser, $result['reason'] ]); return true; } } @@ -60,9 +62,11 @@ class AIReviewJob extends Job { 'isekai-aireview', 'Approve revision on: ' . $title->getText() ); - Utils::addAIReviewLog('approve', $robotUser, $modUser, $title, $modid); - $approveEntry = $entryFactory->findApprovableEntry($modid); + $approveEntry = $entryFactory->findApprovableEntry($mod_id); $approveEntry->approve($robotUser); + Utils::addAIReviewLog('approve', $robotUser, $modUser, $title, $mod_id); + $services->getHookContainer()->run("IsekaiAIReviewResult", + [ true, $title, $mod_id, $modUser, '' ]); return true; } } \ No newline at end of file diff --git a/includes/AliyunAIReview.php b/includes/AliyunAIReview.php index 7a5e954..35434a3 100644 --- a/includes/AliyunAIReview.php +++ b/includes/AliyunAIReview.php @@ -48,9 +48,13 @@ class AliyunAIReview { return $taskList; } + /** + * @throws \AlibabaCloud\Client\Exception\ServerException + * @throws \AlibabaCloud\Client\Exception\ClientException + */ public function doRequest($requestData){ $textScan = Green::v20180509()->textScan(); - $response = $textScan->setMethod('POST')->setAcceptFormat('JSON')->setContent(json_encode($requestData))->request(); + $response = $textScan->method('POST')->accept('JSON')->body(json_encode($requestData))->request(); if($response->getReasonPhrase() === 'OK'){ return $this->parseResponse($response->toArray()); diff --git a/includes/Hooks.php b/includes/Hooks.php index e23f1c4..4bdac33 100644 --- a/includes/Hooks.php +++ b/includes/Hooks.php @@ -2,12 +2,14 @@ namespace Isekai\AIReview; use JobQueueGroup; +use MediaWiki\MediaWikiServices; use Title; class Hooks { - public static function onModerationPending($fields, $modid){ + public static function onModerationPending($fields, $mod_id){ //加入审核队列 - $job = new AIReviewJob(Title::newFromText($fields['mod_title']), ['mod_id' => $modid]); - JobQueueGroup::singleton()->push($job); + $title = Title::newFromText($fields['mod_title']); + $job = new AIReviewJob($title, ['mod_id' => $mod_id]); + MediaWikiServices::getInstance()->getJobQueueGroup()->push($job); } } \ No newline at end of file diff --git a/includes/LogFormatter.php b/includes/LogFormatter.php index bc386e3..8070ebf 100644 --- a/includes/LogFormatter.php +++ b/includes/LogFormatter.php @@ -2,6 +2,7 @@ namespace Isekai\AIReview; use LogFormatter as GlobalLogFormatter; +use MediaWiki\MediaWikiServices; use SpecialPage; use Message; use Linker; @@ -9,44 +10,43 @@ use Title; use User; class LogFormatter extends GlobalLogFormatter { + /** + * @param array $params + * @return array + * @throws \MWException + */ + public function buildBaseParams(array $params): array { + $services = MediaWikiServices::getInstance(); + $linkRenderer = $this->getLinkRenderer(); + $entryParams = $this->entry->getParameters(); + $modId = $entryParams['modid']; + + $user = $services->getUserFactory()->newFromId($entryParams['moduser']); + $userLink = Linker::userLink($user->getId(), $user->getName()); + $params[3] = Message::rawParam($userLink); + + $link = $linkRenderer->makeKnownLink( + SpecialPage::getTitleFor('Moderation'), + $this->msg('moderation-log-change')->params($modId)->text(), + ['title' => $this->msg('tooltip-moderation-rejected-change')->plain()], + ['modaction' => 'show', 'modid' => $modId] + ); + $params[4] = Message::rawParam($link); + return $params; + } + public function getMessageParameters(){ $params = parent::getMessageParameters(); - - $type = $this->entry->getSubtype(); $entryParams = $this->entry->getParameters(); - $linkRenderer = $this->getLinkRenderer(); + $type = $this->entry->getSubtype(); switch($type){ case 'approve': - $modId = $entryParams['modid']; - - $user = User::newFromId($entryParams['moduser']); - $userLink = Linker::userLink( $user->getId(), $user->getName() ); - $params[3] = Message::rawParam( $userLink ); - - $link = $linkRenderer->makeKnownLink( - SpecialPage::getTitleFor( 'Moderation' ), - $this->msg( 'moderation-log-change' )->params( $modId )->text(), - [ 'title' => $this->msg( 'tooltip-moderation-rejected-change' )->plain() ], - [ 'modaction' => 'show', 'modid' => $modId ] - ); - $params[4] = Message::rawParam( $link ); + $params = $this->buildBaseParams($params); break; case 'reject': - $modId = $entryParams['modid']; - - $user = User::newFromId($entryParams['moduser']); - $userLink = Linker::userLink( $user->getId(), $user->getName() ); - $params[3] = Message::rawParam( $userLink ); - - $link = $linkRenderer->makeKnownLink( - SpecialPage::getTitleFor( 'Moderation' ), - $this->msg( 'moderation-log-change' )->params( $modId )->text(), - [ 'title' => $this->msg( 'tooltip-moderation-rejected-change' )->plain() ], - [ 'modaction' => 'show', 'modid' => $modId ] - ); - $params[4] = Message::rawParam( $link ); + $params = $this->buildBaseParams($params); $params[5] = Utils::getReadableReason($entryParams['reason']); break; @@ -55,13 +55,14 @@ class LogFormatter extends GlobalLogFormatter { } public function getPreloadTitles() { + $services = MediaWikiServices::getInstance(); $type = $this->entry->getSubtype(); $params = $this->entry->getParameters(); $titles = []; if ( $params['moduser'] ) { # Not anonymous - $user = User::newFromId($params['moduser']); + $user = $services->getUserFactory()->newFromId($params['moduser']); $titles[] = Title::makeTitle( NS_USER, $user->getName() ); } diff --git a/includes/Utils.php b/includes/Utils.php index cd05ce1..a3cedaa 100644 --- a/includes/Utils.php +++ b/includes/Utils.php @@ -5,7 +5,14 @@ use ManualLogEntry; use PHPHtmlParser\Dom; class Utils { - public static function getDiffAddedLines($diffHtml){ + /** + * @throws \PHPHtmlParser\Exceptions\CurlException + * @throws \PHPHtmlParser\Exceptions\ChildNotFoundException + * @throws \PHPHtmlParser\Exceptions\NotLoadedException + * @throws \PHPHtmlParser\Exceptions\CircularException + * @throws \PHPHtmlParser\Exceptions\StrictException + */ + public static function getDiffAddedLines($diffHtml) { $dom = new Dom(); $dom->load($diffHtml); $lines = []; @@ -37,6 +44,9 @@ class Utils { return implode(', ', $readableReasons); } + /** + * @throws \MWException + */ public static function addAIReviewLog($event, $robotUser, $modUser, $title, $modid, $reason = null){ $entry = new ManualLogEntry('aireview', $event); $entry->setPerformer($robotUser);