| 1 |
<?php
|
| 2 |
|
| 3 |
define('CALAIS_CONTENT_TYPE', 'TEXT/TXT');
|
| 4 |
define('CALAIS_OUTPUT_FORMAT', 'xml/rdf');
|
| 5 |
define('CALAIS_PARAMS_NS', 'c');
|
| 6 |
|
| 7 |
function calais_perm() {
|
| 8 |
return array('use calais webservice');
|
| 9 |
}
|
| 10 |
|
| 11 |
function calais_menu() {
|
| 12 |
$items = array();
|
| 13 |
|
| 14 |
$items['admin/settings/calais'] = array(
|
| 15 |
'title' => t('Calais settings'),
|
| 16 |
'description' => t('Set Calais settings and API key'),
|
| 17 |
'page callback' => 'drupal_get_form',
|
| 18 |
'page arguments' => array('calais_admin'),
|
| 19 |
'access arguments' => array('access administration pages'),
|
| 20 |
'type' => MENU_NORMAL_ITEM,
|
| 21 |
);
|
| 22 |
|
| 23 |
$items['calais/test'] = array(
|
| 24 |
'title' => t('Calais Test'),
|
| 25 |
'page callback' => 'calais_test_rdf',
|
| 26 |
'access arguments' => array('access content'),
|
| 27 |
'type' => MENU_CALLBACK,
|
| 28 |
);
|
| 29 |
|
| 30 |
return $items;
|
| 31 |
}
|
| 32 |
|
| 33 |
function calais_admin() {
|
| 34 |
$options = array('1' => t('Enabled'), '0' => t('Disabled'));
|
| 35 |
$form['calais_apikey'] = array(
|
| 36 |
'#type' => 'textfield',
|
| 37 |
'#title' => t('Calais API Key'),
|
| 38 |
'#default_value' => variable_get('calais_apikey', null),
|
| 39 |
'#size' => 30,
|
| 40 |
'#description' => l(t("Apply for an API key"), 'http://developer.opencalais.com/'),
|
| 41 |
);
|
| 42 |
$form['calais_requestmethod'] = array(
|
| 43 |
'#type' => 'select',
|
| 44 |
'#title' => t('Calais API Request Method'),
|
| 45 |
'#default_value' => variable_get('calais_requestmethod', 'POST'),
|
| 46 |
'#options' => array(
|
| 47 |
'POST' => t('POST'),
|
| 48 |
'SOAP' => t('SOAP'),
|
| 49 |
),
|
| 50 |
'#description' => t('Select your request method. PHP must be compiled with the <a href="http://php.net/soap" title="SOAP Extension Manual">SOAP extension</a> if you want to use the SOAP method.'),
|
| 51 |
);
|
| 52 |
$form['parameters'] = array(
|
| 53 |
'#type' => 'fieldset',
|
| 54 |
'#title' => t('Parameters'),
|
| 55 |
'#collapsible' => TRUE,
|
| 56 |
'#collapsed' => FALSE,
|
| 57 |
);
|
| 58 |
$form['parameters']['calais_allowdistribution'] = array(
|
| 59 |
'#type' => 'radios',
|
| 60 |
'#title' => t('Allow Distribution'),
|
| 61 |
'#default_value' => variable_get('calais_allowdistribution', 1),
|
| 62 |
'#options' => $options,
|
| 63 |
'#description' => t('Indicates whether the extracted metadata can be distributed'),
|
| 64 |
);
|
| 65 |
$form['parameters']['calais_allowsearch'] = array(
|
| 66 |
'#type' => 'radios',
|
| 67 |
'#title' => t('Allow Search'),
|
| 68 |
'#default_value' => variable_get('calais_allowsearch', 1),
|
| 69 |
'#options' => $options,
|
| 70 |
'#description' => t('Indicates whether future searches can be performed on the extracted metadata'),
|
| 71 |
);
|
| 72 |
|
| 73 |
return system_settings_form($form);
|
| 74 |
}
|
| 75 |
|
| 76 |
function calais_form_alter(&$form, $form_state, $form_id) {
|
| 77 |
if (user_access('use calais webservice')) {
|
| 78 |
switch ($form_id) {
|
| 79 |
case 'taxonomy_form_vocabulary':
|
| 80 |
$form['calais'] = array(
|
| 81 |
'#type' => 'checkbox',
|
| 82 |
'#title' => t('Calais Semantic Tagging'),
|
| 83 |
'#description' => t('Utilizes the Calais semantic metadata to automatically generate terms for content.'),
|
| 84 |
'#weight' => -100,
|
| 85 |
);
|
| 86 |
break;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
return $form;
|
| 90 |
}
|
| 91 |
|
| 92 |
// This page is purely for testing the entity parsing. You can test it by going to example.com/calais/test
|
| 93 |
function calais_test_rdf() {
|
| 94 |
$rdf = "<?xml version=\"1.0\" encoding=\"utf-8\"?>
|
| 95 |
<string xmlns=\"http://clearforest.com/\"><!--Use of the Calais Web Service is governed by the Terms of Service located at http://www.opencalais.com. By using this service or the results of the service you agree to these terms of service.--><!--Relations:
|
| 96 |
|
| 97 |
NaturalFeature: Pacific coast, Pacific Ocean, Andes mountains
|
| 98 |
Continent: South America
|
| 99 |
IndustryTerm: mining
|
| 100 |
Country: Republic of Peru, Ecuador, Brazil, Peru, Chile, Colombia, Bolivia--><rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" xmlns:c=\"http://s.opencalais.com/1/pred/\"><rdf:Description c:allowDistribution=\"true\" c:allowSearch=\"true\" c:externalID=\"1\" c:id=\"http://id.opencalais.com/iFyeXU37-DUUqgvtP1F59Q\" rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/DocInfo\"/><c:document><![CDATA[<Document><Title>1208232585882-85FDAB4B-502881</Title><Date>2008-04-15</Date><Body>Peru, officially the Republic of Peru, is a country in western South America. It is bordered on the north by Ecuador and Colombia, on the east by Brazil, on the southeast by Bolivia, on the south by Chile, and on the west by the Pacific Ocean.
|
| 101 |
|
| 102 |
Peruvian territory was home to the Norte Chico civilization, one of the oldest in the world, and to the Inca Empire, the largest state in Pre-Columbian America. The Spanish Empire conquered the region in the 16th century and established a Viceroyalty, which included most of its South American colonies. After achieving independence in 1821, Peru has undergone periods of political unrest and fiscal crisis as well as periods of stability and economic upswing.
|
| 103 |
|
| 104 |
Peru is a representative democratic republic divided into 25 regions. Its geography varies from the arid plains of the Pacific coast to the peaks of the Andes mountains and the tropical forests of the Amazon Basin. It is a developing country with a medium Human Development Index score and a poverty level around 45%. Its main economic activities include agriculture, fishing, mining, and manufacturing of products such as textiles.
|
| 105 |
|
| 106 |
The Peruvian population, estimated at 28 million, is multiethnic, including Amerindians, Europeans, Africans and Asians. The main spoken language is Spanish, although a significant number of Peruvians speak Quechua and other native languages. This mixture of cultural traditions has resulted in a wide diversity of expressions in fields such as art, cuisine, literature, and music.</Body></Document>]]></c:document><c:submitter>admin</c:submitter></rdf:Description><rdf:Description c:contentType=\"TEXT/TXT\" c:emVer=\"UnifiedIM-DJ\" c:langIdVer=\"DefaultLangId\" c:language=\"English\" c:processingVer=\"CalaisJob01\" c:submitionDate=\"2008-04-15 00:09:45.882\" rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/meta\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/DocInfoMeta\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:submitterCode>15f15585-9a3f-b493-050b-4ef4a7bb2aa4</c:submitterCode><c:signature>digestalg-1|v0GKbAcwlZJWmIzzotI5QmzCv+E=|dQWmmjUACaNH1xdfJh9EuPYOhC4Xx+fVx05egCmGg9a6LbClFfLNAg==</c:signature></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/lid/DefaultLangId\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/lid/DefaultLangId\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:lang rdf:resource=\"http://d.opencalais.com/lid/DefaultLangId/English\"/></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/4323fd40-f383-3268-841f-d58aa8e6921b\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Continent\"/><c:name>South America</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/1\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/4323fd40-f383-3268-841f-d58aa8e6921b\"/><!--Continent: South America--><c:detection>[the Republic of Peru, is a country in western ]South America[. It is bordered on the north by Ecuador and]</c:detection><c:offset>146</c:offset><c:length>13</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/f421cd6c-349f-35ed-a36e-e980f35ff7c2\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Chile</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/2\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/f421cd6c-349f-35ed-a36e-e980f35ff7c2\"/><!--Country: Chile--><c:detection>[on the southeast by Bolivia, on the south by ]Chile[, and on the west by the Pacific Ocean.
|
| 107 |
|
| 108 |
Peruvian]</c:detection><c:offset>282</c:offset><c:length>5</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/70fef6b9-73c2-3423-91d4-83d40bfe0e21\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Ecuador</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/3\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/70fef6b9-73c2-3423-91d4-83d40bfe0e21\"/><!--Country: Ecuador--><c:detection>[South America. It is bordered on the north by ]Ecuador[ and Colombia, on the east by Brazil, on the]</c:detection><c:offset>192</c:offset><c:length>7</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/06915d15-11ad-3995-9a0a-a4ce09fad7b9\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Republic of Peru</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/4\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/06915d15-11ad-3995-9a0a-a4ce09fad7b9\"/><!--Country: Republic of Peru--><c:detection>[<Date>2008-04-15</Date><Body>Peru, officially the ]Republic of Peru[, is a country in western South America. It is]</c:detection><c:offset>104</c:offset><c:length>16</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/7aef8a39-83e9-3006-a9ff-d6d310b71d0f\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Colombia</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/5\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/7aef8a39-83e9-3006-a9ff-d6d310b71d0f\"/><!--Country: Colombia--><c:detection>[It is bordered on the north by Ecuador and ]Colombia[, on the east by Brazil, on the southeast by]</c:detection><c:offset>204</c:offset><c:length>8</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/f7b6a62b-11f4-3414-ba7b-a283c5c80af1\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/IndustryTerm\"/><c:name>mining</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/6\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/f7b6a62b-11f4-3414-ba7b-a283c5c80af1\"/><!--IndustryTerm: mining--><c:detection>[activities include agriculture, fishing, ]mining[, and manufacturing of products such as]</c:detection><c:offset>1167</c:offset><c:length>6</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/00e6c6a3-27c1-3878-8e89-70b5e509259a\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Peru</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/7\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/00e6c6a3-27c1-3878-8e89-70b5e509259a\"/><!--Country: Peru--><c:detection>[FDAB4B-502881</Title><Date>2008-04-15</Date><Body>]Peru[, officially the Republic of Peru, is a country]</c:detection><c:offset>83</c:offset><c:length>4</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/8\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/00e6c6a3-27c1-3878-8e89-70b5e509259a\"/><!--Country: Peru--><c:detection>[colonies. After achieving independence in 1821, ]Peru[ has undergone periods of political unrest and]</c:detection><c:offset>670</c:offset><c:length>4</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/9\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/00e6c6a3-27c1-3878-8e89-70b5e509259a\"/><!--Country: Peru--><c:detection>[as periods of stability and economic upswing.
|
| 109 |
|
| 110 |
]Peru[ is a representative democratic republic divided]</c:detection><c:offset>790</c:offset><c:length>4</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/3d119b1f-6f76-3d40-874d-714503a9880b\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/NaturalFeature\"/><c:name>Pacific coast</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/10\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/3d119b1f-6f76-3d40-874d-714503a9880b\"/><!--NaturalFeature: Pacific coast--><c:detection>[Its geography varies from the arid plains of ]the Pacific coast[ to the peaks of the Andes mountains and the]</c:detection><c:offset>905</c:offset><c:length>17</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/4a8b8290-e002-3369-9de3-4e58210d0cd9\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Brazil</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/11\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/4a8b8290-e002-3369-9de3-4e58210d0cd9\"/><!--Country: Brazil--><c:detection>[north by Ecuador and Colombia, on the east by ]Brazil[, on the southeast by Bolivia, on the south by]</c:detection><c:offset>229</c:offset><c:length>6</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/f6cb3d38-504e-3254-8e23-3f9d44d8dab4\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/NaturalFeature\"/><c:name>Andes mountains</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/12\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/f6cb3d38-504e-3254-8e23-3f9d44d8dab4\"/><!--NaturalFeature: Andes mountains--><c:detection>[ arid plains of the Pacific coast to the peaks of ]the Andes mountains[ and the tropical forests of the Amazon Basin. It]</c:detection><c:offset>939</c:offset><c:length>19</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/b0b139ee-aacf-3419-80da-48f8674cb07b\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/Country\"/><c:name>Bolivia</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/13\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/b0b139ee-aacf-3419-80da-48f8674cb07b\"/><!--Country: Bolivia--><c:detection>[on the east by Brazil, on the southeast by ]Bolivia[, on the south by Chile, and on the west by the]</c:detection><c:offset>257</c:offset><c:length>7</c:length></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/genericHasher-1/b7d4103b-9e2a-320e-86fd-a660c5fbde8a\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/em/e/NaturalFeature\"/><c:name>Pacific Ocean</c:name></rdf:Description><rdf:Description rdf:about=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744/Instance/14\"><rdf:type rdf:resource=\"http://s.opencalais.com/1/type/sys/InstanceInfo\"/><c:docId rdf:resource=\"http://d.opencalais.com/dochash-1/b455618e-07e2-37aa-b5da-6159fbf63744\"/><c:subject rdf:resource=\"http://d.opencalais.com/genericHasher-1/b7d4103b-9e2a-320e-86fd-a660c5fbde8a\"/><!--NaturalFeature: Pacific Ocean--><c:detection>[on the south by Chile, and on the west by ]the Pacific Ocean[.
|
| 111 |
|
| 112 |
Peruvian territory was home to the Norte Chico]</c:detection><c:offset>308</c:offset><c:length>17</c:length></rdf:Description></rdf:RDF></string>";
|
| 113 |
|
| 114 |
$entities = calais_get_entities($rdf);
|
| 115 |
foreach ($entities as $entity) {
|
| 116 |
$output .= $entity . '<br>';
|
| 117 |
}
|
| 118 |
return '<pre>' . $output . '</pre>';
|
| 119 |
}
|
| 120 |
|
| 121 |
// @TODO: needs to check permissions?
|
| 122 |
function calais_get_rdf($node) {
|
| 123 |
$licenseID = variable_get('calais_apikey', false);
|
| 124 |
|
| 125 |
// If license is not set, don't bother making request
|
| 126 |
if (!$licenseID) {
|
| 127 |
return false;
|
| 128 |
}
|
| 129 |
|
| 130 |
$content = check_plain($node->body);
|
| 131 |
$directives = array(
|
| 132 |
'processingDirectives' => array(
|
| 133 |
'contentType' => CALAIS_CONTENT_TYPE,
|
| 134 |
'outputFormat' => CALAIS_OUTPUT_FORMAT,
|
| 135 |
),
|
| 136 |
'userDirectives' => array(
|
| 137 |
'allowDistribution' => _calais_bool2string(variable_get('calais_allowdistribution', false)),
|
| 138 |
'allowSearch' => _calais_bool2string(variable_get('calais_allowsearch', false)),
|
| 139 |
'externalID' => $node->nid,
|
| 140 |
'submitter' => $node->name,
|
| 141 |
),
|
| 142 |
);
|
| 143 |
// Maybe grab cck types as externalMetadata?
|
| 144 |
$paramsXML = calais_get_parameters($directives);
|
| 145 |
$method = variable_get('calais_method', 'POST');
|
| 146 |
return _calais_get_rdf($content, $paramsXML, $licenseID, $method);
|
| 147 |
}
|
| 148 |
|
| 149 |
function _calais_get_rdf($content, $paramsXML, $licenseID, $method = null) {
|
| 150 |
switch (strtoupper($method)) {
|
| 151 |
case 'SOAP':
|
| 152 |
ini_set("soap.wsdl_cache_enabled", "0");
|
| 153 |
try {
|
| 154 |
$client = new SoapClient("http://api.opencalais.com/enlighten/?wsdl");
|
| 155 |
|
| 156 |
$enlighten = new stdClass;
|
| 157 |
$enlighten->licenseID = $licenseID;
|
| 158 |
$enlighten->content = check_plain($content);
|
| 159 |
$enlighten->paramsXML = $paramsXML;
|
| 160 |
|
| 161 |
$result = $client->Enlighten($enlighten);
|
| 162 |
return $result->EnlightenResult;
|
| 163 |
}
|
| 164 |
catch (SoapFault $fault) {
|
| 165 |
// @TODO: Check this syntax
|
| 166 |
drupal_set_message(t(trigger_error("SOAP Fault: (faultcode: {$fault->faultcode}, faultstring: {$fault->faultstring})", E_USER_ERROR)), 'error');
|
| 167 |
return false;
|
| 168 |
}
|
| 169 |
break;
|
| 170 |
case 'POST':
|
| 171 |
default:
|
| 172 |
$data = 'licenseID=' . $licenseID . '&content=' . urlencode($content) . '¶msXML=' . urlencode($paramsXML);
|
| 173 |
return drupal_http_request('http://api.opencalais.com/enlighten/calais.asmx/Enlighten', array(), 'POST', $data);
|
| 174 |
break;
|
| 175 |
}
|
| 176 |
return false;
|
| 177 |
}
|
| 178 |
|
| 179 |
// add documentation for directives array structure
|
| 180 |
function calais_get_parameters($directives = array()) {
|
| 181 |
$xml = new XMLWriter(); // requires PHP 5.1.2+
|
| 182 |
$xml->openMemory();
|
| 183 |
$xml->setIndent(FALSE);
|
| 184 |
$xml->startDocument('1.0', 'utf-8');
|
| 185 |
$xml->startElementNS(CALAIS_PARAMS_NS, 'params', 'http://s.opencalais.com/1/pred/');
|
| 186 |
foreach ($directives as $directive => $values) {
|
| 187 |
$xml->startElement(CALAIS_PARAMS_NS . ':' . $directive);
|
| 188 |
foreach ($values as $key => $value) {
|
| 189 |
$xml->writeAttribute(CALAIS_PARAMS_NS . ':' . $key, $value);
|
| 190 |
}
|
| 191 |
$xml->endElement();
|
| 192 |
}
|
| 193 |
$xml->endElement();
|
| 194 |
$xml->endElement();
|
| 195 |
return $xml->outputMemory();
|
| 196 |
}
|
| 197 |
|
| 198 |
function calais_get_entities($rdf) {
|
| 199 |
$entities = array();
|
| 200 |
$entity_types = calais_get_entity_types();
|
| 201 |
|
| 202 |
$parser = ARC2::getRDFXMLParser();
|
| 203 |
$parser->parse(null, $rdf);
|
| 204 |
$items = rdf_select($parser->getSimpleIndex(), true, true, true);
|
| 205 |
|
| 206 |
for ($i = 0, $count = count($items); $i<$count; $i++) {
|
| 207 |
$tokens = explode('/', $items[$i][2]);
|
| 208 |
if (isset($entity_types[end($tokens)])) {
|
| 209 |
$i++;
|
| 210 |
$entities[] = $items[$i][2];
|
| 211 |
}
|
| 212 |
}
|
| 213 |
return $entities;
|
| 214 |
}
|
| 215 |
|
| 216 |
function calais_get_entity_types() {
|
| 217 |
return array(
|
| 218 |
'Anniversary' => 'Anniversary',
|
| 219 |
'City' => 'City',
|
| 220 |
'Company' => 'Company',
|
| 221 |
'Continent' => 'Continent',
|
| 222 |
'Country' => 'Country',
|
| 223 |
'Currency' => 'Currency',
|
| 224 |
'EmailAddress' => 'Email Address',
|
| 225 |
'Facility' => 'Facility',
|
| 226 |
'FaxNumber' => 'Fax Number',
|
| 227 |
'Holiday' => 'Holiday',
|
| 228 |
'IndustryTerm' => 'Industry Term',
|
| 229 |
'NaturalDisaster' => 'Natural Disaster',
|
| 230 |
'NaturalFeature' => 'Natural Feature',
|
| 231 |
'Organization' => 'Organization',
|
| 232 |
'Person' => 'Person',
|
| 233 |
'PhoneNumber' => 'Phone Number',
|
| 234 |
'ProvinceOrState' => 'Province or State',
|
| 235 |
'Region' => 'Region',
|
| 236 |
'Technology' => 'Technology',
|
| 237 |
'URL' => 'URL',
|
| 238 |
);
|
| 239 |
}
|
| 240 |
|
| 241 |
function _calais_bool2string($bool) {
|
| 242 |
if ($bool) {
|
| 243 |
return 'true';
|
| 244 |
}
|
| 245 |
else {
|
| 246 |
return 'false';
|
| 247 |
}
|
| 248 |
}
|