representative-hcard-parsing: Difference between revisions

From Microformats Wiki
Jump to navigation Jump to search
(added some draft hKit code for parsing representative-hcards)
m (Replace <entry-title> with {{DISPLAYTITLE:}})
 
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
<h1>representative hCard parsing</h1>
{{DISPLAYTITLE:representative hCard parsing}}
 
{{latest|representative-h-card-parsing}}


Assuming you are already using code that properly implements [[hcard-parsing]], this page documents how to determine a [[representative hCard]] for a page using the current [[representative-hcard-brainstorming]] proposal.
Assuming you are already using code that properly implements [[hcard-parsing]], this page documents how to determine a [[representative hCard]] for a page using the current [[representative-hcard-brainstorming]] proposal.


== url uid source ==
== representative hCard algorithm ==
After parsing the hCard(s) on a page, if there is one which has a "url" property whose value is the url of the page (source) and is also a "uid" property for the hCard, then that is the representative hCard for the page.
After [[microformats2-parsing|parsing]] the page:
# '''url uid source.''' The first hCard found which has a "url" property whose value is the url of the page (source) and is also a "uid" property for the hCard, is the representative hCard for the page.
# '''url and rel me.''' If the previous step didn't find a representative hCard, then the first hCard with a "url" property that also has the <code>rel="me"</code> relation is the representative hCard for the page.
 
