Skip to content

Commit 242cd28

Browse files
関連ページで, 何と関連しているか提示
1 parent 6c93e5f commit 242cd28

File tree

1 file changed

+67
-67
lines changed

1 file changed

+67
-67
lines changed

Frontend/related-viewer.php

Lines changed: 67 additions & 67 deletions
Original file line numberDiff line numberDiff line change
@@ -37,85 +37,57 @@
3737

3838
SearchEngine\Searcher::LoadIndex($indexFilePath);
3939

40-
// --- 関連コンテンツの検索
40+
// === 関連コンテンツの検索 =================================================
4141

42-
$suggestions = [];
42+
// $suggestions = [];
4343

4444
// "<title> <parent.title>" で検索
4545
// ただし, parent は rootではない
46-
$query = NotBlankText([$currentContent->title, basename($currentContent->path)]) .
46+
$titleQuery = NotBlankText([$currentContent->title, basename($currentContent->path)]) .
4747
(($parent === false || $parent->IsRoot()) ? '' : ' ' . NotBlankText([$parent->title, basename($parent->path)]) );
4848

49-
$workSuggestions = SearchEngine\Searcher::Search($query);
49+
$titleSuggestions = SearchEngine\Searcher::Search($titleQuery);
5050

5151
// score 0.5 未満 は除外
52-
foreach($workSuggestions as $i => $suggestion){
52+
foreach($titleSuggestions as $i => $suggestion){
5353
if($suggestion['score'] < 0.5){
54-
unset($workSuggestions[$i]);
54+
unset($titleSuggestions[$i]);
5555
}
5656
}
5757
// Debug::Log($workSuggestions);
58-
$suggestions = array_merge($suggestions, $workSuggestions);
59-
58+
// $suggestions = array_merge($suggestions, $workSuggestions);
6059

6160
// "<tag1> <tag2> <tag3> ..." で検索
62-
$query = '';
61+
$tagQuery = '';
6362
foreach($currentContent->tags as $tag){
6463
if(!in_array($tag, array('noindex', 'noindex-latest', '編集中', 'editing'))){
65-
$query .= $tag . ' ';
64+
$tagQuery .= $tag . ' ';
6665
}
6766
}
68-
$workSuggestions = SearchEngine\Searcher::Search($query);
67+
$tagSuggestions = SearchEngine\Searcher::Search($tagQuery);
6968

7069
// 全タグをAND検索したとき, score 0.3 未満のものは除外
71-
foreach($workSuggestions as $i => $suggestion){
70+
foreach($tagSuggestions as $i => $suggestion){
7271
if($suggestion['score'] < 0.3){
73-
unset($workSuggestions[$i]);
72+
unset($tagSuggestions[$i]);
7473
}
7574
}
76-
$suggestions = array_merge($suggestions, $workSuggestions);
77-
78-
79-
// 重複を除外
80-
$uniqueKeys = [];
81-
foreach($suggestions as $i => $suggestion){
82-
if(array_key_exists($suggestion['id'], $uniqueKeys)){
83-
unset($suggestions[$i]);
84-
continue;
85-
}
86-
$uniqueKeys[$suggestion['id']] = true;
87-
}
88-
89-
/*
90-
$thisDirectory = $currentContent->path;
91-
// $rootContent = $currentContent;
92-
// while($rootContent !== false){
93-
// $content = $rootContent->Parent();
94-
// if($content === false){
95-
// break;
75+
// $suggestions = array_merge($suggestions, $workSuggestions);
76+
77+
// // 重複を除外
78+
// $uniqueKeys = [];
79+
// foreach($suggestions as $i => $suggestion){
80+
// if(array_key_exists($suggestion['id'], $uniqueKeys)){
81+
// unset($suggestions[$i]);
82+
// continue;
9683
// }
97-
// $rootContent = $content;
84+
// $uniqueKeys[$suggestion['id']] = true;
9885
// }
9986

100-
// $rootContentsFolder = dirname($rootContent->path);
101-
$rootContentsFolder = ContentsDatabaseManager::GetRootContentsFolder($currentContent->path);
102-
for($i = 0; $i < 2; $i++){
103-
$dirname = dirname($thisDirectory);
104-
if($dirname == $rootContentsFolder){
105-
break;
106-
}
107-
$thisDirectory = $dirname;
108-
}
109-
110-
*/
111-
112-
foreach($suggestions as $i => $suggestion){
113-
$steps = CountSteps($suggestion['id'], $currentContent->path);
114-
if($steps !== false && $steps < 5){
115-
unset($suggestions[$i]);
116-
}
117-
}
87+
$titleSuggestions = SelectDifferentDirectoryContents($titleSuggestions, $currentContent->path);
88+
$tagSuggestions = SelectDifferentDirectoryContents($tagSuggestions, $currentContent->path);
11889

90+
// End 関連コンテンツの検索 =================================================
11991

12092
// === ページ内容設定 =======================================================
12193

@@ -139,35 +111,35 @@
139111
['selected' => false, 'innerHTML' => '<a href="' . CreateDirectoryHREF(dirname($contentPath)) .'">ディレクトリ</a>'],
140112
['selected' => true, 'innerHTML' => '<a href="' . CreateContentHREF($currentContent->path) .'?related">関連</a>']];
141113

142-
$vars['contentBody'] = '';
143114

144-
// child list の設定
145115
$vars['childList'] = []; // [ ['title' => '', 'summary' => '', 'url' => ''], ... ]
146116

147-
$content = new Content();
148-
foreach($suggestions as $suggestion){
149-
if($content->SetContent($suggestion['id'])){
150-
$parent = $content->Parent();
151-
$vars['childList'][] = [
152-
'title' => NotBlankText([$content->title, basename($content->path)]) .
153-
($parent === false ? '' : ' | ' . NotBlankText([$parent->title, basename($parent->path)])),
154-
'summary' => GetDecodedText($content)['summary'],
155-
'url' => CreateContentHREF($content->path)
156-
];
157-
}
117+
$body = '';
118+
119+
if(count($titleSuggestions) > 0){
120+
$body .= '<h3>タイトル「' . trim($titleQuery) . '」に関連する</h3>';
121+
$body .= CreateSuggestedContentList($titleSuggestions);
122+
}
123+
124+
if(count($tagSuggestions) > 0){
125+
$body .= '<h3>タグ「' . trim($tagQuery) . '」に関連する</h3>';
126+
$body .= CreateSuggestedContentList($tagSuggestions);
158127
}
159128

160-
if(count($vars['childList']) > 0){
129+
130+
if(count($titleSuggestions) + count($tagSuggestions) > 0){
161131
$vars['contentSummary'] = '<p>「コンテンツ: <a href="' . CreateContentHREF($currentContent->path) . '">' .
162132
NotBlankText([$currentContent->title, basename($currentContent->path)]) .
163-
'</a>」と関連するコンテンツが, 別階層で<em>' . count($vars['childList']) .'件</em>見つかりました.</p>';
133+
'</a>」と関連するコンテンツが, 別階層で<em>' . (count($titleSuggestions) + count($tagSuggestions)) .'件</em>見つかりました.</p>';
164134
}
165135
else{
166136
$vars['contentSummary'] = '<p>「コンテンツ: <a href="' . CreateContentHREF($currentContent->path) . '">' .
167137
NotBlankText([$currentContent->title, basename($currentContent->path)]) .
168138
'</a>」と関連するコンテンツが, 別階層で見つかりませんでした.</p>';
169139
}
170140

141+
$vars['contentBody'] = $body;
142+
171143
// ビルド時間計測 終了
172144
$stopwatch->Stop();
173145
$vars['pageBuildReport']['times']['build']['ms'] = $stopwatch->Elapsed() * 1000;
@@ -224,4 +196,32 @@ function CountSteps($pathFrom, $pathTo){
224196

225197
// Debug::Log($steps);
226198
return $steps;
199+
}
200+
201+
function SelectDifferentDirectoryContents($suggestions, $currentContentPath){
202+
foreach($suggestions as $i => $suggestion){
203+
$steps = CountSteps($suggestion['id'], $currentContentPath);
204+
if($steps !== false && $steps < 5){
205+
unset($suggestions[$i]);
206+
}
207+
}
208+
return $suggestions;
209+
}
210+
211+
function CreateSuggestedContentList($suggestions){
212+
$html = '<ul class="child-list">';
213+
214+
$content = new Content();
215+
foreach ($suggestions as $suggestion) {
216+
if($content->SetContent($suggestion['id'])){
217+
$parent = $content->Parent();
218+
$html .= '<li><div><div class="child-title">' .
219+
'<a href="'. CreateContentHREF($content->path) . '">' .
220+
NotBlankText([$content->title, basename($content->path)]) .
221+
($parent === false ? '' : ' | ' . NotBlankText([$parent->title, basename($parent->path)])) . '</a>' .
222+
'</div><div class="child-summary">' . GetDecodedText($content)['summary'] . '</div></div></li>';
223+
}
224+
}
225+
$html .= '</ul>';
226+
return $html;
227227
}

0 commit comments

Comments
 (0)