73 lines
2.1 KiB
PHP
73 lines
2.1 KiB
PHP
<?php
|
|
|
|
require dirname(__FILE__) . "/../variables.php";
|
|
|
|
$dbbooks = array();
|
|
$parsedbooks = array();
|
|
|
|
$cli = ( php_sapi_name() != "cli" ) ? false : true;
|
|
|
|
$count_total = 0;
|
|
$count_new = 0;
|
|
$count_purged = 0;
|
|
|
|
function walkBooks($path = BOOKDIR) {
|
|
global $globaldbh, $parsedbooks, $count_new, $count_total;
|
|
$contents = glob($path . "/*");
|
|
foreach ( $contents as $item ) {
|
|
if ( is_dir($item) ) {
|
|
walkBooks($item);
|
|
} else {
|
|
$dbpath = ($path == BOOKDIR ) ? "/" : substr($path, strlen(BOOKDIR));
|
|
$dbfile = basename($item);
|
|
$dbmtime = date("Y-m-d H:i:s", filemtime($item));
|
|
$parsedbooks[] = $dbpath . (($path == BOOKDIR) ? "" : "/") . $dbfile;
|
|
$query = "INSERT INTO books (path, filename, mtime) VALUES(:path, :filename, :mtime)";
|
|
$sth = $globaldbh->prepare($query);
|
|
$sth->bindValue(":path", $dbpath, PDO::PARAM_STR);
|
|
$sth->bindValue(":filename", $dbfile, PDO::PARAM_STR);
|
|
$sth->bindValue(":mtime", $dbmtime, PDO::PARAM_STR);
|
|
try {
|
|
$sth->execute();
|
|
$count_new++;
|
|
} catch (PDOException $e) {
|
|
}
|
|
$count_total++;
|
|
}
|
|
}
|
|
}
|
|
|
|
walkBooks();
|
|
$query = "SELECT id, path, filename FROM books ORDER BY path, filename";
|
|
$sth = $globaldbh->prepare($query);
|
|
$sth->execute();
|
|
while ( $row = $sth->fetch(PDO::FETCH_ASSOC) ) {
|
|
$dbbook = $row['path'] . (($row['path'] != "/") ? "/" : "") . $row['filename'];
|
|
$dbbooks[] = $dbbook;
|
|
}
|
|
$query = "DELETE FROM books WHERE CONCAT(path, filename)=:target OR CONCAT(path, '/', filename)=:target";
|
|
$sth = $globaldbh->prepare($query);
|
|
foreach ( $dbbooks as $dbbook ) {
|
|
if ( !is_file(BOOKDIR . $dbbook) ) {
|
|
$sth->bindValue(":target", $dbbook, PDO::PARAM_STR);
|
|
$sth->execute();
|
|
$count_purged++;
|
|
}
|
|
}
|
|
|
|
if ( $cli ) {
|
|
echo "New Books: {$count_new}\n";
|
|
echo "Purged Books: {$count_purged}\n";
|
|
echo "Total Books: {$count_total}\n";
|
|
} else {
|
|
$data = array();
|
|
$data["new"] = $count_new;
|
|
$data["purged"] = $count_purged;
|
|
$data["total"] = $count_total;
|
|
header('Content-Type: application/json');
|
|
echo json_encode($data);
|
|
exit();
|
|
}
|
|
|
|
// vim: set sw=3 ts=3:
|