Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
Last revisionBoth sides next revision
buechereielektrik:unapi:picarecord.php [2009-12-10 19:42] – nun aber: PicaRecord.php strolsbuechereielektrik:unapi:picarecord.php [2009-12-26 14:44] rosenke
Line 1: Line 1:
-<code>+<code php PicaRecord.php>
 <?php <?php
  
Line 26: Line 26:
  
 /*  Changelog /*  Changelog
-    + * 
-  20091210: Put class Picappn to PicaRecord.php.+  20091220: Check whether curl-Module is available, if not use 
 +              file_get_contents(). 
 +              Some minor bugfixes. 
 +  20091215:   Added function "getJson" to class "Picappn"
 +              Made ISBN in getBibTex() unique. 
 +              Filtered some characters in getArrayNice(). 
 +  20091214:   Added function "convOutputNice", "getArrayNice", "getBibsonomy", 
 +              "getKeysText" and "getKeysBibTex" to class "Picappn"
 +              Handled empty records for dc and rdf. 
 +  20091213: Added function "convOutput" to class "Picappn"
 +              Defined text-format. 
 +              Defined bibtex-format. 
 +  20091211:   Added function "final_result". 
 +  20091210: Put class "Picappnto PicaRecord.php.
  * Ordered functions alphabetically.  * Ordered functions alphabetically.
  * Added functions "getArray", "getPlain" and  "getText" to class  * Added functions "getArray", "getPlain" and  "getText" to class
Line 37: Line 50:
  
 $opac_url = "http://pica1l.lhb.tu-darmstadt.de/DB=LHBDA/XML=1.0/PPN?PPN="; $opac_url = "http://pica1l.lhb.tu-darmstadt.de/DB=LHBDA/XML=1.0/PPN?PPN=";
-//$opac_url = "http://opac.ub.uni-marburg.de//XML=1.0/PPN?PPN=";+//$opac_url = "http://opac.ub.uni-marburg.de/XML=1.0/PPN?PPN="; 
 +//$opac_url = "http://cbsopac.rz.uni-frankfurt.de/XML=1.0/PPN?PPN="
 +//$opac_url = "http://gso.gbv.de/XML=1.0/PPN?PPN="; 
 + 
 +$bibsonomy_url = "http://www.bibsonomy.org/BibtexHandler?requTask=upload&selection="; 
 + 
 +$filter_nice = array( 
 + '{' => '', 
 + '}' => '' 
 + );
  
 /** represents a bibliographic data record from oclc/pica LBS  /** represents a bibliographic data record from oclc/pica LBS 
Line 105: Line 127:
              $ch = substr($str,++$i,1); //new tag ahead              $ch = substr($str,++$i,1); //new tag ahead
              $tag = $this->getTagName($ch);              $tag = $this->getTagName($ch);
 +             $res[$tag] = "";
              break;              break;
- 
           case 30: //information separator two, another field to follow           case 30: //information separator two, another field to follow
              break;              break;
- 
           case 226: //pica two byte char accent like "é";           case 226: //pica two byte char accent like "é";
              $ch = substr($str,++$i,1); // read one char ahead              $ch = substr($str,++$i,1); // read one char ahead
-             $res .= $this->getCode2($ch);+             $res[$tag] .= $this->getCode2($ch);
              break;              break;
- 
           default:           default:
              $res[$tag] .= $this->getCode($ch);              $res[$tag] .= $this->getCode($ch);
              break;              break;
         } //switch         } //switch
- 
     }     }
     return $res;     return $res;
Line 195: Line 214:
   function getDublinCore($str) {   function getDublinCore($str) {
     $res = $this->getXmlData($str); //parse dublin core     $res = $this->getXmlData($str); //parse dublin core
 +
 +    if (empty($res)) return "";
  
     $res  = "<?xml version=\"1.0\" ?>\n";     $res  = "<?xml version=\"1.0\" ?>\n";
Line 364: Line 385:
             break;             break;
      }      }
-     return $res;+     //return $res;
   }   }
  
   /** public -- parse dublin core data */   /** public -- parse dublin core data */
   //client functionality   //client functionality
 +  // comment out if using PHP4
   function readDublinCore($str) {   function readDublinCore($str) {
      $doc = new DomDocument();      $doc = new DomDocument();
Line 393: Line 415:
   /** public -- parse dublin core data */   /** public -- parse dublin core data */
   //  experimental, does not work   //  experimental, does not work
 +  /*
   function readRDF_DC($str) {   function readRDF_DC($str) {
      $doc = new DomDocument();      $doc = new DomDocument();
Line 414: Line 437:
      return $res;      return $res;
   }   }
 +  */
  
   /** private -- read tag content from string */   /** private -- read tag content from string */
Line 477: Line 501:
     $this->ppn = 0;     $this->ppn = 0;
     $this->prec = new PicaRecord();     $this->prec = new PicaRecord();
 +  }
 +
 +  /** public -- convert for output using raw array data*/
 +  function convOutput($array,$text,$separator,$category,$subfield,$suffix) {
 +    if (empty($array)) return "";
 +    if (!empty($subfield)) {
 +      if (empty($array[$category][$subfield])) return "";
 +      $res = $text.$separator.$array[$category][$subfield].$suffix;
 +    } else {
 +      if (empty($array[$category])) return "";
 +      $res = $text.$separator.$array[$category].$suffix;
 +    }
 +    return $res;
 +  }
 +
 +  /** public -- convert for output using nice array data*/
 +  function convOutputNice($array_nice,$array_text,$key,$prefix,$suffix) {
 +    if (empty($array_nice)) return "";
 +    if (empty($array_nice[$key])) return "";
 +    $res = $array_text[$key].$prefix.$array_nice[$key].$suffix;
 +    return $res;
   }   }
  