== Issues ==
* In the first step, how should URLs be matched? Resolving relative URLs is a given, but there are other cases. E.g. if the h-card has a URL of 'http://aaronparecki.com/', should 'http://aaronparecki.com' match? What about 'https://aaronparecki.com/'? --[[User:Barnabywalters|bw]] 14:47, 6 October 2014 (UTC)
** Per https://url.spec.whatwg.org/ : [[User:Tantek|Tantek]] 22:18, 6 October 2014 (UTC)
*** Parse the URLs
*** Compare the serialization
*** If all the components match, the two URLs match.
*** (background/discussion: [http://krijnhoetmer.nl/irc-logs/whatwg/20141006#l-843])
* Why is u-uid required in addition to u-url in the first step but not the second step? In what case has an h-card had a u-url property matching the URL of the page it’s on, but *not* been the representative h-card? --[[User:Barnabywalters|bw]] 14:47, 6 October 2014 (UTC)
** Let's try a third step: if there is only one hCard with url == page URL then use that hCard, else there is no representative hCard [[User:Tantek|Tantek]] 22:18, 6 October 2014 (UTC)


== url and rel me ==
== open source implementations ==
If the aforementioned "url uid source" didn't find a representative hCard, then look for an hCard with a "url" property that also has the <code>rel="me"</code> relation. If you find such an hCard then you have found a representative hCard for the page.
The below open source implementations of '''representative hCard parsing''' have been contributed inline and are thus in the public domain per [[Microformats_Wiki:Copyrights]].


== Draft hKit (PHP5) code ==
=== Draft hKit (PHP5) code ===
I - ([[User:TomMorris|Tom]]) - have been working on implementing represenative hCard parsing in PHP so that a user can simply type in a URL pointing to a page with an hCard on it, and have their name and some details automatically filled in the form. The code below implements the following:
I - ([[User:TomMorris|Tom]]) - have been working on implementing represenative hCard parsing in PHP so that a user can simply type in a URL pointing to a page with an hCard on it, and have their name and some details automatically filled in the form. The code below implements the following:
# If there is only one hCard on a page, it uses that.
# If there is only one hCard on a page, it uses that.
Line 16: Line 30:
It's experimental and quite 'alpha', so I'd suggest you test it and make adjustments as necessary.
It's experimental and quite 'alpha', so I'd suggest you test it and make adjustments as necessary.


<code><pre>&lt;?php
<source lang="php">
 
<?php
// example hKit code to extract a representative hCard
// example hKit code to extract a representative hCard
// tom morris <http://tommorris.org>
// tom morris <http://tommorris.org>
// public domain 2007
// public domain 2007


include("hkit/hkit.class.php5");
// An hCard could have more than one url, so we use a multi-dimensional array search. - Sarven Capadisli
 
// From http://nl2.php.net/manual/en/function.array-search.php#80692
function multidimArrayLocate($array, $text){
  foreach($array as $key => $arrayValue){
    if (is_array($arrayValue)){
      if ($key == $text) $arrayResult[$key] = $arrayValue;
      $temp[$key] = multidimArrayLocate($arrayValue, $text);
      if ($temp[$key]) $arrayResult[$key] = $temp[$key];
    }
    else{
      if ($key == $text) $arrayResult[$key] = $arrayValue;
    }
  }
  return $arrayResult;
}
 
include("hkit.class.php");
$hkit = new hKit;
$hkit = new hKit;
$result = $hkit->getByURL('hcard', $HTTP_GET_VARS['url']);
$result = $hkit->getByURL('hcard', $HTTP_GET_VARS['url']);
if (count($result) != 0) {
if (count($result) != 0) {
&nbsp;&nbsp;if (count($result) == 1) {
  if (count($result) == 1) {
&nbsp;&nbsp;&nbsp;&nbsp;$repcard = $result[0];
    $repcard = $result[0];
&nbsp;&nbsp;} else {
  } else {
&nbsp;&nbsp;&nbsp;&nbsp;foreach ($result as $card) {
    foreach ($result as $card) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;if (array_search($HTTP_GET_VARS['url'], $card) == true || $card['uid'] == $HTTP_GET_VARS['url']) {
      if (multidimArrayLocate($card, $HTTP_GET_VARS['url']) == true || $card['uid'] == $HTTP_GET_VARS['url']) {
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;$repcard = $card;
        $repcard = $card;
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}
      }
&nbsp;&nbsp;&nbsp;&nbsp;}
    }
&nbsp;&nbsp;}
  }
}
}
print_r($repcard);
print_r($repcard);
?&gt;</pre></code>
?></source>
 
=== jQuery (JavaScript) code ===
The following JavaScript code follows the [[representative-hcard-brainstorming#current_proposal|current proposal for finding a representative hCard]]. It requires the [http://jquery.com/ jQuery] library, however, it can be easily switched over to another library.
 
* If a vCard has a <code>class="url"</code> and <code>class="uid"</code> with an href value same as the source URI (current document), then that vCard is a representative hCard candidate, otherwise;
* If a vCard has a <code>class="url"</code> and a <code>rel="me"</code> on the same element, then that vCard is a representative hCard candidate, otherwise;
* There is no representative hCard.
* Grab the first hCard from the list of candidates.
 
<source lang="javascript">
/***
    Note: Extracts representative hCard
    Author: Sarven Capadisli http://csarven.ca/
    License: Public Domain 2009-06-04
*/
var sourceURI = window.location.href;
var rep_hCard = new Array();
 
function rep_hCard_uidurlsource() {
    $('.vcard .uid[href='+sourceURI+']').each(function() {
        $(this).each(function() {
            if ($(this).closest('.vcard').find('.url[href='+sourceURI+']').length > 0) {
                rep_hCard.push($(this).closest('.vcard')[0]);
            }
        });
    });
    return (rep_hCard.length > 0) ? true : false;
}
 
function rep_hCard_urlme() {
    $('.vcard .url[rel=me]').each(function() {
        rep_hCard.push($(this).closest('.vcard')[0]);
    });
    return (rep_hCard.length > 0) ? true : false;
}
 
if (rep_hCard_uidurlsource() || rep_hCard_urlme()) {
    rep_hCard = $($(rep_hCard)[0]);
    rep_hCard_url = (rep_hCard.find('.uid').length > 0) ? rep_hCard.find('.uid')[0].href : rep_hCard.find('.url[rel=me]')[0].href;
    hCard_fn = $('.fn', rep_hCard).text();
    hCard_photo_src = ($('.photo', rep_hCard).length > 0) ? $('.photo', rep_hCard)[0].src : '';
}
else {
    //no representative hCard
}
</source>
--[[User:Csarven|Sarven Capadisli]] 04:24, 4 June 2009 (UTC)


== see also ==
== see also ==

Latest revision as of 16:32, 18 July 2020


See latest version: representative-h-card-parsing

Assuming you are already using code that properly implements hcard-parsing, this page documents how to determine a representative hCard for a page using the current representative-hcard-brainstorming proposal.

representative hCard algorithm

After parsing the page:

  1. url uid source. The first hCard found which has a "url" property whose value is the url of the page (source) and is also a "uid" property for the hCard, is the representative hCard for the page.
  2. url and rel me. If the previous step didn't find a representative hCard, then the first hCard with a "url" property that also has the rel="me" relation is the representative hCard for the page.

Issues

  • In the first step, how should URLs be matched? Resolving relative URLs is a given, but there are other cases. E.g. if the h-card has a URL of 'http://aaronparecki.com/', should 'http://aaronparecki.com' match? What about 'https://aaronparecki.com/'? --bw 14:47, 6 October 2014 (UTC)
    • Per https://url.spec.whatwg.org/ : Tantek 22:18, 6 October 2014 (UTC)
      • Parse the URLs
      • Compare the serialization
      • If all the components match, the two URLs match.
      • (background/discussion: [1])
  • Why is u-uid required in addition to u-url in the first step but not the second step? In what case has an h-card had a u-url property matching the URL of the page it’s on, but *not* been the representative h-card? --bw 14:47, 6 October 2014 (UTC)
    • Let's try a third step: if there is only one hCard with url == page URL then use that hCard, else there is no representative hCard Tantek 22:18, 6 October 2014 (UTC)

open source implementations

The below open source implementations of representative hCard parsing have been contributed inline and are thus in the public domain per Microformats_Wiki:Copyrights.

Draft hKit (PHP5) code

I - (Tom) - have been working on implementing represenative hCard parsing in PHP so that a user can simply type in a URL pointing to a page with an hCard on it, and have their name and some details automatically filled in the form. The code below implements the following:

  1. If there is only one hCard on a page, it uses that.
  2. If there is more than one hCard on a page, it looks through to see if any of the cards have UID or URL fields that match the URL it searches, then selects that one if one exists.

It's experimental and quite 'alpha', so I'd suggest you test it and make adjustments as necessary.

<?php
// example hKit code to extract a representative hCard
// tom morris <http://tommorris.org>
// public domain 2007

// An hCard could have more than one url, so we use a multi-dimensional array search. - Sarven Capadisli

// From http://nl2.php.net/manual/en/function.array-search.php#80692
function multidimArrayLocate($array, $text){
  foreach($array as $key => $arrayValue){
    if (is_array($arrayValue)){
      if ($key == $text) $arrayResult[$key] = $arrayValue;
      $temp[$key] = multidimArrayLocate($arrayValue, $text);
      if ($temp[$key]) $arrayResult[$key] = $temp[$key];
    }
    else{
      if ($key == $text) $arrayResult[$key] = $arrayValue;
    }
  }
  return $arrayResult;
}

include("hkit.class.php");
$hkit = new hKit;
$result = $hkit->getByURL('hcard', $HTTP_GET_VARS['url']);
if (count($result) != 0) {
  if (count($result) == 1) {
    $repcard = $result[0];
  } else {
    foreach ($result as $card) {
      if (multidimArrayLocate($card, $HTTP_GET_VARS['url']) == true || $card['uid'] == $HTTP_GET_VARS['url']) {
        $repcard = $card;
      }
    }
  }
}
print_r($repcard);
?>

jQuery (JavaScript) code

The following JavaScript code follows the current proposal for finding a representative hCard. It requires the jQuery library, however, it can be easily switched over to another library.

  • If a vCard has a class="url" and class="uid" with an href value same as the source URI (current document), then that vCard is a representative hCard candidate, otherwise;
  • If a vCard has a class="url" and a rel="me" on the same element, then that vCard is a representative hCard candidate, otherwise;
  • There is no representative hCard.
  • Grab the first hCard from the list of candidates.
/***
    Note: Extracts representative hCard
    Author: Sarven Capadisli http://csarven.ca/
    License: Public Domain 2009-06-04
*/
var sourceURI = window.location.href;
var rep_hCard = new Array();

function rep_hCard_uidurlsource() {
    $('.vcard .uid[href='+sourceURI+']').each(function() {
        $(this).each(function() {
            if ($(this).closest('.vcard').find('.url[href='+sourceURI+']').length > 0) {
                rep_hCard.push($(this).closest('.vcard')[0]);
            }
        });
    });
    return (rep_hCard.length > 0) ? true : false;
}

function rep_hCard_urlme() {
    $('.vcard .url[rel=me]').each(function() {
        rep_hCard.push($(this).closest('.vcard')[0]);
    });
    return (rep_hCard.length > 0) ? true : false;
}

if (rep_hCard_uidurlsource() || rep_hCard_urlme()) {
    rep_hCard = $($(rep_hCard)[0]); 
    rep_hCard_url = (rep_hCard.find('.uid').length > 0) ? rep_hCard.find('.uid')[0].href : rep_hCard.find('.url[rel=me]')[0].href;
    hCard_fn = $('.fn', rep_hCard).text();
    hCard_photo_src = ($('.photo', rep_hCard).length > 0) ? $('.photo', rep_hCard)[0].src : '';
}
else {
    //no representative hCard
}

--Sarven Capadisli 04:24, 4 June 2009 (UTC)

see also