コード例 #1
0
function generate_eac($resource, $end, $lang, $options)
{
    global $identityArray;
    global $processed;
    $processed[] = $resource;
    $id = substr(strstr($resource, 'resource/'), strpos(strstr($resource, 'resource/'), '/') + 1);
    //load dbpedia RDF
    $dbRDF = new DOMDocument();
    $dbRDF->load('http://dbpedia.org/data/' . $id . '.rdf');
    $dxpath = new DOMXpath($dbRDF);
    $dxpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    $dxpath->registerNamespace('dbpedia-owl', "http://dbpedia.org/ontology/");
    $dxpath->registerNamespace('rdfs', "http://www.w3.org/2000/01/rdf-schema#");
    $dxpath->registerNamespace('ns7', "http://live.dbpedia.org/ontology/");
    $xml = '<?xml version="1.0" encoding="utf-8"?><eac-cpf xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink">';
    /************ CONTROL ************/
    $xml .= '<control>';
    $xml .= '<recordId>' . strtolower($id) . '</recordId>';
    //get viaf RDF, if it exists
    $viafId = '';
    $viafIds = $dxpath->query('//dbpprop:viaf');
    foreach ($viafIds as $node) {
        $viafId = $node->nodeValue;
    }
    if (strlen($viafId) > 0) {
        $viafRDF = new DOMDocument();
        $viafRDF->load('http://viaf.org/viaf/' . $viafId . '/rdf.xml');
        $vxpath = new DOMXPath($viafRDF);
        $vxpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        $vxpath->registerNamespace('owl', "http://www.w3.org/2002/07/owl#");
        $vxpath->registerNamespace('schema', "http://schema.org/");
    }
    $xml .= '<maintenanceAgency><agencyName>Agency Name</agencyName></maintenanceAgency>';
    $xml .= '<maintenanceHistory><maintenanceEvent><eventType>created</eventType><eventDateTime standardDateTime="' . date(DATE_W3C) . '"/><agentType>machine</agentType><agent>xEAC dbpedia PHP</agent></maintenanceEvent></maintenanceHistory>';
    $xml .= '<conventionDeclaration><abbreviation>WIKIPEDIA</abbreviation><citation>Wikipedia/DBpedia</citation></conventionDeclaration>';
    $xml .= '<sources><source xlink:type="simple" xlink:href="' . $resource . '"/>';
    if (strlen($viafId) > 0) {
        $xml .= '<source xlink:type="simple" xlink:href="http://viaf.org/viaf/' . $viafId . '/"/>';
    }
    $xml .= '</sources>';
    $xml .= '</control><cpfDescription>';
    /************ IDENTITY ************/
    $xml .= '<identity>';
    //gather entityType
    $types = $dxpath->query('//rdf:type[rdf:resource="http://xmlns.com/foaf/0.1/Person"]');
    if (count($types) > 0) {
        $xml .= '<entityType>person</entityType>';
    } else {
        $xml .= '<entityType>family</entityType>';
    }
    //other entityIDs
    $xml .= '<entityId>' . $resource . '</entityId>';
    //get other records from VIAF
    if (strlen($viafId) > 0) {
        foreach ($vxpath->query("//rdf:Description[rdf:type/@rdf:resource='http://xmlns.com/foaf/0.1/Person']/schema:sameAs") as $ele) {
            if (!strstr($ele->getAttribute('rdf:resource'), 'dbpedia')) {
                $xml .= '<entityId>' . $ele->getAttribute('rdf:resource') . '</entityId>';
            }
        }
    }
    foreach ($dxpath->query('//rdfs:label') as $ele) {
        $xml .= '<nameEntry xml:lang="' . $ele->getAttribute('xml:lang') . '"><part>' . $ele->nodeValue . '</part>';
        //set English as preferred label, otherwise alternative
        if ($ele->getAttribute('xml:lang') == $lang) {
            $xml .= '<preferredForm>WIKIPEDIA</preferredForm>';
        } else {
            $xml .= '<alternativeForm>WIKIPEDIA</alternativeForm>';
        }
        $xml .= '</nameEntry>';
    }
    $xml .= '</identity>';
    /************ DESCRIPTION ************/
    $xml .= '<description>';
    //get dbpedia abstract -> eac:biogHist
    $abstracts = $dxpath->query("//dbpedia-owl:abstract");
    $abstract = getLabel($abstracts, $lang);
    $xml .= '<biogHist><abstract xml:lang="' . $abstract['lang'] . '" localType="wikipedia">' . $abstract['label'] . '</abstract></biogHist>';
    //existDates, get from VIAF by default, if available
    if (strlen($viafId) > 0) {
        $xml .= getExistDates($vxpath->query('//schema:birthDate')->item(0)->nodeValue, $vxpath->query('//schema:deathDate')->item(0)->nodeValue);
    } else {
        //else get from dbpedia (inconsistent)
        $startDates = $dxpath->query('//*[local-name()="birthDate"][@rdf:datatype="http://www.w3.org/2001/XMLSchema#date"]');
        $endDates = $dxpath->query('//*[local-name()="deathDate"][@rdf:datatype="http://www.w3.org/2001/XMLSchema#date"]');
        if (count($startDates) > 0 && count($endDates) > 0) {
            if (strlen($startDates->item(0)->nodeValue) > 0 && strlen($endDates->item(0)->nodeValue) > 0) {
                $gStart = $startDates->item(0)->nodeValue;
                $gEnd = $endDates->item(0)->nodeValue;
                $xml .= '<existDates><dateRange>';
                $xml .= '<fromDate standardDate="' . $gStart . '">' . getDateTextual($gStart) . '</fromDate>';
                $xml .= '<toDate standardDate="' . $gEnd . '">' . getDateTextual($gEnd) . '</toDate>';
                $xml .= '</dateRange></existDates>';
            }
        }
    }
    //get birth and death places
    if ($options['birth/death places'] == true) {
        $bdPlaces = $dxpath->query('descendant::dbpedia-owl:birthPlace|descendant::dbpedia-owl:deathPlace');
        foreach ($bdPlaces as $place) {
            $localname = $place->localName;
            $url = $place->getAttribute('rdf:resource');
            //get label
            $tempId = substr(strstr($url, 'resource/'), strpos(strstr($url, 'resource/'), '/') + 1);
            if (!array_key_exists($url, $identityArray)) {
                $labels = loadTempRDF($tempId);
                $label = getLabel($labels, $lang);
                $name = $label['label'];
            } else {
                $name = $identityArray[$url];
            }
            $xml .= '<place><placeEntry localVocabulary="' . $url . '">' . $name . '</placeEntry>';
            //set placeRole
            if ($localname == 'birthPlace') {
                $xml .= '<placeRole>Place of Birth</placeRole>';
            } else {
                $xml .= '<placeRole>Place of Death</placeRole>';
            }
            //add date for birth or death, if available
            if (strlen($viafId) > 0) {
                $query = $localname == 'birthPlace' ? '//schema:birthDate' : '//schema:deathDate';
                $gDate = normalizeDate($vxpath->query($query)->item(0)->nodeValue);
                $xml .= '<date standardDate="' . $gDate . '">' . getDateTextual($gDate) . '</date>';
            } else {
                if (count($startDates) > 0 && count($endDates) > 0) {
                    if (strlen($startDates->item(0)->nodeValue) > 0 && strlen($endDates->item(0)->nodeValue) > 0) {
                        $gDate = $localname == 'birthPlace' ? $startDates->item(0)->nodeValue : $endDates->item(0)->nodeValue;
                        $xml .= '<date standardDate="' . $gDate . '">' . getDateTextual($gDate) . '</date>';
                    }
                }
            }
            $xml .= '</place>';
        }
    }
    //get occupations
    if ($options['occupations'] == true) {
        $occupations = $dxpath->query('descendant::rdf:Description[@rdf:about="' . $resource . '"]/dbpedia-owl:occupation');
        foreach ($occupations as $occupation) {
            $url = $occupation->getAttribute('rdf:resource');
            $tempId = substr(strstr($url, 'resource/'), strpos(strstr($url, 'resource/'), '/') + 1);
            if (!array_key_exists($url, $identityArray)) {
                $labels = loadTempRDF($tempId);
                $label = getLabel($labels, $lang);
                $name = $label['label'];
            } else {
                $name = $identityArray[$url];
            }
            $xml .= '<occupation>';
            $xml .= '<term vocabularySource="' . $url . '">' . $name . '</term>';
            $xml .= '</occupation>';
        }
    }
    //get subjects
    if ($options['subjects'] == true) {
        $subjects = $dxpath->query('descendant::rdf:Description[@rdf:about="' . $resource . '"]/dcterms:subject');
        foreach ($subjects as $subject) {
            $url = $subject->getAttribute('rdf:resource');
            $tempId = substr(strstr($url, 'resource/'), strpos(strstr($url, 'resource/'), '/') + 1);
            if (!array_key_exists($url, $identityArray)) {
                $labels = loadTempRDF($tempId);
                $label = getLabel($labels, $lang);
                $name = $label['label'];
            } else {
                $name = $identityArray[$url];
            }
            $xml .= '<localDescription localType="subject">';
            $xml .= '<term vocabularySource="' . $url . '">' . $name . '</term>';
            $xml .= '</localDescription>';
        }
    }
    $xml .= '</description>';
    /************ RELATIONS ************/
    $xml .= getRelations($dxpath, $resource, $end, $lang, $options);
    //close EAC-CPF
    $xml .= '</cpfDescription></eac-cpf>';
    return $xml;
}
コード例 #2
0
function generate_eac($array)
{
    global $identityArray;
    global $processed;
    $processed[] = $array['dbpedia'];
    $id = substr(strstr($array['dbpedia'], 'resource/'), strpos(strstr($array['dbpedia'], 'resource/'), '/') + 1);
    //load VIAF RDF
    if (strlen($array['viaf']) > 0) {
        $viafRDF = new DOMDocument();
        $viafRDF->load($array['viaf'] . '/rdf.xml');
        $vxpath = new DOMXPath($viafRDF);
        $vxpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
        $vxpath->registerNamespace('owl', "http://www.w3.org/2002/07/owl#");
        $vxpath->registerNamespace('rdaGr2', "http://rdvocab.info/ElementsGr2/");
    }
    //load dbpedia RDF
    $dbRDF = new DOMDocument();
    $dbRDF->load('http://dbpedia.org/data/' . $id . '.rdf');
    $dxpath = new DOMXpath($dbRDF);
    $dxpath->registerNamespace('rdf', "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
    $dxpath->registerNamespace('dbpedia-owl', "http://dbpedia.org/ontology/");
    $dxpath->registerNamespace('rdfs', "http://www.w3.org/2000/01/rdf-schema#");
    $dxpath->registerNamespace('ns7', "http://live.dbpedia.org/ontology/");
    $xml = '<?xml version="1.0" encoding="utf-8"?><eac-cpf xmlns="urn:isbn:1-931666-33-4" xmlns:xlink="http://www.w3.org/1999/xlink">';
    /************ CONTROL ************/
    $xml .= '<control>';
    $xml .= '<recordId>' . strtolower($id) . '</recordId>';
    //add other records
    $xml .= '<otherRecordId>' . $array['dbpedia'] . '</otherRecordId>';
    if (strlen($array['viaf']) > 0) {
        $xml .= '<otherRecordId>' . $array['viaf'] . '</otherRecordId>';
        foreach ($vxpath->query("//rdf:Description[rdf:type/@rdf:resource='http://xmlns.com/foaf/0.1/Person']/owl:sameAs") as $ele) {
            $xml .= '<otherRecordId>' . $ele->getAttribute('rdf:resource') . '</otherRecordId>';
        }
    }
    if (strlen($array['nomisma']) > 0) {
        $xml .= '<otherRecordId>' . $array['nomisma'] . '</otherRecordId>';
    }
    $xml .= '<maintenanceAgency><agencyName>American Numismatic Society</agencyName></maintenanceAgency>';
    $xml .= '<maintenanceHistory><maintenanceEvent><eventType>created</eventType><eventDateTime standardDateTime="' . date(DATE_W3C) . '"/><agentType>machine</agentType><agent>Ancient Persons PHP</agent></maintenanceEvent></maintenanceHistory>';
    $xml .= '<conventionDeclaration><abbreviation>WIKIPEDIA</abbreviation><citation>Wikipedia/DBpedia</citation></conventionDeclaration>';
    $xml .= '<sources><source xlink:type="simple" xlink:href="' . $array['dbpedia'] . '"/>';
    if (strlen($array['viaf']) > 0) {
        $xml .= '<source xlink:type="simple" xlink:href="' . $array['viaf'] . '"/>';
    }
    $xml .= '</sources>';
    $xml .= '</control><cpfDescription>';
    /************ IDENTITY ************/
    $xml .= '<identity>';
    //gather entityType
    $types = $dxpath->query('//rdf:type[rdf:resource="http://xmlns.com/foaf/0.1/Person"]');
    echo count($types) . "\n\n\n";
    if (count($types) > 0) {
        $xml .= '<entityType>person</entityType>';
    } else {
        $xml .= '<entityType>family</entityType>';
    }
    foreach ($dxpath->query('//rdfs:label') as $ele) {
        $xml .= '<nameEntry xml:lang="' . $ele->getAttribute('xml:lang') . '"><part>' . $ele->nodeValue . '</part>';
        //set English as preferred label, otherwise alternative
        if ($ele->getAttribute('xml:lang') == 'en') {
            $xml .= '<preferredForm>WIKIPEDIA</preferredForm>';
        } else {
            $xml .= '<alternativeForm>WIKIPEDIA</alternativeForm>';
        }
        $xml .= '</nameEntry>';
    }
    $xml .= '</identity>';
    /************ DESCRIPTION ************/
    $xml .= '<description>';
    $xml .= '<biogHist><abstract xml:lang="en" localType="wikipedia">' . $dxpath->query("//dbpedia-owl:abstract[@xml:lang = 'en']")->item(0)->nodeValue . '</abstract></biogHist>';
    //get existDates
    if (strlen($array['viaf']) > 0) {
        $xml .= getExistDates($vxpath->query('//rdaGr2:dateOfBirth')->item(0)->nodeValue, $vxpath->query('//rdaGr2:dateOfDeath')->item(0)->nodeValue);
    } else {
        $startDates = $dxpath->query('//*[local-name()="birthDate"][@rdf:datatype="http://www.w3.org/2001/XMLSchema#date"]');
        $endDates = $dxpath->query('//*[local-name()="deathDate"][@rdf:datatype="http://www.w3.org/2001/XMLSchema#date"]');
        if (count($startDates) > 0 && count($endDates) > 0) {
            $gStart = strlen($startDates->item(0)->nodeValue) > 0 ? $startDates->item(0)->nodeValue : '0001';
            $gEnd = strlen($endDates->item(0)->nodeValue) > 0 ? $endDates->item(0)->nodeValue : '0001';
            $xml .= '<existDates><dateRange>';
            $xml .= '<fromDate standardDate="' . $gStart . '">' . getDateTextual($gStart) . '</fromDate>';
            $xml .= '<toDate standardDate="' . $gEnd . '">' . getDateTextual($gEnd) . '</toDate>';
            $xml .= '</dateRange></existDates>';
        }
    }
    //get occupations
    $occupations = $dxpath->query('descendant::rdf:Description[@rdf:about="' . $array['dbpedia'] . '"]/dbpedia-owl:occupation');
    foreach ($occupations as $occupation) {
        $url = $occupation->getAttribute('rdf:resource');
        $tempId = substr(strstr($url, 'resource/'), strpos(strstr($url, 'resource/'), '/') + 1);
        if (!array_key_exists($url, $identityArray)) {
            $simplexml = simplexml_load_file('http://dbpedia.org/data/' . $tempId . '.rdf');
            $simplexml->registerXPathNamespace('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
            $names = $simplexml->xpath('//rdfs:label[@xml:lang="en"]');
            $name = $names[0];
        } else {
            $name = $identityArray[$url];
        }
        $xml .= '<occupation>';
        $xml .= '<term vocabularySource="' . $url . '">' . $name . '</term>';
        $xml .= '</occupation>';
    }
    //get subjects
    /*$subjects = $dxpath->query('descendant::rdf:Description[@rdf:about="' . $array['dbpedia'] . '"]/dcterms:subject');
    	foreach ($subjects as $subject){
    		$url = $subject->getAttribute('rdf:resource');
    		$tempId =  substr(strstr($url, 'resource/'), strpos(strstr($url, 'resource/'), '/') + 1);
    		if (!array_key_exists($url, $identityArray)) {
    			$simplexml = simplexml_load_file('http://dbpedia.org/data/' . $tempId . '.rdf');
    			$simplexml->registerXPathNamespace('rdfs', 'http://www.w3.org/2000/01/rdf-schema#');
    			$names =  $simplexml->xpath('//rdfs:label[@xml:lang="en"]');
    			$name = $names[0];
    		} else {
    			$name = $identityArray[$url];
    		}
    			
    		$xml .= '<localDescription localType="subject">';
    		$xml .= '<term vocabularySource="' . $url . '">' . $name . '</term>';
    		$xml .= '</localDescription>';
    	}*/
    $xml .= '</description>';
    /************ RELATIONS ************/
    $xml .= get_relations($dxpath, $array);
    //close EAC-CPF
    $xml .= '</cpfDescription></eac-cpf>';
    return $xml;
}