This commit is contained in:
Your Name
2026-10-09 09:36:17 +08:00
parent 88b94c2fc6
commit 51d8fd409e
727 changed files with 3753398 additions and 102 deletions
+5 -47
View File
@@ -3,7 +3,7 @@
declare(strict_types=1);
// Run from the project checkout: php server/scripts/install_tcm_ancient_books.php
// Initialize the pinned corpus submodule and build its local search index.
// Build the local search index from the corpus tracked in this repository.
require dirname(__DIR__) . '/vendor/autoload.php';
@@ -15,53 +15,11 @@ if (PHP_SAPI !== 'cli') {
}
$root = dirname(__DIR__) . '/knowledge/tcm-ancient-books';
$projectRoot = dirname(__DIR__, 2);
$indexPath = TcmAncientBooksReference::indexPath();
function runGit(array $arguments, ?string $directory = null): string
{
$originalDirectory = getcwd();
if ($directory !== null && ($originalDirectory === false || !chdir($directory))) {
throw new RuntimeException('Could not enter Git directory: ' . $directory);
}
try {
$command = 'git ' . implode(' ', array_map('escapeshellarg', $arguments)) . ' 2>&1';
$lines = [];
$code = 0;
exec($command, $lines, $code);
if ($code !== 0) {
throw new RuntimeException('Git command failed: ' . implode("\n", $lines));
}
return trim(implode("\n", $lines));
} finally {
if ($directory !== null) {
chdir($originalDirectory);
}
}
}
try {
if (!is_dir($root . '/.git') && !is_file($root . '/.git')) {
if (is_file($projectRoot . '/.gitmodules')
&& (is_dir($projectRoot . '/.git') || is_file($projectRoot . '/.git'))) {
runGit(['submodule', 'update', '--init', '--', 'server/knowledge/tcm-ancient-books'], $projectRoot);
} else {
runGit(['clone', '--depth', '1', TcmAncientBooksReference::REPOSITORY_URL . '.git', $root]);
}
}
$checkoutRoot = runGit(['rev-parse', '--show-toplevel'], $root);
if (strtolower(str_replace('\\', '/', (string) realpath($checkoutRoot)))
!== strtolower(str_replace('\\', '/', (string) realpath($root)))) {
throw new RuntimeException('Corpus checkout is missing or points to the parent repository');
}
$head = runGit(['rev-parse', 'HEAD'], $root);
if ($head !== TcmAncientBooksReference::COMMIT) {
runGit(['fetch', '--depth', '1', 'origin', TcmAncientBooksReference::COMMIT], $root);
runGit(['checkout', '--detach', TcmAncientBooksReference::COMMIT], $root);
$head = runGit(['rev-parse', 'HEAD'], $root);
}
if ($head !== TcmAncientBooksReference::COMMIT) {
throw new RuntimeException('Corpus commit mismatch');
if (!is_dir($root)) {
throw new RuntimeException('Bundled corpus directory is missing: ' . $root);
}
$terms = TcmAncientBooksReference::searchTerms();
@@ -194,7 +152,7 @@ try {
}
}
$index = [
'repository' => TcmAncientBooksReference::REPOSITORY_URL,
'repository' => 'local:tcm-ancient-books',
'commit' => TcmAncientBooksReference::COMMIT,
'index_version' => TcmAncientBooksReference::INDEX_VERSION,
'encoding' => 'GB18030',
@@ -208,7 +166,7 @@ try {
if (file_put_contents($temporary, $encoded, LOCK_EX) === false || !rename($temporary, $indexPath)) {
throw new RuntimeException('Could not write index');
}
echo 'Indexed ' . count($paths) . ' books at commit ' . $head . ', ' . strlen($encoded)
echo 'Indexed ' . count($paths) . ' books from source commit ' . TcmAncientBooksReference::COMMIT . ', ' . strlen($encoded)
. ' index bytes, skipped ' . $invalidLineCount . " malformed lines.\n";
} catch (Throwable $error) {
fwrite(STDERR, $error->getMessage() . "\n");