Advertisement
Guest User

Untitled

a guest
Jan 20th, 2013
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.80 KB | None | 0 0
  1. <?php
  2. /*Todo:
  3.  * Wrapper
  4.  */
  5. header('Content-type: text/javascript');
  6. class PMIDLookup {
  7.  
  8.     private $id;
  9.    
  10.     public function __construct( $id ) {
  11.         $this->id = $id;
  12.     }
  13.    
  14.     public function getResult() {
  15.         $url = 'http://eutils.ncbi.nlm.nih.gov/entrez/eutils/esummary.fcgi?';
  16.         $url .= "&db=pubmed";
  17.         $url .= '&tool=WikipediaRefToolbar2';
  18.         $url .= '&email=alexz@toolserver.org';
  19.         $url .= "&id={$this->id}";
  20.         $url .= '&retmode=xml';
  21.        
  22.         $ch = curl_init($url);
  23.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  24.         $xml = curl_exec($ch);
  25.         curl_close($ch);
  26.         $data = simplexml_load_string($xml);
  27.         $result = array();
  28.         foreach($data->DocSum->Item as $i) {
  29.             switch ($i['Name']) {
  30.                 case 'PubDate':
  31.                     $result['date'] = (string)$i;
  32.                     break;
  33.                 case 'FullJournalName':
  34.                     $result['journal'] = (string)$i;
  35.                     break;
  36.                 case 'Title':
  37.                     $result['title'] = (string)$i;
  38.                     break;
  39.                 case 'Volume':
  40.                     $result['volume'] = (string)$i;
  41.                     break;
  42.                 case 'Issue':
  43.                     $result['issue'] = (string)$i;
  44.                     break;
  45.                 case 'Pages':
  46.                     $result['pages'] = (string)$i;
  47.                     break;
  48.                 case 'AuthorList':
  49.                     foreach($i->Item as $a) {
  50.                         $r = preg_match('/^(.*?) (\S*)$/', (string)$a, $match);
  51.                         if ($r) {
  52.                             $result['authors'][] = array( $match[1], $match[2] );
  53.                         } else {
  54.                             $result['authors'][] = array( (string)$a, '' );
  55.                         }
  56.                     }
  57.                     break;         
  58.             }
  59.         }
  60.         return $result;
  61.     }
  62. }
  63.  
  64. class ISBNLookup {
  65.  
  66.     private $id;
  67.    
  68.     public function __construct( $id ) {
  69.         $this->id = $id;
  70.     }
  71.    
  72.     public function getResult() {
  73.         $url = "http://xisbn.worldcat.org/webservices/xid/isbn/{$this->id}?method=getMetadata&format=json&fl=year,ed,title,author,publisher,city";
  74.         $ch = curl_init($url);
  75.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  76.         $json = curl_exec($ch);
  77.         curl_close($ch);
  78.         $data = json_decode($json, true);
  79.         $result = array();
  80.         if ($data['stat'] == 'ok') {
  81.             $result['title'] = $data['list'][0]['title'];
  82.             $result['publisher'] = $data['list'][0]['publisher'];
  83.             $result['location'] = $data['list'][0]['city'];
  84.             $result['year'] = $data['list'][0]['year'];
  85.             $result['edition'] = $data['list'][0]['ed'];
  86.             $authors = $data['list'][0]['author'];
  87.             $authors = rtrim($authors, '.');
  88.             if (strpos($authors, 'by ') === 0) {
  89.                 $authors = substr($authors, 3);
  90.             }
  91.             $result['authors'] = array();
  92.             $a = explode(' and ', $authors);
  93.             $alist = array();
  94.             if (count($a) == 2) {
  95.                 $alist = explode(', ', $a[0]);
  96.                 $alist[] = $a[1];
  97.             } else {
  98.                 $alist[] = $authors;
  99.             }
  100.             foreach($alist as $a) {
  101.                 $r = preg_match('/^(.*?) (\S*)$/', $a, $match);
  102.                 if ($r) {
  103.                     $result['authors'][] = array( $match[2], $match[1] );
  104.                 } else {
  105.                     $result['authors'][] = array( $a, '');
  106.                 }
  107.             }
  108.         }
  109.         return $result;    
  110.     }
  111.    
  112. }
  113.  
  114. class DOILookup {
  115.  
  116.     private $id;
  117.    
  118.     public function __construct( $id ) {
  119.         $this->id = $id;
  120.     }
  121.    
  122.     public function getResult() {
  123.         require_once('crossref.php');
  124.         $url = "http://www.crossref.org/openurl/?id={$this->id}&noredirect=true&pid=$crPID&format=unixref";
  125.         $ch = curl_init($url);
  126.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  127.         $xml = curl_exec($ch);
  128.         curl_close($ch);
  129.         $data = simplexml_load_string($xml);
  130.         $result = array();
  131.         if (!$data->doi_record->crossref->error) {
  132.             $res = $data->doi_record->crossref->journal;
  133.  
  134.             $result['title'] = (string)$res->journal_article->titles->title[0];
  135.             $result['journal'] = (string)$res->journal_metadata->full_title;
  136.             $result['volume'] = (string)$res->journal_issue->journal_volume->volume;
  137.             $result['issue'] = (string)$res->journal_issue->issue;
  138.             if ($res->journal_article->pages) {
  139.                 $result['pages'] = (string)$res->journal_article->pages->first_page;
  140.                 $res->journal_article->pages->last_page ? $result['pages'].='–'.$res->journal_article->pages->last_page : 0;
  141.             }
  142.             $result['date'] = (string)$res->journal_article->publication_date->year;
  143.             $res->journal_article->publication_date->month ? (string)$result['date'].='-'.$res->journal_article->publication_date->month : 0;
  144.             $res->journal_article->publication_date->day ? (string)$result['date'].='-'.$res->journal_article->publication_date->day : 0;
  145.             $result['authors'] = array();
  146.  
  147.             foreach($res->journal_article->contributors->person_name as $a) {
  148.                 $result['authors'][] = array((string)$a->surname, (string)$a->given_name);
  149.             }      
  150.         }
  151.         return $result;
  152.     }  
  153. }
  154.  
  155. $k = array_keys($_GET);
  156. switch($k[0]) {
  157.     case 'pmid':
  158.         $class = PMIDLookup;
  159.         break;
  160.     case 'isbn':
  161.         $class = ISBNLookup;
  162.         break;
  163.     case 'doi':
  164.         $class = DOILookup;
  165.         break;
  166.     default:
  167.         die(1);
  168. }
  169. $idval = trim($_GET[$k[0]]);
  170. $look = new $class($idval);
  171. $res = $look->getResult();
  172. $tem = $_GET['template'];
  173. echo 'CiteTB.autoFill('.json_encode($res).", '$tem', '{$k[0]}')";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement