增加1.39支持

master
落雨楓 2 years ago
parent e0be90e676
commit 9d26636e82

@ -7,7 +7,7 @@
}, },
"authors": [ "authors": [
{ {
"name": "量子复合态", "name": "Hyperzlib",
"email": "hyperzlib@outlook.com" "email": "hyperzlib@outlook.com"
} }
] ]

@ -1,13 +1,14 @@
{ {
"name": "IsekaiAIReview", "name": "IsekaiAIReview",
"author": "hyperzlib", "namemsg": "isekai-aireview-name",
"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.0", "version": "1.0.1",
"license-name": "MIT", "license-name": "MIT",
"type": "other", "type": "other",
"requires": { "requires": {
"MediaWiki": ">= 1.31.0", "MediaWiki": ">= 1.35.0",
"extensions": { "extensions": {
"Moderation": ">= 1.5.0" "Moderation": ">= 1.5.0"
} }
@ -52,6 +53,5 @@
"AIReviewBizType": null, "AIReviewBizType": null,
"AIReviewRobotUID": 1 "AIReviewRobotUID": 1
}, },
"load_composer_autoloader": true,
"manifest_version": 1 "manifest_version": 1
} }

@ -1,4 +1,5 @@
{ {
"isekai-aireview-name": "异世界百科 AI自动审核",
"isekai-aireview-desc": "通过AI实现自动审核", "isekai-aireview-desc": "通过AI实现自动审核",
"log-name-aireview": "机器审核记录", "log-name-aireview": "机器审核记录",
"log-description-aireview": "这里会显示经过机器审核的记录", "log-description-aireview": "这里会显示经过机器审核的记录",

@ -3,7 +3,6 @@ namespace Isekai\AIReview;
use Job; use Job;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use MediaWiki\Moderation\AddLogEntryConsequence;
use Title; use Title;
use User; use User;
use RequestContext; use RequestContext;
@ -20,19 +19,20 @@ class AIReviewJob extends Job {
public function run(){ public function run(){
global $wgAIReviewRobotUID; global $wgAIReviewRobotUID;
$dbr = wfGetDB(DB_REPLICA); $services = MediaWikiServices::getInstance();
$robotUser = User::newFromId($wgAIReviewRobotUID); $dbr = $services->getDBLoadBalancer()->getMaintenanceConnectionRef(DB_REPLICA);
$modid = $this->params['mod_id']; $robotUser = $services->getUserFactory()->newFromId($wgAIReviewRobotUID);
$modUser = $dbr->selectField('moderation', 'mod_user', ['mod_id' => $modid], __METHOD__);
$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'); $entryFactory = $services->getService('Moderation.EntryFactory');
$consequenceManager = $services->getService('Moderation.ConsequenceManager'); $consequenceManager = $services->getService('Moderation.ConsequenceManager');
/** @var ModerationViewableEntry $contentEntry */ /** @var ModerationViewableEntry $contentEntry */
$contentEntry = $entryFactory->findViewableEntry($modid); $contentEntry = $entryFactory->findViewableEntry($mod_id);
$title = $contentEntry->getTitle(); $title = $contentEntry->getTitle();
$context = RequestContext::getMain(); $context = RequestContext::getMain();
@ -50,7 +50,9 @@ class AIReviewJob extends Job {
'isekai-aireview', 'isekai-aireview',
'Reject revision on: ' . $title->getText() . ', reason: ' . Utils::getReadableReason($result['reason']) '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; return true;
} }
} }
@ -60,9 +62,11 @@ class AIReviewJob extends Job {
'isekai-aireview', 'isekai-aireview',
'Approve revision on: ' . $title->getText() 'Approve revision on: ' . $title->getText()
); );
Utils::addAIReviewLog('approve', $robotUser, $modUser, $title, $modid); $approveEntry = $entryFactory->findApprovableEntry($mod_id);
$approveEntry = $entryFactory->findApprovableEntry($modid);
$approveEntry->approve($robotUser); $approveEntry->approve($robotUser);
Utils::addAIReviewLog('approve', $robotUser, $modUser, $title, $mod_id);
$services->getHookContainer()->run("IsekaiAIReviewResult",
[ true, $title, $mod_id, $modUser, '' ]);
return true; return true;
} }
} }

