X-Git-Url: http://jsfdemo.indexdata.com/?a=blobdiff_plain;f=src%2Flogic.c;h=ea6e66f56fe72463306fbb034a85adcf57652575;hb=2fdbd5de9185e926401609c22b328f07af0248bd;hp=a3fdd49eb987577874749947fed2b12226166f8b;hpb=fba8c6cb11ed2ab48f433619315fb32e1f0f5184;p=pazpar2-moved-to-github.git diff --git a/src/logic.c b/src/logic.c index a3fdd49..ea6e66f 100644 --- a/src/logic.c +++ b/src/logic.c @@ -1,4 +1,4 @@ -/* $Id: logic.c,v 1.24 2007-04-26 12:12:19 marc Exp $ +/* $Id: logic.c,v 1.35 2007-06-02 04:32:28 quinn Exp $ Copyright (c) 2006-2007, Index Data. This file is part of Pazpar2. @@ -70,6 +70,7 @@ Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA #include "database.h" #include "client.h" #include "settings.h" +#include "normalize7bit.h" #define MAX_CHUNK 15 @@ -81,7 +82,8 @@ struct parameters global_parameters = "", "", 0, - 0, + 0, /* dump_records */ + 0, /* debug_mode */ 30, "81", "Index Data PazPar2", @@ -121,75 +123,6 @@ void pull_terms(NMEM nmem, struct ccl_rpn_node *n, char **termlist, int *num) } } -char *normalize_mergekey(char *buf, int skiparticle) -{ - char *p = buf, *pout = buf; - - if (skiparticle) - { - char firstword[64]; - char articles[] = "the den der die des an a "; // must end in space - - while (*p && !isalnum(*p)) - p++; - pout = firstword; - while (*p && *p != ' ' && pout - firstword < 62) - *(pout++) = tolower(*(p++)); - *(pout++) = ' '; - *(pout++) = '\0'; - if (!strstr(articles, firstword)) - p = buf; - pout = buf; - } - - while (*p) - { - while (*p && !isalnum(*p)) - p++; - while (isalnum(*p)) - *(pout++) = tolower(*(p++)); - if (*p) - *(pout++) = ' '; - while (*p && !isalnum(*p)) - p++; - } - if (buf != pout) - do { - *(pout--) = '\0'; - } - while (pout > buf && *pout == ' '); - - return buf; -} - -// Extract what appears to be years from buf, storing highest and -// lowest values. -static int extract_years(const char *buf, int *first, int *last) -{ - *first = -1; - *last = -1; - while (*buf) - { - const char *e; - int len; - - while (*buf && !isdigit(*buf)) - buf++; - len = 0; - for (e = buf; *e && isdigit(*e); e++) - len++; - if (len == 4) - { - int value = atoi(buf); - if (*first < 0 || value < *first) - *first = value; - if (*last < 0 || value > *last) - *last = value; - } - buf = e; - } - return *first; -} static void add_facet(struct session *s, const char *type, const char *value) @@ -221,48 +154,73 @@ xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec) { struct database_retrievalmap *m; struct database *db = sdb->database; - xmlNode *res; - xmlDoc *rdoc; + xmlDoc *rdoc = 0; + const Odr_oid *oid = rec->direct_reference; - // First normalize to XML - if (sdb->yaz_marc) + /* convert response record to XML somehow */ + if (rec->which == Z_External_octet && oid + && !oid_oidcmp(oid, yaz_oid_recsyn_xml)) { - char *buf; - int len; - if (rec->which != Z_External_octet) + /* xml already */ + rdoc = xmlParseMemory((char*) rec->u.octet_aligned->buf, + rec->u.octet_aligned->len); + if (!rdoc) { - yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s", + yaz_log(YLOG_FATAL, "Non-wellformed XML received from %s", db->url); return 0; } - buf = (char*) rec->u.octet_aligned->buf; - len = rec->u.octet_aligned->len; - if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0) + } + else if (oid && yaz_oid_is_iso2709(oid)) + { + /* ISO2709 gets converted to MARCXML */ + if (!sdb->yaz_marc) { - yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url); + yaz_log(YLOG_FATAL, "Unable to handle ISO2709 record"); return 0; } - - yaz_marc_write_using_libxml2(sdb->yaz_marc, 1); - if (yaz_marc_write_xml(sdb->yaz_marc, &res, - "http://www.loc.gov/MARC21/slim", 0, 0) < 0) + else { - yaz_log(YLOG_WARN, "Failed to encode as XML %s", - db->url); - return 0; + xmlNode *res; + char *buf; + int len; + + if (rec->which != Z_External_octet) + { + yaz_log(YLOG_WARN, "Unexpected external branch, probably BER %s", + db->url); + return 0; + } + buf = (char*) rec->u.octet_aligned->buf; + len = rec->u.octet_aligned->len; + if (yaz_marc_read_iso2709(sdb->yaz_marc, buf, len) < 0) + { + yaz_log(YLOG_WARN, "Failed to decode MARC %s", db->url); + return 0; + } + + yaz_marc_write_using_libxml2(sdb->yaz_marc, 1); + if (yaz_marc_write_xml(sdb->yaz_marc, &res, + "http://www.loc.gov/MARC21/slim", 0, 0) < 0) + { + yaz_log(YLOG_WARN, "Failed to encode as XML %s", + db->url); + return 0; + } + rdoc = xmlNewDoc((xmlChar *) "1.0"); + xmlDocSetRootElement(rdoc, res); } - rdoc = xmlNewDoc((xmlChar *) "1.0"); - xmlDocSetRootElement(rdoc, res); - } else { + char oid_name_buf[OID_STR_MAX]; + const char *oid_name = yaz_oid_to_string_buf(oid, 0, oid_name_buf); yaz_log(YLOG_FATAL, - "Unknown native_syntax in normalize_record from %s", - db->url); + "Unable to handle record of type %s from %s", + oid_name, db->url); return 0; } - + if (global_parameters.dump_records){ fprintf(stderr, "Input Record (normalized) from %s\n----------------\n", @@ -277,7 +235,6 @@ xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec) for (m = sdb->map; m; m = m->next){ xmlDoc *new = 0; -#if 1 { xmlNodePtr root = 0; new = xsltApplyStylesheet(m->stylesheet, rdoc, 0); @@ -291,25 +248,6 @@ xmlDoc *normalize_record(struct session_database *sdb, Z_External *rec) return 0; } } -#endif - -#if 0 - // do it another way to detect transformation errors right now - // but does not seem to work either! - { - xsltTransformContextPtr ctxt; - ctxt = xsltNewTransformContext(m->stylesheet, rdoc); - new = xsltApplyStylesheetUser(m->stylesheet, rdoc, 0, 0, 0, ctxt); - if ((ctxt->state == XSLT_STATE_ERROR) || - (ctxt->state == XSLT_STATE_STOPPED)){ - yaz_log(YLOG_WARN, "XSLT transformation failed from %s", - cl->database->database->url); - xmlFreeDoc(new); - xmlFreeDoc(rdoc); - return 0; - } - } -#endif xmlFreeDoc(rdoc); rdoc = new; @@ -564,9 +502,7 @@ char *search(struct session *se, char *query, char *filter) } for (cl = se->clients; cl; cl = client_next_in_session(cl)) - { client_prep_connection(cl); - } return 0; } @@ -581,6 +517,16 @@ static void session_init_databases_fun(void *context, struct database *db) new->database = db; new->yaz_marc = 0; + +#ifdef HAVE_ICU + if (global_parameters.server && global_parameters.server->icu_chn) + new->pct = pp2_charset_create(global_parameters.server->icu_chn); + else + new->pct = pp2_charset_create(0); +#else // HAVE_ICU + new->pct = pp2_charset_create(0); +#endif // HAVE_ICU + new->map = 0; new->settings = nmem_malloc(se->session_nmem, sizeof(struct settings *) * num); @@ -604,6 +550,8 @@ static void session_database_destroy(struct session_database *sdb) xsltFreeStylesheet(m->stylesheet); if (sdb->yaz_marc) yaz_marc_destroy(sdb->yaz_marc); + if (sdb->pct) + pp2_charset_destroy(sdb->pct); } // Initialize session_database list -- this represents this session's view @@ -644,13 +592,19 @@ void session_apply_setting(struct session *se, char *dbname, char *setting, { struct session_database *sdb = find_session_database(se, dbname); struct setting *new = nmem_malloc(se->session_nmem, sizeof(*new)); - int offset = settings_offset(setting); + int offset = settings_offset_cprefix(setting); if (offset < 0) { yaz_log(YLOG_WARN, "Unknown setting %s", setting); return; } + // Jakub: This breaks the filter setting. + /*if (offset == PZ_ID) + { + yaz_log(YLOG_WARN, "No need to set pz:id setting. Ignoring"); + return; + }*/ new->precedence = 0; new->target = dbname; new->name = setting; @@ -972,24 +926,13 @@ struct record *ingest_record(struct client *cl, Z_External *rec, return 0; } -#if 0 - record = nmem_malloc(se->nmem, sizeof(struct record)); - record->next = 0; - record->client = cl; - record->metadata = nmem_malloc(se->nmem, - sizeof(struct record_metadata*) * service->num_metadata); - memset(record->metadata, 0, - sizeof(struct record_metadata*) * service->num_metadata); - -#else record = record_create(se->nmem, service->num_metadata, service->num_sortkeys); record_assign_client(record, cl); -#endif mergekey_norm = (xmlChar *) nmem_strdup(se->nmem, (char*) mergekey); xmlFree(mergekey); - normalize_mergekey((char *) mergekey_norm, 0); + normalize7bit_mergekey((char *) mergekey_norm, 0); cluster = reclist_insert(se->reclist, global_parameters.server->service, @@ -1006,6 +949,8 @@ struct record *ingest_record(struct client *cl, Z_External *rec, } relevance_newrec(se->relevance, cluster); + + // now parsing XML record and adding data to cluster or record metadata for (n = root->children; n; n = n->next) { if (type) @@ -1032,29 +977,6 @@ struct record *ingest_record(struct client *cl, Z_External *rec, if (!type || !value) continue; -#if 0 - // First, find out what field we're looking at - for (md_field_id = 0; md_field_id < service->num_metadata; - md_field_id++) - if (!strcmp((const char *) type, - service->metadata[md_field_id].name)) - { - ser_md = &service->metadata[md_field_id]; - if (ser_md->sortkey_offset >= 0){ - sk_field_id = ser_md->sortkey_offset; - ser_sk = &service->sortkeys[sk_field_id]; - } - break; - } - - if (!ser_md) - { - yaz_log(YLOG_WARN, - "Ignoring unknown metadata element: %s", type); - continue; - } - -#else md_field_id = conf_service_metadata_field_id(service, (const char *) type); if (md_field_id < 0) @@ -1070,37 +992,30 @@ struct record *ingest_record(struct client *cl, Z_External *rec, sk_field_id = ser_md->sortkey_offset; ser_sk = &service->sortkeys[sk_field_id]; } -#endif - // Find out where we are putting it + // Find out where we are putting it - based on merge or not if (ser_md->merge == Metadata_merge_no) wheretoput = &record->metadata[md_field_id]; else wheretoput = &cluster->metadata[md_field_id]; - // Put it there - -#if 1 //something wrong with constructor, debug tomorrow - rec_md = nmem_malloc(se->nmem, sizeof(struct record_metadata)); - rec_md->next = 0; -#else + // create new record_metadata rec_md = record_metadata_create(se->nmem); -#endif + // and polulate with data: + // type based charmapping decisions follow here if (ser_md->type == Metadata_type_generic) { - char *p, *pe; - for (p = (char *) value; *p && isspace(*p); p++) - ; - for (pe = p + strlen(p) - 1; - pe > p && strchr(" ,/.:([", *pe); pe--) - *pe = '\0'; + + char * p = (char *) value; + p = normalize7bit_generic(p, " ,/.:(["); + rec_md->data.text = nmem_strdup(se->nmem, p); } else if (ser_md->type == Metadata_type_year) { - if (extract_years((char *) value, &first, &last) < 0) + if (extract7bit_years((char *) value, &first, &last) < 0) continue; } else @@ -1109,12 +1024,9 @@ struct record *ingest_record(struct client *cl, Z_External *rec, "Unknown type in metadata element %s", type); continue; } - if (ser_md->type == Metadata_type_year - && ser_md->merge != Metadata_merge_range) - { - yaz_log(YLOG_WARN, "Only range merging supported for years"); - continue; - } + + // and polulate with data: + // assign cluster or record based on merge action if (ser_md->merge == Metadata_merge_unique) { struct record_metadata *mnode; @@ -1142,7 +1054,7 @@ struct record *ingest_record(struct client *cl, Z_External *rec, cluster->sortkeys[sk_field_id] = nmem_malloc(se->nmem, sizeof(union data_types)); - normalize_mergekey(s, + normalize7bit_mergekey(s, (ser_sk->type == Metadata_sortkey_skiparticle)); cluster->sortkeys[sk_field_id]->text = s; } @@ -1156,7 +1068,6 @@ struct record *ingest_record(struct client *cl, Z_External *rec, } else if (ser_md->merge == Metadata_merge_range) { - assert(ser_md->type == Metadata_type_year); if (!*wheretoput) { *wheretoput = rec_md; @@ -1183,14 +1094,14 @@ struct record *ingest_record(struct client *cl, Z_External *rec, } #endif } - else - yaz_log(YLOG_WARN, - "Don't know how to merge on element name %s", - ser_md->name); + + // ranking of _all_ fields enabled ... if (ser_md->rank) relevance_countwords(se->relevance, cluster, (char *) value, ser_md->rank); + + // construct facets ... if (ser_md->termlist) { if (ser_md->type == Metadata_type_year) @@ -1207,6 +1118,8 @@ struct record *ingest_record(struct client *cl, Z_External *rec, else add_facet(se, (char *) type, (char *) value); } + + // cleaning up xmlFree(type); xmlFree(value); type = value = 0;