/
WordCount.php
220 lines (188 loc) · 6.63 KB
/
WordCount.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
<?php
namespace SV\WordCountSearch\Repository;
use SV\StandardLib\Helper;
use SV\Threadmarks\XF\Entity\Forum as ThreadmarkForumEntity;
use SV\WordCountSearch\Entity\IContainerWordCount;
use SV\WordCountSearch\XF\Entity\Post;
use SV\WordCountSearch\XF\Entity\Thread as ExtendedThreadEntity;
use XF\Entity\Forum as ForumEntity;
use XF\Entity\Thread as ThreadEntity;
use XF\Mvc\Entity\Entity;
use XF\Mvc\Entity\Repository;
/**
* Class WordCount
*
* @package SV\WordCountSearch\Repository
*/
class WordCount extends Repository
{
/** @var int */
public const DEFAULT_THREADMARK_CATEGORY_ID = 1;
public static function get(): self
{
return Helper::repository(self::class);
}
public function getDefaultThreadmarkCategoryId(): int
{
return self::DEFAULT_THREADMARK_CATEGORY_ID;
}
protected function str_word_count_utf8(string $str): int
{
// ref: http://php.net/manual/de/function.str-word-count.php#107363
return count(preg_split('~[^\p{L}\p{N}\']+~u', $str, -1, PREG_SPLIT_NO_EMPTY));
}
public function hasRangeQuery(): bool
{
//$this->app()->search()->getQuery();
if (self::$hasElasticSearch === null)
{
self::$hasElasticSearch = false;
self::$hasMySQLSearch = true;
}
return self::$hasElasticSearch || self::$hasMySQLSearch;
}
/** @var bool|null */
protected static $hasElasticSearch = null;
protected static $hasMySQLSearch = true;
protected function getWordCountThreshold(): int
{
return (int)(\XF::app()->options()->wordcountThreshold ?? 20);
}
public function shouldRecordPostWordCount(Post $post, int $wordCount): bool
{
if ($wordCount >= $this->getWordCountThreshold())
{
return true;
}
if ($post->isValidKey('Threadmark') && $post->get('Threadmark'))
{
return true;
}
return false;
}
public function getTextWordCount(string $message): int
{
$strippedText = $this->app()->stringFormatter()->stripBbCode($message, ['stripQuote' => true]);
// remove non-visible placeholders
$strippedText = str_replace('[*]', ' ', $strippedText);
return $this->str_word_count_utf8($strippedText);
}
public function roundWordCount(int $wordCount): string
{
if ($wordCount >= 1000000000)
{
$inexactWordCount = round($wordCount / 1000000000, 1) . 'b';
}
else if ($wordCount >= 1000000)
{
$inexactWordCount = round($wordCount / 1000000, 1) . 'm';
}
else if ($wordCount >= 100000)
{
$inexactWordCount = round($wordCount / 100000, 1) * 100 . 'k';
}
else if ($wordCount >= 10000)
{
$inexactWordCount = round($wordCount / 10000, 1) * 10 . 'k';
}
else if ($wordCount >= 1000)
{
$inexactWordCount = round($wordCount / 1000, 1) . 'k';
}
else if ($wordCount >= 100)
{
$inexactWordCount = round($wordCount / 100, 1) * 100;
}
else if ($wordCount >= 10)
{
$inexactWordCount = round($wordCount / 10, 1) * 10;
}
else if ($wordCount < 0)
{
$inexactWordCount = '0';
}
else
{
$inexactWordCount = $wordCount;
}
return strval($inexactWordCount);
}
public function checkThreadmarkWordCountForRebuild(ThreadEntity $thread): ?int
{
/** @var ExtendedThreadEntity $thread */
if (!\XF::isAddOnActive('SV/Threadmarks'))
{
return $thread->RawWordCount;
}
$wordCount = (int)$thread->RawWordCount;
$threadmarkCount = $this->getThreadWordCountFromEntity($thread);
if ($threadmarkCount !== 0 && $wordCount === 0 ||
$threadmarkCount === 0 && $wordCount !== 0)
{
$thread->rebuildWordCount(true, false);
}
return $thread->RawWordCount;
}
public function isThreadWordCountSupported(?Entity $parentContainer = null): bool
{
if (!\XF::isAddOnActive('SV/Threadmarks'))
{
return false;
}
if ($parentContainer instanceof ForumEntity)
{
/** @var ThreadmarkForumEntity $parentContainer */
if (!$parentContainer->canViewThreadmarks())
{
return false;
}
}
return true;
}
public function getThreadWordCountFromEntity(ThreadEntity $thread): int
{
$defaultCategoryId = $this->getDefaultThreadmarkCategoryId();
return (int)($thread->threadmark_category_data[$defaultCategoryId]['word_count'] ?? 0);
}
public function getContainerWordCount(string $contentType, string $containerType, int $containerId): int
{
if (!\XF::isAddOnActive('SV/Threadmarks'))
{
return 0;
}
return intval($this->db()->fetchOne('
SELECT IFNULL(SUM(post_words.word_count), 0)
FROM xf_sv_threadmark AS threadmark
INNER JOIN xf_post_words AS post_words ON (post_words.post_id = threadmark.content_id AND threadmark.content_type = ?)
WHERE threadmark.container_type = ?
AND threadmark.container_id = ?
AND threadmark.message_state = ?
AND threadmark.threadmark_category_id = ?
', [$contentType, $containerType, $containerId, 'visible', $this->getDefaultThreadmarkCategoryId()]));
}
/**
* Note; does not save the entity!
*
* @param IContainerWordCount|Entity $container
* @param bool $threadmarkSupport
*/
public function rebuildContainerWordCount(Entity $container, bool $threadmarkSupport = true): void
{
if (!($container instanceof IContainerWordCount) || !$container->exists())
{
return;
}
$addOns = \XF::app()->container('addon.cache');
if ($threadmarkSupport && isset($addOns['SV/Threadmarks']) && \is_callable([$container, 'updateThreadmarkDataCache']))
{
// calls getThreadmarkCategoryData/wordCountThreadmarkCacheRebuild
$container->updateThreadmarkDataCache();
return;
}
$wordCount = $this->getContainerWordCount($container->getWordContentType(), $container->getEntityContentType(), $container->getEntityId());
$container->set('word_count', $wordCount);
$container->clearCache('WordCount');
$container->clearCache('RawWordCount');
$container->clearCache('hasThreadmarks');
}
}