增加主动更新标题索引的功能

main
落雨楓 2 years ago
parent 553c66878c
commit cec2ca565d

@ -55,6 +55,9 @@
], ],
"ResourceLoaderGetConfigVars": [ "ResourceLoaderGetConfigVars": [
"Isekai\\AIToolbox\\Hooks::onResourceLoaderGetConfigVars" "Isekai\\AIToolbox\\Hooks::onResourceLoaderGetConfigVars"
],
"onPageSaveComplete": [
"Isekai\\AIToolbox\\Hooks::onPageSaveComplete"
] ]
}, },
"APIModules": { "APIModules": {
@ -100,6 +103,9 @@
"type": "gravatar" "type": "gravatar"
} }
}, },
"wgIsekaiAIBackendEndpoint": {
"value": "http://localhost:8144"
},
"IsekaiAIToolboxFrontendUrl": { "IsekaiAIToolboxFrontendUrl": {
"value": "/ai-toolbox/toolbox/{title}?token={token}" "value": "/ai-toolbox/toolbox/{title}?token={token}"
} }

@ -3,6 +3,7 @@ namespace Isekai\AIToolbox;
use MediaWiki\MediaWikiServices; use MediaWiki\MediaWikiServices;
use Config; use Config;
use Isekai\AIToolbox\Job\UpdateTitleIndexJob;
class Hooks { class Hooks {
/** /**
@ -27,4 +28,20 @@ class Hooks {
public static function onResourceLoaderGetConfigVars(array &$vars, string $skin, Config $config){ public static function onResourceLoaderGetConfigVars(array &$vars, string $skin, Config $config){
$vars['wgIsekaiAIToolboxFrontendUrl'] = $config->get('IsekaiAIToolboxFrontendUrl'); $vars['wgIsekaiAIToolboxFrontendUrl'] = $config->get('IsekaiAIToolboxFrontendUrl');
} }
/**
* @param \WikiPage $wikiPage
* @param \MediaWiki\User\UserIdentity $user
* @param string $summary
* @param int $flags
* @param \MediaWiki\Revision\RevisionRecord $revisionRecord
* @param \MediaWiki\Storage\EditResult $editResult
*/
public static function onPageSaveComplete($wikiPage, $user, $summary, $flags, $revisionRecord, $editResult) {
$title = $wikiPage->getTitle();
if ($title && $title->exists() && $title->getNamespace() === NS_MAIN) {
$job = new UpdateTitleIndexJob($wikiPage->getTitle(), []);
MediaWikiServices::getInstance()->getJobQueueGroup()->push($job);
}
}
} }

@ -0,0 +1,36 @@
<?php
namespace Isekai\AIToolbox\Job;
use Job;
use MediaWiki\MediaWikiServices;
use Title;
class UpdateTitleIndexJob extends Job {
public function __construct(Title $title, array $params){
parent::__construct('IsekaiAIUpdateTitleIndex', $title, $params);
}
/**
* 触发AI后端更新标题索引
*/
public function run() {
$services = MediaWikiServices::getInstance();
$config = $services->getMainConfig();
$endpoint = $config->get('wgIsekaiAIBackendEndpoint');
$title = $this->title;
$pageTitle = $title->getText();
$factory = MediaWikiServices::getInstance()->getHttpRequestFactory();
$req = $factory->create($endpoint + '/sys/embedding_search/title/update', [
'method' => 'POST',
'postData' => [
'title' => $pageTitle
],
'timeout' => 10
], __METHOD__);
return true;
}
}
Loading…
Cancel
Save