-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
ContentSearchService.php
93 lines (78 loc) · 2.77 KB
/
ContentSearchService.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
<?php
namespace humhub\modules\content\services;
use humhub\modules\comment\models\Comment;
use humhub\modules\content\jobs\SearchDeleteDocument;
use humhub\modules\content\jobs\SearchUpdateDocument;
use humhub\modules\content\models\Content;
use humhub\modules\content\Module;
use humhub\modules\content\search\driver\AbstractDriver;
use humhub\modules\file\converter\TextConverter;
use humhub\modules\file\models\File;
use Yii;
class ContentSearchService
{
public Content $content;
public function __construct(Content $content)
{
$this->content = $content;
}
public function update($asActiveJob = true): void
{
if (!$this->isIndexable()) {
return;
}
if ((new ContentStateService(['content' => $this->content]))->isPublished()) {
if ($asActiveJob) {
Yii::$app->queue->push(new SearchUpdateDocument(['contentId' => $this->content->id]));
} else {
$this->getSearchDriver()->update($this->content);
}
} else {
$this->delete($asActiveJob);
}
}
public function delete($asActiveJob = true): void
{
if (!$this->isIndexable()) {
return;
}
if ($asActiveJob) {
Yii::$app->queue->push(new SearchDeleteDocument(['contentId' => $this->content->id]));
} else {
$this->getSearchDriver()->delete($this->content);
}
}
public function getFileContentAsText(): string
{
$result = '';
$textConverter = new TextConverter();
foreach (File::findAll(['object_model' => $this->content->object_model, 'object_id' => $this->content->object_id]) as $file) {
if ($textConverter->applyFile($file)) {
$result .= $file->file_name . ': ' . $textConverter->getContentAsText() . "\n\n\n\n";
}
}
return $result;
}
public function getCommentsAsText(): string
{
$result = '';
foreach (Comment::findAll(['object_model' => $this->content->object_model, 'object_id' => $this->content->object_id]) as $comment) {
$result .= "\n\n" . $comment->message . "\n\n";
foreach (Comment::findAll(['object_model' => Comment::class, 'object_id' => $comment->id]) as $subComment) {
$result .= "\n\n" . $subComment->message . "\n\n";
}
// ToDo: Add related files
}
return $result;
}
public function isIndexable(): bool
{
return $this->content->stream_channel === Content::STREAM_CHANNEL_DEFAULT;
}
private function getSearchDriver(): AbstractDriver
{
/** @var Module $module */
$module = Yii::$app->getModule('content');
return $module->getSearchDriver();
}
}