static grn_obj * func_snippet_html(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data) { grn_obj *snippets = NULL; /* TODO: support parameters */ if (nargs == 1) { grn_obj *text = args[0]; grn_obj *expression = NULL; grn_obj *condition_ptr = NULL; grn_obj *condition = NULL; grn_obj *snip = NULL; int flags = GRN_SNIP_SKIP_LEADING_SPACES; unsigned int width = 200; unsigned int max_n_results = 3; const char *open_tag = "<span class=\"keyword\">"; const char *close_tag = "</span>"; grn_snip_mapping *mapping = GRN_SNIP_MAPPING_HTML_ESCAPE; grn_proc_get_info(ctx, user_data, NULL, NULL, &expression); condition_ptr = grn_expr_get_var(ctx, expression, GRN_SELECT_INTERNAL_VAR_CONDITION, strlen(GRN_SELECT_INTERNAL_VAR_CONDITION)); if (condition_ptr) { condition = GRN_PTR_VALUE(condition_ptr); } if (condition) { grn_obj *snip_ptr; snip_ptr = grn_expr_get_var(ctx, expression, GRN_FUNC_SNIPPET_HTML_CACHE_NAME, strlen(GRN_FUNC_SNIPPET_HTML_CACHE_NAME)); if (snip_ptr) { snip = GRN_PTR_VALUE(snip_ptr); } else { snip_ptr = grn_expr_get_or_add_var(ctx, expression, GRN_FUNC_SNIPPET_HTML_CACHE_NAME, strlen(GRN_FUNC_SNIPPET_HTML_CACHE_NAME)); GRN_OBJ_FIN(ctx, snip_ptr); GRN_PTR_INIT(snip_ptr, GRN_OBJ_OWN, GRN_DB_OBJECT); snip = grn_snip_open(ctx, flags, width, max_n_results, open_tag, strlen(open_tag), close_tag, strlen(close_tag), mapping); if (snip) { grn_snip_set_normalizer(ctx, snip, GRN_NORMALIZER_AUTO); grn_expr_snip_add_conditions(ctx, condition, snip, 0, NULL, NULL, NULL, NULL); GRN_PTR_SET(ctx, snip_ptr, snip); } } } if (snip) { snippets = snippet_exec(ctx, snip, text, user_data, NULL, 0, NULL, 0); } } if (!snippets) { snippets = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_VOID, 0); } return snippets; }
static mrn_bool mrn_snippet_html_prepare(mrn_snippet_html_info *info, UDF_ARGS *args, char *message, grn_obj **snippet) { MRN_DBUG_ENTER_FUNCTION(); grn_ctx *ctx = info->ctx; int flags = GRN_SNIP_SKIP_LEADING_SPACES; unsigned int width = 200; unsigned int max_n_results = 3; const char *open_tag = "<span class=\"keyword\">"; const char *close_tag = "</span>"; grn_snip_mapping *mapping = GRN_SNIP_MAPPING_HTML_ESCAPE; grn_obj *expr = NULL; *snippet = NULL; mrn::encoding::set_raw(ctx, system_charset_info); if (!(system_charset_info->state & (MY_CS_BINSORT | MY_CS_CSSORT))) { flags |= GRN_SNIP_NORMALIZE; } *snippet = grn_snip_open(ctx, flags, width, max_n_results, open_tag, strlen(open_tag), close_tag, strlen(close_tag), mapping); if (ctx->rc != GRN_SUCCESS) { if (message) { snprintf(message, MYSQL_ERRMSG_SIZE, "mroonga_snippet_html(): failed to open grn_snip: <%s>", ctx->errbuf); } goto error; } if (info->query_mode.used) { if (!info->query_mode.table) { grn_obj *short_text; short_text = grn_ctx_at(info->ctx, GRN_DB_SHORT_TEXT); info->query_mode.table = grn_table_create(info->ctx, NULL, 0, NULL, GRN_TABLE_HASH_KEY, short_text, NULL); } if (!info->query_mode.default_column) { info->query_mode.default_column = grn_obj_column(info->ctx, info->query_mode.table, GRN_COLUMN_NAME_KEY, GRN_COLUMN_NAME_KEY_LEN); } grn_obj *record = NULL; GRN_EXPR_CREATE_FOR_QUERY(info->ctx, info->query_mode.table, expr, record); if (!expr) { if (message) { snprintf(message, MYSQL_ERRMSG_SIZE, "mroonga_snippet_html(): " "failed to create expression: <%s>", ctx->errbuf); } goto error; } mrn::QueryParser query_parser(info->ctx, current_thd, expr, info->query_mode.default_column, 0, NULL); grn_rc rc = query_parser.parse(args->args[1], args->lengths[1]); if (rc != GRN_SUCCESS) { if (message) { snprintf(message, MYSQL_ERRMSG_SIZE, "mroonga_snippet_html(): " "failed to parse query: <%s>", ctx->errbuf); } goto error; } rc = grn_expr_snip_add_conditions(info->ctx, expr, *snippet, 0, NULL, NULL, NULL, NULL); if (rc != GRN_SUCCESS) { if (message) { snprintf(message, MYSQL_ERRMSG_SIZE, "mroonga_snippet_html(): " "failed to add conditions: <%s>", ctx->errbuf); } goto error; } } else { unsigned int i; for (i = 1; i < args->arg_count; ++i) { if (!args->args[i]) { continue; } grn_rc rc = grn_snip_add_cond(ctx, *snippet, args->args[i], args->lengths[i], NULL, 0, NULL, 0); if (rc != GRN_SUCCESS) { if (message) { snprintf(message, MYSQL_ERRMSG_SIZE, "mroonga_snippet_html(): " "failed to add a condition to grn_snip: <%s>", ctx->errbuf); } goto error; } } } DBUG_RETURN(false); error: if (expr) { grn_obj_close(ctx, expr); } if (*snippet) { grn_obj_close(ctx, *snippet); } DBUG_RETURN(true); }