diff --git a/bootstrap.php b/bootstrap.php index bc77382..1e134da 100644 --- a/bootstrap.php +++ b/bootstrap.php @@ -8,43 +8,10 @@ namespace Plugin\XunSearch; -use Flarum\Event\ConfigureWebApp; -use Flarum\Event\DiscussionWasHidden; -use Flarum\Event\DiscussionWasRenamed; -use Flarum\Event\DiscussionWasRestored; -use Flarum\Event\ExtensionWasEnabled; -use Flarum\Event\PostWasPosted; -use Flarum\Event\PostWasRevised; -use Flarum\Event\PostWasHidden; -use Flarum\Event\PostWasRestored; -use Flarum\Event\ConfigureApiRoutes; use Illuminate\Contracts\Events\Dispatcher; -return function (Dispatcher $event) { +return function (Dispatcher $events) { - // 添加帖子到搜索引擎索引 - $event->listen(PostWasPosted::class, array(XunSearchDispatcher::class, "posted")); + $events->subscribe(XunSearchDispatcher::class); - // 更新帖子到搜索引擎索引 - $event->listen(PostWasRevised::class, array(XunSearchDispatcher::class, "revised")); - - // 隐藏帖子话题到搜索引擎索引 - $event->listen(PostWasHidden::class, array(XunSearchDispatcher::class, "hidden")); - - // 恢复帖子到搜索引擎索引 - $event->listen(PostWasRestored::class, array(XunSearchDispatcher::class, "restored")); - - // 修改话题到搜索引擎索引 - $event->listen(DiscussionWasRenamed::class, array(XunSearchDispatcher::class, "discussionRenamed")); - - // 隐藏话题到搜索引擎索引 - $event->listen(DiscussionWasHidden::class, array(XunSearchDispatcher::class, "discussionHidden")); - - // 恢复话题到搜索引擎索引 - $event->listen(DiscussionWasRestored::class, array(XunSearchDispatcher::class, "discussionRestored")); - - $event->listen(ConfigureApiRoutes::class, - array(XunSearchDispatcher::class, "registerApi")); - $event->listen(ConfigureWebApp::class, - array(XunSearchDispatcher::class, "initView")); }; \ No newline at end of file diff --git a/composer.json b/composer.json index f3b77a5..fc1cbc9 100644 --- a/composer.json +++ b/composer.json @@ -1,7 +1,7 @@ { "name": "jjandxa/flarum-ext-chinese-search", "description": "chinese search", - "version": "0.0.3", + "version": "0.0.4", "type": "flarum-extension", "license": "Apache License 2.0", "authors": [ diff --git a/src/Controller/XunSearchController.php b/src/Controller/XunSearchController.php index 5ce850e..98c923e 100644 --- a/src/Controller/XunSearchController.php +++ b/src/Controller/XunSearchController.php @@ -12,13 +12,29 @@ use Flarum\Core\Discussion; use Flarum\Core\Post; use Plugin\XunSearch\Service\XunSearchService; -use Plugin\XunSearch\Utils\XunSearchUtils; +use Flarum\Core\Search\Discussion\DiscussionSearcher; use Psr\Http\Message\ServerRequestInterface; use Tobscure\JsonApi\Document; +use Flarum\Api\UrlGenerator; class XunSearchController extends ListDiscussionsController { + private $searchService; + + /** + * XunSearchController constructor. + * @param XunSearchService $searchService + * @param DiscussionSearcher $searcher + * @param UrlGenerator $url + */ + public function __construct(XunSearchService $searchService, + DiscussionSearcher $searcher, UrlGenerator $url) + { + parent::__construct($searcher, $url); + $this->searchService = $searchService; + } + // 查询数据 protected function data(ServerRequestInterface $request, Document $document) { @@ -30,7 +46,7 @@ protected function data(ServerRequestInterface $request, Document $document) // 排序 $sort = $this->extractSort($request); - $result = XunSearchService::search($query, $limit, $offset, $sort); + $result = $this->searchService->search($query, $limit, $offset, $sort); $document->addPaginationLinks( $this->url->toRoute('xun.discussions.index'), diff --git a/src/Service/XunSearchService.php b/src/Service/XunSearchService.php index da866e8..efc264c 100644 --- a/src/Service/XunSearchService.php +++ b/src/Service/XunSearchService.php @@ -16,14 +16,27 @@ class XunSearchService { - static function search($query, $limit, $offset, $sort) { - $convertData = static::convertDiscussion($query, $limit + 1, $offset, $sort); + private $xunSearchUtils; + + /** + * XunSearchService constructor. + * @param $xunSearchUtils + */ + public function __construct(XunSearchUtils $xunSearchUtils) + { + $this->xunSearchUtils = $xunSearchUtils; + } + + + function search($query, $limit, $offset, $sort) { + + $convertData = $this->convertDiscussion($query, $limit + 1, $offset, $sort); $discussions = new Collection(); if (count($convertData) > 0) { $query = Discussion::query()-> - whereIn("id", static::getDiscussionIds($convertData)); + whereIn("id", $this->getDiscussionIds($convertData)); if ($sort !== null) { // 最新回复 @@ -50,7 +63,7 @@ static function search($query, $limit, $offset, $sort) { $discussions = $query->get(); - static::loadRelevantPosts($discussions, $convertData); + $this->loadRelevantPosts($discussions, $convertData); } $areMoreResults = $limit > 0 && count($convertData) > $limit; @@ -64,7 +77,7 @@ static function search($query, $limit, $offset, $sort) { return $result; } - static function getDiscussionIds($convertData) { + function getDiscussionIds($convertData) { $temp = []; foreach ($convertData as $data) { array_push($temp, $data["id"]); @@ -73,10 +86,10 @@ static function getDiscussionIds($convertData) { } // 转换数据 - static function convertDiscussion($query, $limit, $offset, $sort) { + function convertDiscussion($query, $limit, $offset, $sort) { $tempData = []; // 搜索逻辑 - $search = XunSearchUtils::getSearch(); + $search = $this->xunSearchUtils->getSearch(); $search->setLimit($limit, $offset); // 折叠字段 @@ -128,8 +141,7 @@ static function convertDiscussion($query, $limit, $offset, $sort) { } // 处理帖子回复内容 - static function loadRelevantPosts(Collection $discussions, $relevantPostIds) - { + function loadRelevantPosts(Collection $discussions, $relevantPostIds) { $postIds = []; foreach ($relevantPostIds as $ids) { $postIds = array_merge($postIds, array_slice($ids["postIds"], 0, 2)); @@ -149,13 +161,13 @@ static function loadRelevantPosts(Collection $discussions, $relevantPostIds) } // 根据话题id获取所有的帖子 - static function getPostsByDiscussionId($discussionId) { + function getPostsByDiscussionId($discussionId) { return Post::query()->where("discussion_id", $discussionId)->where("type", "comment")->get(); } // 添加帖子到索引 - static function addPostToIndex(Post $post) { - $posts = static::getPostsByDiscussionId($post->discussion_id); + function addPostToIndex(Post $post) { + $posts = $this->getPostsByDiscussionId($post->discussion_id); // 打开索引 $index = XunSearchUtils::getIndex(); // 打开缓冲区 @@ -164,12 +176,12 @@ static function addPostToIndex(Post $post) { foreach ($posts as $item) { // 需要新增的索引 if ($item->id === $post->id) { - $doc = XunSearchUtils::getDocument($post->discussion, + $doc = $this->xunSearchUtils->getDocument($post->discussion, $item, $posts->count()); $index->add($doc); } else { // 需要更新的索引 - $doc = XunSearchUtils::getDocument($post->discussion, + $doc = $this->xunSearchUtils->getDocument($post->discussion, $item, $posts->count()); $index->update($doc); } @@ -178,61 +190,61 @@ static function addPostToIndex(Post $post) { } // 删除帖子到索引 - static function deletePostToIndex(Post $post) { + function deletePostToIndex(Post $post) { // 获取索引 - $index = XunSearchUtils::getIndex(); + $index = $this->xunSearchUtils->getIndex(); $index->openBuffer(); // 删除当前记录 - XunSearchUtils::getIndex()->del($post->id); + $this->xunSearchUtils->getIndex()->del($post->id); // 更新其他记录的count - $posts = static::getPostsByDiscussionId($post->discussion_id); + $posts = $this->getPostsByDiscussionId($post->discussion_id); foreach ($posts as $item) { - $doc = XunSearchUtils::getDocument($post->discussion, $item, $posts->count()); + $doc = $this->xunSearchUtils->getDocument($post->discussion, $item, $posts->count()); $index->update($doc); } $index->closeBuffer(); } // 话题重命名 - static function renameDiscussion(Discussion $discussion) { + function renameDiscussion(Discussion $discussion) { // 获取索引 - $index = XunSearchUtils::getIndex(); + $index = $this->xunSearchUtils->getIndex(); $index->openBuffer(); // 更新所有记录 - $posts = static::getPostsByDiscussionId($discussion->id); + $posts = $this->getPostsByDiscussionId($discussion->id); foreach ($posts as $item) { - $doc = XunSearchUtils::getDocument($discussion, $item, $posts->count()); + $doc = $this->xunSearchUtils->getDocument($discussion, $item, $posts->count()); $index->update($doc); } $index->closeBuffer(); } // 删除话题 - static function deleteDiscussion(Discussion $discussion) { + function deleteDiscussion(Discussion $discussion) { // 获取索引 - $index = XunSearchUtils::getIndex(); + $index = $this->xunSearchUtils->getIndex(); $index->openBuffer(); // 更新所有记录 - $posts = static::getPostsByDiscussionId($discussion->id); + $posts = $this->getPostsByDiscussionId($discussion->id); foreach ($posts as $item) { $index->del($item->id); } $index->closeBuffer(); } - // 删除话题 - static function addDiscussion(Discussion $discussion) { + // 添加话题 + function addDiscussion(Discussion $discussion) { // 获取索引 - $index = XunSearchUtils::getIndex(); + $index = $this->xunSearchUtils->getIndex(); $index->openBuffer(); // 更新所有记录 - $posts = static::getPostsByDiscussionId($discussion->id); + $posts = $this->getPostsByDiscussionId($discussion->id); foreach ($posts as $item) { - $doc = XunSearchUtils::getDocument($discussion, $item, $posts->count()); + $doc = $this->xunSearchUtils->getDocument($discussion, $item, $posts->count()); $index->add($doc); } $index->closeBuffer(); diff --git a/src/Utils/XunSearchUtils.php b/src/Utils/XunSearchUtils.php index 02542a4..b169b85 100644 --- a/src/Utils/XunSearchUtils.php +++ b/src/Utils/XunSearchUtils.php @@ -14,22 +14,22 @@ class XunSearchUtils { - static function getXuSearch(): \XS { + function getXuSearch(): \XS { return new \XS(dirname(dirname(__DIR__))."/app.ini"); } // 获取搜索引擎索引 - static function getIndex(): \XSIndex { - return self::getXuSearch()->index; + function getIndex(): \XSIndex { + return $this->getXuSearch()->index; } // 获取搜索 - static function getSearch(): \XSSearch { - return self::getXuSearch()->search; + function getSearch(): \XSSearch { + return $this->getXuSearch()->search; } // 获取文档 - static function getDocument(Discussion $discussion, Post $post, $count): \XSDocument { + function getDocument(Discussion $discussion, Post $post, $count): \XSDocument { $data = array( "id" => $post->id, "discId" => $discussion->id, diff --git a/src/XunSearchDispatcher.php b/src/XunSearchDispatcher.php index bc895a6..b5dc483 100644 --- a/src/XunSearchDispatcher.php +++ b/src/XunSearchDispatcher.php @@ -21,11 +21,61 @@ use Plugin\XunSearch\Controller\XunSearchController; use Plugin\XunSearch\Service\XunSearchService; use Plugin\XunSearch\Utils\XunSearchUtils; +use Illuminate\Contracts\Events\Dispatcher; class XunSearchDispatcher { + + private $xunSearchUtils; + + private $xunSearchService; + + /** + * XunSearchDispatcher constructor. + * @param $xunSearchUtils + * @param $xunSearchService + */ + public function __construct(XunSearchUtils $xunSearchUtils, XunSearchService $xunSearchService) + { + $this->xunSearchUtils = $xunSearchUtils; + $this->xunSearchService = $xunSearchService; + } + + + /** + * @param Dispatcher $events + */ + public function subscribe(Dispatcher $events) + { + // 添加帖子到搜索引擎索引 + $events->listen(PostWasPosted::class, [$this, "posted"]); + + // 更新帖子到搜索引擎索引 + $events->listen(PostWasRevised::class, [$this, "revised"]); + + // 隐藏帖子话题到搜索引擎索引 + $events->listen(PostWasHidden::class, [$this, "hidden"]); + + // 恢复帖子到搜索引擎索引 + $events->listen(PostWasRestored::class, [$this, "restored"]); + + // 修改话题到搜索引擎索引 + $events->listen(DiscussionWasRenamed::class, [$this, "discussionRenamed"]); + + // 隐藏话题到搜索引擎索引 + $events->listen(DiscussionWasHidden::class, [$this, "discussionHidden"]); + + // 恢复话题到搜索引擎索引 + $events->listen(DiscussionWasRestored::class, [$this, "discussionRestored"]); + + $events->listen(ConfigureApiRoutes::class, + array($this, "registerApi")); + $events->listen(ConfigureWebApp::class, + array($this, "initView")); + } + // 添加后台Api - static function registerApi(ConfigureApiRoutes $event) { + function registerApi(ConfigureApiRoutes $event) { $event->get( "/xun/discussions", "xun.discussions.index", @@ -34,7 +84,7 @@ static function registerApi(ConfigureApiRoutes $event) { } // 添加前台逻辑 - static function initView(ConfigureWebApp $event) { + function initView(ConfigureWebApp $event) { if ($event->isForum()) { $event->addAssets(dirname(__DIR__).'/js/forum/dist/extension.js'); $event->addBootstrapper('jjandxa/flarum-ext-chinese-search/main'); @@ -42,49 +92,49 @@ static function initView(ConfigureWebApp $event) { } // 添加帖子到索引 - static function posted(PostWasPosted $event) { + function posted(PostWasPosted $event) { if ($event->post->type === "comment") { - XunSearchService::addPostToIndex($event->post); + $this->xunSearchService->addPostToIndex($event->post); } } // 修改帖子到索引 - static function revised(PostWasRevised $event) { + function revised(PostWasRevised $event) { if ($event->post->type === "comment") { - XunSearchUtils::getIndex()->update(XunSearchUtils::getDocument($event->post->discussion, + $this->xunSearchUtils->getIndex()->update(XunSearchUtils::getDocument($event->post->discussion, $event->post, $event->post->discussion->comments_count)); } } // 隐藏帖子到索引 - static function hidden(PostWasHidden $event) { + function hidden(PostWasHidden $event) { if ($event->post->type === "comment") { - XunSearchService::deletePostToIndex($event->post); + $this->xunSearchService->deletePostToIndex($event->post); } } // 恢复帖子到索引 - static function restored(PostWasRestored $event) { + function restored(PostWasRestored $event) { if ($event->post->type === "comment") { - XunSearchService::addPostToIndex($event->post); + $this->xunSearchService->addPostToIndex($event->post); } } // 话题修改名称到索引 - static function discussionRenamed(DiscussionWasRenamed $event) { - XunSearchService::renameDiscussion($event->discussion); + function discussionRenamed(DiscussionWasRenamed $event) { + $this->xunSearchService->renameDiscussion($event->discussion); } // 话题隐藏到索引 - static function discussionHidden(DiscussionWasHidden $event) { - XunSearchService::deleteDiscussion($event->discussion); + function discussionHidden(DiscussionWasHidden $event) { + $this->xunSearchService->deleteDiscussion($event->discussion); } // 话题恢复到索引 - static function discussionRestored(DiscussionWasRestored $event) { - XunSearchService::addDiscussion($event->discussion); + function discussionRestored(DiscussionWasRestored $event) { + $this->xunSearchService->addDiscussion($event->discussion); } } \ No newline at end of file