Skip to content

Commit 3eb4bd1

Browse files
Merge pull request #148 from ContentsViewer/pre
タグ付けされていないものも検索対象. 関連コンテンツの提示方法修正
2 parents c8da3de + 7762aba commit 3eb4bd1

File tree

8 files changed

+386
-254
lines changed

8 files changed

+386
-254
lines changed

Frontend/contents-viewer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@
164164
// インデックスの更新
165165
$indexFilePath = ContentsDatabaseManager::GetRelatedIndexFileName($contentPath);
166166
ContentsDatabaseManager::RegistIndex($currentContent);
167-
SearchEngine\Indexer::ApplyIndex($indexFilePath);
167+
SearchEngine\Index::Apply($indexFilePath);
168168

169169

170170
// === ページ内容設定 =======================================================

Frontend/related-viewer.php

Lines changed: 100 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -30,39 +30,76 @@
3030
$vars['rootContentPath'] = ContentsDatabaseManager::GetRelatedRootFile($vars['contentPath']);
3131
$vars['rootDirectory'] = substr(GetTopDirectory($vars['rootContentPath']), 1);
3232

33+
34+
// ContentsDatabaseManager::LoadRelatedMetadata($vars['rootContentPath']);
35+
// $tag2path = array_key_exists('tag2path', ContentsDatabase::$metadata) ? ContentsDatabase::$metadata['tag2path'] : [];
36+
// $path2tag = array_key_exists('path2tag', ContentsDatabase::$metadata) ? ContentsDatabase::$metadata['path2tag'] : [];
37+
// ksort($tag2path);
38+
39+
3340
// layerの再設定
3441
$out = UpdateLayerNameAndResetLocalization($vars['contentPath'], $vars['layerName'], $vars['language']);
3542
$vars['layerName'] = $out['layerName'];
3643
$vars['language'] = $out['language'];
3744

38-
$parent = $currentContent->Parent();
3945

4046
$indexFilePath = ContentsDatabaseManager::GetRelatedIndexFileName($contentPath);
47+
SearchEngine\Index::Load($indexFilePath);
4148

42-
SearchEngine\Searcher::LoadIndex($indexFilePath);
49+
50+
$parent = $currentContent->Parent();
51+
$childPathList = [];
52+
if($parent !== false){
53+
$childCount = $parent->ChildCount();
54+
for($i = 0; $i < $childCount; $i++){
55+
if(($child = $parent->Child($i)) !== false){
56+
$childPathList[] = $child->path;
57+
}
58+
}
59+
}
4360

4461
// === 関連コンテンツの検索 =================================================
62+
$titleSuggestions = [];
63+
64+
/**
65+
* [
66+
* ['tag' => '', 'suggestions' => []],
67+
* ...
68+
* ]
69+
*/
70+
$tagSuggestions = [];
4571

46-
// $suggestions = [];
72+
$countSuggestions = 0;
4773

48-
// "<title> <parent.title> <tag1> <tag2> <tag3> ..."で検索
74+
// "<title> <parent.title> で検索
4975
// ただし, parent は rootではない
50-
$query = NotBlankText([$currentContent->title, ContentsDatabaseManager::GetContentPathInfo($currentContent->path)['filename']]);
76+
$titleQuery = NotBlankText(
77+
[$currentContent->title, ContentsDatabaseManager::GetContentPathInfo($currentContent->path)['filename']]
78+
);
5179
if($parent !== false){
5280
$parentPathInfo = ContentsDatabaseManager::GetContentPathInfo($parent->path);
5381
if($parentPathInfo['filename'] != ROOT_FILE_NAME){
54-
$query .= ' ' . NotBlankText([$parent->title, $parentPathInfo['filename']]);
82+
$titleQuery .= ' ' . NotBlankText([$parent->title, $parentPathInfo['filename']]);
5583
}
56-
}
84+
}
85+
$titleSuggestions = SelectSuggestions(
86+
SearchEngine\Searcher::Search($titleQuery), $currentContent->path, $childPathList, 0.5
87+
);
88+
$countSuggestions += count($titleSuggestions);
5789

