Source Code for /public/inc/searchindex.php
<?php
define('SEARCH_INDEX_CACHE_TIME', 1 );
function search_index_json() {
if( file_exists('searchindex.json') && filemtime('searchindex.json') > time() - SEARCH_INDEX_CACHE_TIME ) {
return file_get_contents('searchindex.json');
}
$results = [];
chdir(__DIR__ . '/../');
$files = array_merge(
glob('*.php'),
glob('**/*.php'),
glob('**/**/*.php'),
);
function extract_code_contents($file) {
$contents = file_get_contents($file);
return [ "title" => basename($file), "body" => trim($contents) ];
}
foreach ($files as $page) {
if ( str_starts_with($page, 'inc/') || str_starts_with($page, 'toc/') || str_starts_with($page, '404.php') ||
str_starts_with($page, 'demo-harness.php') || str_starts_with($page, 'search/') || str_starts_with($page, 'source-viewer.php')
) {
continue;
} else if ( str_ends_with($page, 'index.php') ) {
$results[] = array_merge([
"id" => "/" . str_replace('index.php', '', $page),
"kind" => "page",
], extract_page_contents($page));
} else {
$results[] = array_merge([
"id" => "/" . $page,
"kind" => "code",
], extract_code_contents($page));
}
}
file_put_contents('searchindex.json', json_encode($results, JSON_PRETTY_PRINT));
return json_encode($results, JSON_PRETTY_PRINT);
}
function extract_page_contents($file) {
$contents = file_get_contents($file);
$title_matches = [];
$title_regex = <<<'REGEX'
/add_header\(\s*(["'])(.+?)\1/
REGEX;
if ( preg_match($title_regex, $contents, $title_matches) ) {
// Don't forget to remove the quote marks, which could be of either type:
$title = strip_tags($title_matches[2]);
} else {
$title = 'Home';
}
// Strip anything that looks like a PHP block:
$contents = preg_replace('/<\\?php.*?\\?>/s', '', $contents);
// Strip tags:
$contents = strip_tags($contents);
// Trim whitespace:
$contents = trim( preg_replace('/[ \t]*[\r\n]+[ \t]*/', "\n", preg_replace('/[ \t]+/', ' ', $contents) ) );
// Anything that remains that WAS escaped... can now be treated as searchable text content, so unescape it:
$contents = html_entity_decode($contents);
// Remove any HTML tags that are left:
$contents = strip_tags($contents);
return [ "title" => $title, "body" => $contents ];
}
// If this file is accessed directly, output the search index JSON:
if( __FILE__ == $_SERVER['SCRIPT_FILENAME'] ) {
header('Content-Type: application/json');
echo search_index_json();
}