Line 484: Line 529:
     $res = $this->getData($this->ppn);     $res = $this->getData($this->ppn);
     $res = $this->prec->getArray($res);     $res = $this->prec->getArray($res);
 +    return $res;
 +  }
 +
 +  /** public -- return array with speaking keys */
 +  function getArrayNice($array_raw) {
 +    if (empty($array_raw)) return "";
 +    $record = $array_raw;
 +
 +    global $filter_nice;
 +
 +    //Type
 +    $res['type'] = strtr($record['002@']['x0'], $filter_nice);
 +
 +    //Author
 +    $res['author'] = strtr($record['028A']['x8'], $filter_nice);
 +    //Editor
 +    $res['editor'] = strtr($record['028C']['x8'], $filter_nice);
 +
 +    //Title
 +    $res['title'] = strtr($record['021A']['a'], $filter_nice);
 +
 +    //Series
 +    $res['series'] = strtr($record['036F']['x8'], $filter_nice)." ".strtr($record['036F']['l'], $filter_nice);
 +
 +    //Address, especially city
 +    $res['address'] = strtr($record['033A']['p'], $filter_nice);
 +    //Publisher
 +    $res['publisher'] = strtr($record['033A']['n'], $filter_nice);
 +    //Edition
 +    $res['edition'] = strtr($record['032@']['a'], $filter_nice);
 +    //Year
 +    $res['year'] = strtr($record['011@']['a'], $filter_nice);
 +
 +    //ISBNs
 +    $res['isbn10'] = strtr($record['004A']['x0'], $filter_nice);
 +    $res['isbn13'] = strtr($record['004A']['A'], $filter_nice);
 +
 +    //PPN
 +    $res['ppn'] = strtr($record['003@']['x0'], $filter_nice);
 +    return $res;
 +  }
 +
 +  /** public -- return BibTex record from pica data */
 +  //used by bibsonomy
 +  function getBibsonomy() {
 +    if ($this->ppn == 0) return "";
 +
 +    $res = $this->getBibTex();
 +    $res = urlencode($res);
 +
 +    global $bibsonomy_url;
 +    $res = $bibsonomy_url.$res;
 +
 +    return $res;
 +  }
 +
 +  /** public -- return BibTex record from pica data */
 +  //used by unapi
 +  function getBibTex() {
 +    if ($this->ppn == 0) return "";
 +    $record_raw = $this->getArray($this->ppn);
 +    $record_nice = $this->getArrayNice($record_raw);
 +    $keys_nice = $this->getKeysBibTex();
 +
 +    //make sure there is only one ISBN
 +    if (!empty($record_nice['isbn13'])) unset($record_nice['isbn10']);
 +
 +    if (substr($record_nice['type'], 0, 2) == "Aa") {
 +      //Type
 +      $res = "@book {";
 +
 +      //create identifier
 +      $res .= $this->convOutput($record_raw,"","",'003@','x0',"");
 +      $res .= $this->convOutput($record_raw,"","",'011@','a',",\n");
 +
 +      //Author et al.
 +      $i = array('author', 'editor', 'title', 'series', 'address', 'publisher',
 +        'edition', 'year', 'isbn10', 'isbn13');
 +
 +      foreach ($i as $j) {
 +        $res .= $this->convOutputNice($record_nice,$keys_nice,$j," = {","},\n");
 +      }
 +
 +      //Call Number
 +      $res .= $this->convOutput($record_raw,"  note"," = {","209A/01",'a',"}");
 +    }
 +    $res .= "\n}";
     return $res;     return $res;
   }   }
