Skip to content

Commit

Permalink
优化代码,去除所有静态方法调用
Browse files Browse the repository at this point in the history
  • Loading branch information
jjandxa committed Aug 11, 2018
1 parent 9913475 commit 1e92560
Show file tree
Hide file tree
Showing 6 changed files with 136 additions and 91 deletions.
37 changes: 2 additions & 35 deletions bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
};
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -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": [
Expand Down
20 changes: 18 additions & 2 deletions src/Controller/XunSearchController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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'),
Expand Down
74 changes: 43 additions & 31 deletions src/Service/XunSearchService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
// 最新回复
Expand All @@ -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;
Expand All @@ -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"]);
Expand All @@ -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);

// 折叠字段
Expand Down Expand Up @@ -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));
Expand All @@ -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();
// 打开缓冲区
Expand All @@ -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);
}
Expand All @@ -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();
Expand Down
12 changes: 6 additions & 6 deletions src/Utils/XunSearchUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Loading

0 comments on commit 1e92560

Please sign in to comment.