Drupal: indexing nodes with Agrovoc (without a module)
Instructions to implement manual Agrovoc indexing (with an Agrovoc search popup) in Drupal without the need of a dedicated module.
__________________________________________________________________
$vid = 16; // id of the vocabulary you use for multilingual Agrovoc terms
$terms_list = $node->field_subject; // the field you use for Agrovoc codes
require_once 'sites/all/modules/i18n/i18ntaxonomy/i18ntaxonomy.admin.inc';
$agrovocURIbase = "http://www.fao.org/aims/aos/agrovoc/c_";
$url = "http://www.fao.org/webservices/AgrovocWS";
$function_soap = "simpleSearchByMode2";
$uri = "AgrovocWS";
$client = new SoapClient(null, array('location' => $url, 'uri' => $uri, 'exceptions' => true));
//Test if Web Services are working: if not, display message after saving:
$soapOk = true;
$paramsTest = array('termcode' => 1234,'language' => 'en');
try {
$resultTest = $client->__soapCall("getTermByLanguage", $paramsTest);
} catch (Exception $e) {
drupal_set_message("WARNING. SOAP call failed: " . $e->getMessage() . ". AGROVOC INDEXING NOT UPDATED.");
$soapOk = false;
}
if ($soapOk) {
$tridOrig = 0;
$num_max = count($terms_list);
$i=0;
$k=1;
// REMOVE ALL ASSOCIATIONS WITH TERMS IN AGROVOC VOCABULARIES
// change the name of the field if you did not use our field names
$node->field_agrovocsubject_all = array();
if (empty($terms_list)==false){
//for each term, look up labels in all enabled languages
foreach ($terms_list as $key => $val) {
$dTerm = taxonomy_get_term($val['value']);
$agrovocCode = $dTerm->name;
$agrovocCodeint=(int)$agrovocCode;
$function_soaplang="getTermByLanguage";
$s=0;
$lang_list=Array();
$lanPrm=Array();
//change according to the languages you need: the languages
//have to be enabled in Drupal and existing in Agrovoc
//demo version: only English and French:
$lanPrm[0]="FR";
$lanPrm[1]="EN";
while ($s < 2){
$paramslang = array('termcode' => $agrovocCodeint,'language' => $lanPrm[$s]);
$resultlang = $client->__soapCall($function_soaplang, $paramslang);
$a = 0;
while ($resultlang == "" && $a < 10) { // 10 attempts for WS
$resultlang = $client->__soapCall($function_soaplang, $paramslang);
$a++;
}
$lang_list[$lanPrm[$s]] = trim($resultlang);
$s++;
}
$Frterm=trim($lang_list['FR']);
$Enterm=trim($lang_list['EN']);
// DEMO PACKAGE: ONLY ENGLISH AND FRENCH
// TO ADD NEW LANGUAGES, MODIFY THE ARRAYS
// ABOVE AND REPEAT THE SECTIONS BELOW
// NOTICE: THE LANGUAGES YOU ADD HERE MUST
// BE ENABLED IN YOUR WEBSITE
// for looking up terms in Drupal taxonomy, impossible to use
// taxonomy_get_term_by_name: no language parameter
// ENGLISH
if (trim($Enterm)!= "") {
$sql="SELECT * FROM {term_data} WHERE name = '%s' and language='%s'";
$term=db_query($sql,$Enterm,"en");
$term=db_fetch_object($term);
if ($term->name==""){
$lan="en";
$edit = array('vid' => $vid,
'name' => $Enterm ,
'language' => $lan,
'description' => $agrovocURIbase . $agrovocCode);
taxonomy_save_term($edit);
}
$sql="SELECT * FROM {term_data} WHERE name = '%s' and language='%s'";
$term=db_query($sql,$Enterm,"en");
$term=db_fetch_object($term);
$agrovocTermId=$term->tid;
// TRANSLATIONS: i18ntaxonomy_translation_save doesn't work
// set trid for translations: for English (original)
$tridOrig = i18ntaxonomy_next_trid();
$sql="UPDATE {term_data} SET trid = %d WHERE tid = %d";
$r = db_query($sql,$tridOrig,$term->tid);
$node->field_agrovocsubject_all[$i]['value'] = $agrovocTermId;
$arrTerms['en']=$agrovocTermId;
$arrTermAll[$i]=$agrovocTermId;
}
// FRENCH
if (trim($Frterm)!= "") {
$sql="SELECT * FROM {term_data} WHERE name = '%s' and language='%s'";
$term=db_query($sql,$Frterm,"fr");
$term=db_fetch_object($term);
if (empty($term)==true){
$lan="fr";
$edit = array('vid' => $vid,
'name' => $Frterm ,
'language' => $lan,
'description' => $agrovocURIbase . $agrovocCode);
taxonomy_save_term($edit);
}
$sql="SELECT * FROM {term_data} WHERE name = '%s' and language='%s'";
$term=db_query($sql,$Frterm,"fr");
$term=db_fetch_object($term);
$agrovocTermId=$term->tid;
// TRANSLATIONS: i18ntaxonomy_translation_save doesn't work
// set trid TO ENGLISH TRID (original) for translations
$sql="UPDATE {term_data} SET trid = %d WHERE tid = %d";
$r = db_query($sql,$tridOrig,$term->tid);
$node->field_agrovocsubject_all[$k]['value'] = $agrovocTermId;
$arrTerms['fr']=$agrovocTermId;
$arrTermAll[$k]=$agrovocTermId;
}
// i18ntaxonomy_translation_save($arrTerms);
// to set terms as translations: doesn't work
$i=$i+2;
$k = $k+2;
}
$node->taxonomy[16]=$arrTermAll;
return $node;
}
}
__________________________________________________________________
Below are attached the three files you need to copy to your website root and a Word version of this post.