<?php
/*
Plugin Name: FOAF Output Add-on - Semantic Visits
Plugin URI: http://www.wasab.dk/morten/blog/archives/2004/07/05/wordpress-plugin-semantic-visits
Description: Extend FOAF profile by adding statements about visited countries and US states based on special "links" in link categories named "Countries" and "US States".
Version: 1.2
Author: Morten Frederiksen
Author URI: http://purl.org/net/morten/

Changes since 1.1:
* Added generator information.

Changes since 1.0:
* Added visits to Canadian regions.
* Fixed "wrong" code/name/etc. properties.
* The URIs for US states are now correct, pointing at the
  http://www.daml.ri.cmu.edu/ont/USRegionState.daml ontology containing state
  definitions instead of http://www.daml.ri.cmu.edu/ont/State.daml which
  only contains classes and properties.

*/
  
function semantic_visits_rdf_namespaces($text) {
    
$text.='
    xmlns:caregion="http://www.daml.ri.cmu.edu/ont/CanadianState.daml#"
    xmlns:usstate="http://www.daml.ri.cmu.edu/ont/USRegionState.daml#"
    xmlns:state="http://www.daml.ri.cmu.edu/ont/State.daml#"
    xmlns:iso="http://www.daml.org/2001/09/countries/iso-3166-ont#"
    xmlns:visit="http://purl.org/net/vocab/2004/07/visit#"'
;
    return 
$text;
}
  
function 
semantic_visits_rdf_document($text) {
    
$text.='
    <admin:generatorAgent rdf:resource="http://www.wasab.dk/morten/blog/archives/2004/07/05/wordpress-plugin-semantic-visits?version=1.2"/>'
;
    return 
$text;
}

function 
semantic_visits_rdf_person($text) {
    
# Countries.
    
$countries get_semantic_visits_countries();
    foreach (
$countries as $country) {
        
$text .= "\n\t" '<visit:country>
        <iso:Country rdf:about="' 
$country['country'] . '">
            <iso:code>' 
$country['code'] . '</iso:code>';
        if (
'' != $country['name'])
            
$text .= '
            <iso:name>' 
$country['name'] . '</iso:name>';
        if (
'' != $country['uri'])
            
$text .= '
            <page rdf:resource="' 
htmlspecialchars($country['uri']) . '"/>';
        
$text .= '
        </iso:Country>
    </visit:country>'
;
    }

    
# US states.
    
$usstates get_semantic_visits_usstates();
    foreach (
$usstates as $usstate) {
        
$text .= "\n\t" '<visit:usstate>
        <usstate:USState rdf:about="' 
$usstate['usstate'] . '">
            <state:code>' 
$usstate['code'] . '</state:code>';
        if (
'' != $usstate['name'])
            
$text .= '
            <state:name>' 
$usstate['name'] . '</state:name>';
        if (
'' != $usstate['uri'])
            
$text .= '
            <page rdf:resource="' 
htmlspecialchars($usstate['uri']) . '"/>';
        
$text .= '
        </usstate:USState>
    </visit:usstate>'
;
    }
    
    
# CA regions.
    
$caregions get_semantic_visits_caregions();
    foreach (
$caregions as $caregion) {
        
$text .= "\n\t" '<visit:caregion>
        <visit:CanadianRegion rdf:about="' 
$caregion['caregion'] . '">
            <state:code>' 
$caregion['code'] . '</state:code>';
        if (
'' != $caregion['name'])
            
$text .= '
            <state:name>' 
$caregion['name'] . '</state:name>';
        if (
'' != $caregion['uri'])
            
$text .= '
            <page rdf:resource="' 
htmlspecialchars($caregion['uri']) . '"/>';
        
$text .= '
        </visit:CanadianRegion>
    </visit:caregion>'
;
    }
    
    return 
$text;
}

function 
semantic_visits_html_person($text) {
    
# Countries.
    
$countries get_semantic_visits_countries();
    if (
sizeof($countries)) {
        
$text .= "\t\t\t" '<dt class="semantic-visits-countries">Visited countries:</dt>'
                
'<dd>' sizeof($countries) . ' ('
                
join(', 'array_map('semantic_visits_linked_region_name'$countries))
                . 
')<br/>' get_semantic_visits_countries_image($countries) . '</dd>';
    }

    
# US states.
    
$usstates get_semantic_visits_usstates();
    if (
sizeof($usstates)) {
        
$text .= "\n\t\t\t" '<dt class="semantic-visits-usstates">Visited US states:</dt>'
                
'<dd>' sizeof($usstates) . ' ('
                
join(', 'array_map('semantic_visits_linked_region_name'$usstates))
                . 
')<br/>' get_semantic_visits_usstates_image($usstates) . '</dd>';
    }
    
    
# CA regions.
    
$caregions get_semantic_visits_caregions();
    if (
sizeof($caregions)) {
        
$text .= "\n\t\t\t" '<dt class="semantic-visits-caregions">Visited Canadian regions:</dt>'
                
'<dd>' sizeof($caregions) . ' ('
                
join(', 'array_map('semantic_visits_linked_region_name'$caregions))
                . 
')<br/>' get_semantic_visits_caregions_image($caregions) . '</dd>';
    }
    
    return 
$text;
}

