53 lines
1.3 KiB
PHP
53 lines
1.3 KiB
PHP
<?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
|
|
*/
|
|
function searchInPages(string $searchTerm) {
|
|
|
|
$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.
|
|
$snippet = preg_replace('/[\[{\(].*[\]}\)]/U' , '', strip_tags($contents['content']));
|
|
$curResults['snippet'] = createTextSnippet($snippet, 180);
|
|
$results[$file] = $curResults;
|
|
|
|
}
|
|
|
|
usort($results, function($a, $b) {
|
|
if ($a == $b) return 0;
|
|
return ($a > $b) ? -1 : 1;
|
|
});
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
?>
|