Skip to content

Instantly share code, notes, and snippets.

@kaizu
Last active March 2, 2017 06:40
Show Gist options
  • Save kaizu/e90c276e31b0ffcb4a55287b50c0bd4d to your computer and use it in GitHub Desktop.
Save kaizu/e90c276e31b0ffcb4a55287b50c0bd4d to your computer and use it in GitHub Desktop.
<?php
define( 'PLUGIN_PAPER_DIR', 'papers' );
define( 'PDF_ICON_FILE_PATH', 'image/pdf_alt.png' );
function ref_paper_get_paper_filename( $pubmed_id )
{
return PLUGIN_PAPER_DIR . '/' . $pubmed_id . '.xml';
}
function ref_paper_get_pubmed_url( $pubmed_id )
{
return'http://www.ncbi.nlm.nih.gov/entrez/query.fcgi?cmd=Retrieve&db=pubmed&dopt=Abstract&list_uids=' . $pubmed_id;
}
function ref_paper_get_pdf_filename( $pubmed_id )
{
return PLUGIN_PAPER_DIR . '/' . $pubmed_id . '.pdf';
}
function ref_paper_store_paper_info( $pubmed_id )
{
$xml_filename = ref_paper_get_paper_filename( $pubmed_id );
if ( file_exists( $xml_filename ) ) {
return;
}
$entrez_eutils_url = 'https://www.ncbi.nlm.nih.gov/entrez/eutils/efetch.fcgi?db=pubmed&retmode=xml&rettype=abstract&id=' . $pubmed_id;
//XXX: http_request doesn't work with HTTPS. Use cUrl instead.
// $paper_info = http_request( $entrez_eutils_url );
// file_put_contents( $xml_filename, $paper_info[ 'data' ] );
//XXX: The following code doesn't care about proxy.
$ch = curl_init();
curl_setopt( $ch, CURLOPT_URL, $entrez_eutils_url );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$response = curl_exec( $ch );
curl_close( $ch );
file_put_contents( $xml_filename, $response );
}
function ref_paper_read_paper_info( $pubmed_id )
{
$xml_filename = ref_paper_get_paper_filename( $pubmed_id );
$simple_xml = simplexml_load_file( $xml_filename );
if ( !$simple_xml ) {
return false;
}
$paper_info = array();
$article_node = $simple_xml->PubmedArticle[ 0 ]->MedlineCitation->Article;
// AuthorList
$paper_info[ 'AuthorList' ] = array();
foreach ( $article_node->AuthorList->Author as $author_item ) {
array_push( $paper_info[ 'AuthorList' ], htmlspecialchars( (string) $author_item->LastName . ' ' . (string) $author_item->Initials ) );
}
// ArticleTitle
$paper_info[ 'ArticleTitle' ] = htmlspecialchars( (string) $article_node->ArticleTitle );
// Journal
$paper_info[ 'Journal' ] = htmlspecialchars( (string) $simple_xml->PubmedArticle[ 0 ]->MedlineCitation->MedlineJournalInfo->MedlineTA );
// PubDate
$paper_info[ 'PubDateYear' ] = htmlspecialchars( (string) $article_node->Journal->JournalIssue->PubDate[ 0 ]->Year[ 0 ] );
$paper_info[ 'PubDateMonth' ] = htmlspecialchars( (string) $article_node->Journal->JournalIssue->PubDate[ 0 ]->Month[ 0 ] );
$paper_info[ 'PubDateDay' ] = htmlspecialchars( (string) $article_node->Journal->JournalIssue->PubDate[ 0 ]->Day[ 0 ] );
// Volume
$paper_info[ 'Volume' ] = htmlspecialchars( (string) $article_node->Journal->JournalIssue->Volume[ 0 ] );
// Issue
$paper_info[ 'Issue' ] = htmlspecialchars( (string) $article_node->Journal->JournalIssue->Issue[ 0 ] );
// Pagination
$paper_info[ 'Pagination' ] = htmlspecialchars( (string) $article_node->Pagination->MedlinePgn[ 0 ] );
// Abstract
$paper_info[ 'Abstract' ] = htmlspecialchars( (string) $article_node->Abstract[ 0 ]->AbstractText[ 0 ] );
return $paper_info;
}
function ref_paper_format_paper_info( &$paper_info )
{
// Author
if ( array_key_exists( 'AuthorList', $paper_info ) ) {
$paper_info[ 'Author' ] = join( ', ', $paper_info[ 'AuthorList' ] ) . '.';
}
else {
$paper_info[ 'Author' ] = '';
}
// PubDate
if ( array_key_exists( 'PubDateYear', $paper_info ) and ( $paper_info[ 'PubDateYear' ] != '' ) ) {
$paper_info[ 'PubDate' ] = $paper_info[ 'PubDateYear' ];
if ( array_key_exists( 'PubDateMonth', $paper_info ) and ( $paper_info[ 'PubDateMonth' ] != '' ) ) {
$paper_info[ 'PubDate' ] = $paper_info[ 'PubDate' ] . ' ' . $paper_info[ 'PubDateMonth' ];
if ( array_key_exists( 'PubDateDay', $paper_info ) and ( $paper_info[ 'PubDateDay' ] != '' ) ) {
$paper_info[ 'PubDate' ] = $paper_info[ 'PubDate' ] . ' ' . $paper_info[ 'PubDateDay' ];
}
}
}
else {
$paper_info[ 'PubDate' ] = '';
}
// FormattedVolume
if ( array_key_exists( 'Volume', $paper_info ) and ( $paper_info[ 'Volume' ] != '' ) ) {
$paper_info[ 'FormattedVolume' ] = $paper_info[ 'Volume' ];
if ( array_key_exists( 'Issue', $paper_info ) and ( $paper_info[ 'Issue' ] != '' ) ) {
$paper_info[ 'FormattedVolume' ] = $paper_info[ 'FormattedVolume' ] . '(' . $paper_info[ 'Issue' ] . ')';
}
}
else {
$paper_info[ 'FormattedVolume' ] = '';
}
}
function plugin_ref_paper_inline( $pubmed_id )
{
ref_paper_store_paper_info( $pubmed_id );
$paper_info = ref_paper_read_paper_info( $pubmed_id );
if ( !$paper_info ) {
return 'ERROR: Unable to read a stored XML file.';
}
ref_paper_format_paper_info( $paper_info );
$pubmed_url = ref_paper_get_pubmed_url( $pubmed_id );
$paper_info_str = '<a href="' . $pubmed_url . '">' . $paper_info[ 'Author' ] . '</a>, ' . $paper_info[ 'ArticleTitle' ] . ', ' . $paper_info[ 'Journal' ] . '. ' . $paper_info[ 'PubDate' ] . ';' . $paper_info[ 'FormattedVolume' ] . ':' . $paper_info[ 'Pagination' ] . '.';
$paper_pdf_filename = ref_paper_get_pdf_filename( $pubmed_id );
if ( file_exists( $paper_pdf_filename ) ) {
$paper_info_str = '<a href="' . $paper_pdf_filename . '"><img src="' . PDF_ICON_FILE_PATH . '" /></a>' . $paper_info_str;
}
return $paper_info_str;
}
function plugin_ref_paper_convert( $pubmed_id )
{
ref_paper_store_paper_info( $pubmed_id );
$paper_info = ref_paper_read_paper_info( $pubmed_id );
if ( !$paper_info ) {
return 'ERROR: Unable to read a stored XML file.';
}
ref_paper_format_paper_info( $paper_info );
$pubmed_url = ref_paper_get_pubmed_url( $pubmed_id );
$paper_info_str = '<p>';
$paper_pdf_filename = ref_paper_get_pdf_filename( $pubmed_id );
if ( file_exists( $paper_pdf_filename ) ) {
$paper_info_str .= '<a href="' . $paper_pdf_filename . '"><img src="' . PDF_ICON_FILE_PATH . '" /></a>';
}
$paper_info_str .= '<a href="' . $pubmed_url . '">' . $paper_info[ 'Author' ] . '</a></p>';
$paper_info_str .= '<p>' . $paper_info[ 'ArticleTitle' ] . '</p>';
$paper_info_str .= '<p>' . $paper_info[ 'Journal' ] . '. ' . $paper_info[ 'PubDate' ] . ';' . $paper_info[ 'FormattedVolume' ] . ':' . $paper_info[ 'Pagination' ] . '.</p>';
$paper_info_str .= '<p>' . $paper_info[ 'Abstract' ] . '</p>';
$paper_info_str .= '<p>PMID: ' . htmlspecialchars( (string) $pubmed_id ) . ' [PubMed - indexed for MEDLINE]</p>';
return $paper_info_str;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment