2222
2323if (isServiceWithPDFList ($ service )) {
2424 handleMultiPdfService ($ vis_id , $ paper_id , $ images_path , $ filename , $ vis_type );
25+ } else if ($ service == "pubmed " ) {
26+ handlePubmedPdfService ($ vis_id , $ paper_id , $ url , $ images_path , $ filename );
2527} else {
2628 handleSingleUrlService ($ vis_id , $ paper_id , $ url , $ images_path , $ filename , $ vis_type );
2729}
@@ -59,7 +61,6 @@ function handleSingleUrlService(
5961 string $ vis_type
6062): void {
6163 $ valid_pdf_urls = getValidURLs ($ vis_id , $ paper_id , $ vis_type );
62-
6364 $ decoded_input_url = urldecode ($ url );
6465 $ normalized_valid_urls = array_map ('urldecode ' , $ valid_pdf_urls );
6566
@@ -70,6 +71,40 @@ function handleSingleUrlService(
7071 getPDFAndDownload ($ decoded_input_url , $ images_path , $ filename );
7172}
7273
74+ function handlePubmedPdfService (
75+ string $ vis_id ,
76+ string $ paper_id ,
77+ string $ url ,
78+ string $ images_path ,
79+ string $ filename ,
80+ ): void {
81+ $ revision_data = fetchLatestRevision ($ vis_id );
82+
83+ if (!$ revision_data ) {
84+ returnError ("There are no revision data for such visualization id " );
85+ }
86+
87+ $ inner_data = json_decode ($ revision_data ["data " ], true );
88+ $ documents_raw = $ inner_data ["documents " ] ?? null ;
89+ $ documents = json_decode ($ documents_raw , true );
90+
91+ $ filtered_documents = array_filter ($ documents , function ($ entry ) use ($ paper_id ) {
92+ return ($ entry ["id " ] ?? null ) === $ paper_id ;
93+ });
94+
95+ $ entry = array_shift ($ filtered_documents );
96+ if (!$ entry ) {
97+ returnError ("No valid entry found for the provided paper ID " );
98+ }
99+
100+ $ pmcid = $ entry ["pmcid " ] ?? null ;
101+ $ pubmed_url = "https://www.ncbi.nlm.nih.gov/pmc/articles/ " . $ pmcid . "/ " . "pdf/ " ;
102+
103+ $ content = getContentFromURL ($ pubmed_url );
104+ $ url = $ content [1 ];
105+ getPDFAndDownload ($ url , $ images_path , $ filename );
106+ }
107+
73108function getValidURLs (string $ vis_id , string $ paper_id , string $ vis_type ) {
74109 $ revision_data = fetchLatestRevision ($ vis_id );
75110
@@ -257,9 +292,10 @@ function startsWith($haystack, $needle) {
257292 return (substr ($ haystack , 0 , $ length ) === $ needle );
258293}
259294
260- function getPDFAndDownload ($ url , $ images_path , $ filename ) {
295+ function getPDFAndDownload (string $ url , string $ images_path , string $ filename ): void {
261296 $ output_path = $ images_path . $ filename ;
262- $ pdf = getContentFromURL ($ url )[0 ];
297+
298+ list ($ pdf , $ redirected_url ) = getContentFromURL ($ url );
263299
264300 if ($ pdf !== false ) {
265301 file_put_contents ($ output_path , $ pdf );
@@ -273,7 +309,7 @@ function getPDFAndDownload($url, $images_path, $filename) {
273309
274310 if (strtolower ($ mime_type ) != "application/pdf " ) {
275311 unlink ($ output_path );
276- returnError ("MIME type is not application/pdf " );
312+ returnError ("MIME type is not application/pdf! MIME type: { $ mime_type } " );
277313 }
278314}
279315
0 commit comments