2018-06-15 11:26:25 +02:00
|
|
|
<?PHP
|
|
|
|
/**
|
|
|
|
* This file provides search functions.
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
*
|
|
|
|
* @author Joshua Ramon Enslin <joshua@jrenslin.de>
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Function for searching in all static pages.
|
|
|
|
*
|
|
|
|
* @param string $searchTerm Search term.
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
2018-06-15 13:31:31 +02:00
|
|
|
function searchInPages(string $searchTerm):array {
|
2018-06-15 11:26:25 +02:00
|
|
|
|
|
|
|
$files = scanDirConts(__DIR__ . "/../data/static");
|
|
|
|
|
|
|
|
$results = [];
|
|
|
|
foreach ($files as $file) {
|
|
|
|
|
|
|
|
$curResults = [];
|
|
|
|
$contents = json_decode(file_get_contents(__DIR__ . "/../data/static/$file"), true);
|
|
|
|
|
|
|
|
if (!$contents['public']) continue; // Don't display non-public files.
|
|
|
|
|
|
|
|
$curResults['inTitle'] = substr_count($contents['title'], $searchTerm);
|
|
|
|
$curResults['inDescription'] = substr_count($contents['content'], $searchTerm);
|
|
|
|
$curResults['priority'] = $curResults['inTitle'] * 3 + $curResults['inDescription'];
|
|
|
|
|
|
|
|
if ($curResults['priority'] == 0) continue;
|
|
|
|
|
|
|
|
$curResults['title'] = $contents['title'];
|
|
|
|
// Sanitize content for snippets.
|
2018-06-15 13:31:31 +02:00
|
|
|
$snippet = preg_replace('/[\[{\(].*[\]}\)]/U', '', strip_tags($contents['content']));
|
2018-06-15 11:26:25 +02:00
|
|
|
$curResults['snippet'] = createTextSnippet($snippet, 180);
|
|
|
|
$results[$file] = $curResults;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-22 15:07:33 +02:00
|
|
|
uasort($results, function($a, $b) {
|
2018-06-15 13:31:31 +02:00
|
|
|
if ($a['priority'] == $b['priority']) return 0;
|
|
|
|
return ($a['priority'] > $b['priority']) ? -1 : 1;
|
2018-06-15 11:26:25 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-06-15 13:31:31 +02:00
|
|
|
/**
|
|
|
|
* Function for generating and listing search results.
|
|
|
|
*
|
|
|
|
* @param string $searchTerm Search term.
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
function displaySearchResults(string $searchTerm):string {
|
|
|
|
|
|
|
|
$searchResults = searchInPages($searchTerm);
|
|
|
|
$output = '
|
|
|
|
<ul id="pagesSearchList">';
|
|
|
|
|
|
|
|
foreach ($searchResults as $file => $item) {
|
|
|
|
$output .= '
|
|
|
|
<li>
|
|
|
|
|
|
|
|
<a href="./?id=' . str_replace(".json", "", $file) . '"><h4>' . $item['title'] . '</h4></a>
|
|
|
|
<p>' . (string)($item['snippet']) . '</p>
|
|
|
|
<dl class="searchMetadataLine">
|
|
|
|
<dt class="toTranslate" data-content="Hits"></dt>
|
|
|
|
<dd>' . (string)($item['inTitle'] + $item['inDescription']) . '</dd>
|
|
|
|
</dl>
|
|
|
|
|
|
|
|
</li>
|
|
|
|
';
|
|
|
|
}
|
|
|
|
|
|
|
|
$output .= "
|
|
|
|
</ul>";
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2018-06-15 11:26:25 +02:00
|
|
|
?>
|