Debian Codegraph @

lib/proc/proc_snippet.c

raw symbols SCIP
1 /*
2 Copyright (C) 2009-2016 Brazil
3 Copyright (C) 2019-2023 Sutou Kouhei <[email protected]>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 #include "../grn_proc.h"
21 #include "../grn_expr.h"
22
23 #include <groonga/plugin.h>
24 #include <string.h>
25
26 #define GRN_FUNC_SNIPPET_HTML_CACHE_NAME "$snippet_html"
27
28 static grn_obj *
29 snippet_exec(grn_ctx *ctx,
30 grn_obj *snip,
31 grn_obj *target,
32 grn_obj *default_return_value,
33 grn_user_data *user_data,
34 const char *prefix,
35 size_t prefix_length,
36 const char *suffix,
37 size_t suffix_length)
38 {
39 uint32_t n_texts = 0;
40 bool is_vector = false;
41 if (grn_obj_is_text_family_vector(ctx, target)) {
42 is_vector = true;
43 n_texts = grn_vector_size(ctx, target);
44 if (n_texts == 0) {
45 return NULL;
46 }
47 } else if (grn_obj_is_text_family_bulk(ctx, target)) {
48 if (GRN_TEXT_LEN(target) == 0) {
49 return NULL;
50 }
51 n_texts = 1;
52 } else {
53 return NULL;
54 }
55
56 grn_obj *snippets =
57 grn_plugin_proc_alloc(ctx, user_data, GRN_DB_SHORT_TEXT, GRN_OBJ_VECTOR);
58 if (!snippets) {
59 return NULL;
60 }
61
62 grn_obj snippet_buffer;
63 GRN_TEXT_INIT(&snippet_buffer, 0);
64
65 grn_rc rc = GRN_SUCCESS;
66 uint32_t i;
67 for (i = 0; i < n_texts; i++) {
68 const char *text;
69 uint32_t text_length;
70 if (is_vector) {
71 grn_id domain;
72 text_length =
73 grn_vector_get_element_float(ctx, target, i, &text, NULL, &domain);
74 if (!grn_type_id_is_text_family(ctx, domain)) {
75 continue;
76 }
77 if (text_length == 0) {
78 continue;
79 }
80 } else {
81 text = GRN_TEXT_VALUE(target);
82 text_length = (uint32_t)GRN_TEXT_LEN(target);
83 }
84
85 unsigned int i, n_results, max_tagged_length;
86 rc = grn_snip_exec(ctx,
87 snip,
88 text,
89 text_length,
90 &n_results,
91 &max_tagged_length);
92 if (rc != GRN_SUCCESS) {
93 break;
94 }
95
96 if (n_results == 0) {
97 continue;
98 }
99
100 grn_bulk_space(ctx,
101 &snippet_buffer,
102 prefix_length + max_tagged_length + suffix_length);
103 for (i = 0; i < n_results; i++) {
104 unsigned int snippet_length;
105
106 GRN_BULK_REWIND(&snippet_buffer);
107 if (prefix_length > 0) {
108 GRN_TEXT_PUT(ctx, &snippet_buffer, prefix, prefix_length);
109 }
110 rc = grn_snip_get_result(ctx,
111 snip,
112 i,
113 GRN_TEXT_VALUE(&snippet_buffer) + prefix_length,
114 &snippet_length);
115 if (rc != GRN_SUCCESS) {
116 continue;
117 }
118 grn_strncat(GRN_TEXT_VALUE(&snippet_buffer),
119 GRN_BULK_WSIZE(&snippet_buffer),
120 suffix,
121 suffix_length);
122 grn_vector_add_element(ctx,
123 snippets,
124 GRN_TEXT_VALUE(&snippet_buffer),
125 (uint32_t)prefix_length + snippet_length +
126 (uint32_t)suffix_length,
127 0,
128 GRN_DB_SHORT_TEXT);
129 }
130 }
131 GRN_OBJ_FIN(ctx, &snippet_buffer);
132
133 if (rc != GRN_SUCCESS) {
134 return NULL;
135 }
136
137 if (grn_vector_size(ctx, snippets) == 0) {
138 if (default_return_value) {
139 return default_return_value;
140 } else {
141 return grn_plugin_proc_alloc(ctx, user_data, GRN_DB_VOID, 0);
142 }
143 }
144
145 return snippets;
146 }
147
148 static grn_obj *
149 func_snippet(grn_ctx *ctx, int nargs, grn_obj **args, grn_user_data *user_data)
150 {
151 const char *tag = "[snippet]";
152 grn_obj *snippets = NULL;
153 grn_obj *snip = NULL;
154 grn_obj cache_key;
155 GRN_TEXT_INIT(&cache_key, 0);
156
157 #define N_REQUIRED_ARGS 1
158 #define KEYWORD_SET_SIZE 3
159 if (nargs < N_REQUIRED_ARGS) {
160 GRN_PLUGIN_ERROR(ctx,
161 GRN_INVALID_ARGUMENT,
162 "%s wrong number of arguments (%d for %d..)",
163 tag,
164 nargs,
165 N_REQUIRED_ARGS);
166 goto exit;
167 }
168
169 grn_obj *target = args[0];
170 grn_proc_func_generate_cache_key(ctx,
171 "snippet",
172 args + 1,
173 nargs - 1,
174 &cache_key);
175
176 grn_obj *snip_ptr = NULL;
177 grn_obj *expression;
178 grn_proc_get_info(ctx, user_data, NULL, NULL, &expression);
179 if (GRN_TEXT_LEN(&cache_key) <= GRN_TABLE_MAX_KEY_SIZE) {
180 snip_ptr = grn_expr_get_var(ctx,
181 expression,
182 GRN_TEXT_VALUE(&cache_key),
183 (unsigned int)GRN_TEXT_LEN(&cache_key));
184 if (snip_ptr) {
185 snip = GRN_PTR_VALUE(snip_ptr);
186 } else {
187 snip_ptr =
188 grn_expr_get_or_add_var(ctx,
189 expression,
190 GRN_TEXT_VALUE(&cache_key),
191 (unsigned int)GRN_TEXT_LEN(&cache_key));
192 if (ctx->rc != GRN_SUCCESS) {
193 goto exit;
194 }
195 GRN_OBJ_FIN(ctx, snip_ptr);
196 GRN_PTR_INIT(snip_ptr, GRN_OBJ_OWN, GRN_DB_OBJECT);
197 }
198 }
199
200 grn_obj *end_arg = args[nargs - 1];
201 uint32_t width = 200;
202 uint32_t max_n_results = 3;
203 bool skip_leading_spaces = true;
204 bool html_escape = false;
205 grn_obj *prefix = NULL;
206 grn_obj *suffix = NULL;
207 grn_obj *normalizer_name = NULL;
208 grn_obj *normalizers = NULL;
209 grn_obj *default_open_tag = NULL;
210 grn_obj *default_close_tag = NULL;
211 int n_args_without_option = nargs;
212 grn_obj *default_return_value = NULL;
213 grn_obj *delimiter_regexp = NULL;
214
215 if (end_arg->header.type == GRN_TABLE_HASH_KEY) {
216 grn_obj *options = end_arg;
217 n_args_without_option--;
218 grn_rc rc = grn_proc_options_parse(ctx,
219 options,
220 tag,
221 "width",
222 GRN_PROC_OPTION_VALUE_UINT32,
223 &width,
224 "max_n_results",
225 GRN_PROC_OPTION_VALUE_UINT32,
226 &max_n_results,
227 "skip_leading_spaces",
228 GRN_PROC_OPTION_VALUE_BOOL,
229 &skip_leading_spaces,
230 "html_escape",
231 GRN_PROC_OPTION_VALUE_BOOL,
232 &html_escape,
233 "prefix",
234 GRN_PROC_OPTION_VALUE_RAW,
235 &prefix,
236 "suffix",
237 GRN_PROC_OPTION_VALUE_RAW,
238 &suffix,
239 "normalizer",
240 GRN_PROC_OPTION_VALUE_RAW,
241 &normalizer_name,
242 "normalizers",
243 GRN_PROC_OPTION_VALUE_RAW,
244 &normalizers,
245 "default_open_tag",
246 GRN_PROC_OPTION_VALUE_RAW,
247 &default_open_tag,
248 "default_close_tag",
249 GRN_PROC_OPTION_VALUE_RAW,
250 &default_close_tag,
251 "default",
252 GRN_PROC_OPTION_VALUE_RAW,
253 &default_return_value,
254 "delimiter_regexp",
255 GRN_PROC_OPTION_VALUE_RAW,
256 &delimiter_regexp,
257 NULL);
258 if (rc != GRN_SUCCESS) {
259 goto exit;
260 }
261 }
262
263 if (default_return_value && default_return_value->header.type == GRN_PTR) {
264 default_return_value = GRN_PTR_VALUE(default_return_value);
265 }
266
267 if (!snip) {
268 int flags = 0;
269 if (skip_leading_spaces) {
270 flags |= GRN_SNIP_SKIP_LEADING_SPACES;
271 }
272 grn_snip_mapping *mapping = NULL;
273 if (html_escape) {
274 mapping = GRN_SNIP_MAPPING_HTML_ESCAPE;
275 }
276 snip = grn_snip_open(
277 ctx,
278 flags,
279 width,
280 max_n_results,
281 default_open_tag ? GRN_TEXT_VALUE(default_open_tag) : NULL,
282 default_open_tag ? (unsigned int)GRN_TEXT_LEN(default_open_tag) : 0,
283 default_close_tag ? GRN_TEXT_VALUE(default_close_tag) : NULL,
284 default_close_tag ? (unsigned int)GRN_TEXT_LEN(default_close_tag) : 0,
285 mapping);
286 if (!snip) {
287 goto exit;
288 }
289 if (normalizers && grn_obj_is_text_family_bulk(ctx, normalizers) &&
290 GRN_TEXT_LEN(normalizers) > 0) {
291 grn_rc rc = grn_snip_set_normalizers(ctx, snip, normalizers);
292 if (rc != GRN_SUCCESS) {
293 GRN_PLUGIN_ERROR(ctx,
294 rc,
295 "%s invalid normalizers: <%.*s>: %s",
296 tag,
297 (int)GRN_TEXT_LEN(normalizers),
298 GRN_TEXT_VALUE(normalizers),
299 ctx->errbuf);
300 goto exit;
301 }
302 } else if (!normalizer_name) {
303 grn_snip_set_normalizer(ctx, snip, GRN_NORMALIZER_AUTO);
304 } else if (GRN_TEXT_LEN(normalizer_name) > 0) {
305 grn_obj *normalizer;
306 normalizer = grn_ctx_get(ctx,
307 GRN_TEXT_VALUE(normalizer_name),
308 (int)GRN_TEXT_LEN(normalizer_name));
309 if (!grn_obj_is_normalizer_proc(ctx, normalizer)) {
310 grn_obj inspected;
311 GRN_TEXT_INIT(&inspected, 0);
312 grn_inspect(ctx, &inspected, normalizer);
313 GRN_PLUGIN_ERROR(ctx,
314 GRN_INVALID_ARGUMENT,
315 "%s not normalizer: <%.*s>",
316 tag,
317 (int)GRN_TEXT_LEN(&inspected),
318 GRN_TEXT_VALUE(&inspected));
319 GRN_OBJ_FIN(ctx, &inspected);
320 grn_obj_unlink(ctx, normalizer);
321 goto exit;
322 }
323 grn_snip_set_normalizer(ctx, snip, normalizer);
324 grn_obj_unlink(ctx, normalizer);
325 }
326 if ((!default_open_tag || GRN_TEXT_LEN(default_open_tag) == 0) &&
327 (!default_close_tag || GRN_TEXT_LEN(default_close_tag) == 0)) {
328 unsigned int i;
329 unsigned int n_keyword_sets =
330 (unsigned int)((n_args_without_option - N_REQUIRED_ARGS) /
331 KEYWORD_SET_SIZE);
332 grn_obj **keyword_set_args = args + N_REQUIRED_ARGS;
333 for (i = 0; i < n_keyword_sets; i++) {
334 unsigned int index = i * KEYWORD_SET_SIZE;
335 grn_rc rc = grn_snip_add_cond(
336 ctx,
337 snip,
338 GRN_TEXT_VALUE(keyword_set_args[index]),
339 (unsigned int)GRN_TEXT_LEN(keyword_set_args[index]),
340 GRN_TEXT_VALUE(keyword_set_args[index + 1]),
341 (unsigned int)GRN_TEXT_LEN(keyword_set_args[index + 1]),
342 GRN_TEXT_VALUE(keyword_set_args[index + 2]),
343 (unsigned int)GRN_TEXT_LEN(keyword_set_args[index + 2]));
344 if (rc != GRN_SUCCESS) {
345 goto exit;
346 }
347 }
348 } else {
349 unsigned int n_keywords =
350 (unsigned int)(n_args_without_option - N_REQUIRED_ARGS);
351 if (n_keywords == 0) {
352 grn_obj *condition = grn_expr_get_condition(ctx, expression);
353 for (; condition; condition = grn_expr_get_parent(ctx, condition)) {
354 grn_expr_snip_add_conditions(ctx,
355 condition,
356 snip,
357 0,
358 NULL,
359 NULL,
360 NULL,
361 NULL);
362 }
363 } else {
364 unsigned int i;
365 grn_obj **keyword_args = args + N_REQUIRED_ARGS;
366 for (i = 0; i < n_keywords; i++) {
367 grn_rc rc =
368 grn_snip_add_cond(ctx,
369 snip,
370 GRN_TEXT_VALUE(keyword_args[i]),
371 (unsigned int)GRN_TEXT_LEN(keyword_args[i]),
372 NULL,
373 0,
374 NULL,
375 0);
376 if (rc != GRN_SUCCESS) {
377 goto exit;
378 }
379 }
380 }
381 }
382 if (grn_obj_is_text_family_bulk(ctx, delimiter_regexp)) {
383 grn_snip_set_delimiter_regexp(ctx,
384 snip,
385 GRN_TEXT_VALUE(delimiter_regexp),
386 (int)GRN_TEXT_LEN(delimiter_regexp));
387 if (ctx->rc != GRN_SUCCESS) {
388 goto exit;
389 }
390 }
391 if (snip_ptr) {
392 GRN_PTR_SET(ctx, snip_ptr, snip);
393 }
394 }
395
396 snippets = snippet_exec(ctx,
397 snip,
398 target,
399 default_return_value,
400 user_data,
401 prefix ? GRN_TEXT_VALUE(prefix) : NULL,
402 prefix ? GRN_TEXT_LEN(prefix) : 0,
403 suffix ? GRN_TEXT_VALUE(suffix) : NULL,
404 suffix ? GRN_TEXT_LEN(suffix) : 0);
405 #undef KEYWORD_SET_SIZE
406 #undef N_REQUIRED_ARGS
407
408 exit:
409 GRN_OBJ_FIN(ctx, &cache_key);
410 if (!snippets) {
411 snippets = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_VOID, 0);
412 }
413
414 return snippets;
415 }
416
417 void
418 grn_proc_init_snippet(grn_ctx *ctx)
419 {
420 grn_proc_create(ctx,
421 "snippet",
422 -1,
423 GRN_PROC_FUNCTION,
424 func_snippet,
425 NULL,
426 NULL,
427 0,
428 NULL);
429 }
430
431 static grn_obj *
432 func_snippet_html(grn_ctx *ctx,
433 int nargs,
434 grn_obj **args,
435 grn_user_data *user_data)
436 {
437 grn_obj *snippets = NULL;
438
439 if (nargs > 0) {
440 grn_obj *target = args[0];
441 grn_obj *expression = NULL;
442 grn_obj *condition = NULL;
443 grn_obj *snip = NULL;
444 grn_obj *default_return_value = NULL;
445 int flags = GRN_SNIP_SKIP_LEADING_SPACES;
446 unsigned int width = 200;
447 unsigned int max_n_results = 3;
448 const char *open_tag = "<span class=\"keyword\">";
449 const char *close_tag = "</span>";
450 grn_snip_mapping *mapping = GRN_SNIP_MAPPING_HTML_ESCAPE;
451
452 if (nargs > 1 && args[1]->header.type == GRN_TABLE_HASH_KEY) {
453 grn_obj *options = args[1];
454 void *key;
455 uint32_t key_size;
456 grn_obj *value;
457
458 GRN_HASH_EACH_BEGIN(ctx, (grn_hash *)options, cursor, id)
459 {
460 grn_raw_string option_name;
461
462 grn_hash_cursor_get_key_value(ctx,
463 cursor,
464 &key,
465 &key_size,
466 (void **)&value);
467 option_name.value = key;
468 option_name.length = key_size;
469 if (GRN_RAW_STRING_EQUAL_CSTRING(option_name, "default")) {
470 if (value->header.type == GRN_PTR) {
471 default_return_value = GRN_PTR_VALUE(value);
472 } else {
473 default_return_value = value;
474 }
475 } else {
476 GRN_PLUGIN_ERROR(ctx,
477 GRN_INVALID_ARGUMENT,
478 "snippet_html(): invalid option name: <%.*s>",
479 key_size,
480 (char *)key);
481 break;
482 }
483 }
484 GRN_HASH_EACH_END(ctx, cursor);
485 if (ctx->rc != GRN_SUCCESS) {
486 goto exit;
487 }
488 }
489
490 grn_proc_get_info(ctx, user_data, NULL, NULL, &expression);
491 condition = grn_expr_get_condition(ctx, expression);
492 if (condition) {
493 grn_obj *snip_ptr;
494 snip_ptr = grn_expr_get_var(ctx,
495 expression,
496 GRN_FUNC_SNIPPET_HTML_CACHE_NAME,
497 strlen(GRN_FUNC_SNIPPET_HTML_CACHE_NAME));
498 if (snip_ptr) {
499 snip = GRN_PTR_VALUE(snip_ptr);
500 } else {
501 snip_ptr =
502 grn_expr_get_or_add_var(ctx,
503 expression,
504 GRN_FUNC_SNIPPET_HTML_CACHE_NAME,
505 strlen(GRN_FUNC_SNIPPET_HTML_CACHE_NAME));
506 GRN_OBJ_FIN(ctx, snip_ptr);
507 GRN_PTR_INIT(snip_ptr, GRN_OBJ_OWN, GRN_DB_OBJECT);
508
509 snip = grn_snip_open(ctx,
510 flags,
511 width,
512 max_n_results,
513 open_tag,
514 (unsigned int)strlen(open_tag),
515 close_tag,
516 (unsigned int)strlen(close_tag),
517 mapping);
518 if (snip) {
519 grn_snip_set_normalizer(ctx, snip, GRN_NORMALIZER_AUTO);
520 for (; condition; condition = grn_expr_get_parent(ctx, condition)) {
521 grn_expr_snip_add_conditions(ctx,
522 condition,
523 snip,
524 0,
525 NULL,
526 NULL,
527 NULL,
528 NULL);
529 }
530 GRN_PTR_SET(ctx, snip_ptr, snip);
531 }
532 }
533 }
534
535 if (snip) {
536 snippets = snippet_exec(ctx,
537 snip,
538 target,
539 default_return_value,
540 user_data,
541 NULL,
542 0,
543 NULL,
544 0);
545 }
546 }
547
548 exit:
549 if (!snippets) {
550 snippets = grn_plugin_proc_alloc(ctx, user_data, GRN_DB_VOID, 0);
551 }
552
553 return snippets;
554 }
555
556 void
557 grn_proc_init_snippet_html(grn_ctx *ctx)
558 {
559 grn_proc_create(ctx,
560 "snippet_html",
561 -1,
562 GRN_PROC_FUNCTION,
563 func_snippet_html,
564 NULL,
565 NULL,
566 0,
567 NULL);
568 }