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-13 22:22] – format bibtex, text; function convOutput strolsbuechereielektrik:unapi:picarecord.php [2009-12-26 14:44] rosenke
Line 1: Line 1:
-<code>+<code php PicaRecord.php>
 <?php <?php
  
Line 27: Line 27:
 /*  Changelog /*  Changelog
  *  *
 +  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".   20091213: Added function "convOutput" to class "Picappn".
               Defined text-format.               Defined text-format.
Line 44: Line 53:
 //$opac_url = "http://cbsopac.rz.uni-frankfurt.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="; //$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 111: 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 201: 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 370: 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 399: 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 420: Line 437:
      return $res;      return $res;
   }   }
 +  */
  
   /** private -- read tag content from string */   /** private -- read tag content from string */
Line 485: Line 503:
   }   }
  
-  /** public -- convert for output */+  /** public -- convert for output using raw array data*/
   function convOutput($array,$text,$separator,$category,$subfield,$suffix) {   function convOutput($array,$text,$separator,$category,$subfield,$suffix) {
     if (empty($array)) return "";     if (empty($array)) return "";
Line 495: Line 513:
       $res = $text.$separator.$array[$category].$suffix;       $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;      return $res;
   }   }
Line 503: 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;     return $res;
   }   }
  
   /** public -- return BibTex record from pica data */   /** public -- return BibTex record from pica data */
