function merge_pdfs($pdf_ids) { // TODO: this entire function should be gaurded by some sort of mutex if (count($pdf_ids) < 2) { err_bad_input_format("expected at least 2 pdf ids"); } // validate pdf arguments and gather merged pdf attributes $paths = array(); foreach ($pdf_ids as $pdf_id) { $pdf = get_pdf_info($pdf_id); if (!$pdf) { err_bad_input_data('pdfid', $pdf_id, 'not a valid pdf id'); } $paths[] = $pdf['path']; } $new_path = merge_pdf_files($paths); $new_id = merge_pdf_metadata($pdf_ids, $new_path); if (!$new_id) { err_internal("could not merge pdfs"); } }
$dupes = get_pdf_ids_with_hash($pdf['md5']); if (count($dupes) > 1) { // merge with duplicates $path = null; $dupe_paths = array(); foreach ($dupes as $dupe_id) { $dupe_info = get_pdf_info($dupe_id); if ($path == null) { $path = $dupe_info['path']; } else { $dupe_paths[] = $dupe_info['path']; } } echo "merging metadata: "; print_r($dupes); $new_id = merge_pdf_metadata($dupes, $path); if ($new_id) { echo "new pdf id: {$new_id}\n"; foreach ($dupe_paths as $dupe_path) { delete_pdf_file($dupe_path); } } else { echo "merge failed\n"; } } } } echo "Total records: " . count(find_pdfs_all()) . "\n"; echo "orphans: \n"; foreach ($orphans as $pdf_id) { print_r(get_pdf_info($pdf_id));