90+
// <tag1> <tag2> <tag3> ..."で検索
5891
foreach($currentContent->tags as $tag){
59-
if(!in_array($tag, array('noindex', 'noindex-latest', Localization\Localize('editing', 'editing')))){
60-
$query .= ' ' . $tag;
92+
if(!in_array($tag, array('noindex', 'noindex-latest'))){
93+
$suggestions = SelectSuggestions(
94+
SearchEngine\Searcher::Search($tag), $currentContent->path, $childPathList
95+
);
96+
$countSuggestions += count($suggestions);
97+
$tagSuggestions[] = ['tag' => $tag, 'suggestions' => $suggestions];
6198
}
6299
}
63100

64-
$suggestions = SearchEngine\Searcher::Search($query);
65101

102+
/*
66103
$terms = explode(' ', $query);
67104
$termCount = count($terms);
68105
for($i = $termCount - 1; $i >= 0; $i--){
@@ -71,12 +108,6 @@
71108
array_splice($terms, $i, 1);
72109
}
73110
}
74-
// Debug::Log(count($terms));
75-
// Debug::Log(0.7 / count($terms));
76-
// Debug::Log(count($terms));
77-
// Debug::Log(0.5 / (1+log10(count($terms))));
78-
// Debug::Log($suggestions);
79-
80111
// フィルタ例:
81112
// 0.3以上のもの:
82113
// 問題点:
@@ -95,30 +126,16 @@
95126
unset($suggestions[$i]);
96127
}
97128
}
98-
99-
$childPathList = [];
100-
if($parent !== false){
101-
$childCount = $parent->ChildCount();
102-
for($i = 0; $i < $childCount; $i++){
103-
if(($child = $parent->Child($i)) !== false){
104-
$childPathList[] = $child->path;
105-
}
106-
}
107-
}
108-
// Debug::Log($childPathList);
109-
// $titleSuggestions = SelectDifferentDirectoryContents($titleSuggestions, $currentContent->path, $childPathList);
110-
$suggestions = SelectDifferentDirectoryContents($suggestions, $currentContent->path, $childPathList);
111-
$suggestions = array_slice($suggestions, 0, 30); // 最大30件
129+
*/
112130

113131
// End 関連コンテンツの検索 =================================================
114132

115133
// === ページ内容設定 =======================================================
116134

117135
$vars['pageTitle'] = Localization\Localize('related', 'Related') . ': ' . NotBlankText([$currentContent->title, basename($currentContent->path)]);
118-
119136
$vars['pageHeading']['parents'] = [];
120137
$vars['pageHeading']['title'] = $vars['pageTitle'];
121-
138+
$vars['childList'] = [];
122139
$vars['navigator'] = '';
123140
if(($navigator = GetNavigator($currentContent->path)) !== false){
124141
$vars['navigator'] = $navigator;
@@ -135,37 +152,52 @@
135152
['selected' => true, 'innerHTML' => '<a href="' . CreateContentHREF($currentContent->path) .'?related">' . Localization\Localize('related', 'Related') . '</a>']
136153
];
137154

138-
139-
$vars['childList'] = []; // [ ['title' => '', 'summary' => '', 'url' => ''], ... ]
155+
$summary = '';
156+
if($countSuggestions > 0){
157+
$summary = '<p>' .
158+
Localization\Localize(
159+
'related-viewer.foundRelatedContents',
160+
'Found <em>{0} Related Contents</em> in another direcotry.', $countSuggestions
161+
) . '</p>';
162+
}
163+
else{
164+
$summary = '<p>' .
165+
Localization\Localize(
166+
'related-viewer.notFoundRelatedContents',
167+
'Not Found Related Contents in another directory.'
168+
) . '</p>';
169+
}
170+
$vars['contentSummary'] = $summary;
140171

141172
$body = '';
142-
143-
if(count($suggestions) > 0){
144-
// $body .= '<h3>「' . trim($query) . '」に関連する</h3>';
145-
$body .= CreateSuggestedContentList($suggestions);
173+
if(count($titleSuggestions) > 0){
174+
$body .= '<h2>"' . $titleQuery . '"</h2><div class="section">';
175+
$body .= CreateSuggestedContentList($titleSuggestions);
176+
$body .= '</div>';
146177
}
147178

148-
149-
if(count($suggestions) > 0){
150-
$vars['contentSummary'] = '<p>' .
151-
Localization\Localize('related-viewer.foundRelatedContents',
152-
'Found <em>{1} Contents</em> related with <em>"{0}"</em> in another direcotry.', trim($query), count($suggestions)) .
153-
'</p>';
154-
}
155-
else{
156-
$vars['contentSummary'] = '<p>' .
157-
Localization\Localize('related-viewer.notFoundRelatedContents',
158-
'Not Found Contents related with <em>"{0}"</em> in another directory.', trim($query)) .
159-
'</p>';
179+
foreach($tagSuggestions as $each){
180+
if(count($each['suggestions'])){
181+
$body .= '<h2>"' . $each['tag'] . '"</h2><div class="section">';
182+
$body .= '<ul class="tagline" style="text-align: right;"><li><a href="' .
183+
CreateTagMapHREF([[$each['tag']]], $vars['rootDirectory'], $vars['layerName']) .
184+
'">' . $each['tag'] . '</a></li></ul>';
185+
$body .= CreateSuggestedContentList($each['suggestions']);
186+
$body .= '</div>';
187+
}
160188
}
161189

190+
// if(count($suggestions) > 0){
191+
// // $body .= '<h3>「' . trim($query) . '」に関連する</h3>';
192+
// $body .= CreateSuggestedContentList($suggestions);
193+
// }
194+
162195
$vars['contentBody'] = $body;
163196

197+
$vars['htmlLang'] = $vars['layerName'];
164198
$vars['canonialUrl'] = (empty($_SERVER["HTTPS"]) ? "http://" : "https://") .
165199
$_SERVER["HTTP_HOST"] . CreateContentHREF($vars['contentPath']) . '?related';
166200

167-
$vars['htmlLang'] = $vars['layerName'];
168-
169201
// ビルド時間計測 終了
170202
$stopwatch->Stop();
171203
$vars['pageBuildReport']['times']['build']['ms'] = $stopwatch->Elapsed() * 1000;
@@ -225,13 +257,13 @@ function CountSteps($pathFrom, $pathTo){
225257
}
226258

227259
function SelectDifferentDirectoryContents($suggestions, $currentContentPath, $childPathList){
228-
foreach($suggestions as $i => $suggestion){
229-
if(in_array($suggestion['id'], $childPathList)){
260+
foreach($suggestions as $i => $suggested){
261+
if(in_array($suggested['id'], $childPathList)){
230262
unset($suggestions[$i]);
231263
continue;
232264
}
233265

234-
$steps = CountSteps($suggestion['id'], $currentContentPath);
266+
$steps = CountSteps($suggested['id'], $currentContentPath);
235267
if($steps !== false && $steps < 4){
236268
unset($suggestions[$i]);
237269
continue;
@@ -240,12 +272,23 @@ function SelectDifferentDirectoryContents($suggestions, $currentContentPath, $ch
240272
return $suggestions;
241273
}
242274

275+
function SelectSuggestions($suggestions, $currentContentPath, $childPathList, $scoreThres = 0.8){
276+
foreach($suggestions as $i => $suggested){
277+
if($suggested['score'] < $scoreThres){
278+
unset($suggestions[$i]);
279+
}
280+
}
281+
$suggestions = SelectDifferentDirectoryContents($suggestions, $currentContentPath, $childPathList);
282+
$suggestions = array_slice($suggestions, 0, 30);
283+
return $suggestions;
284+
}
285+
243286
function CreateSuggestedContentList($suggestions){
244287
$html = '<ul class="child-list">';
245288

246289
$content = new Content();
247-
foreach ($suggestions as $suggestion) {
248-
if($content->SetContent($suggestion['id'])){
290+
foreach ($suggestions as $suggested) {
291+
if($content->SetContent($suggested['id'])){
249292
$parent = $content->Parent();
250293
$html .= '<li><div><div class="child-title">' .
251294
'<a href="'. CreateContentHREF($content->path) . '">' .

0 commit comments

Comments
 (0)