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.

71 lines
2.6 KiB
PHTML

5 years ago
<?php
namespace Isekai\AIReview;
use Job;
use MediaWiki\MediaWikiServices;
use MediaWiki\Title\Title;
use MediaWiki\Context\RequestContext;
5 years ago
use ModerationViewableEntry;
class AIReviewJob extends Job {
public function __construct(Title $title, array $params){
parent::__construct('IsekaiAIReview', $title, $params);
}
/**
* 运行job开始进行AI审核
*/
public function run(){
global $wgAIReviewRobotUID;
2 years ago
$services = MediaWikiServices::getInstance();
5 years ago
2 years ago
$dbr = $services->getDBLoadBalancer()->getMaintenanceConnectionRef(DB_REPLICA);
5 years ago
2 years ago
$robotUser = $services->getUserFactory()->newFromId($wgAIReviewRobotUID);
$mod_id = $this->params['mod_id'];
$modUser = $dbr->selectField('moderation', 'mod_user', ['mod_id' => $mod_id], __METHOD__);
5 years ago
$entryFactory = $services->getService('Moderation.EntryFactory');
$consequenceManager = $services->getService('Moderation.ConsequenceManager');
/** @var ModerationViewableEntry $contentEntry */
2 years ago
$contentEntry = $entryFactory->findViewableEntry($mod_id);
5 years ago
$title = $contentEntry->getTitle();
$context = RequestContext::getMain();
$context->setTitle($title);
//获取diff内容
$diffHtml = $contentEntry->getDiffHTML($context);
//取出增加的文本内容
$addedText = Utils::getDiffAddedLines($diffHtml);
if(strlen($addedText) > 0){
//开始进行AI审核
$reviewer = new AliyunAIReview();
$result = $reviewer->reviewText($addedText);
if(!$result['pass']){ //审核不通过
wfDebugLog(
'isekai-aireview',
'Reject revision on: ' . $title->getText() . ', reason: ' . Utils::getReadableReason($result['reason'])
);
2 years ago
Utils::addAIReviewLog('reject', $robotUser, $modUser, $title, $mod_id, $result['reason']);
$services->getHookContainer()->run("IsekaiAIReviewResult",
[ false, $title, $mod_id, $modUser, $result['reason'] ]);
5 years ago
return true;
}
}
//审核通过
wfDebugLog(
'isekai-aireview',
'Approve revision on: ' . $title->getText()
);
2 years ago
$approveEntry = $entryFactory->findApprovableEntry($mod_id);
5 years ago
$approveEntry->approve($robotUser);
2 years ago
Utils::addAIReviewLog('approve', $robotUser, $modUser, $title, $mod_id);
$services->getHookContainer()->run("IsekaiAIReviewResult",
[ true, $title, $mod_id, $modUser, '' ]);
5 years ago
return true;
}
}