<?php echo('<?xml version="1.0" encoding="UTF-8"?>'); ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
 "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>illustrert</title>
</head>
<body>

<h1>illustrert</h1>

<form method="get">
<input type="text" name="q" value="<?php echo($_GET['q']); ?>" />
<input type="submit" value="S&oslash;k" />
</form>

<?php 

// Config

$zlimit 250;
$cd_count 0

require 
'File/MARC.php';

// phplastfmapi: http://www.matto1990.com/lastfm_protocol/
// Usage: http://apps.sourceforge.net/mediawiki/phplastfmapi/index.php?title=Usage
require 'lastfmapi/lastfmapi.php';

// Get key from http://last.fm/
$authVars['apiKey'] = 'xxx';

// End config

$auth = new lastfmApiAuth('setsession'$authVars);
$apiClass = new lastfmApi();

if (!empty(
$_GET['q'])) {

  
$host 'z3950.deich.folkebibl.no:210/data';
  
// $host = 'torfeus.deich.folkebibl.no:9999/biblios';
  
$id yaz_connect($host);
  
yaz_element($id"F");
  
yaz_syntax($id"NORMARC");
  
yaz_range($id1$zlimit);
    
// $rpn = '@or @attr 1=1 "' . $_GET['q'] . '" @attr 1=2 "' . $_GET['q'] . '"';
    
$rpn '@attr 1=1016 "' $_GET['q'] . '"';
    echo(
"<p>$rpn</p>");
  
yaz_search($id"rpn"$rpn);
  
yaz_wait();
  echo 
"<hr /> $host: \n";
  
$error yaz_error($id);
  
  if (!empty(
$error)) {
  
    echo 
"Error: $error \nError number: " yaz_errno($id) . "\n";
  
  } else {
  
    
$hits yaz_hits($id);
    echo 
"Result Count $hits";
      
      echo 
'<ul>';
    for (
$p 1$p <= $zlimit$p++) {
    
$rec yaz_record($id$p'raw');
    if (empty(
$rec)) continue;
          
            
// DEBUG
            // echo($rec);
            
            // Use File_MARC to pick out interesting stuff
            
$marc_file = new File_MARC($recFile_MARC::SOURCE_STRING);
      while (
$marc_record $marc_file->next()) {
              
// Is this a CD? 
                
$is_cd false;
                if (
clean_field($marc_record->getField('245')->getSubfield('h')) == 'lydopptak') {
                  
$is_cd true;
                }
              
// Title
                
$title clean_field($marc_record->getField('245')->getSubfield('a'));
                
// if ($marc_record->getField('245')->getSubfield('b')) {
                //   $title = $title . " : " . clean_field($marc_record->getField('245')->getSubfield('b'));
                // }
                
$title htmlentities($title);
                
// Artist
                
$artist "";
                
// DEBUG print_r($marc_record);
                
if ($marc_record->getField('100')) {
                  
$artist clean_field($marc_record->getField('100')->getSubfield('a'));
                    
$artist uninvert($artist);
                } elseif (
$marc_record->getField('110')) {
                  
$artist clean_field($marc_record->getField('110')->getSubfield('a'));
                } elseif (
$marc_record->getField('700')) {
                  
$artist clean_field($marc_record->getField('700')->getSubfield('a'));
                    
$artist uninvert($artist);
                } elseif (
$marc_record->getField('710')) {
                  
$artist clean_field($marc_record->getField('710')->getSubfield('a'));
                }
                
// URL
                
$url clean_field($marc_record->getField('996')->getSubfield('u'));
                
                if (
$is_cd) {
                
                  
$cd_count++;
                    
                    if (
$cd_count 10) {
                      exit;
                    }
                
                  
// Output
                  
echo("<li><a href=\"$url\">$title</a></li>");
                  
// Search Last.fm
                  
$lastfm_album $apiClass->getPackage($auth'album');
                  
$methodVars = array(
                    
'artist' => $artist
              
'album' =>  $title
          
);
          if (
$info $lastfm_album->getInfo($methodVars) ) {
              
// DEBUG print_r($info);
                        
if($info['image']['large']) {
                        echo(
"<img src=\"" $info['image']['large'] . "\" alt=\"Cover-bilde\" />");
                        } else {
                          echo(
"<p>Ikke bilde...</p>");
                        }
                        
// print_r($methodVars);
          
} else {
              
// Error: show which error and go no further.
                        // print_r($methodVars);
              
echo '<b>Error '.$lastfm_album->error['code'].' - </b><i>'.$lastfm_album->error['desc'].'</i>';
          }
                } 
            }
    }
    echo 
'</ul>';
      
  }

}

function 
clean_field($s) {

  
// For some reason this: 
    // $marc_record->getField('245')->getSubfield('a')
    // returns this: 
    // "[a]: Wuthering heights"

  
return substr($s5);

}
    
function 
uninvert($s) {

  if (
substr_count($s',')) {
      list(
$first$last) = explode(','$s);
        
$s "$last $first"
    }
      
    return 
$s;
    
}

?>

</body>
</html>