Line 489: Line 621:
   /** private -- get data via pica xml interface */   /** private -- get data via pica xml interface */
   function getData($ppn) {   function getData($ppn) {
-    $ch = curl_init("$this->url"); +    //check whether curl-Module is available, if not available use fallback 
-    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); +    if (function_exists('curl_init')) { 
-    curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.session_id() );  +      $ch = curl_init("$this->url"); 
-    //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );  +      curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); 
-    curl_setopt($ch, CURLOPT_HEADER, 0); +      curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.session_id() );  
-    $res = curl_exec($ch); +      //curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1 );  
-    curl_close($ch);+      curl_setopt($ch, CURLOPT_HEADER, 0); 
 +      $res = curl_exec($ch); 
 +      curl_close($ch); 
 +    } else { 
 +      $res = file_get_contents("$this->url"); 
 +    }
     return $res;     return $res;
   }   }
Line 510: Line 647:
     $res = $this->getData($this->ppn);     $res = $this->getData($this->ppn);
     $res = $this->prec->getXmlData($res); //parses dublin core     $res = $this->prec->getXmlData($res); //parses dublin core
 +
 +    if (empty($res)) return "";
  
     $res  = "<?xml version=\"1.0\"?>\n";     $res  = "<?xml version=\"1.0\"?>\n";
Line 519: Line 658:
              xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";              xmlns:dc=\"http://purl.org/dc/elements/1.1/\">\n";
     $res .= "<rdf:Description rdf:about=\""     $res .= "<rdf:Description rdf:about=\""
-            .$this->myself."?ppn=$ppn&amp;format=rdf"."\">\n";+            .$this->myself."?ppn=".$this->ppn."&amp;format=rdf"."\">\n";
     $res .= "<dc:contributor>".$this->prec->contributor."</dc:contributor>\n";     $res .= "<dc:contributor>".$this->prec->contributor."</dc:contributor>\n";
     $res .= "<dc:coverage>".$this->prec->coverage."</dc:coverage>\n";     $res .= "<dc:coverage>".$this->prec->coverage."</dc:coverage>\n";
Line 540: Line 679:
   }   }
  
