1) {
$s .= " <$tag>\n \n";
foreach ($a as $val) {
$s .= " $val\n";
}
$s .= " \n $tag>\n";
} else {
$val = $a[0];
$s .= " <$tag rdf:parseType=\"Literal\">$val$tag>\n";
}
} else {
$val = $a;
$s .= " <$tag rdf:parseType=\"Literal\">$val$tag>\n";
}
}
return $s;
}
function qout (&$book, $name, $tag, $tag2) {
if (!isset ($book[$name])) {
return "";
}
$a = $book[$name];
if (is_array ($a)) {
if (count ($a) > 1) {
$s = " <$tag>\n \n";
foreach ($a as $val) {
$s .= " <$tag2>$val$tag2>\n";
}
$s .= " \n $tag>\n";
return $s;
} else {
$val = $a[0];
return " <$tag><$tag2>$val$tag2>$tag>\n";
}
}
$val = $a;
return " <$tag><$tag2>$val$tag2>$tag>\n";
}
_log ("Initializing ...");
$db = $config->db ();
_log (" Connected to Database ...");
$base_url = "http://www.gutenberg.org";
$file_base = "$base_url";
$now = date ("Y-m-d");
$books = array ();
_log (" Done\n");
_log ("Loading data from database ...");
_log (" Books");
$db->exec ("select * from books");
if ($db->FirstRow ()) {
do {
$pk = $db->get ("pk", SQLINT);
if ($reldate = $db->get ("release_date", SQLDATE)) {
$books[$pk]['release_date'] = date ("Y-m-d", $reldate);
$books[$pk]['downloads'] = $db->get ("downloads", SQLINT);
}
if ($db->get ("copyrighted", SQLINT)) {
$books[$pk]['copyrighted'] = 1;
}
} while ($db->NextRow ());
}
_log (" Authors");
$db->exec ("select * from v_books_authors order by fk_books");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$role = $db->get ("role", SQLCHAR);
if ($role == "Creator" || $role == "Author") {
$val = htmlspecialchars (FormatAuthorDate ($db));
$books[$fk_books]['creators'][] = $val;
} else {
$val = htmlspecialchars (FormatAuthorDateRole ($db));
$books[$fk_books]['contributors'][] = $val;
}
} while ($db->NextRow ());
}
_log (" FriendlyTitles");
foreach ($books as $fk_books => $dummy) {
$books[$fk_books]['friendlytitle'][] = htmlspecialchars (friendlytitle ($fk_books));
}
/* _log (" Titles");
$db->exec ("select * from titles");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$title = htmlspecialchars ($db->get ("title", SQLCHAR));
// $title = preg_replace ("/\s*\n/", "
", $title);
switch ($db->get ("title_order", SQLINT)) {
case 1:
$books[$fk_books]['240'][] = $title; break;
case 2:
case 4:
case 5:
$books[$fk_books]['246'][] = $title; break;
case 3:
$books[$fk_books]['505'][] = $title; break;
}
} while ($db->NextRow ());
} */
_log (" Attributes");
$db->exec ("select * from attributes");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$fk_attriblist = (string) $db->get ("fk_attriblist", SQLINT);
$text = htmlspecialchars ($db->get ("text", SQLCHAR));
$books[$fk_books][$fk_attriblist][] = $text;
} while ($db->NextRow ());
}
_log (" Categories");
$db->exec ("select * from mn_books_categories, categories where fk_categories = pk");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$books[$fk_books]['categories'][] = $db->get ("category", SQLCHAR);
} while ($db->NextRow ());
} else {
$books[$fk_books]['categories'][] = 'eBook';
}
_log (" Languages");
$db->exec ("select * from mn_books_langs");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$books[$fk_books]['languages'][] = $db->get ("fk_langs", SQLCHAR);
} while ($db->NextRow ());
}
_log (" Subjects");
$db->exec ("select * from mn_books_subjects, subjects where fk_subjects = pk");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$subject = htmlspecialchars ($db->get ("subject", SQLCHAR));
$books[$fk_books]['subjects'][] = $subject;
} while ($db->NextRow ());
}
_log (" LoCC");
$db->exec ("select * from mn_books_loccs");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$books[$fk_books]['loccs'][] = $db->get ("fk_loccs", SQLCHAR);
} while ($db->NextRow ());
}
_log (" Done\n");
$fp = fopen ("php://stdout", "w");
$s = <<< EOF
]>
$now
EOF;
fputs ($fp, $s);
// debug
// $books = array_slice ($books, 0, 500);
reset ($books);
while (list ($fk_books, $book) = each ($books)) {
$s = "\n";
$s .= " &pg;\n";
$s .= lout ($book, '240', 'dc:title');
$s .= lout ($book, '245', 'dc:title');
$s .= lout ($book, '246', 'dc:alternative');
$s .= lout ($book, '500', 'dc:description');
$s .= lout ($book, '505', 'dc:tableOfContents');
$s .= lout ($book, 'creators', 'dc:creator');
$s .= lout ($book, 'contributors', 'dc:contributor');
$s .= lout ($book, 'friendlytitle', 'pgterms:friendlytitle');
$s .= qout ($book, 'languages', 'dc:language', 'dcterms:ISO639-2');
$s .= qout ($book, 'subjects', 'dc:subject', 'dcterms:LCSH');
$s .= qout ($book, 'loccs', 'dc:subject', 'dcterms:LCC');
$s .= qout ($book, 'release_date', 'dc:created', 'dcterms:W3CDTF');
$s .= qout ($book, 'downloads', 'pgterms:downloads', 'xsd:nonNegativeInteger');
$s .= qout ($book, 'categories', 'dc:type', 'pgterms:category');
if (isset ($book['copyrighted'])) {
$s .= " Copyrighted work. See license inside work.\n";
} else {
$s .= " \n";
}
$s .= "\n\n";
fputs ($fp, $s);
}
$books = null;
// files
$db->exec ("select fk_books, mediatype, filetype, fk_filetypes, fk_compressions, fk_encodings, " .
"edition, filename, filesize, filemtime " .
"from files " .
"left join filetypes on files.fk_filetypes = filetypes.pk " .
"where fk_books is not null and obsoleted = 0 and diskstatus = 0 " .
"order by fk_books, filename;");
if ($db->FirstRow ()) {
do {
$fk_books = $db->get ("fk_books", SQLINT);
$filename = $db->get ("filename", SQLCHAR);
$filesize = $db->get ("filesize", SQLINT);
$filetype = $db->get ("filetype", SQLCHAR);
$fk_filetypes = $db->get ("fk_filetypes", SQLCHAR);
$filemtime = $db->get ("filemtime", SQLDATE);
$mediatype = $db->get ("mediatype", SQLCHAR);
$fk_encodings = $db->get ("fk_encodings", SQLCHAR);
$fk_compressions = $db->get ("fk_compressions", SQLCHAR);
if (!strncmp ($filename, "cache/", 6)) {
$filename = "&f;$filename";
} else {
$filename = "&f;dirs/$filename";
}
$mtime = date ("Y-m-d", $filemtime);
if (!empty ($fk_encodings) && !strncmp ($mediatype, "text/", 5)) {
$mediatype .= "; charset=\"$fk_encodings\"";
}
if (empty ($mediatype)) {
$mediatype = "application/octet-stream";
if (!empty ($fk_filetypes)) {
$mediatype .= "; type=\"$filetype ($fk_filetypes)\"";
}
}
$compression = "";
if ($fk_compressions == "zip") {
$compression = "\n application/zip";
}
$s = "
$mediatype$compression
$filesize
$mtime
";
fputs ($fp, $s);
} while ($db->NextRow ());
}
fputs ($fp, "\n\n");
fclose ($fp);
_log (" Done!\n");
?>