web/scripts/update-ebook-database

55 lines
1.3 KiB
PHP
Executable file

#!/usr/bin/php
<?
require_once('/standardebooks.org/web/lib/Core.php');
use function Safe\getopt;
$longopts = ['ebookWwwFilesystemPath:', 'verbose'];
$options = getopt('v', $longopts);
$ebookWwwFilesystemPath = $options['ebookWwwFilesystemPath'] ?? null;
$verbose = false;
if(isset($options['v']) || isset($options['verbose'])){
$verbose = true;
}
if($ebookWwwFilesystemPath === null){
print("Expected usage: update-ebook-database [-v,--verbose] --ebookWwwFilesystemPath <path>\n");
exit(1);
}
if($verbose){
print("ebookWwwFilesystemPath: $ebookWwwFilesystemPath\n");
}
$ebook = Ebook::FromFilesystem($ebookWwwFilesystemPath);
if($verbose){
print("Title: $ebook->Title\n");
print("Identifier: $ebook->Identifier\n");
}
try{
$ebook->CreateOrUpdate();
}
catch(Exceptions\ValidationException $validationException){
$exceptions = $validationException->Exceptions;
if(count($exceptions) == 1){
print("\nA ValidationException occurred when updating the ebook database:\n");
print($ex->getMessage() . "\n");
print("[ERROR] Found 1 error\n");
}
else{
print("\nValidationExceptions when updating the ebook database:\n");
foreach($exceptions as $ex){
print("\t" . $ex->getMessage() . "\n");
}
print("[ERROR] Found " . count($exceptions) . " errors\n");
}
exit(1);
}
exit(0);