function 
semantic_visits_region_name($region) {
    if (
'' != $region['name'])
        return 
$region['name'];
    return 
$region['code'];
}

function 
semantic_visits_linked_region_name($region) {
    if (
'' != $region['uri'])
        return 
'<a href="' htmlspecialchars($region['uri']) . '">'
                
semantic_visits_region_name($region) . '</a>';
    return 
semantic_visits_region_name($region);
}

function 
get_semantic_visits_countries_image($countries = array()) {
    if (!
sizeof($countries))
        
$countries get_semantic_visits_countries();
    if (!
sizeof($countries))
        return 
'';
    return 
'<img src="http://www.world66.com/myworld66/visitedCountries/worldmap?visited='
            
join(''array_keys($countries)) . '" alt="World map: '
            
join(', 'array_map('semantic_visits_region_name'$countries))
            . 
'"/><br/><a href="http://www.world66.com/myworld66">create your own visited '
            
'country map</a> or <a href="http://www.world66.com">write about '
            
'it on the open travel guide</a>';
}

function 
get_semantic_visits_usstates_image($usstates = array()) {
    if (!
sizeof($usstates))
        
$usstates get_semantic_visits_usstates();
    if (!
sizeof($usstates))
        return 
'';
    return 
'<img src="http://www.world66.com/myworld66/visitedStates/statemap?visited='
            
join(''array_keys($usstates)) . '" alt="US map: '
            
join(', 'array_map('semantic_visits_region_name'$usstates))
            . 
'"/><br/><a href="http://www.world66.com/myworld66">create your own personalised '
            
'map of the USA</a> or <a href="http://www.world66.com">write about '
            
'it on the open travel guide</a>';
}

function 
get_semantic_visits_caregions_image($caregions = array()) {
    if (!
sizeof($caregions))
        
$caregions get_semantic_visits_caregions();
    if (!
sizeof($caregions))
        return 
'';
    return 
'<img src="http://www.world66.com/myworld66/visitedCanadianStates/countrymap?visited='
            
join(''array_keys($caregions)) . '" alt="CA map: '
            
join(', 'array_map('semantic_visits_region_name'$caregions))
            . 
'"/><br/><a href="http://www.world66.com/myworld66">create your own personalised '
            
'map of Canada</a> or <a href="http://www.world66.com">write about '
            
'it on the open travel guide</a>';
}

function 
get_semantic_visits_countries() {
    global 
$authordata;
    
# Return array of visited countries.
    
$countries = array();
    
$links get_linkobjectsbyname('Countries');
    if (
$links) {
        
$cat_id 0;
        foreach (
$links as $link) {
            if(
preg_match('|^[A-Z]{2}$|'$link->link_name)
                    && (
$link->link_category == $cat_id
                    
|| !$cat_id && 'Countries' == get_linkcatname($link->link_category))
                    && 
$authordata->ID == $link->link_owner)
                
$countries[$link->link_name] = array(
                        
'code' => $link->link_name,
                        
'name' => $link->link_description,
                        
'uri' => $link->link_url,
                        
'country' => 'http://www.daml.org/2001/09/countries/iso#' $link->link_name);
        }
    }
    return 
$countries;
}

function 
get_semantic_visits_usstates() {
    global 
$authordata;
    
# Return array of visited US states.
    
$usstates = array();
    
$links get_linkobjectsbyname('US States');
    if (
$links) {
        
$cat_id 0;
        foreach (
$links as $link) {
            if(
preg_match('|^[A-Z]{2}$|'$link->link_name)
                    && (
$link->link_category == $cat_id
                    
|| !$cat_id && 'US States' == get_linkcatname($link->link_category))
                    && 
$authordata->ID == $link->link_owner) {
                
$cat_id $link->link_category;
                
$usstates[$link->link_name] = array(
                        
'code' => $link->link_name,
                        
'name' => $link->link_description,
                        
'uri' => $link->link_url,
                        
'usstate' => 'http://www.daml.ri.cmu.edu/ont/USRegionState.daml#' $link->link_name);
            }
        }
    }
    return 
$usstates;
}

function 
get_semantic_visits_caregions() {
    global 
$authordata;
    
# Return array of visited Canadian regions.
    
$caregions = array();
    
$links get_linkobjectsbyname('CA Regions');
    if (
$links) {
        
$cat_id 0;
        foreach (
$links as $link) {
            if(
preg_match('|^[A-Z]{2}$|'$link->link_name)
                    && (
$link->link_category == $cat_id
                    
|| !$cat_id && 'CA Regions' == get_linkcatname($link->link_category))
                    && 
$authordata->ID == $link->link_owner) {
                
$cat_id $link->link_category;
                
$caregions[$link->link_name] = array(
                        
'code' => $link->link_name,
                        
'name' => $link->link_description,
                        
'uri' => $link->link_url,
                        
'caregion' => 'http://www.daml.ri.cmu.edu/ont/CanadianState.daml#' $link->link_name);
            }
        }
    }
    return 
$caregions;
}

# Add filters for profile output.
add_filter('foaf_output_profile_rdf_namespaces''semantic_visits_rdf_namespaces');
add_filter('foaf_output_profile_rdf_document''semantic_visits_rdf_document');
add_filter('foaf_output_profile_rdf_person''semantic_visits_rdf_person');
add_filter('foaf_output_profile_html_person''semantic_visits_html_person');

?>