-  //used by unapi +  //used by bibsonomy 
-  function getBibTex() {+  function getBibsonomy() {
     if ($this->ppn == 0) return "";     if ($this->ppn == 0) return "";
-    $record = $this->getArray($this->ppn); 
-      if (substr($record['002@']['x0'], 0, 2) == "Aa") { 
-        //Type 
-        $res .= "@book {"; 
  
-        //create identifier +    $res = $this->getBibTex(); 
-        $res .= $this->convOutput($record,"","",'003@','x0',""); +    $res = urlencode($res);
-        $res .$this->convOutput($record,"","",'011@','a',",\n");+
  
-        //Author +    global $bibsonomy_url
-        $res .= $this->convOutput($record,"  author"," = {",'028A','x8',"},\n")+    $res = $bibsonomy_url.$res;
-        //Editor +
-        $res .= $this->convOutput($record,"  editor"," = {",'028C','x8',"},\n");+
  
-        //Title +    return $res
-        $res .= $this->convOutput($record,"  title"," = {",'021A','a',"},\n");+  }
  
-        //Series +  /** public -- return BibTex record from pica data *
-        $res .= $this->convOutput($record,"  series"," {",'036M','a',"},\n");+  //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();
  
-        //Address, especially city +    //make sure there is only one ISBN 
-        $res .= $this->convOutput($record,"  address"," = {",'033A','p',"},\n"); +    if (!empty($record_nice['isbn13'])) unset($record_nice['isbn10']);
-        //Publisher +
-        $res .= $this->convOutput($record,"  publisher"," = {",'033A','n',"},\n")+
-        //Edition +
-        $res .= $this->convOutput($record,"  edition"," = {",'032@','a',"},\n")+
-        //Year +
-        $res .= $this->convOutput($record,"  year"," = {",'011@','a',"},\n");+
  
-        //ISBNs +    if (substr($record_nice['type']02) == "Aa"{ 
-        $res .= $this->convOutput($record,"  ISBN"," = {",'004A','x0',"},\n"); +      //Type 
-        $res .$this->convOutput($record, ISBN","{",'004A','A',"},\n");+      $res = "@book {";
  
-        //Call Number +      //create identifier 
-        $0; +      $res .= $this->convOutput($record_raw,"","",'003@','x0',"")
-        while ( $i <= 99 ) { +      $res .= $this->convOutput($record_raw,"","",'011@','a',",\n"); 
-          if ($i <= 9 ) { + 
-           $j = "0".$i+      //Author et al. 
-          } else { +      $i = array('author', 'editor', 'title', 'series', 'address', 'publisher', 
-           $j = $i; +        'edition', 'year', 'isbn10', 'isbn13'); 
-          } + 
-          $res .= $this->convOutput($record,"  note"," = {","209A/$j",'a',"}\n}"); +      foreach ($i as $j) { 
-          $i+++        $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 560: 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 581: 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 590: 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 608: Line 676:
     $res .= "</rdf:Description>\n";     $res .= "</rdf:Description>\n";
     $res .= "</rdf:RDF>\n";     $res .= "</rdf:RDF>\n";
 +    return $res;
 +  }
 +
 +  /** public -- return JSON record from pica data */
 +  //used by unapi
 +  function getJson() {
 +    if ($this->ppn == 0) return "";
 +
 +    $res = $this->getArray();
 +    //(PHP 5 >= 5.2.0, PECL json >= 1.2.0)
 +    $res = json_encode($res);
 +
 +    return $res;
 +  }
 +
 +  /** public -- return array with key names for BibTex*/
 +  function getKeysBibTex() {
 +    //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;     return $res;
   }   }
Line 634: 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); 
-      if (substr($record['002@']['x0'], 0, 2) == "Aa"+    $record_nice = $this->getArrayNice($record_raw)
-        //Type +    $keys_nice $this->getKeysText(); 
-        $res .= "Art:\t\tMonographie\n";+    $res = "";
  
-        //Author +    if (substr($record_nice['type']02) == "Aa"{ 
-        $res .= $this->convOutput($record,"Autor",":\t\t",'028A','x8',"\n"); +      //Type 
-        //Editor +      $res .= "Art:\t\tMonographie\n";
-        $res .= $this->convOutput($record,"Herausgeber",":\t",'028C','x8',"\n");+
  
-        //Title +      //Author 
-        $res .= $this->convOutput($record,"Titel",":\t\t",'021A','a',"\n");+      $res .= $this->convOutputNice($record_nice,$keys_nice,"author",":\t\t","\n"); 
 +      //Editor 
 +      $res .= $this->convOutputNice($record_nice,$keys_nice,"editor",":\t","\n");
  
-        //Series +      //Title 
-        $res .= $this->convOutput($record,"Schriftenreihe",":\t",'036M','a',"\n");+      $res .= $this->convOutputNice($record_nice,$keys_nice,"title",":\t\t","\n");
  
-        //Address, especially city +      //Series 
-        $res .= $this->convOutput($record,"Ort",":\t\t",'033A','p',"\n"); +      $res .= $this->convOutputNice($record_nice,$keys_nice,"series",":\t","\n");
-        //Publisher +
-        $res .= $this->convOutput($record,"Verlag",":\t\t",'033A','n',"\n"); +
-        //Edition +
-        $res .= $this->convOutput($record,"Auflage",":\t",'032@','a',"\n"); +
-        //Year +
-        $res .= $this->convOutput($record,"Jahr",":\t\t",'011@','a',"\n");+
  
-        //ISBNs +      //Address, especially city 
-        $res .= $this->convOutput($record,"ISBN-10",":\t",'004A','x0',"\n"); +      $res .= $this->convOutputNice($record_nice,$keys_nice,"address",":\t\t","\n"); 
-        $res .= $this->convOutput($record,"ISBN-13",":\t",'004A','A',"\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");
  
-        //Call Number +      //ISBNs 
-        $0; +      $res .= $this->convOutputNice($record_nice,$keys_nice,"isbn10",":\t","\n")
-        while ( $i <= 99 ) { +      $res .= $this->convOutputNice($record_nice,$keys_nice,"isbn13",":\t","\n");
-          if ($i <= 9 ) { +
-           $j = "0".$i+
-          } else { +
-           $j = $i; +
-          } +
-          $res .= $this->convOutput($record,"Signatur",":\t","209A/$j",'a',"\n"); +
-          $i++; +
-        }+
  
-        $res .= $this->convOutput($record,"PPN",":\t\t",'003@','x0',"\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;
   }   }
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