User login

Reviving FOAF (scavanging Walkah's Drupal 4.6-era foaf module)

The release page as of October only showed 4.6 and a HEAD version from the same date (November 13, 2006) and about the same size. (Maybe HEAD was compatible with 4.7, i didn't try to use it, but that's the one examined below).

Here are the parts Agaric found interesting:

From foaf_user (implementation of hook_user), we see the addition of an HTML header and, in the content, a link to a FOAF download:

<?php
    case 'view':
      drupal_set_html_head("\n".'<link rel="meta" type="application/rdf+xml" title="FOAF" href="'.url('foaf/'.$user->uid, NULL, NULL, TRUE).'" />');
      $output = '';
      if (_foaf_allow_export($user)) {
          return array(t('Personal Information') => l(t('Download <acronym title="Friend of a Friend">FOAF</acronym>'), 'foaf/'.$user->uid, NULL, NULL, NULL, FALSE, TRUE));
      }
      break;
?>

Ah. The module did not add RDFa data to the inline, HTML presentation of user profiles– it only adds the RDF XML download. Cool though. Yes, the header added above is another link to the same data the HTML link added with the l() function goes to, the XML FOAF file generated from the profile information. There is no FOAF data on the profile page itself; it is linked to it.

Here's the export/download RDF XML generating function. NOTE: There is no space between the ? and > bracket on the first output (<?xml ...) line; it is inserted to not prematurely end the PHP formatting.

<?php
function foaf_export($uid = null) {
  if (!$uid || !$account = user_load(array('uid' => $uid))) {
    drupal_not_found();
    return;
  }

  if ( !_foaf_allow_export($account)) {
    drupal_access_denied();
    return;
  }

  $output .= '<?xml version="1.0" encoding="iso-8859-1" ? >'."\n";
  $output .= '<!-- generator="Drupal FOAF.Module" -->'."\n";
  $output .= '<rdf:RDF xmlns="http://xmlns.com/foaf/0.1"'."\n";
  $output .= '  xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"'."\n";
  $output .= '  xmlns:rdfs="http://www.w3.org/2000/01/rdf-schema#"'."\n";
  $output .= '  xmlns:dc="http://purl.org/dc/elements/1.1/"'."\n";
  $output .= '  xmlns:admin="http://webns.net/mvcb/"'. "\n";
  $output .= '  xmlns:bio="http://purl.org/vocab/bio/0.1/"'."\n";
  $output .= '  xmlns:foaf="http://xmlns.com/foaf/0.1/"'."\n";
  $output .= '  xmlns:vCard="http://www.w3.org/2001/vcard-rdf/3.0#">'."\n";

  // foaf:PersonalProfileDocument
  $output .= '<foaf:PersonalProfileDocument rdf:about="">' ."\n";
  $output .= '  <foaf:maker rdf:nodeID="_'.$uid.'" />'."\n";
  $output .= '  <foaf:primaryTopic rdf:nodeID="_'.$uid.'" />'."\n";
  $output .= '  <dc:title>'. t('FOAF for %name at %site', array('%name' => $account->name, '%site' => variable_get('site_name', 'drupal'))) ."</dc:title>\n";
  $output .= '  <dc:description>' . t('Friend of a Friend description for %name', array('%name' => $account->name)) . "</dc:description>\n";
  $output .= '  <admin:generatorAgent rdf:resource="http://www.drupal.org/" />'."\n";
  $output .= "</foaf:PersonalProfileDocument>\n";

  //$name = ($val = _foaf_profile_get($uid, variable_get('foaf_name',''))) ? $val : $account->name;
  $picture = ($account->picture) ? file_create_url($account->picture) : '';

  // foaf:Person
  $output.= '<foaf:Person rdf:nodeID="_'.$uid.'">'."\n";
  if ($firstName = _foaf_profile_get($uid, variable_get('foaf_firstName',''))) {
    $output.= '  <foaf:firstName>'.$firstName."</foaf:firstName>\n";
  }
  if ($surname = _foaf_profile_get($uid, variable_get('foaf_surname',''))) {
    $output.= '  <foaf:surname>'.$surname."</foaf:surname>\n";
  }

  // Guess foaf:name (if not present)
  if (!$name = _foaf_profile_get($uid, variable_get('foaf_name',''))) {
    if ($firstName && $surname) {
      $name = $firstName . ' ' . $surname;
    }
    else {
      $name = $account->name;
    }
  }
  $output.= '  <foaf:name>'.$name."</foaf:name>\n";
  if ($account->mail) {
    $output.= '  <foaf:mbox_sha1sum>'.sha1('mailto:' . $account->mail)."</foaf:mbox_sha1sum>\n";
  }
  if ($picture) {
    $output.= '  <foaf:img rdf:resource="'.$picture.'" />' . "\n";
    $output.= '  <foaf:depiction rdf:resource="'.$picture.'" />' . "\n";
  }
  $output.= '  <foaf:nick>'.$account->name ."</foaf:nick>\n";

  // weblog
  if (module_exist('blog')) {
    $output.= '  <foaf:weblog rdf:resource="'.url('blog/'.$uid, null, null, true) . '" />' . "\n";
  }

  if ($title = _foaf_profile_get($uid, variable_get('foaf_title',''))) {
    $output.= '  <foaf:title>'.$title."</foaf:title>\n";
  }
  if ($Organization = _foaf_profile_get($uid, variable_get('foaf_Organization',''))) {
    $output.= '  <foaf:Organization>'.$Organization."</foaf:Organization>\n";
  }
  if ($phone = _foaf_profile_get($uid, variable_get('foaf_phone',''))) {
    $output.= '  <foaf:phone rdf:resource="'.$phone.'"/>'."\n";
  }
  if ($aimChatID = _foaf_profile_get($uid, variable_get('foaf_aimChatID',''))) {
    $output.= '  <foaf:aimChatID>'.$aimChatID."</foaf:aimChatID>\n";
  }
  if ($icqChatID = _foaf_profile_get($uid, variable_get('foaf_icqChatID',''))) {
    $output.= '  <foaf:icqChatID>'.$icqChatID."</foaf:icqChatID>\n";
  }
  if ($msnChatID = _foaf_profile_get($uid, variable_get('foaf_msnChatID',''))) {
    $output.= '  <foaf:msnChatID>'.$msnChatID."</foaf:msnChatID>\n";
  }
  if ($yahooChatID = _foaf_profile_get($uid, variable_get('foaf_yahooChatID',''))) {
    $output.= '  <foaf:yahooChatID>'.$yahooChatID."</foaf:yahooChatID>\n";
  }
  if ($jabberID = _foaf_profile_get($uid, variable_get('foaf_jabberID',''))) {
    $output.= '  <foaf:jabberID>'.$jabberID."</foaf:jabberID>\n";
  }
  if ($workplaceHomepage = _foaf_profile_get($uid, variable_get('foaf_workplaceHomepage',''))) {
    $output.= '  <foaf:workplaceHomepage rdf:resource="'.htmlentities($workplaceHomepage).'" />'."\n";
  }

  $address = '';
  if ($street = _foaf_profile_get($uid, variable_get('foaf_street',''))) {
    $address.= '    <vCard:Street>'.$street."</vCard:Street>\n";
  }
  if ($locality = _foaf_profile_get($uid, variable_get('foaf_locality',''))) {
    $address.= '    <vCard:Locality>'.$locality."</vCard:Locality>\n";
  }
  if ($region = _foaf_profile_get($uid, variable_get('foaf_region',''))) {
    $address.= '    <vCard:Region>'.$region."</vCard:Region>\n";
  }
  if ($pcode = _foaf_profile_get($uid, variable_get('foaf_pcode',''))) {
    $address.= '    <vCard:Pcode>'.$pcode."</vCard:Pcode>\n";
  }
  if ($country = _foaf_profile_get($uid, variable_get('foaf_country',''))) {
    $address.= '    <vCard:Country>'.$country."</vCard:Country>\n";
  }

  if ($address) {
    $output.= '  <vCard:ADR rdf:parseType="Resource">'."\n";
    $output.= $address;
    $output.= "  </vCard:ADR>\n";
  }

  if ($keywords = _foaf_profile_get($uid, variable_get('foaf_keywords', ''))) {
    $output .= '  <bio:keywords>'.$keywords."</bio:keywords>\n";
  }

  if (function_exists('buddylist_get_buddies')) {
    $buddies = buddylist_get_buddies($uid);  
    foreach ($buddies as $buddy) {
      $output .= "  <foaf:knows>\n";
      $output .= "    <foaf:Person>\n";
      $output .= "      <foaf:name>".$buddy->name."</foaf:name>\n";
      if ($buddy->mail) {
        $output .= '      <foaf:mbox_sha1sum>'.sha1('mailto:' . $buddy->mail)."</foaf:mbox_sha1sum>\n";
      }
      if ($buddy->uid) {
        $output .= '      <rdfs:seeAlso rdf:resource="'. url('foaf/'.$buddy->uid) . '" />' ."\n";
      }
      $output .= "    </foaf:Person>\n";
      $output .= "  </foaf:knows>\n";
    }
  }
 
  $output .= "</foaf:Person>\n";
  $output .= "</rdf:RDF>\n";

  header ('Content-Type: application/rdf+xml');
  header ('Content-Disposition: attachment; filename="'.$account->name.'.rdf"');
  print $output;
}
?>

Resolution

Searched words: 
foaf phone show personal contact information data and semantic web of friends friend-of-a-friend for Drupal

Comments

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <blockquote> <small> <h2> <h3> <h4> <h5> <h6> <sub> <sup> <p> <br> <strike> <table> <tr> <td> <thead> <th> <tbody> <tt> <output>
  • Lines and paragraphs break automatically.

More information about formatting options

By submitting this form, you accept the Mollom privacy policy.