-  /** public -- return xml formated pica record */ +  /** public -- return JSON record from pica data */ 
-  function getXmlData() {+  //used by unapi 
 +  function getJson() {
     if ($this->ppn == 0) return "";     if ($this->ppn == 0) return "";
-    $res = $this->getData($this->ppn); + 
-    $res = $this->prec->getXmlData($res);  +    $res = $this->getArray(); 
-    $head "<?xml version='1.0?>\n"+    //(PHP 5 >= 5.2.0, PECL json >= 1.2.0) 
-    //$head ."<record>\n"+    $res = json_encode($res); 
-    //$head ."<record url=\"$this->opac\">\n"+ 
-    //$head .= "<record url=\"$this->url\">\n"+    return $res; 
-    //$head ."<record url=\"".htmlspecialchars($this->url)."\">\n"+  } 
-    //$head ."<record url=\"".$this->opac.$this->ppn."\">\n"+ 
-    $head ."<record url=\"".htmlentities($this->url)."\">\n"+  /** public -- return array with key names for BibTex*/ 
-    $tail "</record>\n"+  function getKeysBibTex() { 
-    return $head.$res.$tail;+    //Type 
 +    $res['type'] = 'Art'; 
 + 
 +    //Author 
 +    $res['author' author'; 
 +    //Editor 
 +    $res['editor'= '  editor'; 
 + 
 +    //Title 
 +    $res['title' title'; 
 + 
 +    //Series 
 +    $res['series' series'; 
 + 
 +    //Address, especially city 
 +    $res['address' address'; 
 +    //Publisher 
 +    $res['publisher'] = '  publisher'
 +    //Edition 
 +    $res['edition' edition'; 
 +    //Year 
 +    $res['year'] = '  year'; 
 + 
 +    //ISBNs 
 +    $res['isbn10'' isbn'; 
 +    $res['isbn13' isbn'; 
 + 
 +    //PPN 
 +    $res['ppn'] = '  note'; 
 +    return $res; 
 +  } 
 + 
 +  /** public -- return array with key names for text*/ 
 +  function getKeysText(
 +    //Type 
 +    $res['type'] = 'Art'; 
 + 
 +    //Author 
 +    $res['author''Autor'; 
 +    //Editor 
 +    $res['editor''Herausgeber'; 
 + 
 +    //Title 
 +    $res['title'] = 'Titel'; 
 + 
 +    //Series 
 +    $res['series'] = 'Schriftenreihe'; 
 + 
 +    //Address, especially city 
 +    $res['address''Ort'; 
 +    //Publisher 
 +    $res['publisher''Verlag'; 
 +    //Edition 
 +    $res['edition'] = 'Auflage'; 
 +    //Year 
 +    $res['year''Jahr'; 
 + 
 +    //ISBNs 
 +    $res['isbn10'] = 'ISBN-10'
 +    $res['isbn13'] = 'ISBN-13'; 
 + 
 +    //PPN 
 +    $res['ppn'] = 'PPN'; 
 +    return $res;
   }   }
  
Line 579: Line 782:
   function getText() {   function getText() {
     if ($this->ppn == 0) return "";     if ($this->ppn == 0) return "";
-    $record = $this->getArray($this->ppn); +    $record_raw = $this->getArray($this->ppn); 
-    $res .= "PPN:\t".$record['003@']['x0'];+    $record_nice = $this->getArrayNice($record_raw); 
 +    $keys_nice = $this->getKeysText(); 
 +    $res = ""; 
 + 
 +    if (substr($record_nice['type'], 0, 2) == "Aa") { 
 +      //Type 
 +      $res .= "Art:\t\tMonographie\n"
 + 
 +      //Author 
 +      $res .$this->convOutputNice($record_nice,$keys_nice,"author",":\t\t","\n"); 
 +      //Editor 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"editor",":\t","\n"); 
 + 
 +      //Title 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"title",":\t\t","\n"); 
 + 
 +      //Series 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"series",":\t","\n"); 
 + 
 +      //Address, especially city 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"address",":\t\t","\n"); 
 +      //Publisher 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"publisher",":\t\t","\n"); 
 +      //Edition 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"edition",":\t","\n"); 
 +      //Year 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"year",":\t\t","\n"); 
 + 
 +      //ISBNs 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"isbn10",":\t","\n"); 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"isbn13",":\t","\n"); 
 + 
 +      //Call Number 
 +      $i = 1; 
 +      $j = 0; 
 +      while ( $i <= 99 ) { 
 +        if (($i <= 9) && ($j == 0)) { 
 +          $j = "0".$i; 
 +        } elseif (($i > 9) && ($j == 0)) { 
 +          $j = $i; 
 +        } 
 +        $res .= $this->convOutput($record_raw,"Signatur",":\t","209A/$j",'a',"\n"); 
 +        $i++; 
 +        if ($i <= 9 ) { 
 +          $j = "0".$i; 
 +        } else { 
 +          $j = $i; 
 +        } 
 +        if (empty($record_raw["209A/$j"]['a'])) break; 
 +      } 
 + 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"ppn",":\t\t","\n"); 
 +    }
     return $res;     return $res;
 +  }
 +
 +  /** public -- return xml formated pica record */
 +  function getXmlData() {
 +    if ($this->ppn == 0) return "";
 +    $res = $this->getData($this->ppn);
 +    $res = $this->prec->getXmlData($res); 
 +    $head = "<?xml version='1.0' ?>\n";
 +    //$head .= "<record>\n";
 +    //$head .= "<record url=\"$this->opac\">\n";
 +    //$head .= "<record url=\"$this->url\">\n";
 +    //$head .= "<record url=\"".htmlspecialchars($this->url)."\">\n";
 +    //$head .= "<record url=\"".$this->opac.$this->ppn."\">\n";
 +    $head .= "<record url=\"".htmlentities($this->url)."\">\n";
 +    $tail = "</record>\n";
 +    return $head.$res.$tail;
   }   }
  
Line 597: Line 868:
  
 } //end class Picappn } //end class Picappn
 +
 +/* Return result or 404 */
 +function final_result($res, $content_type) {
 +  if (!empty($res)) {
 +    header("Content-type: $content_type");
 +    echo $res;
 +  } else {
 +    header('HTTP/1.0 404 Not Found');
 +  }
 +}
  
 ?> ?>
 </code> </code>
buechereielektrik/unapi/picarecord.php.txt · Last modified: 2010-01-10 22:28 by rosenke
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0