101f06932SAndreas Gohr<?php 201f06932SAndreas Gohr 37ebc7895Ssplitbrainuse dokuwiki\Extension\SyntaxPlugin; 401f06932SAndreas Gohruse dokuwiki\plugin\aichat\Chunk; 501f06932SAndreas Gohruse dokuwiki\Search\Indexer; 601f06932SAndreas Gohr 701f06932SAndreas Gohr/** 801f06932SAndreas Gohr * DokuWiki Plugin aichat (Syntax Component) 901f06932SAndreas Gohr * 1001f06932SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 1101f06932SAndreas Gohr * @author Andreas Gohr <gohr@cosmocode.de> 1201f06932SAndreas Gohr */ 137ebc7895Ssplitbrainclass syntax_plugin_aichat_similar extends SyntaxPlugin 1401f06932SAndreas Gohr{ 1501f06932SAndreas Gohr /** @inheritDoc */ 1601f06932SAndreas Gohr public function getType() 1701f06932SAndreas Gohr { 1801f06932SAndreas Gohr return 'substition'; 1901f06932SAndreas Gohr } 2001f06932SAndreas Gohr 2101f06932SAndreas Gohr /** @inheritDoc */ 2201f06932SAndreas Gohr public function getPType() 2301f06932SAndreas Gohr { 2401f06932SAndreas Gohr return 'block'; 2501f06932SAndreas Gohr } 2601f06932SAndreas Gohr 2701f06932SAndreas Gohr /** @inheritDoc */ 2801f06932SAndreas Gohr public function getSort() 2901f06932SAndreas Gohr { 3001f06932SAndreas Gohr return 155; 3101f06932SAndreas Gohr } 3201f06932SAndreas Gohr 3301f06932SAndreas Gohr /** @inheritDoc */ 3401f06932SAndreas Gohr public function connectTo($mode) 3501f06932SAndreas Gohr { 3601f06932SAndreas Gohr $this->Lexer->addSpecialPattern('~~similar~~', $mode, 'plugin_aichat_similar'); 3701f06932SAndreas Gohr } 3801f06932SAndreas Gohr 3901f06932SAndreas Gohr 4001f06932SAndreas Gohr /** @inheritDoc */ 4101f06932SAndreas Gohr public function handle($match, $state, $pos, Doku_Handler $handler) 4201f06932SAndreas Gohr { 4301f06932SAndreas Gohr return []; 4401f06932SAndreas Gohr } 4501f06932SAndreas Gohr 4601f06932SAndreas Gohr /** @inheritDoc */ 4701f06932SAndreas Gohr public function render($mode, Doku_Renderer $renderer, $data) 4801f06932SAndreas Gohr { 4901f06932SAndreas Gohr global $INFO; 5001f06932SAndreas Gohr 5101f06932SAndreas Gohr /** @var helper_plugin_aichat $helper */ 5201f06932SAndreas Gohr $helper = $this->loadHelper('aichat'); 5301f06932SAndreas Gohr 5401f06932SAndreas Gohr $id = $INFO['id']; 5501f06932SAndreas Gohr 5601f06932SAndreas Gohr $pages = (new Indexer())->getPages(); 5701f06932SAndreas Gohr $pos = array_search($id, $pages); 5801f06932SAndreas Gohr if ($pos === false) return true; 5901f06932SAndreas Gohr 6001f06932SAndreas Gohr $storage = $helper->getStorage(); 6101f06932SAndreas Gohr $chunks = $storage->getPageChunks($id, $pos * 100); 6201f06932SAndreas Gohr $similar = []; 6301f06932SAndreas Gohr foreach ($chunks as $chunk) { 642ad4d158SAndreas Gohr $similar += $storage->getSimilarChunks($chunk->getEmbedding(), 10); 6501f06932SAndreas Gohr } 6601f06932SAndreas Gohr $similar = array_unique($similar); 67*30b9cbc7Ssplitbrain $similar = array_filter($similar, static fn($chunk) => $chunk->getPage() !== $id); 68*30b9cbc7Ssplitbrain usort($similar, static fn($a, $b) => 6901f06932SAndreas Gohr /** @var Chunk $a */ 7001f06932SAndreas Gohr /** @var Chunk $b */ 71*30b9cbc7Ssplitbrain $b->getScore() <=> $a->getScore()); 7201f06932SAndreas Gohr 7301f06932SAndreas Gohr if (!$similar) return true; 7401f06932SAndreas Gohr 752ad4d158SAndreas Gohr $similar = array_slice($similar, 0, 5); 762ad4d158SAndreas Gohr 7701f06932SAndreas Gohr $renderer->listu_open(); 7801f06932SAndreas Gohr foreach ($similar as $chunk) { 7901f06932SAndreas Gohr $renderer->listitem_open(1); 8001f06932SAndreas Gohr $renderer->listcontent_open(); 812ad4d158SAndreas Gohr $renderer->internallink($chunk->getPage(), null, null, false, 'navigation'); 8201f06932SAndreas Gohr $renderer->listcontent_close(); 8301f06932SAndreas Gohr $renderer->listitem_close(); 8401f06932SAndreas Gohr } 8501f06932SAndreas Gohr $renderer->listu_close(); 8601f06932SAndreas Gohr 8701f06932SAndreas Gohr return true; 8801f06932SAndreas Gohr } 8901f06932SAndreas Gohr} 90