function add_pdf_to_db($path, $attributes) { global $db; $path = canonicalize_path($path); if (!file_exists(get_full_path($path))) { err_bad_input_data('file_path', $path, "file doesn't exist"); } $attributes['md5'] = get_md5_hash($path); if (!$attributes['md5']) { err_internal("could not generate hash of file {$path}"); } // TODO: find number of pages $pages = 0; $sql = "INSERT INTO files VALUES (NULL, '" . addslashes($path) . "'" . ', ' . ($attributes['title'] ? "'" . addslashes($attributes['title']) . "'" : 'NULL') . ', ' . ($attributes['md5'] ? "'" . addslashes($attributes['md5']) . "'" : 'NULL') . ', ' . ($attributes['date'] ? "'" . addslashes($attributes['date']) . "'" : 'NULL') . ", " . $pages . ', ' . ($attributes['origin'] ? "'" . addslashes($attributes['origin']) . "'" : 'NULL') . ', ' . ($attributes['recipient'] ? "'" . addslashes($attributes['recipient']) . "'" : 'NULL') . ")"; $db->exec($sql); return $db->lastInsertRowID(); }
} function err_internal($msg) { fatal_error($msg); } $data_dir = $argv[1]; require_once "../services/lib.php"; echo "using datadir: {$data_dir}\n"; $data_dir = realpath($data_dir); // look at each file in db directory foreach (new RecursiveIteratorIterator(new RecursiveDirectoryIterator($data_dir)) as $file) { $path = canonicalize_path(substr($file->getPathname(), strlen($data_dir))); if (preg_match("/\\.(pdf|PDF)\$/", $path)) { // make sure it ends in .pdf $id = get_pdf_id($path); $md5 = get_md5_hash($path); if ($id) { // it already has a db record $attributes = get_pdf_info($id); if ($attributes['md5'] != $md5) { // ...with an incorrect md5 value, correct it. unset($attributes['id']); echo "fixing md5 info: {$id}: {$path} \n"; $attributes['md5'] = $md5; print_r($attributes); update_pdf_info($id, $attributes); } } else { // it has no db record... insert it echo "inserting: {$path} \n"; $attributes = array('md5' => $md5);