556
557
558
559
560
561
562
563
564
565
566
567
568
569
|
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
|
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
|
sqlite3_create_function(db, "title", 3, enc, 0,
search_title_sqlfunc, 0, 0);
sqlite3_create_function(db, "body", 3, enc, 0,
search_body_sqlfunc, 0, 0);
sqlite3_create_function(db, "urlencode", 1, enc, 0,
search_urlencode_sqlfunc, 0, 0);
}
/*
** Search scope abbreviations and names.
*/
static const struct {
const char *z;
const char *zNm;
unsigned m;
} aScope[] = {
{ "all", "All", SRCH_ALL },
{ "c", "Check-ins", SRCH_CKIN },
{ "d", "Docs", SRCH_DOC },
{ "t", "Tickets", SRCH_TKT },
{ "w", "Wiki", SRCH_WIKI },
{ "e", "Tech Notes", SRCH_TECHNOTE },
{ "f", "Forum", SRCH_FORUM },
};
/*
** Testing the search function.
**
** COMMAND: search*
**
** Usage: %fossil search [-a|-all] [-n|-limit #] [-W|-width #] pattern...
|
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
|
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
|
+
+
+
+
+
|
** option can be used to output all matches, regardless of their search
** score. The -limit option can be used to limit the number of entries
** returned. The -width option can be used to set the output width used
** when printing matches.
**
** Options:
** -a|--all Output all matches, not just best matches
** --debug Show additional debug content on --fts search
** --fts Use the full-text search mechanism (testing only)
** -n|--limit N Limit output to N matches
** --scope SCOPE Scope of search. Valid for --fts only. One or
** more of: all, c, d, e, f, t, w. Defaults to all.
** -W|--width WIDTH Set display width to WIDTH columns, 0 for
** unlimited. Defaults the terminal's width.
*/
void search_cmd(void){
Blob pattern;
int i;
Blob sql = empty_blob;
Stmt q;
int iBest;
char fAll = NULL != find_option("all", "a", 0);
const char *zLimit = find_option("limit","n",1);
const char *zWidth = find_option("width","W",1);
const char *zScope = find_option("scope",0,1);
int bDebug = find_option("debug",0,0)!=0;
int nLimit = zLimit ? atoi(zLimit) : -1000;
int width;
int bFts = find_option("fts",0,0)!=0;
if( zWidth ){
width = atoi(zWidth);
if( (width!=0) && (width<=20) ){
|
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
|
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
|
+
+
+
-
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
-
+
+
+
+
+
+
+
+
+
-
-
+
+
+
+
+
+
+
+
+
+
|
blob_init(&pattern, g.argv[2], -1);
for(i=3; i<g.argc; i++){
blob_appendf(&pattern, " %s", g.argv[i]);
}
if( bFts ){
/* Search using FTS */
Blob com;
Blob snip;
const char *zPattern = blob_str(&pattern);
int srchFlags;
if( zScope==0 ){
int srchFlags = SRCH_ALL;
srchFlags = SRCH_ALL;
}else{
srchFlags = 0;
for(i=0; zScope[i]; i++){
switch( zScope[i] ){
case 'a': srchFlags = SRCH_ALL; break;
case 'c': srchFlags |= SRCH_CKIN; break;
case 'd': srchFlags |= SRCH_DOC; break;
case 'e': srchFlags |= SRCH_TECHNOTE; break;
case 'f': srchFlags |= SRCH_FORUM; break;
case 't': srchFlags |= SRCH_TKT; break;
case 'w': srchFlags |= SRCH_WIKI; break;
}
}
}
search_sql_setup(g.db);
add_content_sql_commands(g.db);
db_multi_exec(
"CREATE TEMP TABLE x(label,url,score,id,date,snip);"
);
if( !search_index_exists() ){
search_fullscan(zPattern, srchFlags); /* Full-scan search */
}else{
search_update_index(srchFlags); /* Update the index */
search_indexed(zPattern, srchFlags); /* Indexed search */
}
db_prepare(&q, "SELECT snip, label, score, id, substr(date,1,10)"
db_prepare(&q, "SELECT snip, label, score, id, date"
" FROM x"
" ORDER BY score DESC, date DESC;");
blob_init(&com, 0, 0);
blob_init(&snip, 0, 0);
if( width<0 ) width = 80;
while( db_step(&q)==SQLITE_ROW ){
const char *zSnippet = db_column_text(&q, 0);
const char *zLabel = db_column_text(&q, 1);
const char *zDate = db_column_text(&q, 4);
const char *zScore = db_column_text(&q, 2);
const char *zId = db_column_text(&q, 3);
blob_appendf(&snip, "%s", zSnippet);
for(i=0; i<snip.nUsed; i++){
if( snip.aData[i]=='\n' ){
if( i>0 && snip.aData[i-1]=='\r' ) snip.aData[i-1] = ' ';
snip.aData[i] = ' ';
}
}
blob_appendf(&com, "%s\n%s\n%s score=%s id=%d", zLabel, zSnippet,
zDate, zScore, zId);
blob_appendf(&com, "%s\n%s\n%s", zLabel, blob_str(&snip), zDate);
if( bDebug ){
blob_appendf(&com," score: %s id: %s", zScore, zId);
}
comment_print(blob_str(&com), 0, 5, width,
COMMENT_PRINT_TRIM_CRLF |
COMMENT_PRINT_WORD_BREAK |
COMMENT_PRINT_TRIM_SPACE);
blob_reset(&com);
blob_reset(&snip);
if( nLimit>=1 ){
nLimit--;
if( nLimit==0 ) break;
}
}
db_finalize(&q);
blob_reset(&pattern);
}else{
/* Legacy timeline search (the default) */
(void)search_init(blob_str(&pattern),"*","*","...",SRCHFLG_STATIC);
blob_reset(&pattern);
|