@ -48,9 +48,13 @@ class AliyunAIReview {
return $taskList; return $taskList;
} }
/**
* @throws \AlibabaCloud\Client\Exception\ServerException
* @throws \AlibabaCloud\Client\Exception\ClientException
*/
public function doRequest($requestData){ public function doRequest($requestData){
$textScan = Green::v20180509()->textScan(); $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'){ if($response->getReasonPhrase() === 'OK'){
return $this->parseResponse($response->toArray()); return $this->parseResponse($response->toArray());

@ -2,12 +2,14 @@
namespace Isekai\AIReview; namespace Isekai\AIReview;
use JobQueueGroup; use JobQueueGroup;
use MediaWiki\MediaWikiServices;
use Title; use Title;
class Hooks { 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]); $title = Title::newFromText($fields['mod_title']);
JobQueueGroup::singleton()->push($job); $job = new AIReviewJob($title, ['mod_id' => $mod_id]);
MediaWikiServices::getInstance()->getJobQueueGroup()->push($job);
} }
} }

@ -2,6 +2,7 @@
namespace Isekai\AIReview; namespace Isekai\AIReview;
use LogFormatter as GlobalLogFormatter; use LogFormatter as GlobalLogFormatter;
use MediaWiki\MediaWikiServices;
use SpecialPage; use SpecialPage;
use Message; use Message;
use Linker; use Linker;
@ -9,44 +10,43 @@ use Title;
use User; use User;
class LogFormatter extends GlobalLogFormatter { 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(){ public function getMessageParameters(){
$params = parent::getMessageParameters(); $params = parent::getMessageParameters();
$type = $this->entry->getSubtype();
$entryParams = $this->entry->getParameters(); $entryParams = $this->entry->getParameters();
$linkRenderer = $this->getLinkRenderer(); $type = $this->entry->getSubtype();
switch($type){ switch($type){
case 'approve': case 'approve':
$modId = $entryParams['modid']; $params = $this->buildBaseParams($params);
$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 );
break; break;
case 'reject': case 'reject':
$modId = $entryParams['modid']; $params = $this->buildBaseParams($params);
$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[5] = Utils::getReadableReason($entryParams['reason']); $params[5] = Utils::getReadableReason($entryParams['reason']);
break; break;
@ -55,13 +55,14 @@ class LogFormatter extends GlobalLogFormatter {
} }
public function getPreloadTitles() { public function getPreloadTitles() {
$services = MediaWikiServices::getInstance();
$type = $this->entry->getSubtype(); $type = $this->entry->getSubtype();
$params = $this->entry->getParameters(); $params = $this->entry->getParameters();
$titles = []; $titles = [];
if ( $params['moduser'] ) { # Not anonymous if ( $params['moduser'] ) { # Not anonymous
$user = User::newFromId($params['moduser']); $user = $services->getUserFactory()->newFromId($params['moduser']);
$titles[] = Title::makeTitle( NS_USER, $user->getName() ); $titles[] = Title::makeTitle( NS_USER, $user->getName() );
} }

@ -5,7 +5,14 @@ use ManualLogEntry;
use PHPHtmlParser\Dom; use PHPHtmlParser\Dom;
class Utils { 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 = new Dom();
$dom->load($diffHtml); $dom->load($diffHtml);
$lines = []; $lines = [];
@ -37,6 +44,9 @@ class Utils {
return implode(', ', $readableReasons); return implode(', ', $readableReasons);
} }
/**
* @throws \MWException
*/
public static function addAIReviewLog($event, $robotUser, $modUser, $title, $modid, $reason = null){ public static function addAIReviewLog($event, $robotUser, $modUser, $title, $modid, $reason = null){
$entry = new ManualLogEntry('aireview', $event); $entry = new ManualLogEntry('aireview', $event);
$entry->setPerformer($robotUser); $entry->setPerformer($robotUser);

Loading…
Cancel
Save