Fossil SCM

fossil-scm / src / search.c
Source Blame History 2942 lines
232d10b… drh 1 /*
c19f34c… drh 2 ** Copyright (c) 2009 D. Richard Hipp
232d10b… drh 3 **
232d10b… drh 4 ** This program is free software; you can redistribute it and/or
c06edd2… drh 5 ** modify it under the terms of the Simplified BSD License (also
c06edd2… drh 6 ** known as the "2-Clause License" or "FreeBSD License".)
c06edd2… drh 7
232d10b… drh 8 ** This program is distributed in the hope that it will be useful,
c06edd2… drh 9 ** but without any warranty; without even the implied warranty of
c06edd2… drh 10 ** merchantability or fitness for a particular purpose.
232d10b… drh 11 **
232d10b… drh 12 ** Author contact information:
232d10b… drh 13 ** [email protected]
232d10b… drh 14 ** http://www.hwaci.com/drh/
232d10b… drh 15 **
232d10b… drh 16 *******************************************************************************
232d10b… drh 17 **
375bc71… drh 18 ** This file contains code to implement a search functions
4d35160… drh 19 ** against timeline comments, check-in content, wiki pages, tickets,
4d35160… drh 20 ** and/or forum posts.
375bc71… drh 21 **
375bc71… drh 22 ** The search can be either a per-query "grep"-like search that scans
17eb363… stephan 23 ** the entire corpus. Or it can use the FTS5 search engine of SQLite.
99a319b… wyoung 24 ** The choice is an administrator configuration option.
375bc71… drh 25 **
375bc71… drh 26 ** The first option is referred to as "full-scan search". The second
375bc71… drh 27 ** option is called "indexed search".
375bc71… drh 28 **
375bc71… drh 29 ** The code in this file is ordered approximately as follows:
85f2656… drh 30 **
375bc71… drh 31 ** (1) The full-scan search engine
375bc71… drh 32 ** (2) The indexed search engine
cd82235… drh 33 ** (3) Higher level interfaces that use either (1) or (b2) according
375bc71… drh 34 ** to the current search configuration settings
232d10b… drh 35 */
232d10b… drh 36 #include "config.h"
232d10b… drh 37 #include "search.h"
232d10b… drh 38 #include <assert.h>
232d10b… drh 39
232d10b… drh 40 #if INTERFACE
128e95e… drh 41
375bc71… drh 42 /* Maximum number of search terms for full-scan search */
128e95e… drh 43 #define SEARCH_MAX_TERM 8
128e95e… drh 44
232d10b… drh 45 /*
375bc71… drh 46 ** A compiled search pattern used for full-scan search.
232d10b… drh 47 */
232d10b… drh 48 struct Search {
85f2656… drh 49 int nTerm; /* Number of search terms */
85f2656… drh 50 struct srchTerm { /* For each search term */
85f2656… drh 51 char *z; /* Text */
85f2656… drh 52 int n; /* length */
128e95e… drh 53 } a[SEARCH_MAX_TERM];
128e95e… drh 54 /* Snippet controls */
046d743… drh 55 char *zPattern; /* The search pattern */
5260fbf… jan.nijtmans 56 char *zMarkBegin; /* Start of a match */
128e95e… drh 57 char *zMarkEnd; /* End of a match */
128e95e… drh 58 char *zMarkGap; /* A gap between two matches */
128e95e… drh 59 unsigned fSrchFlg; /* Flags */
a00a140… drh 60 int iScore; /* Score of the last match attempt */
a00a140… drh 61 Blob snip; /* Snippet for the most recent match */
232d10b… drh 62 };
128e95e… drh 63
046d743… drh 64 #define SRCHFLG_HTML 0x01 /* Escape snippet text for HTML */
046d743… drh 65 #define SRCHFLG_STATIC 0x04 /* The static gSearch object */
128e95e… drh 66
232d10b… drh 67 #endif
046d743… drh 68
046d743… drh 69 /*
046d743… drh 70 ** There is a single global Search object:
046d743… drh 71 */
046d743… drh 72 static Search gSearch;
128e95e… drh 73
232d10b… drh 74
232d10b… drh 75 /*
232d10b… drh 76 ** Theses characters constitute a word boundary
232d10b… drh 77 */
232d10b… drh 78 static const char isBoundary[] = {
232d10b… drh 79 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
232d10b… drh 80 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
232d10b… drh 81 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
232d10b… drh 82 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1,
232d10b… drh 83 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 84 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
232d10b… drh 85 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 86 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1,
232d10b… drh 87 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 88 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 89 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 90 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 91 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 92 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 93 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 94 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
232d10b… drh 95 };
128e95e… drh 96 #define ISALNUM(x) (!isBoundary[(x)&0xff])
128e95e… drh 97
046d743… drh 98
046d743… drh 99 /*
375bc71… drh 100 ** Destroy a full-scan search context.
046d743… drh 101 */
046d743… drh 102 void search_end(Search *p){
046d743… drh 103 if( p ){
046d743… drh 104 fossil_free(p->zPattern);
046d743… drh 105 fossil_free(p->zMarkBegin);
046d743… drh 106 fossil_free(p->zMarkEnd);
046d743… drh 107 fossil_free(p->zMarkGap);
a00a140… drh 108 if( p->iScore ) blob_reset(&p->snip);
046d743… drh 109 memset(p, 0, sizeof(*p));
046d743… drh 110 if( p!=&gSearch ) fossil_free(p);
046d743… drh 111 }
046d743… drh 112 }
046d743… drh 113
128e95e… drh 114 /*
375bc71… drh 115 ** Compile a full-scan search pattern
128e95e… drh 116 */
e06e65b… drh 117 static Search *search_init(
046d743… drh 118 const char *zPattern, /* The search pattern */
5260fbf… jan.nijtmans 119 const char *zMarkBegin, /* Start of a match */
046d743… drh 120 const char *zMarkEnd, /* End of a match */
046d743… drh 121 const char *zMarkGap, /* A gap between two matches */
046d743… drh 122 unsigned fSrchFlg /* Flags */
046d743… drh 123 ){
128e95e… drh 124 Search *p;
128e95e… drh 125 char *z;
128e95e… drh 126 int i;
128e95e… drh 127
046d743… drh 128 if( fSrchFlg & SRCHFLG_STATIC ){
046d743… drh 129 p = &gSearch;
046d743… drh 130 search_end(p);
046d743… drh 131 }else{
046d743… drh 132 p = fossil_malloc(sizeof(*p));
046d743… drh 133 memset(p, 0, sizeof(*p));
046d743… drh 134 }
20adff9… stephan 135 p->zPattern = z = mprintf("%s",zPattern);
20adff9… stephan 136 p->zMarkBegin = mprintf("%s",zMarkBegin);
20adff9… stephan 137 p->zMarkEnd = mprintf("%s",zMarkEnd);
20adff9… stephan 138 p->zMarkGap = mprintf("%s",zMarkGap);
046d743… drh 139 p->fSrchFlg = fSrchFlg;
a00a140… drh 140 blob_init(&p->snip, 0, 0);
128e95e… drh 141 while( *z && p->nTerm<SEARCH_MAX_TERM ){
128e95e… drh 142 while( *z && !ISALNUM(*z) ){ z++; }
128e95e… drh 143 if( *z==0 ) break;
128e95e… drh 144 p->a[p->nTerm].z = z;
128e95e… drh 145 for(i=1; ISALNUM(z[i]); i++){}
128e95e… drh 146 p->a[p->nTerm].n = i;
128e95e… drh 147 z += i;
128e95e… drh 148 p->nTerm++;
128e95e… drh 149 }
128e95e… drh 150 return p;
128e95e… drh 151 }
128e95e… drh 152
232d10b… drh 153
232d10b… drh 154 /*
128e95e… drh 155 ** Append n bytes of text to snippet zTxt. Encode the text appropriately.
128e95e… drh 156 */
128e95e… drh 157 static void snippet_text_append(
128e95e… drh 158 Search *p, /* The search context */
128e95e… drh 159 Blob *pSnip, /* Append to this snippet */
128e95e… drh 160 const char *zTxt, /* Text to append */
128e95e… drh 161 int n /* How many bytes to append */
128e95e… drh 162 ){
046d743… drh 163 if( n>0 ){
046d743… drh 164 if( p->fSrchFlg & SRCHFLG_HTML ){
046d743… drh 165 blob_appendf(pSnip, "%#h", n, zTxt);
046d743… drh 166 }else{
046d743… drh 167 blob_append(pSnip, zTxt, n);
046d743… drh 168 }
128e95e… drh 169 }
128e95e… drh 170 }
128e95e… drh 171
375bc71… drh 172 /* This the core search engine for full-scan search.
375bc71… drh 173 **
128e95e… drh 174 ** Compare a search pattern against one or more input strings which
a00a140… drh 175 ** collectively comprise a document. Return a match score. Any
a00a140… drh 176 ** postive value means there was a match. Zero means that one or
a00a140… drh 177 ** more terms are missing.
a00a140… drh 178 **
a00a140… drh 179 ** The score and a snippet are record for future use.
232d10b… drh 180 **
232d10b… drh 181 ** Scoring:
232d10b… drh 182 ** * All terms must match at least once or the score is zero
128e95e… drh 183 ** * One point for each matching term
128e95e… drh 184 ** * Extra points if consecutive words of the pattern are consecutive
232d10b… drh 185 ** in the document
232d10b… drh 186 */
a00a140… drh 187 static int search_match(
128e95e… drh 188 Search *p, /* Search pattern and flags */
128e95e… drh 189 int nDoc, /* Number of strings in this document */
a00a140… drh 190 const char **azDoc /* Text of each string */
128e95e… drh 191 ){
128e95e… drh 192 int score; /* Final score */
128e95e… drh 193 int i; /* Offset into current document */
128e95e… drh 194 int ii; /* Loop counter */
128e95e… drh 195 int j; /* Loop over search terms */
128e95e… drh 196 int k; /* Loop over prior terms */
128e95e… drh 197 int iWord = 0; /* Current word number */
128e95e… drh 198 int iDoc; /* Current document number */
128e95e… drh 199 int wantGap = 0; /* True if a zMarkGap is wanted */
128e95e… drh 200 const char *zDoc; /* Current document text */
128e95e… drh 201 const int CTX = 50; /* Amount of snippet context */
128e95e… drh 202 int anMatch[SEARCH_MAX_TERM]; /* Number of terms in best match */
128e95e… drh 203 int aiBestDoc[SEARCH_MAX_TERM]; /* Document containing best match */
128e95e… drh 204 int aiBestOfst[SEARCH_MAX_TERM]; /* Byte offset to start of best match */
128e95e… drh 205 int aiLastDoc[SEARCH_MAX_TERM]; /* Document containing most recent match */
128e95e… drh 206 int aiLastOfst[SEARCH_MAX_TERM]; /* Byte offset to the most recent match */
128e95e… drh 207 int aiWordIdx[SEARCH_MAX_TERM]; /* Word index of most recent match */
128e95e… drh 208
128e95e… drh 209 memset(anMatch, 0, sizeof(anMatch));
128e95e… drh 210 memset(aiWordIdx, 0xff, sizeof(aiWordIdx));
128e95e… drh 211 for(iDoc=0; iDoc<nDoc; iDoc++){
128e95e… drh 212 zDoc = azDoc[iDoc];
85f2656… drh 213 if( zDoc==0 ) continue;
128e95e… drh 214 iWord++;
85f2656… drh 215 for(i=0; zDoc[i]; i++){
128e95e… drh 216 if( !ISALNUM(zDoc[i]) ) continue;
128e95e… drh 217 iWord++;
128e95e… drh 218 for(j=0; j<p->nTerm; j++){
128e95e… drh 219 int n = p->a[j].n;
128e95e… drh 220 if( sqlite3_strnicmp(p->a[j].z, &zDoc[i], n)==0
128e95e… drh 221 && (!ISALNUM(zDoc[i+n]) || p->a[j].z[n]=='*')
128e95e… drh 222 ){
128e95e… drh 223 aiWordIdx[j] = iWord;
128e95e… drh 224 aiLastDoc[j] = iDoc;
128e95e… drh 225 aiLastOfst[j] = i;
128e95e… drh 226 for(k=1; j-k>=0 && anMatch[j-k] && aiWordIdx[j-k]==iWord-k; k++){}
128e95e… drh 227 for(ii=0; ii<k; ii++){
128e95e… drh 228 if( anMatch[j-ii]<k ){
0f96ffb… drh 229 anMatch[j-ii] = k*(nDoc-iDoc);
128e95e… drh 230 aiBestDoc[j-ii] = aiLastDoc[j-ii];
128e95e… drh 231 aiBestOfst[j-ii] = aiLastOfst[j-ii];
128e95e… drh 232 }
128e95e… drh 233 }
128e95e… drh 234 break;
128e95e… drh 235 }
128e95e… drh 236 }
128e95e… drh 237 while( ISALNUM(zDoc[i]) ){ i++; }
41e5da0… drh 238 if( zDoc[i]==0 ) break;
128e95e… drh 239 }
128e95e… drh 240 }
128e95e… drh 241
128e95e… drh 242 /* Finished search all documents.
5260fbf… jan.nijtmans 243 ** Every term must be seen or else the score is zero
128e95e… drh 244 */
128e95e… drh 245 score = 1;
128e95e… drh 246 for(j=0; j<p->nTerm; j++) score *= anMatch[j];
a00a140… drh 247 blob_reset(&p->snip);
a00a140… drh 248 p->iScore = score;
a00a140… drh 249 if( score==0 ) return score;
128e95e… drh 250
128e95e… drh 251
128e95e… drh 252 /* Prepare a snippet that describes the matching text.
128e95e… drh 253 */
128e95e… drh 254 while(1){
128e95e… drh 255 int iOfst;
128e95e… drh 256 int iTail;
128e95e… drh 257 int iBest;
128e95e… drh 258 for(ii=0; ii<p->nTerm && anMatch[ii]==0; ii++){}
128e95e… drh 259 if( ii>=p->nTerm ) break; /* This is where the loop exits */
128e95e… drh 260 iBest = ii;
128e95e… drh 261 iDoc = aiBestDoc[ii];
128e95e… drh 262 iOfst = aiBestOfst[ii];
128e95e… drh 263 for(; ii<p->nTerm; ii++){
128e95e… drh 264 if( anMatch[ii]==0 ) continue;
128e95e… drh 265 if( aiBestDoc[ii]>iDoc ) continue;
128e95e… drh 266 if( aiBestOfst[ii]>iOfst ) continue;
128e95e… drh 267 iDoc = aiBestDoc[ii];
128e95e… drh 268 iOfst = aiBestOfst[ii];
128e95e… drh 269 iBest = ii;
128e95e… drh 270 }
128e95e… drh 271 iTail = iOfst + p->a[iBest].n;
128e95e… drh 272 anMatch[iBest] = 0;
128e95e… drh 273 for(ii=0; ii<p->nTerm; ii++){
128e95e… drh 274 if( anMatch[ii]==0 ) continue;
128e95e… drh 275 if( aiBestDoc[ii]!=iDoc ) continue;
128e95e… drh 276 if( aiBestOfst[ii]<=iTail+CTX*2 ){
128e95e… drh 277 if( iTail<aiBestOfst[ii]+p->a[ii].n ){
128e95e… drh 278 iTail = aiBestOfst[ii]+p->a[ii].n;
128e95e… drh 279 }
128e95e… drh 280 anMatch[ii] = 0;
128e95e… drh 281 ii = -1;
128e95e… drh 282 continue;
128e95e… drh 283 }
128e95e… drh 284 }
128e95e… drh 285 zDoc = azDoc[iDoc];
128e95e… drh 286 iOfst -= CTX;
128e95e… drh 287 if( iOfst<0 ) iOfst = 0;
128e95e… drh 288 while( iOfst>0 && ISALNUM(zDoc[iOfst-1]) ) iOfst--;
128e95e… drh 289 while( zDoc[iOfst] && !ISALNUM(zDoc[iOfst]) ) iOfst++;
128e95e… drh 290 for(ii=0; ii<CTX && zDoc[iTail]; ii++, iTail++){}
128e95e… drh 291 while( ISALNUM(zDoc[iTail]) ) iTail++;
a00a140… drh 292 if( iOfst>0 || wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
128e95e… drh 293 wantGap = zDoc[iTail]!=0;
128e95e… drh 294 zDoc += iOfst;
128e95e… drh 295 iTail -= iOfst;
128e95e… drh 296
128e95e… drh 297 /* Add a snippet segment using characters iOfst..iOfst+iTail from zDoc */
128e95e… drh 298 for(i=0; i<iTail; i++){
128e95e… drh 299 if( !ISALNUM(zDoc[i]) ) continue;
85f2656… drh 300 for(j=0; j<p->nTerm; j++){
85f2656… drh 301 int n = p->a[j].n;
128e95e… drh 302 if( sqlite3_strnicmp(p->a[j].z, &zDoc[i], n)==0
128e95e… drh 303 && (!ISALNUM(zDoc[i+n]) || p->a[j].z[n]=='*')
128e95e… drh 304 ){
a00a140… drh 305 snippet_text_append(p, &p->snip, zDoc, i);
128e95e… drh 306 zDoc += i;
128e95e… drh 307 iTail -= i;
a00a140… drh 308 blob_append(&p->snip, p->zMarkBegin, -1);
128e95e… drh 309 if( p->a[j].z[n]=='*' ){
128e95e… drh 310 while( ISALNUM(zDoc[n]) ) n++;
128e95e… drh 311 }
a00a140… drh 312 snippet_text_append(p, &p->snip, zDoc, n);
128e95e… drh 313 zDoc += n;
128e95e… drh 314 iTail -= n;
a00a140… drh 315 blob_append(&p->snip, p->zMarkEnd, -1);
128e95e… drh 316 i = -1;
85f2656… drh 317 break;
128e95e… drh 318 } /* end-if */
128e95e… drh 319 } /* end for(j) */
128e95e… drh 320 if( j<p->nTerm ){
128e95e… drh 321 while( ISALNUM(zDoc[i]) && i<iTail ){ i++; }
128e95e… drh 322 }
128e95e… drh 323 } /* end for(i) */
a00a140… drh 324 snippet_text_append(p, &p->snip, zDoc, iTail);
128e95e… drh 325 }
a00a140… drh 326 if( wantGap ) blob_append(&p->snip, p->zMarkGap, -1);
232d10b… drh 327 return score;
232d10b… drh 328 }
232d10b… drh 329
232d10b… drh 330 /*
a00a140… drh 331 ** COMMAND: test-match
a00a140… drh 332 **
8bfd995… rberteig 333 ** Usage: %fossil test-match SEARCHSTRING FILE1 FILE2 ...
375bc71… drh 334 **
375bc71… drh 335 ** Run the full-scan search algorithm using SEARCHSTRING against
375bc71… drh 336 ** the text of the files listed. Output matches and snippets.
4d35160… drh 337 **
4d35160… drh 338 ** Options:
4d35160… drh 339 ** --begin TEXT Text to insert before each match
4d35160… drh 340 ** --end TEXT Text to insert after each match
4d35160… drh 341 ** --gap TEXT Text to indicate elided content
4d35160… drh 342 ** --html Input is HTML
4d35160… drh 343 ** --static Use the static Search object
128e95e… drh 344 */
a00a140… drh 345 void test_match_cmd(void){
128e95e… drh 346 Search *p;
128e95e… drh 347 int i;
128e95e… drh 348 Blob x;
128e95e… drh 349 int score;
128e95e… drh 350 char *zDoc;
046d743… drh 351 int flg = 0;
046d743… drh 352 char *zBegin = (char*)find_option("begin",0,1);
046d743… drh 353 char *zEnd = (char*)find_option("end",0,1);
046d743… drh 354 char *zGap = (char*)find_option("gap",0,1);
046d743… drh 355 if( find_option("html",0,0)!=0 ) flg |= SRCHFLG_HTML;
046d743… drh 356 if( find_option("static",0,0)!=0 ) flg |= SRCHFLG_STATIC;
046d743… drh 357 verify_all_options();
128e95e… drh 358 if( g.argc<4 ) usage("SEARCHSTRING FILE1...");
046d743… drh 359 if( zBegin==0 ) zBegin = "[[";
046d743… drh 360 if( zEnd==0 ) zEnd = "]]";
046d743… drh 361 if( zGap==0 ) zGap = " ... ";
046d743… drh 362 p = search_init(g.argv[2], zBegin, zEnd, zGap, flg);
128e95e… drh 363 for(i=3; i<g.argc; i++){
1772357… drh 364 blob_read_from_file(&x, g.argv[i], ExtFILE);
128e95e… drh 365 zDoc = blob_str(&x);
a00a140… drh 366 score = search_match(p, 1, (const char**)&zDoc);
a00a140… drh 367 fossil_print("%s: %d\n", g.argv[i], p->iScore);
128e95e… drh 368 blob_reset(&x);
128e95e… drh 369 if( score ){
a00a140… drh 370 fossil_print("%.78c\n%s\n%.78c\n\n", '=', blob_str(&p->snip), '=');
128e95e… drh 371 }
046d743… drh 372 }
a00a140… drh 373 search_end(p);
128e95e… drh 374 }
128e95e… drh 375
128e95e… drh 376 /*
375bc71… drh 377 ** An SQL function to initialize the full-scan search pattern:
046d743… drh 378 **
046d743… drh 379 ** search_init(PATTERN,BEGIN,END,GAP,FLAGS)
046d743… drh 380 **
375bc71… drh 381 ** All arguments are optional. PATTERN is the search pattern. If it
375bc71… drh 382 ** is omitted, then the global search pattern is reset. BEGIN and END
375bc71… drh 383 ** and GAP are the strings used to construct snippets. FLAGS is an
375bc71… drh 384 ** integer bit pattern containing the various SRCH_CKIN, SRCH_DOC,
4d35160… drh 385 ** SRCH_TKT, SRCH_FORUM, or SRCH_ALL bits to determine what is to be
4d35160… drh 386 ** searched.
046d743… drh 387 */
046d743… drh 388 static void search_init_sqlfunc(
046d743… drh 389 sqlite3_context *context,
046d743… drh 390 int argc,
046d743… drh 391 sqlite3_value **argv
046d743… drh 392 ){
046d743… drh 393 const char *zPattern = 0;
a00a140… drh 394 const char *zBegin = "<mark>";
a00a140… drh 395 const char *zEnd = "</mark>";
046d743… drh 396 const char *zGap = " ... ";
046d743… drh 397 unsigned int flg = SRCHFLG_HTML;
046d743… drh 398 switch( argc ){
046d743… drh 399 default:
046d743… drh 400 flg = (unsigned int)sqlite3_value_int(argv[4]);
046d743… drh 401 case 4:
046d743… drh 402 zGap = (const char*)sqlite3_value_text(argv[3]);
046d743… drh 403 case 3:
046d743… drh 404 zEnd = (const char*)sqlite3_value_text(argv[2]);
046d743… drh 405 case 2:
046d743… drh 406 zBegin = (const char*)sqlite3_value_text(argv[1]);
046d743… drh 407 case 1:
046d743… drh 408 zPattern = (const char*)sqlite3_value_text(argv[0]);
046d743… drh 409 }
046d743… drh 410 if( zPattern && zPattern[0] ){
046d743… drh 411 search_init(zPattern, zBegin, zEnd, zGap, flg | SRCHFLG_STATIC);
046d743… drh 412 }else{
046d743… drh 413 search_end(&gSearch);
046d743… drh 414 }
046d743… drh 415 }
046d743… drh 416
375bc71… drh 417 /* search_match(TEXT, TEXT, ....)
375bc71… drh 418 **
375bc71… drh 419 ** Using the full-scan search engine created by the most recent call
375bc71… drh 420 ** to search_init(), match the input the TEXT arguments.
4d35160… drh 421 ** Remember the results in the global full-scan search object.
a00a140… drh 422 ** Return non-zero on a match and zero on a miss.
a00a140… drh 423 */
a00a140… drh 424 static void search_match_sqlfunc(
a00a140… drh 425 sqlite3_context *context,
a00a140… drh 426 int argc,
a00a140… drh 427 sqlite3_value **argv
a00a140… drh 428 ){
0f96ffb… drh 429 const char *azDoc[5];
0f96ffb… drh 430 int nDoc;
a00a140… drh 431 int rc;
3cb9ba4… andygoth 432 for(nDoc=0; nDoc<count(azDoc) && nDoc<argc; nDoc++){
0f96ffb… drh 433 azDoc[nDoc] = (const char*)sqlite3_value_text(argv[nDoc]);
0f96ffb… drh 434 if( azDoc[nDoc]==0 ) azDoc[nDoc] = "";
0f96ffb… drh 435 }
0f96ffb… drh 436 rc = search_match(&gSearch, nDoc, azDoc);
a00a140… drh 437 sqlite3_result_int(context, rc);
a00a140… drh 438 }
a00a140… drh 439
375bc71… drh 440
375bc71… drh 441 /* search_score()
375bc71… drh 442 **
375bc71… drh 443 ** Return the match score for the last successful search_match call.
232d10b… drh 444 */
232d10b… drh 445 static void search_score_sqlfunc(
232d10b… drh 446 sqlite3_context *context,
232d10b… drh 447 int argc,
232d10b… drh 448 sqlite3_value **argv
232d10b… drh 449 ){
a00a140… drh 450 sqlite3_result_int(context, gSearch.iScore);
a00a140… drh 451 }
375bc71… drh 452
375bc71… drh 453 /* search_snippet()
375bc71… drh 454 **
375bc71… drh 455 ** Return a snippet for the last successful search_match() call.
375bc71… drh 456 */
a00a140… drh 457 static void search_snippet_sqlfunc(
a00a140… drh 458 sqlite3_context *context,
a00a140… drh 459 int argc,
a00a140… drh 460 sqlite3_value **argv
a00a140… drh 461 ){
a00a140… drh 462 if( blob_size(&gSearch.snip)>0 ){
a00a140… drh 463 sqlite3_result_text(context, blob_str(&gSearch.snip), -1, fossil_free);
a00a140… drh 464 blob_init(&gSearch.snip, 0, 0);
8e02c26… drh 465 }
8e02c26… drh 466 }
8e02c26… drh 467
375bc71… drh 468 /* stext(TYPE, RID, ARG)
375bc71… drh 469 **
8e02c26… drh 470 ** This is an SQLite function that computes the searchable text.
5260fbf… jan.nijtmans 471 ** It is a wrapper around the search_stext() routine. See the
8e02c26… drh 472 ** search_stext() routine for further detail.
8e02c26… drh 473 */
8e02c26… drh 474 static void search_stext_sqlfunc(
8e02c26… drh 475 sqlite3_context *context,
8e02c26… drh 476 int argc,
8e02c26… drh 477 sqlite3_value **argv
8e02c26… drh 478 ){
0f96ffb… drh 479 const char *zType = (const char*)sqlite3_value_text(argv[0]);
0f96ffb… drh 480 int rid = sqlite3_value_int(argv[1]);
0f96ffb… drh 481 const char *zName = (const char*)sqlite3_value_text(argv[2]);
0f96ffb… drh 482 sqlite3_result_text(context, search_stext_cached(zType[0],rid,zName,0), -1,
0f96ffb… drh 483 SQLITE_TRANSIENT);
0f96ffb… drh 484 }
375bc71… drh 485
375bc71… drh 486 /* title(TYPE, RID, ARG)
375bc71… drh 487 **
375bc71… drh 488 ** Return the title of the document to be search.
375bc71… drh 489 */
0f96ffb… drh 490 static void search_title_sqlfunc(
0f96ffb… drh 491 sqlite3_context *context,
0f96ffb… drh 492 int argc,
0f96ffb… drh 493 sqlite3_value **argv
0f96ffb… drh 494 ){
0f96ffb… drh 495 const char *zType = (const char*)sqlite3_value_text(argv[0]);
0f96ffb… drh 496 int rid = sqlite3_value_int(argv[1]);
0f96ffb… drh 497 const char *zName = (const char*)sqlite3_value_text(argv[2]);
d037468… mistachkin 498 int nHdr = 0;
0f96ffb… drh 499 char *z = search_stext_cached(zType[0], rid, zName, &nHdr);
0f96ffb… drh 500 if( nHdr || zType[0]!='d' ){
0f96ffb… drh 501 sqlite3_result_text(context, z, nHdr, SQLITE_TRANSIENT);
0f96ffb… drh 502 }else{
0f96ffb… drh 503 sqlite3_result_value(context, argv[2]);
0f96ffb… drh 504 }
0f96ffb… drh 505 }
375bc71… drh 506
375bc71… drh 507 /* body(TYPE, RID, ARG)
375bc71… drh 508 **
375bc71… drh 509 ** Return the body of the document to be search.
375bc71… drh 510 */
0f96ffb… drh 511 static void search_body_sqlfunc(
0f96ffb… drh 512 sqlite3_context *context,
0f96ffb… drh 513 int argc,
0f96ffb… drh 514 sqlite3_value **argv
0f96ffb… drh 515 ){
8e02c26… drh 516 const char *zType = (const char*)sqlite3_value_text(argv[0]);
ca833ff… drh 517 int rid = sqlite3_value_int(argv[1]);
ca833ff… drh 518 const char *zName = (const char*)sqlite3_value_text(argv[2]);
d037468… mistachkin 519 int nHdr = 0;
0f96ffb… drh 520 char *z = search_stext_cached(zType[0], rid, zName, &nHdr);
0f96ffb… drh 521 sqlite3_result_text(context, z+nHdr+1, -1, SQLITE_TRANSIENT);
8e02c26… drh 522 }
8e02c26… drh 523
375bc71… drh 524 /* urlencode(X)
375bc71… drh 525 **
375bc71… drh 526 ** Encode a string for use as a query parameter in a URL. This is
375bc71… drh 527 ** the equivalent of printf("%T",X).
8e02c26… drh 528 */
8e02c26… drh 529 static void search_urlencode_sqlfunc(
8e02c26… drh 530 sqlite3_context *context,
8e02c26… drh 531 int argc,
8e02c26… drh 532 sqlite3_value **argv
8e02c26… drh 533 ){
8e02c26… drh 534 char *z = mprintf("%T",sqlite3_value_text(argv[0]));
8e02c26… drh 535 sqlite3_result_text(context, z, -1, fossil_free);
232d10b… drh 536 }
232d10b… drh 537
232d10b… drh 538 /*
375bc71… drh 539 ** Register the various SQL functions (defined above) needed to implement
375bc71… drh 540 ** full-scan search.
232d10b… drh 541 */
046d743… drh 542 void search_sql_setup(sqlite3 *db){
a00a140… drh 543 static int once = 0;
9826bd0… drh 544 static const int enc = SQLITE_UTF8|SQLITE_INNOCUOUS;
a00a140… drh 545 if( once++ ) return;
9826bd0… drh 546 sqlite3_create_function(db, "search_match", -1, enc, 0,
a00a140… drh 547 search_match_sqlfunc, 0, 0);
9826bd0… drh 548 sqlite3_create_function(db, "search_score", 0, enc, 0,
a00a140… drh 549 search_score_sqlfunc, 0, 0);
9826bd0… drh 550 sqlite3_create_function(db, "search_snippet", 0, enc, 0,
a00a140… drh 551 search_snippet_sqlfunc, 0, 0);
9826bd0… drh 552 sqlite3_create_function(db, "search_init", -1, enc, 0,
a00a140… drh 553 search_init_sqlfunc, 0, 0);
9826bd0… drh 554 sqlite3_create_function(db, "stext", 3, enc, 0,
a00a140… drh 555 search_stext_sqlfunc, 0, 0);
9826bd0… drh 556 sqlite3_create_function(db, "title", 3, enc, 0,
0f96ffb… drh 557 search_title_sqlfunc, 0, 0);
9826bd0… drh 558 sqlite3_create_function(db, "body", 3, enc, 0,
0f96ffb… drh 559 search_body_sqlfunc, 0, 0);
9826bd0… drh 560 sqlite3_create_function(db, "urlencode", 1, enc, 0,
8e02c26… drh 561 search_urlencode_sqlfunc, 0, 0);
232d10b… drh 562 }
232d10b… drh 563
232d10b… drh 564 /*
232d10b… drh 565 ** Testing the search function.
232d10b… drh 566 **
841772c… drh 567 ** COMMAND: search*
bd559ff… jan.nijtmans 568 **
6d9f0f5… drh 569 ** Usage: %fossil search [OPTIONS] PATTERN...
6d9f0f5… drh 570 **
eeec6c8… drh 571 ** Search the repository for PATTERN and show matches. Depending on
eeec6c8… drh 572 ** options and how the administrator has search configured for the
eeec6c8… drh 573 ** repository, the search can cover:
eeec6c8… drh 574 **
eeec6c8… drh 575 ** * check-in comments (-c)
eeec6c8… drh 576 ** * embedded documentation (--docs)
eeec6c8… drh 577 ** * forum posts (--forum)
eeec6c8… drh 578 ** * tickets (--tickets)
eeec6c8… drh 579 ** * tech notes (--technotes)
eeec6c8… drh 580 ** * wiki pages (--wiki)
eeec6c8… drh 581 ** * built-in fossil help text (-h)
eeec6c8… drh 582 ** * all of the above (-a)
eeec6c8… drh 583 **
eeec6c8… drh 584 ** Use options below to select the scope of the search. The
eeec6c8… drh 585 ** default is check-in comments only (-c).
eeec6c8… drh 586 **
eeec6c8… drh 587 ** Output is colorized if writing to a TTY and if the NO_COLOR environment
eeec6c8… drh 588 ** variable is not set. Use the "--highlight 0" option to disable colorization
eeec6c8… drh 589 ** or use "--highlight 91" to force it on. Change the argument to --highlight
eeec6c8… drh 590 ** to change the color.
26eef7f… rberteig 591 **
26eef7f… rberteig 592 ** Options:
6d9f0f5… drh 593 ** -a|--all Search everything
e2bdc10… danield 594 ** -c|--checkins Search check-in comments
6d9f0f5… drh 595 ** --docs Search embedded documentation
6d9f0f5… drh 596 ** --forum Search forum posts
6d9f0f5… drh 597 ** -h|--bi-help Search built-in help
eeec6c8… drh 598 ** --highlight N Used VT100 color N for matching text. 0 means "off".
2e56ef4… km 599 ** -n|--limit N Limit output to N matches
e129e11… Torsten 600 ** --subject Restrict --forum search to subject lines only
6d9f0f5… drh 601 ** --technotes Search tech notes
6d9f0f5… drh 602 ** --tickets Search tickets
26eef7f… rberteig 603 ** -W|--width WIDTH Set display width to WIDTH columns, 0 for
6d9f0f5… drh 604 ** unlimited. Defaults to the terminal's width.
6d9f0f5… drh 605 ** --wiki Search wiki
232d10b… drh 606 */
2060010… drh 607 void search_cmd(void){
232d10b… drh 608 Blob pattern;
232d10b… drh 609 int i;
1064dfa… stephan 610 Blob sql = empty_blob;
232d10b… drh 611 Stmt q;
2060010… drh 612 int iBest;
6d9f0f5… drh 613 int srchFlags = 0;
6d9f0f5… drh 614 int bFts = 1; /* Use FTS search by default now */
2ddf62a… drh 615 char fAll = NULL != find_option("all", "a", 0);
38421a9… jan.nijtmans 616 const char *zLimit = find_option("limit","n",1);
6d9f0f5… drh 617 const char *zScope = 0;
e4a6159… mistachkin 618 const char *zWidth = find_option("width","W",1);
6d9f0f5… drh 619 int bDebug = find_option("debug",0,0)!=0; /* Undocumented */
2ddf62a… drh 620 int nLimit = zLimit ? atoi(zLimit) : -1000;
e4a6159… mistachkin 621 int width;
eeec6c8… drh 622 int nTty = 0; /* VT100 highlight color for matching text */
eeec6c8… drh 623 const char *zHighlight = 0;
3db304c… tsbg 624 int bFlags = 0; /* DB open flags */
f0d9fe2… drh 625
f0d9fe2… drh 626 nTty = terminal_is_vt100();
6d9f0f5… drh 627
6d9f0f5… drh 628 /* Undocumented option to change highlight color */
eeec6c8… drh 629 zHighlight = find_option("highlight",0,1);
6d9f0f5… drh 630 if( zHighlight ) nTty = atoi(zHighlight);
6d9f0f5… drh 631
6d9f0f5… drh 632 /* Undocumented option (legacy) */
6d9f0f5… drh 633 zScope = find_option("scope",0,1);
6d9f0f5… drh 634
6d9f0f5… drh 635 if( find_option("fts",0,0)!=0 ) bFts = 1; /* Undocumented legacy */
6d9f0f5… drh 636 if( find_option("legacy",0,0)!=0 ) bFts = 0; /* Undocumented */
2ddf62a… drh 637
e4a6159… mistachkin 638 if( zWidth ){
e4a6159… mistachkin 639 width = atoi(zWidth);
e4a6159… mistachkin 640 if( (width!=0) && (width<=20) ){
fa6ece7… jan.nijtmans 641 fossil_fatal("-W|--width value must be >20 or 0");
e4a6159… mistachkin 642 }
e4a6159… mistachkin 643 }else{
0ff6a9e… mistachkin 644 width = -1;
0ff6a9e… mistachkin 645 }
6d9f0f5… drh 646 if( zScope ){
6d9f0f5… drh 647 for(i=0; zScope[i]; i++){
6d9f0f5… drh 648 switch( zScope[i] ){
6d9f0f5… drh 649 case 'a': srchFlags = SRCH_ALL; break;
6d9f0f5… drh 650 case 'c': srchFlags |= SRCH_CKIN; break;
6d9f0f5… drh 651 case 'd': srchFlags |= SRCH_DOC; break;
6d9f0f5… drh 652 case 'e': srchFlags |= SRCH_TECHNOTE; break;
6d9f0f5… drh 653 case 'f': srchFlags |= SRCH_FORUM; break;
6d9f0f5… drh 654 case 'h': srchFlags |= SRCH_HELP; break;
6d9f0f5… drh 655 case 't': srchFlags |= SRCH_TKT; break;
6d9f0f5… drh 656 case 'w': srchFlags |= SRCH_WIKI; break;
6d9f0f5… drh 657 }
6d9f0f5… drh 658 }
6d9f0f5… drh 659 bFts = 1;
6d9f0f5… drh 660 }
6d9f0f5… drh 661 if( find_option("all","a",0) ){ srchFlags |= SRCH_ALL; bFts = 1; }
6d9f0f5… drh 662 if( find_option("bi-help","h",0) ){ srchFlags |= SRCH_HELP; bFts = 1; }
6d9f0f5… drh 663 if( find_option("checkins","c",0) ){ srchFlags |= SRCH_CKIN; bFts = 1; }
6d9f0f5… drh 664 if( find_option("docs",0,0) ){ srchFlags |= SRCH_DOC; bFts = 1; }
6d9f0f5… drh 665 if( find_option("forum",0,0) ){ srchFlags |= SRCH_FORUM; bFts = 1; }
e129e11… Torsten 666 if( find_option("subject",0,0) ){ srchFlags |= SRCH_FORUM_SUBJ; }
6d9f0f5… drh 667 if( find_option("technotes",0,0) ){ srchFlags |= SRCH_TECHNOTE; bFts = 1; }
6d9f0f5… drh 668 if( find_option("tickets",0,0) ){ srchFlags |= SRCH_TKT; bFts = 1; }
6d9f0f5… drh 669 if( find_option("wiki",0,0) ){ srchFlags |= SRCH_WIKI; bFts = 1; }
0ff6a9e… mistachkin 670
6d9f0f5… drh 671 /* If no search objects are specified, default to "check-in comments" */
e129e11… Torsten 672 if( (srchFlags & SRCH_ALL)==0 ) srchFlags = SRCH_CKIN;
6d9f0f5… drh 673
3db304c… tsbg 674 if( srchFlags==SRCH_HELP ) bFlags = OPEN_OK_NOT_FOUND|OPEN_SUBSTITUTE;
3db304c… tsbg 675 db_find_and_open_repository(bFlags, 0);
6d9f0f5… drh 676 verify_all_options();
2d69772… jan.nijtmans 677 if( g.argc<3 ) return;
6d9f0f5… drh 678 login_set_capabilities("s", 0);
2e9e369… drh 679 if( search_restrict(srchFlags)==0 && (srchFlags & SRCH_HELP)==0 ){
6d9f0f5… drh 680 const char *zC1 = 0, *zPlural = "s";
6d9f0f5… drh 681 if( srchFlags & SRCH_TECHNOTE ){ zC1 = "technote"; }
6d9f0f5… drh 682 if( srchFlags & SRCH_TKT ){ zC1 = "ticket"; }
6d9f0f5… drh 683 if( srchFlags & SRCH_FORUM ){ zC1 = "forum"; zPlural = ""; }
6d9f0f5… drh 684 if( srchFlags & SRCH_DOC ){ zC1 = "document"; }
6d9f0f5… drh 685 if( srchFlags & SRCH_WIKI ){ zC1 = "wiki"; zPlural = ""; }
6d9f0f5… drh 686 if( srchFlags & SRCH_CKIN ){ zC1 = "check-in"; }
6d9f0f5… drh 687 fossil_print(
6d9f0f5… drh 688 "Search of %s%s is disabled on this repository.\n"
6d9f0f5… drh 689 "Enable using \"fossil fts-config enable %s\".\n",
6d9f0f5… drh 690 zC1, zPlural, zC1
6d9f0f5… drh 691 );
6d9f0f5… drh 692 return;
6d9f0f5… drh 693 }
6d9f0f5… drh 694
232d10b… drh 695 blob_init(&pattern, g.argv[2], -1);
232d10b… drh 696 for(i=3; i<g.argc; i++){
232d10b… drh 697 blob_appendf(&pattern, " %s", g.argv[i]);
232d10b… drh 698 }
2ddf62a… drh 699 if( bFts ){
2ddf62a… drh 700 /* Search using FTS */
2ddf62a… drh 701 Blob com;
b7ef90c… drh 702 Blob snip;
2ddf62a… drh 703 const char *zPattern = blob_str(&pattern);
2ddf62a… drh 704 search_sql_setup(g.db);
2ddf62a… drh 705 add_content_sql_commands(g.db);
2ddf62a… drh 706 db_multi_exec(
2ddf62a… drh 707 "CREATE TEMP TABLE x(label,url,score,id,date,snip);"
2ddf62a… drh 708 );
2ddf62a… drh 709 if( !search_index_exists() ){
2ddf62a… drh 710 search_fullscan(zPattern, srchFlags); /* Full-scan search */
2ddf62a… drh 711 }else{
2ddf62a… drh 712 search_update_index(srchFlags); /* Update the index */
e129e11… Torsten 713 if( srchFlags & SRCH_FORUM_SUBJ ){
e129e11… Torsten 714 unsigned int idxFlags = srchFlags & ~SRCH_FORUM & ~SRCH_FORUM_SUBJ;
e129e11… Torsten 715 if( idxFlags ) search_indexed(zPattern, idxFlags);
e129e11… Torsten 716 search_fullscan(zPattern, SRCH_FORUM | SRCH_FORUM_SUBJ);
e129e11… Torsten 717 }else{
e129e11… Torsten 718 search_indexed(zPattern, srchFlags); /* Indexed search */
e129e11… Torsten 719 }
6d9f0f5… drh 720 if( srchFlags & SRCH_HELP ){
6d9f0f5… drh 721 search_fullscan(zPattern, SRCH_HELP);
6d9f0f5… drh 722 }
2ddf62a… drh 723 }
b7ef90c… drh 724 db_prepare(&q, "SELECT snip, label, score, id, date"
2ddf62a… drh 725 " FROM x"
2ddf62a… drh 726 " ORDER BY score DESC, date DESC;");
2ddf62a… drh 727 blob_init(&com, 0, 0);
b7ef90c… drh 728 blob_init(&snip, 0, 0);
fd59eb1… drh 729 if( width<0 ) width = terminal_get_width(80);
2ddf62a… drh 730 while( db_step(&q)==SQLITE_ROW ){
2ddf62a… drh 731 const char *zSnippet = db_column_text(&q, 0);
2ddf62a… drh 732 const char *zLabel = db_column_text(&q, 1);
2ddf62a… drh 733 const char *zDate = db_column_text(&q, 4);
2ddf62a… drh 734 const char *zScore = db_column_text(&q, 2);
2ddf62a… drh 735 const char *zId = db_column_text(&q, 3);
fd59eb1… drh 736 char *zOrig;
b7ef90c… drh 737 blob_appendf(&snip, "%s", zSnippet);
fd59eb1… drh 738 zOrig = blob_materialize(&snip);
fd59eb1… drh 739 blob_init(&snip, 0, 0);
d62ca2b… drh 740 html_to_plaintext(zOrig, &snip, (nTty?HTOT_VT100:0)|HTOT_FLOW|HTOT_TRIM);
fd59eb1… drh 741 fossil_free(zOrig);
b7ef90c… drh 742 blob_appendf(&com, "%s\n%s\n%s", zLabel, blob_str(&snip), zDate);
b7ef90c… drh 743 if( bDebug ){
b7ef90c… drh 744 blob_appendf(&com," score: %s id: %s", zScore, zId);
b7ef90c… drh 745 }
2ddf62a… drh 746 comment_print(blob_str(&com), 0, 5, width,
2ddf62a… drh 747 COMMENT_PRINT_TRIM_CRLF |
b7ef90c… drh 748 COMMENT_PRINT_WORD_BREAK |
2ddf62a… drh 749 COMMENT_PRINT_TRIM_SPACE);
2ddf62a… drh 750 blob_reset(&com);
b7ef90c… drh 751 blob_reset(&snip);
b7ef90c… drh 752 if( nLimit>=1 ){
b7ef90c… drh 753 nLimit--;
b7ef90c… drh 754 if( nLimit==0 ) break;
b7ef90c… drh 755 }
2ddf62a… drh 756 }
2ddf62a… drh 757 db_finalize(&q);
2ddf62a… drh 758 blob_reset(&pattern);
2ddf62a… drh 759 }else{
2ddf62a… drh 760 /* Legacy timeline search (the default) */
2ddf62a… drh 761 (void)search_init(blob_str(&pattern),"*","*","...",SRCHFLG_STATIC);
2ddf62a… drh 762 blob_reset(&pattern);
2ddf62a… drh 763 search_sql_setup(g.db);
275da70… danield 764
2ddf62a… drh 765 db_multi_exec(
2ddf62a… drh 766 "CREATE TEMP TABLE srch(rid,uuid,date,comment,x);"
2ddf62a… drh 767 "CREATE INDEX srch_idx1 ON srch(x);"
2ddf62a… drh 768 "INSERT INTO srch(rid,uuid,date,comment,x)"
2ddf62a… drh 769 " SELECT blob.rid, uuid, datetime(event.mtime,toLocal()),"
2ddf62a… drh 770 " coalesce(ecomment,comment),"
2ddf62a… drh 771 " search_score()"
2ddf62a… drh 772 " FROM event, blob"
2ddf62a… drh 773 " WHERE blob.rid=event.objid"
2ddf62a… drh 774 " AND search_match(coalesce(ecomment,comment));"
2ddf62a… drh 775 );
2ddf62a… drh 776 iBest = db_int(0, "SELECT max(x) FROM srch");
2ddf62a… drh 777 blob_append(&sql,
2ddf62a… drh 778 "SELECT rid, uuid, date, comment, 0, 0 FROM srch "
2ddf62a… drh 779 "WHERE 1 ", -1);
2ddf62a… drh 780 if(!fAll){
2ddf62a… drh 781 blob_append_sql(&sql,"AND x>%d ", iBest/3);
2ddf62a… drh 782 }
2ddf62a… drh 783 blob_append(&sql, "ORDER BY x DESC, date DESC ", -1);
2ddf62a… drh 784 db_prepare(&q, "%s", blob_sql_text(&sql));
2ddf62a… drh 785 blob_reset(&sql);
2ddf62a… drh 786 print_timeline(&q, nLimit, width, 0, 0);
2ddf62a… drh 787 db_finalize(&q);
2ddf62a… drh 788 }
6714c94… drh 789 }
6714c94… drh 790
6714c94… drh 791 #if INTERFACE
6714c94… drh 792 /* What to search for */
e129e11… Torsten 793 #define SRCH_CKIN 0x0001 /* Search over check-in comments */
e129e11… Torsten 794 #define SRCH_DOC 0x0002 /* Search over embedded documents */
e129e11… Torsten 795 #define SRCH_TKT 0x0004 /* Search over tickets */
e129e11… Torsten 796 #define SRCH_WIKI 0x0008 /* Search over wiki */
e129e11… Torsten 797 #define SRCH_TECHNOTE 0x0010 /* Search over tech notes */
e129e11… Torsten 798 #define SRCH_FORUM 0x0020 /* Search over forum messages */
e129e11… Torsten 799 #define SRCH_HELP 0x0040 /* Search built-in help (full-scan only) */
e129e11… Torsten 800 #define SRCH_ALL 0x007f /* Search over everything */
e129e11… Torsten 801 #define SRCH_FORUM_SUBJ 0x0080 /* Restrict forum search to subject lines only */
6714c94… drh 802 #endif
6714c94… drh 803
6714c94… drh 804 /*
6714c94… drh 805 ** Remove bits from srchFlags which are disallowed by either the
4d35160… drh 806 ** current server configuration or by user permissions. Return
4d35160… drh 807 ** the revised search flags mask.
6d9f0f5… drh 808 **
6d9f0f5… drh 809 ** If bFlex is true, that means allow through the SRCH_HELP option
6d9f0f5… drh 810 ** even if it is not explicitly enabled.
6714c94… drh 811 */
2e9e369… drh 812 unsigned int search_restrict(unsigned int srchFlags){
c0c0bae… drh 813 static unsigned int knownGood = 0;
c0c0bae… drh 814 static unsigned int knownBad = 0;
c0c0bae… drh 815 static const struct { unsigned m; const char *zKey; } aSetng[] = {
6d9f0f5… drh 816 { SRCH_CKIN, "search-ci" },
6d9f0f5… drh 817 { SRCH_DOC, "search-doc" },
6d9f0f5… drh 818 { SRCH_TKT, "search-tkt" },
6d9f0f5… drh 819 { SRCH_WIKI, "search-wiki" },
a551d50… andygoth 820 { SRCH_TECHNOTE, "search-technote" },
6d9f0f5… drh 821 { SRCH_FORUM, "search-forum" },
6d9f0f5… drh 822 { SRCH_HELP, "search-help" },
c0c0bae… drh 823 };
c0c0bae… drh 824 int i;
a551d50… andygoth 825 if( g.perm.Read==0 ) srchFlags &= ~(SRCH_CKIN|SRCH_DOC|SRCH_TECHNOTE);
a00a140… drh 826 if( g.perm.RdTkt==0 ) srchFlags &= ~(SRCH_TKT);
a00a140… drh 827 if( g.perm.RdWiki==0 ) srchFlags &= ~(SRCH_WIKI);
99fcc43… drh 828 if( g.perm.RdForum==0) srchFlags &= ~(SRCH_FORUM);
3cb9ba4… andygoth 829 for(i=0; i<count(aSetng); i++){
c0c0bae… drh 830 unsigned int m = aSetng[i].m;
c0c0bae… drh 831 if( (srchFlags & m)==0 ) continue;
c0c0bae… drh 832 if( ((knownGood|knownBad) & m)!=0 ) continue;
c0c0bae… drh 833 if( db_get_boolean(aSetng[i].zKey,0) ){
c0c0bae… drh 834 knownGood |= m;
c0c0bae… drh 835 }else{
c0c0bae… drh 836 knownBad |= m;
c0c0bae… drh 837 }
c0c0bae… drh 838 }
b8e3dc1… jan.nijtmans 839 return srchFlags & ~knownBad;
6714c94… drh 840 }
6714c94… drh 841
6714c94… drh 842 /*
a00a140… drh 843 ** When this routine is called, there already exists a table
a00a140… drh 844 **
375bc71… drh 845 ** x(label,url,score,id,snip).
375bc71… drh 846 **
375bc71… drh 847 ** label: The "name" of the document containing the match
375bc71… drh 848 ** url: A URL for the document
375bc71… drh 849 ** score: How well the document matched
375bc71… drh 850 ** id: The document id. Format: xNNNNN, x: type, N: number
375bc71… drh 851 ** snip: A snippet for the match
a00a140… drh 852 **
a00a140… drh 853 ** And the srchFlags parameter has been validated. This routine
375bc71… drh 854 ** fills the X table with search results using a full-scan search.
6714c94… drh 855 **
375bc71… drh 856 ** The companion indexed search routine is search_indexed().
6714c94… drh 857 */
2ddf62a… drh 858 LOCAL void search_fullscan(
6714c94… drh 859 const char *zPattern, /* The query pattern */
6714c94… drh 860 unsigned int srchFlags /* What to search over */
6714c94… drh 861 ){
23d659d… drh 862 search_init(zPattern, "<mark>", "</mark>", " ... ",
a00a140… drh 863 SRCHFLG_STATIC|SRCHFLG_HTML);
6714c94… drh 864 if( (srchFlags & SRCH_DOC)!=0 ){
6714c94… drh 865 char *zDocGlob = db_get("doc-glob","");
ca0d66b… danield 866 const char *zMainBranch = db_main_branch();
3a6dd83… drh 867 char *zDocBr = db_get("doc-branch", zMainBranch);
6714c94… drh 868 if( zDocGlob && zDocGlob[0] && zDocBr && zDocBr[0] ){
40d0b36… stephan 869 Glob * pGlob = glob_create(zDocBr)
40d0b36… stephan 870 /* We're misusing a Glob as a list of comma-/space-delimited
40d0b36… stephan 871 ** tokens. We're not actually doing glob matches here. */;
40d0b36… stephan 872 int i;
6714c94… drh 873 db_multi_exec(
a00a140… drh 874 "CREATE VIRTUAL TABLE IF NOT EXISTS temp.foci USING files_of_checkin;"
6714c94… drh 875 );
40d0b36… stephan 876 for( i = 0; i < pGlob->nPattern; ++i ){
40d0b36… stephan 877 const char * zBranch = pGlob->azPattern[i];
40d0b36… stephan 878 db_multi_exec(
40d0b36… stephan 879 "INSERT INTO x(label,url,score,id,date,snip)"
40d0b36… stephan 880 " SELECT printf('Document: %%s',title('d',blob.rid,foci.filename)),"
40d0b36… stephan 881 " printf('/doc/%T/%%s',foci.filename),"
40d0b36… stephan 882 " search_score(),"
40d0b36… stephan 883 " 'd'||blob.rid,"
40d0b36… stephan 884 " (SELECT datetime(event.mtime) FROM event"
40d0b36… stephan 885 " WHERE objid=symbolic_name_to_rid(%Q)),"
40d0b36… stephan 886 " search_snippet()"
40d0b36… stephan 887 " FROM foci CROSS JOIN blob"
40d0b36… stephan 888 " WHERE checkinID=symbolic_name_to_rid(%Q)"
40d0b36… stephan 889 " AND blob.uuid=foci.uuid"
40d0b36… stephan 890 " AND search_match(title('d',blob.rid,foci.filename),"
40d0b36… stephan 891 " body('d',blob.rid,foci.filename))"
40d0b36… stephan 892 " AND %z",
40d0b36… stephan 893 zBranch, zBranch, zBranch, glob_expr("foci.filename", zDocGlob)
40d0b36… stephan 894 );
40d0b36… stephan 895 }
40d0b36… stephan 896 glob_free(pGlob);
163f2f5… stephan 897 }
163f2f5… stephan 898 fossil_free(zDocGlob);
163f2f5… stephan 899 fossil_free(zDocBr);
6714c94… drh 900 }
6714c94… drh 901 if( (srchFlags & SRCH_WIKI)!=0 ){
6714c94… drh 902 db_multi_exec(
6714c94… drh 903 "WITH wiki(name,rid,mtime) AS ("
6714c94… drh 904 " SELECT substr(tagname,6), tagxref.rid, max(tagxref.mtime)"
6714c94… drh 905 " FROM tag, tagxref"
6714c94… drh 906 " WHERE tag.tagname GLOB 'wiki-*'"
6714c94… drh 907 " AND tagxref.tagid=tag.tagid"
6714c94… drh 908 " GROUP BY 1"
6714c94… drh 909 ")"
0f96ffb… drh 910 "INSERT INTO x(label,url,score,id,date,snip)"
6714c94… drh 911 " SELECT printf('Wiki: %%s',name),"
7e56e36… mistachkin 912 " printf('/wiki?name=%%s',urlencode(name)),"
a00a140… drh 913 " search_score(),"
0f96ffb… drh 914 " 'w'||rid,"
6714c94… drh 915 " datetime(mtime),"
a00a140… drh 916 " search_snippet()"
a00a140… drh 917 " FROM wiki"
0f96ffb… drh 918 " WHERE search_match(title('w',rid,name),body('w',rid,name));"
6714c94… drh 919 );
6714c94… drh 920 }
6714c94… drh 921 if( (srchFlags & SRCH_CKIN)!=0 ){
6714c94… drh 922 db_multi_exec(
6714c94… drh 923 "WITH ckin(uuid,rid,mtime) AS ("
6714c94… drh 924 " SELECT blob.uuid, event.objid, event.mtime"
6714c94… drh 925 " FROM event, blob"
6714c94… drh 926 " WHERE event.type='ci'"
6714c94… drh 927 " AND blob.rid=event.objid"
6714c94… drh 928 ")"
0f96ffb… drh 929 "INSERT INTO x(label,url,score,id,date,snip)"
6714c94… drh 930 " SELECT printf('Check-in [%%.10s] on %%s',uuid,datetime(mtime)),"
cde6cbf… drh 931 " printf('/timeline?c=%%s',uuid),"
a00a140… drh 932 " search_score(),"
0f96ffb… drh 933 " 'c'||rid,"
6714c94… drh 934 " datetime(mtime),"
a00a140… drh 935 " search_snippet()"
a00a140… drh 936 " FROM ckin"
0f96ffb… drh 937 " WHERE search_match('',body('c',rid,NULL));"
6714c94… drh 938 );
6714c94… drh 939 }
6714c94… drh 940 if( (srchFlags & SRCH_TKT)!=0 ){
6714c94… drh 941 db_multi_exec(
0f96ffb… drh 942 "INSERT INTO x(label,url,score,id,date,snip)"
0f96ffb… drh 943 " SELECT printf('Ticket: %%s (%%s)',title('t',tkt_id,NULL),"
0f96ffb… drh 944 "datetime(tkt_mtime)),"
7e56e36… mistachkin 945 " printf('/tktview/%%.20s',tkt_uuid),"
a00a140… drh 946 " search_score(),"
0f96ffb… drh 947 " 't'||tkt_id,"
6714c94… drh 948 " datetime(tkt_mtime),"
a00a140… drh 949 " search_snippet()"
a00a140… drh 950 " FROM ticket"
0f96ffb… drh 951 " WHERE search_match(title('t',tkt_id,NULL),body('t',tkt_id,NULL));"
a551d50… andygoth 952 );
a551d50… andygoth 953 }
a551d50… andygoth 954 if( (srchFlags & SRCH_TECHNOTE)!=0 ){
a551d50… andygoth 955 db_multi_exec(
a551d50… andygoth 956 "WITH technote(uuid,rid,mtime) AS ("
a551d50… andygoth 957 " SELECT substr(tagname,7), tagxref.rid, max(tagxref.mtime)"
a551d50… andygoth 958 " FROM tag, tagxref"
a551d50… andygoth 959 " WHERE tag.tagname GLOB 'event-*'"
a551d50… andygoth 960 " AND tagxref.tagid=tag.tagid"
a551d50… andygoth 961 " GROUP BY 1"
a551d50… andygoth 962 ")"
a551d50… andygoth 963 "INSERT INTO x(label,url,score,id,date,snip)"
a551d50… andygoth 964 " SELECT printf('Tech Note: %%s',uuid),"
a551d50… andygoth 965 " printf('/technote/%%s',uuid),"
a551d50… andygoth 966 " search_score(),"
a551d50… andygoth 967 " 'e'||rid,"
a551d50… andygoth 968 " datetime(mtime),"
a551d50… andygoth 969 " search_snippet()"
a551d50… andygoth 970 " FROM technote"
a551d50… andygoth 971 " WHERE search_match('',body('e',rid,NULL));"
99fcc43… drh 972 );
99fcc43… drh 973 }
99fcc43… drh 974 if( (srchFlags & SRCH_FORUM)!=0 ){
e129e11… Torsten 975 if( srchFlags & SRCH_FORUM_SUBJ ){
e129e11… Torsten 976 /* Search only forum subject lines. The subject is extracted from
e129e11… Torsten 977 ** event.comment which is stored as "TYPE: SUBJECT" for every forum
e129e11… Torsten 978 ** post (original, reply, edit, …). */
e129e11… Torsten 979 db_multi_exec(
e129e11… Torsten 980 "INSERT INTO x(label,url,score,id,date,snip)"
e129e11… Torsten 981 " SELECT 'Forum '||comment,"
e129e11… Torsten 982 " '/forumpost/'||uuid,"
e129e11… Torsten 983 " search_score(),"
e129e11… Torsten 984 " 'f'||rid,"
e129e11… Torsten 985 " datetime(event.mtime),"
e129e11… Torsten 986 " search_snippet()"
e129e11… Torsten 987 " FROM event JOIN blob ON event.objid=blob.rid"
e129e11… Torsten 988 " WHERE event.type='f'"
e129e11… Torsten 989 " AND search_match("
e129e11… Torsten 990 " substr(event.comment,instr(event.comment,':')+2),'');"
e129e11… Torsten 991 );
e129e11… Torsten 992 }else{
e129e11… Torsten 993 db_multi_exec(
e129e11… Torsten 994 "INSERT INTO x(label,url,score,id,date,snip)"
e129e11… Torsten 995 " SELECT 'Forum '||comment,"
e129e11… Torsten 996 " '/forumpost/'||uuid,"
e129e11… Torsten 997 " search_score(),"
e129e11… Torsten 998 " 'f'||rid,"
e129e11… Torsten 999 " datetime(event.mtime),"
e129e11… Torsten 1000 " search_snippet()"
e129e11… Torsten 1001 " FROM event JOIN blob ON event.objid=blob.rid"
e129e11… Torsten 1002 " WHERE event.type='f'"
e129e11… Torsten 1003 " AND search_match('',body('f',rid,NULL));"
e129e11… Torsten 1004 );
e129e11… Torsten 1005 }
6d9f0f5… drh 1006 }
6d9f0f5… drh 1007 if( (srchFlags & SRCH_HELP)!=0 ){
90e564f… drh 1008 const char *zPrefix;
6d9f0f5… drh 1009 helptext_vtab_register(g.db);
90e564f… drh 1010 if( srchFlags==SRCH_HELP ){
90e564f… drh 1011 zPrefix = "The";
90e564f… drh 1012 }else{
90e564f… drh 1013 zPrefix = "Built-in help for the";
90e564f… drh 1014 }
99fcc43… drh 1015 db_multi_exec(
6d9f0f5… drh 1016 "INSERT INTO x(label,url,score,id,snip)"
90e564f… drh 1017 " SELECT format('%q \"%%s\" %%s',name,type),"
c64f28d… drh 1018 " '/help/'||name,"
99fcc43… drh 1019 " search_score(),"
6d9f0f5… drh 1020 " 'h'||rowid,"
99fcc43… drh 1021 " search_snippet()"
6d9f0f5… drh 1022 " FROM helptext"
55dd989… drh 1023 " WHERE search_match(format('the \"%%s\" %%s',name,type),"
90e564f… drh 1024 " helptext.helptext);",
90e564f… drh 1025 zPrefix
7fbc1c3… jan.nijtmans 1026 );
7fbc1c3… jan.nijtmans 1027 }
0f96ffb… drh 1028 }
0f96ffb… drh 1029
0f96ffb… drh 1030 /*
0f96ffb… drh 1031 ** Number of significant bits in a u32
0f96ffb… drh 1032 */
0f96ffb… drh 1033 static int nbits(u32 x){
7fbc1c3… jan.nijtmans 1034 int n = 0;
0f96ffb… drh 1035 while( x ){ n++; x >>= 1; }
0f96ffb… drh 1036 return n;
a00a140… drh 1037 }
a00a140… drh 1038
a00a140… drh 1039 /*
a00a140… drh 1040 ** Implemenation of the rank() function used with rank(matchinfo(*,'pcsx')).
a00a140… drh 1041 */
a00a140… drh 1042 static void search_rank_sqlfunc(
a00a140… drh 1043 sqlite3_context *context,
a00a140… drh 1044 int argc,
a00a140… drh 1045 sqlite3_value **argv
a00a140… drh 1046 ){
a00a140… drh 1047 const unsigned *aVal = (unsigned int*)sqlite3_value_blob(argv[0]);
a00a140… drh 1048 int nVal = sqlite3_value_bytes(argv[0])/4;
0f96ffb… drh 1049 int nCol; /* Number of columns in the index */
a00a140… drh 1050 int nTerm; /* Number of search terms in the query */
0f96ffb… drh 1051 int i, j; /* Loop counter */
0f96ffb… drh 1052 double r = 0.0; /* Score */
0f96ffb… drh 1053 const unsigned *aX, *aS;
a00a140… drh 1054
0f96ffb… drh 1055 if( nVal<2 ) return;
a00a140… drh 1056 nTerm = aVal[0];
0f96ffb… drh 1057 nCol = aVal[1];
0f96ffb… drh 1058 if( nVal<2+3*nCol*nTerm+nCol ) return;
0f96ffb… drh 1059 aS = aVal+2;
0f96ffb… drh 1060 aX = aS+nCol;
0f96ffb… drh 1061 for(j=0; j<nCol; j++){
0f96ffb… drh 1062 double x;
0f96ffb… drh 1063 if( aS[j]>0 ){
0f96ffb… drh 1064 x = 0.0;
0f96ffb… drh 1065 for(i=0; i<nTerm; i++){
0f96ffb… drh 1066 int hits_this_row;
0f96ffb… drh 1067 int hits_all_rows;
0f96ffb… drh 1068 int rows_with_hit;
0f96ffb… drh 1069 double avg_hits_per_row;
0f96ffb… drh 1070
0f96ffb… drh 1071 hits_this_row = aX[j + i*nCol*3];
0f96ffb… drh 1072 if( hits_this_row==0 )continue;
0f96ffb… drh 1073 hits_all_rows = aX[j + i*nCol*3 + 1];
0f96ffb… drh 1074 rows_with_hit = aX[j + i*nCol*3 + 2];
0f96ffb… drh 1075 if( rows_with_hit==0 ) continue;
0f96ffb… drh 1076 avg_hits_per_row = hits_all_rows/(double)rows_with_hit;
0f96ffb… drh 1077 x += hits_this_row/(avg_hits_per_row*nbits(rows_with_hit));
0f96ffb… drh 1078 }
0f96ffb… drh 1079 x *= (1<<((30*(aS[j]-1))/nTerm));
0f96ffb… drh 1080 }else{
0f96ffb… drh 1081 x = 0.0;
0f96ffb… drh 1082 }
0f96ffb… drh 1083 r = r*10.0 + x;
a00a140… drh 1084 }
a00a140… drh 1085 #define SEARCH_DEBUG_RANK 0
a00a140… drh 1086 #if SEARCH_DEBUG_RANK
a00a140… drh 1087 {
a00a140… drh 1088 Blob x;
a00a140… drh 1089 blob_init(&x,0,0);
a00a140… drh 1090 blob_appendf(&x,"%08x", (int)r);
a00a140… drh 1091 for(i=0; i<nVal; i++){
a00a140… drh 1092 blob_appendf(&x," %d", aVal[i]);
a00a140… drh 1093 }
a00a140… drh 1094 blob_appendf(&x," r=%g", r);
a00a140… drh 1095 sqlite3_result_text(context, blob_str(&x), -1, fossil_free);
a00a140… drh 1096 }
a00a140… drh 1097 #else
a00a140… drh 1098 sqlite3_result_double(context, r);
a00a140… drh 1099 #endif
a00a140… drh 1100 }
a00a140… drh 1101
a00a140… drh 1102 /*
fc85382… stephan 1103 ** Expects a search pattern string. Makes a copy of the string,
fc85382… stephan 1104 ** replaces all non-alphanum ASCII characters with a space, and
fc85382… stephan 1105 ** lower-cases all upper-case ASCII characters. The intent is to avoid
fc85382… stephan 1106 ** causing errors in FTS5 searches with inputs which contain AND, OR,
fc85382… stephan 1107 ** and symbols like #. The caller is responsible for passing the
fc85382… stephan 1108 ** result to fossil_free().
fc85382… stephan 1109 */
fc85382… stephan 1110 char *search_simplify_pattern(const char * zPattern){
20adff9… stephan 1111 char *zPat = mprintf("%s",zPattern);
fc85382… stephan 1112 int i;
fc85382… stephan 1113 for(i=0; zPat[i]; i++){
fc85382… stephan 1114 if( (zPat[i]&0x80)==0 && !fossil_isalnum(zPat[i]) ) zPat[i] = ' ';
fc85382… stephan 1115 if( fossil_isupper(zPat[i]) ) zPat[i] = fossil_tolower(zPat[i]);
fc85382… stephan 1116 }
fc85382… stephan 1117 for(i--; i>=0 && zPat[i]==' '; i--){}
fc85382… stephan 1118 if( i<0 ){
fc85382… stephan 1119 fossil_free(zPat);
fc85382… stephan 1120 zPat = mprintf("\"\"");
fc85382… stephan 1121 }
fc85382… stephan 1122 return zPat;
fc85382… stephan 1123 }
fc85382… stephan 1124
fc85382… stephan 1125 /*
a00a140… drh 1126 ** When this routine is called, there already exists a table
a00a140… drh 1127 **
375bc71… drh 1128 ** x(label,url,score,id,snip).
375bc71… drh 1129 **
375bc71… drh 1130 ** label: The "name" of the document containing the match
375bc71… drh 1131 ** url: A URL for the document
375bc71… drh 1132 ** score: How well the document matched
375bc71… drh 1133 ** id: The document id. Format: xNNNNN, x: type, N: number
375bc71… drh 1134 ** snip: A snippet for the match
a00a140… drh 1135 **
a00a140… drh 1136 ** And the srchFlags parameter has been validated. This routine
375bc71… drh 1137 ** fills the X table with search results using FTS indexed search.
a00a140… drh 1138 **
375bc71… drh 1139 ** The companion full-scan search routine is search_fullscan().
a00a140… drh 1140 */
2ddf62a… drh 1141 LOCAL void search_indexed(
a00a140… drh 1142 const char *zPattern, /* The query pattern */
a00a140… drh 1143 unsigned int srchFlags /* What to search over */
a00a140… drh 1144 ){
a00a140… drh 1145 Blob sql;
fc85382… stephan 1146 char *zPat;
c3c4ef1… stephan 1147 static const char *zSnippetCall;
a00a140… drh 1148 if( srchFlags==0 ) return;
9826bd0… drh 1149 sqlite3_create_function(g.db, "rank", 1, SQLITE_UTF8|SQLITE_INNOCUOUS, 0,
a00a140… drh 1150 search_rank_sqlfunc, 0, 0);
fc85382… stephan 1151 zPat = search_simplify_pattern(zPattern);
a00a140… drh 1152 blob_init(&sql, 0, 0);
c3c4ef1… stephan 1153 if( search_index_type(0)==4 ){
17eb363… stephan 1154 /* If this repo is still using the legacy FTS5 search index, then
c3c4ef1… stephan 1155 ** the snippet() function is slightly different */
c3c4ef1… stephan 1156 zSnippetCall = "snippet(ftsidx,'<mark>','</mark>',' ... ',-1,35)";
c3c4ef1… stephan 1157 }else{
c3c4ef1… stephan 1158 /* This is the common case - Using newer FTS5 search index */
c3c4ef1… stephan 1159 zSnippetCall = "snippet(ftsidx,-1,'<mark>','</mark>',' ... ',35)";
c3c4ef1… stephan 1160 }
a00a140… drh 1161 blob_appendf(&sql,
0f96ffb… drh 1162 "INSERT INTO x(label,url,score,id,date,snip) "
a00a140… drh 1163 " SELECT ftsdocs.label,"
a00a140… drh 1164 " ftsdocs.url,"
a00a140… drh 1165 " rank(matchinfo(ftsidx,'pcsx')),"
0f96ffb… drh 1166 " ftsdocs.type || ftsdocs.rid,"
a00a140… drh 1167 " datetime(ftsdocs.mtime),"
c3c4ef1… stephan 1168 " %s"
005ba1e… drh 1169 " FROM ftsidx CROSS JOIN ftsdocs"
a00a140… drh 1170 " WHERE ftsidx MATCH %Q"
c3c4ef1… stephan 1171 " AND ftsdocs.rowid=ftsidx.rowid",
c3c4ef1… stephan 1172 zSnippetCall /*safe-for-%s*/, zPat
a00a140… drh 1173 );
fa6da0d… stephan 1174 fossil_free(zPat);
a00a140… drh 1175 if( srchFlags!=SRCH_ALL ){
a00a140… drh 1176 const char *zSep = " AND (";
a00a140… drh 1177 static const struct { unsigned m; char c; } aMask[] = {
a551d50… andygoth 1178 { SRCH_CKIN, 'c' },
a551d50… andygoth 1179 { SRCH_DOC, 'd' },
a551d50… andygoth 1180 { SRCH_TKT, 't' },
a551d50… andygoth 1181 { SRCH_WIKI, 'w' },
a551d50… andygoth 1182 { SRCH_TECHNOTE, 'e' },
99fcc43… drh 1183 { SRCH_FORUM, 'f' },
6d9f0f5… drh 1184 { SRCH_HELP, 'h' },
a00a140… drh 1185 };
a00a140… drh 1186 int i;
3cb9ba4… andygoth 1187 for(i=0; i<count(aMask); i++){
a00a140… drh 1188 if( srchFlags & aMask[i].m ){
a00a140… drh 1189 blob_appendf(&sql, "%sftsdocs.type='%c'", zSep, aMask[i].c);
a00a140… drh 1190 zSep = " OR ";
a00a140… drh 1191 }
a00a140… drh 1192 }
a00a140… drh 1193 blob_append(&sql,")",1);
a00a140… drh 1194 }
a00a140… drh 1195 db_multi_exec("%s",blob_str(&sql)/*safe-for-%s*/);
a00a140… drh 1196 #if SEARCH_DEBUG_RANK
a00a140… drh 1197 db_multi_exec("UPDATE x SET label=printf('%%s (score=%%s)',label,score)");
a00a140… drh 1198 #endif
a00a140… drh 1199 }
a00a140… drh 1200
bc8075d… drh 1201 /*
bc8075d… drh 1202 ** If z[] is of the form "<mark>TEXT</mark>" where TEXT contains
bc8075d… drh 1203 ** no white-space or punctuation, then return the length of the mark.
bc8075d… drh 1204 */
bc8075d… drh 1205 static int isSnippetMark(const char *z){
bc8075d… drh 1206 int n;
bc8075d… drh 1207 if( strncmp(z,"<mark>",6)!=0 ) return 0;
bc8075d… drh 1208 n = 6;
bc8075d… drh 1209 while( fossil_isalnum(z[n]) ) n++;
bc8075d… drh 1210 if( strncmp(&z[n],"</mark>",7)!=0 ) return 0;
bc8075d… drh 1211 return n+7;
bc8075d… drh 1212 }
bc8075d… drh 1213
bc8075d… drh 1214 /*
bc8075d… drh 1215 ** Return a copy of zSnip (in memory obtained from fossil_malloc()) that
5260fbf… jan.nijtmans 1216 ** has all "<" characters, other than those on <mark> and </mark>,
bc8075d… drh 1217 ** converted into "&lt;". This is similar to htmlize() except that
bc8075d… drh 1218 ** <mark> and </mark> are preserved.
bc8075d… drh 1219 */
bc8075d… drh 1220 static char *cleanSnippet(const char *zSnip){
bc8075d… drh 1221 int i;
bc8075d… drh 1222 int n = 0;
bc8075d… drh 1223 char *z;
cee4af9… drh 1224 if( zSnip==0 ) zSnip = "";
bc8075d… drh 1225 for(i=0; zSnip[i]; i++) if( zSnip[i]=='<' ) n++;
23d659d… drh 1226 z = fossil_malloc( i+n*4+1 );
bc8075d… drh 1227 i = 0;
bc8075d… drh 1228 while( zSnip[0] ){
bc8075d… drh 1229 if( zSnip[0]=='<' ){
bc8075d… drh 1230 n = isSnippetMark(zSnip);
bc8075d… drh 1231 if( n ){
bc8075d… drh 1232 memcpy(&z[i], zSnip, n);
bc8075d… drh 1233 zSnip += n;
bc8075d… drh 1234 i += n;
bc8075d… drh 1235 continue;
bc8075d… drh 1236 }else{
bc8075d… drh 1237 memcpy(&z[i], "&lt;", 4);
bc8075d… drh 1238 i += 4;
bc8075d… drh 1239 zSnip++;
bc8075d… drh 1240 }
bc8075d… drh 1241 }else{
bc8075d… drh 1242 z[i++] = zSnip[0];
bc8075d… drh 1243 zSnip++;
bc8075d… drh 1244 }
bc8075d… drh 1245 }
bc8075d… drh 1246 z[i] = 0;
bc8075d… drh 1247 return z;
bc8075d… drh 1248 }
bc8075d… drh 1249
a00a140… drh 1250
a00a140… drh 1251 /*
a00a140… drh 1252 ** This routine generates web-page output for a search operation.
a00a140… drh 1253 ** Other web-pages can invoke this routine to add search results
a00a140… drh 1254 ** in the middle of the page.
a00a140… drh 1255 **
375bc71… drh 1256 ** This routine works for both full-scan and indexed search. The
375bc71… drh 1257 ** appropriate low-level search routine is called according to the
375bc71… drh 1258 ** current configuration.
375bc71… drh 1259 **
a00a140… drh 1260 ** Return the number of rows.
a00a140… drh 1261 */
a00a140… drh 1262 int search_run_and_output(
a00a140… drh 1263 const char *zPattern, /* The query pattern */
0f96ffb… drh 1264 unsigned int srchFlags, /* What to search over */
0f96ffb… drh 1265 int fDebug /* Extra debugging output */
a00a140… drh 1266 ){
a00a140… drh 1267 Stmt q;
a00a140… drh 1268 int nRow = 0;
13dfcdd… drh 1269 int nLimit = db_get_int("search-limit", 100);
a00a140… drh 1270
13dfcdd… drh 1271 if( P("searchlimit")!=0 ){
13dfcdd… drh 1272 nLimit = atoi(P("searchlimit"));
13dfcdd… drh 1273 }
2e9e369… drh 1274 srchFlags = search_restrict(srchFlags) | (srchFlags & SRCH_HELP);
a00a140… drh 1275 if( srchFlags==0 ) return 0;
a00a140… drh 1276 search_sql_setup(g.db);
a00a140… drh 1277 add_content_sql_commands(g.db);
a00a140… drh 1278 db_multi_exec(
0f96ffb… drh 1279 "CREATE TEMP TABLE x(label,url,score,id,date,snip);"
a00a140… drh 1280 );
a00a140… drh 1281 if( !search_index_exists() ){
375bc71… drh 1282 search_fullscan(zPattern, srchFlags); /* Full-scan search */
a00a140… drh 1283 }else{
375bc71… drh 1284 search_update_index(srchFlags); /* Update the index, if necessary */
e129e11… Torsten 1285 if( srchFlags & SRCH_FORUM_SUBJ ){
e129e11… Torsten 1286 /* Forum subject search: exclude forum from the indexed search and
e129e11… Torsten 1287 ** instead use fullscan restricted to forum subject lines only. */
e129e11… Torsten 1288 unsigned int idxFlags = srchFlags & ~SRCH_FORUM & ~SRCH_FORUM_SUBJ;
e129e11… Torsten 1289 if( idxFlags ) search_indexed(zPattern, idxFlags);
e129e11… Torsten 1290 search_fullscan(zPattern, SRCH_FORUM | SRCH_FORUM_SUBJ);
e129e11… Torsten 1291 }else{
e129e11… Torsten 1292 search_indexed(zPattern, srchFlags); /* Indexed search */
e129e11… Torsten 1293 }
6d9f0f5… drh 1294 if( srchFlags & SRCH_HELP ){
6d9f0f5… drh 1295 search_fullscan(zPattern, SRCH_HELP);
6d9f0f5… drh 1296 }
a00a140… drh 1297 }
13dfcdd… drh 1298 db_prepare(&q, "SELECT url, snip, label, score, id, substr(date,1,10)"
a00a140… drh 1299 " FROM x"
a00a140… drh 1300 " ORDER BY score DESC, date DESC;");
6714c94… drh 1301 while( db_step(&q)==SQLITE_ROW ){
6714c94… drh 1302 const char *zUrl = db_column_text(&q, 0);
6714c94… drh 1303 const char *zSnippet = db_column_text(&q, 1);
6714c94… drh 1304 const char *zLabel = db_column_text(&q, 2);
13dfcdd… drh 1305 const char *zDate = db_column_text(&q, 5);
6714c94… drh 1306 if( nRow==0 ){
6714c94… drh 1307 @ <ol>
6714c94… drh 1308 }
6714c94… drh 1309 nRow++;
9dba92f… drh 1310 @ <li><p><a href='%R%s(zUrl)'>%h(zLabel)</a>
0f96ffb… drh 1311 if( fDebug ){
58034a4… drh 1312 @ (%e(db_column_double(&q,3)), %s(db_column_text(&q,4))
58034a4… drh 1313 }
f5482a0… wyoung 1314 @ <br><span class='snippet'>%z(cleanSnippet(zSnippet)) \
11ffd84… drh 1315 if( zLabel && zDate && zDate[0] && strstr(zLabel,zDate)==0 ){
13dfcdd… drh 1316 @ <small>(%h(zDate))</small>
13dfcdd… drh 1317 }
13dfcdd… drh 1318 @ </span></li>
13dfcdd… drh 1319 if( nLimit && nRow>=nLimit ) break;
6714c94… drh 1320 }
6714c94… drh 1321 db_finalize(&q);
6714c94… drh 1322 if( nRow ){
6714c94… drh 1323 @ </ol>
6714c94… drh 1324 }
6714c94… drh 1325 return nRow;
6714c94… drh 1326 }
6714c94… drh 1327
6714c94… drh 1328 /*
bb4a130… drh 1329 ** Generate some HTML for doing search. At a minimum include the
bb4a130… drh 1330 ** Search-Text entry form. If the "s" query parameter is present, also
bb4a130… drh 1331 ** show search results.
bb4a130… drh 1332 **
c0c0bae… drh 1333 ** The srchFlags parameter restricts the set of documents to be searched.
c0c0bae… drh 1334 ** srchFlags should normally be either a single search category or all
c0c0bae… drh 1335 ** categories. Any srchFlags with two or more bits set
bb4a130… drh 1336 ** is treated like SRCH_ALL for display purposes.
bb4a130… drh 1337 **
c0c0bae… drh 1338 ** This routine automatically restricts srchFlag according to user
c0c0bae… drh 1339 ** permissions and the server configuration. The entry box is shown
c0c0bae… drh 1340 ** disabled if srchFlags is 0 after these restrictions are applied.
c0c0bae… drh 1341 **
99fcc43… drh 1342 ** The mFlags value controls options:
99fcc43… drh 1343 **
99fcc43… drh 1344 ** 0x01 If the y= query parameter is present, use it as an addition
99fcc43… drh 1345 ** restriction what to search.
99fcc43… drh 1346 **
99fcc43… drh 1347 ** 0x02 Show nothing if search is disabled.
99fcc43… drh 1348 **
99fcc43… drh 1349 ** Return true if there are search results.
bb4a130… drh 1350 */
2e9e369… drh 1351 int search_screen(unsigned srchAllowed, int mFlags){
bb4a130… drh 1352 const char *zType = 0;
bb4a130… drh 1353 const char *zClass = 0;
c799881… drh 1354 const char *zDisable1;
c799881… drh 1355 const char *zDisable2;
bb4a130… drh 1356 const char *zPattern;
0f96ffb… drh 1357 int fDebug = PB("debug");
99fcc43… drh 1358 int haveResult = 0;
2e9e369… drh 1359 int srchThisTime;
2e9e369… drh 1360 const char *zY = PD("y","all");
2e9e369… drh 1361 if( zY[0]=='h' && zY[1]==0 ){
2e9e369… drh 1362 srchAllowed = search_restrict(srchAllowed) | (srchAllowed & SRCH_HELP);
2e9e369… drh 1363 }else{
2e9e369… drh 1364 srchAllowed = search_restrict(srchAllowed);
2e9e369… drh 1365 }
2e9e369… drh 1366 switch( srchAllowed ){
a551d50… andygoth 1367 case SRCH_CKIN: zType = " Check-ins"; zClass = "Ckin"; break;
a551d50… andygoth 1368 case SRCH_DOC: zType = " Docs"; zClass = "Doc"; break;
a551d50… andygoth 1369 case SRCH_TKT: zType = " Tickets"; zClass = "Tkt"; break;
a551d50… andygoth 1370 case SRCH_WIKI: zType = " Wiki"; zClass = "Wiki"; break;
a551d50… andygoth 1371 case SRCH_TECHNOTE: zType = " Tech Notes"; zClass = "Note"; break;
99fcc43… drh 1372 case SRCH_FORUM: zType = " Forum"; zClass = "Frm"; break;
6d9f0f5… drh 1373 case SRCH_HELP: zType = " Help"; zClass = "Hlp"; break;
a551d50… andygoth 1374 }
2e9e369… drh 1375 if( srchAllowed==0 ){
99fcc43… drh 1376 if( mFlags & 0x02 ) return 0;
c799881… drh 1377 zDisable1 = " disabled";
c799881… drh 1378 zDisable2 = " disabled";
bb4a130… drh 1379 zPattern = "";
bb4a130… drh 1380 }else{
dc7d0a2… drh 1381 zDisable1 = ""; /* Was: " autofocus" */
c799881… drh 1382 zDisable2 = "";
bb4a130… drh 1383 zPattern = PD("s","");
bb4a130… drh 1384 }
9dba92f… drh 1385 @ <form method='GET' action='%R/%T(g.zPath)'>
bb4a130… drh 1386 if( zClass ){
bb4a130… drh 1387 @ <div class='searchForm searchForm%s(zClass)'>
bb4a130… drh 1388 }else{
bb4a130… drh 1389 @ <div class='searchForm'>
bb4a130… drh 1390 }
c799881… drh 1391 @ <input type="text" name="s" size="40" value="%h(zPattern)"%s(zDisable1)>
2e9e369… drh 1392 srchThisTime = srchAllowed;
2e9e369… drh 1393 if( (mFlags & 0x01)!=0 && (srchAllowed & (srchAllowed-1))!=0 ){
2e9e369… drh 1394 static const struct {
2e9e369… drh 1395 const char *z;
2e9e369… drh 1396 const char *zNm;
2e9e369… drh 1397 unsigned m;
2e9e369… drh 1398 } aY[] = {
a551d50… andygoth 1399 { "all", "All", SRCH_ALL },
a551d50… andygoth 1400 { "c", "Check-ins", SRCH_CKIN },
a551d50… andygoth 1401 { "d", "Docs", SRCH_DOC },
a551d50… andygoth 1402 { "t", "Tickets", SRCH_TKT },
a551d50… andygoth 1403 { "w", "Wiki", SRCH_WIKI },
a551d50… andygoth 1404 { "e", "Tech Notes", SRCH_TECHNOTE },
99fcc43… drh 1405 { "f", "Forum", SRCH_FORUM },
6d9f0f5… drh 1406 { "h", "Help", SRCH_HELP },
c0c0bae… drh 1407 };
c0c0bae… drh 1408 int i;
c0c0bae… drh 1409 @ <select size='1' name='y'>
3cb9ba4… andygoth 1410 for(i=0; i<count(aY); i++){
2e9e369… drh 1411 if( (aY[i].m & srchAllowed)==0 ) continue;
2e9e369… drh 1412 if( aY[i].m==SRCH_HELP && fossil_strcmp(zY,"h")!=0
2e9e369… drh 1413 && search_restrict(SRCH_HELP)==0 ) continue;
c0c0bae… drh 1414 cgi_printf("<option value='%s'", aY[i].z);
c0c0bae… drh 1415 if( fossil_strcmp(zY,aY[i].z)==0 ){
2e9e369… drh 1416 srchThisTime &= aY[i].m;
c0c0bae… drh 1417 cgi_printf(" selected");
c0c0bae… drh 1418 }
c0c0bae… drh 1419 cgi_printf(">%s</option>\n", aY[i].zNm);
c0c0bae… drh 1420 }
c0c0bae… drh 1421 @ </select>
0f96ffb… drh 1422 }
0f96ffb… drh 1423 if( fDebug ){
0f96ffb… drh 1424 @ <input type="hidden" name="debug" value="1">
c0c0bae… drh 1425 }
c799881… drh 1426 @ <input type="submit" value="Search%s(zType)"%s(zDisable2)>
e129e11… Torsten 1427 if( (srchThisTime & SRCH_FORUM)!=0 ){
e129e11… Torsten 1428 int bSubjOnly = PB("subj");
e129e11… Torsten 1429 @ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<label><input type='checkbox' name='subj' value='1'%s(bSubjOnly?" checked":"")>
e129e11… Torsten 1430 @ search subject lines only</label>
e129e11… Torsten 1431 if( bSubjOnly ) srchThisTime |= SRCH_FORUM_SUBJ;
e129e11… Torsten 1432 }
2e9e369… drh 1433 if( srchAllowed==0 && srchThisTime==0 ){
bb4a130… drh 1434 @ <p class="generalError">Search is disabled</p>
bb4a130… drh 1435 }
bb4a130… drh 1436 @ </div></form>
bb4a130… drh 1437 while( fossil_isspace(zPattern[0]) ) zPattern++;
bb4a130… drh 1438 if( zPattern[0] ){
bb4a130… drh 1439 if( zClass ){
bb4a130… drh 1440 @ <div class='searchResult searchResult%s(zClass)'>
bb4a130… drh 1441 }else{
bb4a130… drh 1442 @ <div class='searchResult'>
bb4a130… drh 1443 }
2e9e369… drh 1444 if( search_run_and_output(zPattern, srchThisTime, fDebug)==0 ){
bb4a130… drh 1445 @ <p class='searchEmpty'>No matches for: <span>%h(zPattern)</span></p>
bb4a130… drh 1446 }
bb4a130… drh 1447 @ </div>
99fcc43… drh 1448 haveResult = 1;
bb4a130… drh 1449 }
99fcc43… drh 1450 return haveResult;
bb4a130… drh 1451 }
bb4a130… drh 1452
bb4a130… drh 1453 /*
7ab0328… drh 1454 ** WEBPAGE: search
6714c94… drh 1455 **
6714c94… drh 1456 ** Search for check-in comments, documents, tickets, or wiki that
6714c94… drh 1457 ** match a user-supplied pattern.
7ab0328… drh 1458 **
7ab0328… drh 1459 ** s=PATTERN Specify the full-text pattern to search for
7ab0328… drh 1460 ** y=TYPE What to search.
faf1982… drh 1461 ** c -> check-ins,
faf1982… drh 1462 ** d -> documentation,
faf1982… drh 1463 ** t -> tickets,
faf1982… drh 1464 ** w -> wiki,
faf1982… drh 1465 ** e -> tech notes,
faf1982… drh 1466 ** f -> forum,
faf1982… drh 1467 ** h -> built-in help,
faf1982… drh 1468 ** all -> everything.
6714c94… drh 1469 */
6714c94… drh 1470 void search_page(void){
809c010… stephan 1471 const int isSearch = P("s")!=0;
6714c94… drh 1472 login_check_credentials();
809c010… stephan 1473 style_header("Search%s", isSearch ? " Results" : "");
57f1e87… drh 1474 cgi_check_for_malice();
c0c0bae… drh 1475 search_screen(SRCH_ALL, 1);
112c713… drh 1476 style_finish_page();
48e1e18… drh 1477 }
48e1e18… drh 1478
48e1e18… drh 1479
48e1e18… drh 1480 /*
48e1e18… drh 1481 ** This is a helper function for search_stext(). Writing into pOut
48e1e18… drh 1482 ** the search text obtained from pIn according to zMimetype.
112c713… drh 1483 **
9d9bf1a… drh 1484 ** If a title is not specified in zTitle (e.g. for wiki pages that do not
9d9bf1a… drh 1485 ** include the title in the body), it is determined from the page content.
9d9bf1a… drh 1486 **
0f96ffb… drh 1487 ** The title of the document is the first line of text. All subsequent
0f96ffb… drh 1488 ** lines are the body. If the document has no title, the first line
0f96ffb… drh 1489 ** is blank.
48e1e18… drh 1490 */
48e1e18… drh 1491 static void get_stext_by_mimetype(
48e1e18… drh 1492 Blob *pIn,
48e1e18… drh 1493 const char *zMimetype,
9d9bf1a… drh 1494 const char *zTitle,
48e1e18… drh 1495 Blob *pOut
48e1e18… drh 1496 ){
48e1e18… drh 1497 Blob html, title;
9d9bf1a… drh 1498 Blob *pHtml = &html;
48e1e18… drh 1499 blob_init(&html, 0, 0);
9d9bf1a… drh 1500 if( zTitle==0 ){
9d9bf1a… drh 1501 blob_init(&title, 0, 0);
9d9bf1a… drh 1502 }else{
9d9bf1a… drh 1503 blob_init(&title, zTitle, -1);
9d9bf1a… drh 1504 }
48e1e18… drh 1505 if( zMimetype==0 ) zMimetype = "text/plain";
48e1e18… drh 1506 if( fossil_strcmp(zMimetype,"text/x-fossil-wiki")==0 ){
9d9bf1a… drh 1507 if( blob_size(&title) ){
0f96ffb… drh 1508 wiki_convert(pIn, &html, 0);
9d9bf1a… drh 1509 }else{
9d9bf1a… drh 1510 Blob tail;
9d9bf1a… drh 1511 blob_init(&tail, 0, 0);
9d9bf1a… drh 1512 if( wiki_find_title(pIn, &title, &tail) ){
9d9bf1a… drh 1513 blob_appendf(pOut, "%s\n", blob_str(&title));
9d9bf1a… drh 1514 wiki_convert(&tail, &html, 0);
9d9bf1a… drh 1515 blob_reset(&tail);
9d9bf1a… drh 1516 }else{
9d9bf1a… drh 1517 blob_append(pOut, "\n", 1);
9d9bf1a… drh 1518 wiki_convert(pIn, &html, 0);
9d9bf1a… drh 1519 }
0f96ffb… drh 1520 }
fd59eb1… drh 1521 html_to_plaintext(blob_str(&html), pOut, 0);
48e1e18… drh 1522 }else if( fossil_strcmp(zMimetype,"text/x-markdown")==0 ){
9d9bf1a… drh 1523 markdown_to_html(pIn, blob_size(&title) ? NULL : &title, &html);
48e1e18… drh 1524 }else if( fossil_strcmp(zMimetype,"text/html")==0 ){
9d9bf1a… drh 1525 if( blob_size(&title)==0 ) doc_is_embedded_html(pIn, &title);
9d9bf1a… drh 1526 pHtml = pIn;
9d9bf1a… drh 1527 }
9d9bf1a… drh 1528 blob_appendf(pOut, "%s\n", blob_str(&title));
9d9bf1a… drh 1529 if( blob_size(pHtml) ){
fd59eb1… drh 1530 html_to_plaintext(blob_str(pHtml), pOut, 0);
48e1e18… drh 1531 }else{
0f96ffb… drh 1532 blob_append(pOut, blob_buffer(pIn), blob_size(pIn));
48e1e18… drh 1533 }
48e1e18… drh 1534 blob_reset(&html);
48e1e18… drh 1535 blob_reset(&title);
48e1e18… drh 1536 }
e2230a4… drh 1537
e2230a4… drh 1538 /*
e2230a4… drh 1539 ** Query pQuery is pointing at a single row of output. Append a text
e2230a4… drh 1540 ** representation of every text-compatible column to pAccum.
e2230a4… drh 1541 */
0f96ffb… drh 1542 static void append_all_ticket_fields(Blob *pAccum, Stmt *pQuery, int iTitle){
e2230a4… drh 1543 int n = db_column_count(pQuery);
e2230a4… drh 1544 int i;
0f96ffb… drh 1545 const char *zMime = 0;
0f96ffb… drh 1546 if( iTitle>=0 && iTitle<n ){
0f96ffb… drh 1547 if( db_column_type(pQuery,iTitle)==SQLITE_TEXT ){
0f96ffb… drh 1548 blob_append(pAccum, db_column_text(pQuery,iTitle), -1);
0f96ffb… drh 1549 }
0f96ffb… drh 1550 blob_append(pAccum, "\n", 1);
0f96ffb… drh 1551 }
e2230a4… drh 1552 for(i=0; i<n; i++){
e2230a4… drh 1553 const char *zColName = db_column_name(pQuery,i);
0f96ffb… drh 1554 int eType = db_column_type(pQuery,i);
0f96ffb… drh 1555 if( i==iTitle ) continue;
e2230a4… drh 1556 if( fossil_strnicmp(zColName,"tkt_",4)==0 ) continue;
0f96ffb… drh 1557 if( fossil_strnicmp(zColName,"private_",8)==0 ) continue;
0f96ffb… drh 1558 if( eType==SQLITE_BLOB || eType==SQLITE_NULL ) continue;
0f96ffb… drh 1559 if( fossil_stricmp(zColName,"mimetype")==0 ){
0f96ffb… drh 1560 zMime = db_column_text(pQuery,i);
0f96ffb… drh 1561 if( fossil_strcmp(zMime,"text/plain")==0 ) zMime = 0;
0f96ffb… drh 1562 }else if( zMime==0 || eType!=SQLITE_TEXT ){
0f96ffb… drh 1563 blob_appendf(pAccum, "%s: %s |\n", zColName, db_column_text(pQuery,i));
0f96ffb… drh 1564 }else{
0f96ffb… drh 1565 Blob txt;
0f96ffb… drh 1566 blob_init(&txt, db_column_text(pQuery,i), -1);
0f96ffb… drh 1567 blob_appendf(pAccum, "%s: ", zColName);
9d9bf1a… drh 1568 get_stext_by_mimetype(&txt, zMime, NULL, pAccum);
0f96ffb… drh 1569 blob_append(pAccum, " |", 2);
0f96ffb… drh 1570 blob_reset(&txt);
e2230a4… drh 1571 }
e2230a4… drh 1572 }
e2230a4… drh 1573 }
e2230a4… drh 1574
ca833ff… drh 1575
ca833ff… drh 1576 /*
48e1e18… drh 1577 ** Return "search text" - a reduced version of a document appropriate for
48e1e18… drh 1578 ** full text search and/or for constructing a search result snippet.
48e1e18… drh 1579 **
48e1e18… drh 1580 ** cType: d Embedded documentation
48e1e18… drh 1581 ** w Wiki page
48e1e18… drh 1582 ** c Check-in comment
48e1e18… drh 1583 ** t Ticket text
a551d50… andygoth 1584 ** e Tech note
99fcc43… drh 1585 ** f Forum
ca833ff… drh 1586 **
ca833ff… drh 1587 ** rid The RID of an artifact that defines the object
ca833ff… drh 1588 ** being searched.
48e1e18… drh 1589 **
375bc71… drh 1590 ** zName Name of the object being searched. This is used
375bc71… drh 1591 ** only to help figure out the mimetype (text/plain,
375bc71… drh 1592 ** test/html, test/x-fossil-wiki, or text/x-markdown)
375bc71… drh 1593 ** so that the code can know how to simplify the text.
48e1e18… drh 1594 */
48e1e18… drh 1595 void search_stext(
48e1e18… drh 1596 char cType, /* Type of document */
ca833ff… drh 1597 int rid, /* BLOB.RID or TAG.TAGID value for document */
a00a140… drh 1598 const char *zName, /* Auxiliary information */
48e1e18… drh 1599 Blob *pOut /* OUT: Initialize to the search text */
48e1e18… drh 1600 ){
48e1e18… drh 1601 blob_init(pOut, 0, 0);
48e1e18… drh 1602 switch( cType ){
ca833ff… drh 1603 case 'd': { /* Documents */
48e1e18… drh 1604 Blob doc;
0f96ffb… drh 1605 content_get(rid, &doc);
48e1e18… drh 1606 blob_to_utf8_no_bom(&doc, 0);
9d9bf1a… drh 1607 get_stext_by_mimetype(&doc, mimetype_from_name(zName), NULL, pOut);
48e1e18… drh 1608 blob_reset(&doc);
48e1e18… drh 1609 break;
48e1e18… drh 1610 }
99fcc43… drh 1611 case 'f': /* Forum messages */
a551d50… andygoth 1612 case 'e': /* Tech Notes */
ca833ff… drh 1613 case 'w': { /* Wiki */
a551d50… andygoth 1614 Manifest *pWiki = manifest_get(rid,
99fcc43… drh 1615 cType == 'e' ? CFTYPE_EVENT :
99fcc43… drh 1616 cType == 'f' ? CFTYPE_FORUM : CFTYPE_WIKI, 0);
48e1e18… drh 1617 Blob wiki;
48e1e18… drh 1618 if( pWiki==0 ) break;
99fcc43… drh 1619 if( cType=='f' ){
99fcc43… drh 1620 blob_init(&wiki, 0, 0);
99fcc43… drh 1621 if( pWiki->zThreadTitle ){
99fcc43… drh 1622 blob_appendf(&wiki, "<h1>%h</h1>\n", pWiki->zThreadTitle);
99fcc43… drh 1623 }
99fcc43… drh 1624 blob_appendf(&wiki, "From %s:\n\n%s", pWiki->zUser, pWiki->zWiki);
99fcc43… drh 1625 }else{
99fcc43… drh 1626 blob_init(&wiki, pWiki->zWiki, -1);
99fcc43… drh 1627 }
48e1e18… drh 1628 get_stext_by_mimetype(&wiki, wiki_filter_mimetypes(pWiki->zMimetype),
9d9bf1a… drh 1629 cType=='w' ? pWiki->zWikiTitle : NULL, pOut);
48e1e18… drh 1630 blob_reset(&wiki);
48e1e18… drh 1631 manifest_destroy(pWiki);
48e1e18… drh 1632 break;
48e1e18… drh 1633 }
e2230a4… drh 1634 case 'c': { /* Check-in Comments */
8e02c26… drh 1635 static Stmt q;
0f96ffb… drh 1636 static int isPlainText = -1;
8e02c26… drh 1637 db_static_prepare(&q,
8e02c26… drh 1638 "SELECT coalesce(ecomment,comment)"
8e02c26… drh 1639 " ||' (user: '||coalesce(euser,user,'?')"
8e02c26… drh 1640 " ||', tags: '||"
8e02c26… drh 1641 " (SELECT group_concat(substr(tag.tagname,5),',')"
8e02c26… drh 1642 " FROM tag, tagxref"
8e02c26… drh 1643 " WHERE tagname GLOB 'sym-*' AND tag.tagid=tagxref.tagid"
8e02c26… drh 1644 " AND tagxref.rid=event.objid AND tagxref.tagtype>0)"
8e02c26… drh 1645 " ||')'"
8e02c26… drh 1646 " FROM event WHERE objid=:x AND type='ci'");
0f96ffb… drh 1647 if( isPlainText<0 ){
0f96ffb… drh 1648 isPlainText = db_get_boolean("timeline-plaintext",0);
0f96ffb… drh 1649 }
8e02c26… drh 1650 db_bind_int(&q, ":x", rid);
8e02c26… drh 1651 if( db_step(&q)==SQLITE_ROW ){
8e02c26… drh 1652 blob_append(pOut, "\n", 1);
0f96ffb… drh 1653 if( isPlainText ){
0f96ffb… drh 1654 db_column_blob(&q, 0, pOut);
0f96ffb… drh 1655 }else{
0f96ffb… drh 1656 Blob x;
0f96ffb… drh 1657 blob_init(&x,0,0);
0f96ffb… drh 1658 db_column_blob(&q, 0, &x);
9d9bf1a… drh 1659 get_stext_by_mimetype(&x, "text/x-fossil-wiki", NULL, pOut);
0f96ffb… drh 1660 blob_reset(&x);
0f96ffb… drh 1661 }
8e02c26… drh 1662 }
8e02c26… drh 1663 db_reset(&q);
8e02c26… drh 1664 break;
8e02c26… drh 1665 }
e2230a4… drh 1666 case 't': { /* Tickets */
e2230a4… drh 1667 static Stmt q1;
0f96ffb… drh 1668 static int iTitle = -1;
e2230a4… drh 1669 db_static_prepare(&q1, "SELECT * FROM ticket WHERE tkt_id=:rid");
e2230a4… drh 1670 db_bind_int(&q1, ":rid", rid);
e2230a4… drh 1671 if( db_step(&q1)==SQLITE_ROW ){
0f96ffb… drh 1672 if( iTitle<0 ){
0f96ffb… drh 1673 int n = db_column_count(&q1);
0f96ffb… drh 1674 for(iTitle=0; iTitle<n; iTitle++){
0f96ffb… drh 1675 if( fossil_stricmp(db_column_name(&q1,iTitle),"title")==0 ) break;
0f96ffb… drh 1676 }
0f96ffb… drh 1677 }
0f96ffb… drh 1678 append_all_ticket_fields(pOut, &q1, iTitle);
e2230a4… drh 1679 }
e2230a4… drh 1680 db_reset(&q1);
e2230a4… drh 1681 if( db_table_exists("repository","ticketchng") ){
e2230a4… drh 1682 static Stmt q2;
e2230a4… drh 1683 db_static_prepare(&q2, "SELECT * FROM ticketchng WHERE tkt_id=:rid"
e2230a4… drh 1684 " ORDER BY tkt_mtime");
e2230a4… drh 1685 db_bind_int(&q2, ":rid", rid);
e2230a4… drh 1686 while( db_step(&q2)==SQLITE_ROW ){
0f96ffb… drh 1687 append_all_ticket_fields(pOut, &q2, -1);
e2230a4… drh 1688 }
e2230a4… drh 1689 db_reset(&q2);
e2230a4… drh 1690 }
e2230a4… drh 1691 break;
e2230a4… drh 1692 }
8e02c26… drh 1693 }
0f96ffb… drh 1694 }
0f96ffb… drh 1695
0f96ffb… drh 1696 /*
7fbc1c3… jan.nijtmans 1697 ** This routine is a wrapper around search_stext().
0f96ffb… drh 1698 **
0f96ffb… drh 1699 ** This routine looks up the search text, stores it in an internal
0f96ffb… drh 1700 ** buffer, and returns a pointer to the text. Subsequent requests
0f96ffb… drh 1701 ** for the same document return the same pointer. The returned pointer
0f96ffb… drh 1702 ** is valid until the next invocation of this routine. Call this routine
0f96ffb… drh 1703 ** with an eType of 0 to clear the cache.
0f96ffb… drh 1704 */
0f96ffb… drh 1705 char *search_stext_cached(
0f96ffb… drh 1706 char cType, /* Type of document */
0f96ffb… drh 1707 int rid, /* BLOB.RID or TAG.TAGID value for document */
375bc71… drh 1708 const char *zName, /* Auxiliary information, for mimetype */
0f96ffb… drh 1709 int *pnTitle /* OUT: length of title in bytes excluding \n */
0f96ffb… drh 1710 ){
0f96ffb… drh 1711 static struct {
0f96ffb… drh 1712 Blob stext; /* Cached search text */
0f96ffb… drh 1713 char cType; /* The type */
0f96ffb… drh 1714 int rid; /* The RID */
0f96ffb… drh 1715 int nTitle; /* Number of bytes in title */
0f96ffb… drh 1716 } cache;
0f96ffb… drh 1717 int i;
0f96ffb… drh 1718 char *z;
0f96ffb… drh 1719 if( cType!=cache.cType || rid!=cache.rid ){
0f96ffb… drh 1720 if( cache.rid>0 ){
0f96ffb… drh 1721 blob_reset(&cache.stext);
0f96ffb… drh 1722 }else{
0f96ffb… drh 1723 blob_init(&cache.stext,0,0);
0f96ffb… drh 1724 }
0f96ffb… drh 1725 cache.cType = cType;
0f96ffb… drh 1726 cache.rid = rid;
0f96ffb… drh 1727 if( cType==0 ) return 0;
0f96ffb… drh 1728 search_stext(cType, rid, zName, &cache.stext);
0f96ffb… drh 1729 z = blob_str(&cache.stext);
0f96ffb… drh 1730 for(i=0; z[i] && z[i]!='\n'; i++){}
0f96ffb… drh 1731 cache.nTitle = i;
0f96ffb… drh 1732 }
0f96ffb… drh 1733 if( pnTitle ) *pnTitle = cache.nTitle;
0f96ffb… drh 1734 return blob_str(&cache.stext);
ca833ff… drh 1735 }
48e1e18… drh 1736
48e1e18… drh 1737 /*
48e1e18… drh 1738 ** COMMAND: test-search-stext
48e1e18… drh 1739 **
375bc71… drh 1740 ** Usage: fossil test-search-stext TYPE RID NAME
375bc71… drh 1741 **
375bc71… drh 1742 ** Compute the search text for document TYPE-RID whose name is NAME.
a551d50… andygoth 1743 ** The TYPE is one of "c", "d", "t", "w", or "e". The RID is the document
375bc71… drh 1744 ** ID. The NAME is used to figure out a mimetype to use for formatting
375bc71… drh 1745 ** the raw document text.
48e1e18… drh 1746 */
48e1e18… drh 1747 void test_search_stext(void){
48e1e18… drh 1748 Blob out;
ca833ff… drh 1749 db_find_and_open_repository(0,0);
ca833ff… drh 1750 if( g.argc!=5 ) usage("TYPE RID NAME");
ca833ff… drh 1751 search_stext(g.argv[2][0], atoi(g.argv[3]), g.argv[4], &out);
a00a140… drh 1752 fossil_print("%s\n",blob_str(&out));
a00a140… drh 1753 blob_reset(&out);
a00a140… drh 1754 }
a00a140… drh 1755
0f96ffb… drh 1756 /*
0f96ffb… drh 1757 ** COMMAND: test-convert-stext
0f96ffb… drh 1758 **
0f96ffb… drh 1759 ** Usage: fossil test-convert-stext FILE MIMETYPE
0f96ffb… drh 1760 **
0f96ffb… drh 1761 ** Read the content of FILE and convert it to stext according to MIMETYPE.
0f96ffb… drh 1762 ** Send the result to standard output.
0f96ffb… drh 1763 */
0f96ffb… drh 1764 void test_convert_stext(void){
0f96ffb… drh 1765 Blob in, out;
0f96ffb… drh 1766 db_find_and_open_repository(0,0);
0f96ffb… drh 1767 if( g.argc!=4 ) usage("FILENAME MIMETYPE");
1772357… drh 1768 blob_read_from_file(&in, g.argv[2], ExtFILE);
0f96ffb… drh 1769 blob_init(&out, 0, 0);
9d9bf1a… drh 1770 get_stext_by_mimetype(&in, g.argv[3], NULL, &out);
0f96ffb… drh 1771 fossil_print("%s\n",blob_str(&out));
0f96ffb… drh 1772 blob_reset(&in);
0f96ffb… drh 1773 blob_reset(&out);
0f96ffb… drh 1774 }
0f96ffb… drh 1775
0e5d27f… stephan 1776 /*
0e5d27f… stephan 1777 ** The schema for the full-text index. The %s part must be an empty
0e5d27f… stephan 1778 ** string or a comma followed by additional flags for the FTS virtual
0e5d27f… stephan 1779 ** table.
a00a140… drh 1780 */
5260fbf… jan.nijtmans 1781 static const char zFtsSchema[] =
a00a140… drh 1782 @ -- One entry for each possible search result
06aec61… drh 1783 @ CREATE TABLE IF NOT EXISTS repository.ftsdocs(
c3c4ef1… stephan 1784 @ rowid INTEGER PRIMARY KEY, -- Maps to the ftsidx.rowid
a00a140… drh 1785 @ type CHAR(1), -- Type of document
a00a140… drh 1786 @ rid INTEGER, -- BLOB.RID or TAG.TAGID for the document
a00a140… drh 1787 @ name TEXT, -- Additional document description
a00a140… drh 1788 @ idxed BOOLEAN, -- True if currently in the index
a00a140… drh 1789 @ label TEXT, -- Label to print on search results
a00a140… drh 1790 @ url TEXT, -- URL to access this document
a00a140… drh 1791 @ mtime DATE, -- Date when document created
0f96ffb… drh 1792 @ bx TEXT, -- Temporary "body" content cache
a00a140… drh 1793 @ UNIQUE(type,rid)
a00a140… drh 1794 @ );
06aec61… drh 1795 @ CREATE INDEX repository.ftsdocIdxed ON ftsdocs(type,rid,name) WHERE idxed==0;
06aec61… drh 1796 @ CREATE INDEX repository.ftsdocName ON ftsdocs(name) WHERE type='w';
06aec61… drh 1797 @ CREATE VIEW IF NOT EXISTS repository.ftscontent AS
a00a140… drh 1798 @ SELECT rowid, type, rid, name, idxed, label, url, mtime,
0f96ffb… drh 1799 @ title(type,rid,name) AS 'title', body(type,rid,name) AS 'body'
a00a140… drh 1800 @ FROM ftsdocs;
06aec61… drh 1801 @ CREATE VIRTUAL TABLE IF NOT EXISTS repository.ftsidx
c3c4ef1… stephan 1802 @ USING fts5(content="ftscontent", title, body%s);
a00a140… drh 1803 ;
a00a140… drh 1804 static const char zFtsDrop[] =
06aec61… drh 1805 @ DROP TABLE IF EXISTS repository.ftsidx;
06aec61… drh 1806 @ DROP VIEW IF EXISTS repository.ftscontent;
06aec61… drh 1807 @ DROP TABLE IF EXISTS repository.ftsdocs;
fc85382… stephan 1808 @ DROP TABLE IF EXISTS repository.chatfts1;
a00a140… drh 1809 ;
0e5d27f… stephan 1810
0e5d27f… stephan 1811 #if INTERFACE
0e5d27f… stephan 1812 /*
0e5d27f… stephan 1813 ** Values for the search-tokenizer config option.
0e5d27f… stephan 1814 */
e180dbb… stephan 1815 #define FTS5TOK_NONE 0 /* disabled */
e180dbb… stephan 1816 #define FTS5TOK_PORTER 1 /* porter stemmer */
e180dbb… stephan 1817 #define FTS5TOK_UNICODE61 2 /* unicode61 tokenizer */
e180dbb… stephan 1818 #define FTS5TOK_TRIGRAM 3 /* trigram tokenizer */
0e5d27f… stephan 1819 #endif
0e5d27f… stephan 1820
0e5d27f… stephan 1821 /*
0e5d27f… stephan 1822 ** Cached FTS5TOK_xyz value for search_tokenizer_type() and
0e5d27f… stephan 1823 ** friends.
0e5d27f… stephan 1824 */
0e5d27f… stephan 1825 static int iFtsTokenizer = -1;
0e5d27f… stephan 1826
0e5d27f… stephan 1827 /*
0e5d27f… stephan 1828 ** Returns one of the FTS5TOK_xyz values, depending on the value of
0e5d27f… stephan 1829 ** the search-tokenizer config entry, defaulting to FTS5TOK_NONE. The
0e5d27f… stephan 1830 ** result of the first call is cached for subsequent calls unless
0e5d27f… stephan 1831 ** bRecheck is true.
0e5d27f… stephan 1832 */
0e5d27f… stephan 1833 int search_tokenizer_type(int bRecheck){
0e5d27f… stephan 1834 char *z;
0e5d27f… stephan 1835 if( iFtsTokenizer>=0 && bRecheck==0 ){
0e5d27f… stephan 1836 return iFtsTokenizer;
0e5d27f… stephan 1837 }
0e5d27f… stephan 1838 z = db_get("search-tokenizer",0);
0e5d27f… stephan 1839 if( 0==z ){
0e5d27f… stephan 1840 iFtsTokenizer = FTS5TOK_NONE;
0e5d27f… stephan 1841 }else if(0==fossil_strcmp(z,"porter")){
0e5d27f… stephan 1842 iFtsTokenizer = FTS5TOK_PORTER;
e180dbb… stephan 1843 }else if(0==fossil_strcmp(z,"unicode61")){
e180dbb… stephan 1844 iFtsTokenizer = FTS5TOK_UNICODE61;
0e5d27f… stephan 1845 }else if(0==fossil_strcmp(z,"trigram")){
0e5d27f… stephan 1846 iFtsTokenizer = FTS5TOK_TRIGRAM;
0e5d27f… stephan 1847 }else{
0e5d27f… stephan 1848 iFtsTokenizer = is_truth(z) ? FTS5TOK_PORTER : FTS5TOK_NONE;
0e5d27f… stephan 1849 }
0e5d27f… stephan 1850 fossil_free(z);
0e5d27f… stephan 1851 return iFtsTokenizer;
e180dbb… stephan 1852 }
e180dbb… stephan 1853
e180dbb… stephan 1854 /*
fc85382… stephan 1855 ** Returns a string in the form ",tokenize=X", where X is the string
fc85382… stephan 1856 ** counterpart of the given FTS5TOK_xyz value. Returns "" if tokType
fc85382… stephan 1857 ** does not correspond to a known FTS5 tokenizer.
fc85382… stephan 1858 */
fc85382… stephan 1859 const char * search_tokenize_arg_for_type(int tokType){
fc85382… stephan 1860 switch( tokType ){
fc85382… stephan 1861 case FTS5TOK_PORTER: return ",tokenize=porter";
fc85382… stephan 1862 case FTS5TOK_UNICODE61: return ",tokenize=unicode61";
fc85382… stephan 1863 case FTS5TOK_TRIGRAM: return ",tokenize=trigram";
fc85382… stephan 1864 case FTS5TOK_NONE:
fc85382… stephan 1865 default: return "";
fc85382… stephan 1866 }
fc85382… stephan 1867 }
fc85382… stephan 1868
fc85382… stephan 1869 /*
0e5d27f… stephan 1870 ** Returns a string value suitable for use as the search-tokenizer
0e5d27f… stephan 1871 ** setting's value, depending on the value of z. If z is 0 then the
0e5d27f… stephan 1872 ** current search-tokenizer value is used as the basis for formulating
0e5d27f… stephan 1873 ** the result (which may differ from the current value but will have
0e5d27f… stephan 1874 ** the same meaning). Any unknown/unsupported value is interpreted as
0e5d27f… stephan 1875 ** "off".
0e5d27f… stephan 1876 */
0e5d27f… stephan 1877 const char *search_tokenizer_for_string(const char *z){
0e5d27f… stephan 1878 char * zTmp = 0;
0e5d27f… stephan 1879 const char *zRc = 0;
0e5d27f… stephan 1880
0e5d27f… stephan 1881 if( 0==z ){
0e5d27f… stephan 1882 z = zTmp = db_get("search-tokenizer",0);
0e5d27f… stephan 1883 }
0e5d27f… stephan 1884 if( 0==z ){
0e5d27f… stephan 1885 zRc = "off";
0e5d27f… stephan 1886 }else if( 0==fossil_strcmp(z,"porter") ){
0e5d27f… stephan 1887 zRc = "porter";
e180dbb… stephan 1888 }else if( 0==fossil_strcmp(z,"unicode61") ){
e180dbb… stephan 1889 zRc = "unicode61";
0e5d27f… stephan 1890 }else if( 0==fossil_strcmp(z,"trigram") ){
0e5d27f… stephan 1891 zRc = "trigram";
0e5d27f… stephan 1892 }else{
0e5d27f… stephan 1893 zRc = is_truth(z) ? "porter" : "off";
0e5d27f… stephan 1894 }
0e5d27f… stephan 1895 fossil_free(zTmp);
0e5d27f… stephan 1896 return zRc;
0e5d27f… stephan 1897 }
0e5d27f… stephan 1898
0e5d27f… stephan 1899 /*
0e5d27f… stephan 1900 ** Sets the search-tokenizer config setting to the value of
0e5d27f… stephan 1901 ** search_tokenizer_for_string(zName).
0e5d27f… stephan 1902 */
0e5d27f… stephan 1903 void search_set_tokenizer(const char *zName){
0e5d27f… stephan 1904 db_set("search-tokenizer", search_tokenizer_for_string( zName ), 0);
0e5d27f… stephan 1905 iFtsTokenizer = -1;
0e5d27f… stephan 1906 }
a00a140… drh 1907
a00a140… drh 1908 /*
a00a140… drh 1909 ** Create or drop the tables associated with a full-text index.
a00a140… drh 1910 */
18c0ab6… drh 1911 static int searchIdxExists = -1;
a00a140… drh 1912 void search_create_index(void){
fc85382… stephan 1913 const char *zExtra =
fc85382… stephan 1914 search_tokenize_arg_for_type(search_tokenizer_type(0));
fc85382… stephan 1915 assert( zExtra );
a00a140… drh 1916 search_sql_setup(g.db);
06aec61… drh 1917 db_multi_exec(zFtsSchema/*works-like:"%s"*/, zExtra/*safe-for-%s*/);
18c0ab6… drh 1918 searchIdxExists = 1;
a00a140… drh 1919 }
a00a140… drh 1920 void search_drop_index(void){
06aec61… drh 1921 db_multi_exec(zFtsDrop/*works-like:""*/);
18c0ab6… drh 1922 searchIdxExists = 0;
a00a140… drh 1923 }
a00a140… drh 1924
a00a140… drh 1925 /*
c3c4ef1… stephan 1926 ** Return true if the full-text search index exists. See also the
c3c4ef1… stephan 1927 ** search_index_type() function.
a00a140… drh 1928 */
a00a140… drh 1929 int search_index_exists(void){
18c0ab6… drh 1930 if( searchIdxExists<0 ){
18c0ab6… drh 1931 searchIdxExists = db_table_exists("repository","ftsdocs");
18c0ab6… drh 1932 }
18c0ab6… drh 1933 return searchIdxExists;
c3c4ef1… stephan 1934 }
c3c4ef1… stephan 1935
c3c4ef1… stephan 1936 /*
c3c4ef1… stephan 1937 ** Determine which full-text search index is currently being used to
c3c4ef1… stephan 1938 ** add searching. Return values:
c3c4ef1… stephan 1939 **
c3c4ef1… stephan 1940 ** 0 No search index is available
c3c4ef1… stephan 1941 ** 4 FTS3/4
c3c4ef1… stephan 1942 ** 5 FTS5
c3c4ef1… stephan 1943 **
c3c4ef1… stephan 1944 ** Results are cached. Make the argument 1 to reset the cache. See
c3c4ef1… stephan 1945 ** also the search_index_exists() routine.
c3c4ef1… stephan 1946 */
c3c4ef1… stephan 1947 int search_index_type(int bReset){
c3c4ef1… stephan 1948 static int idxType = -1;
c3c4ef1… stephan 1949 if( idxType<0 || bReset ){
c3c4ef1… stephan 1950 idxType = db_int(0,
c3c4ef1… stephan 1951 "SELECT CASE WHEN sql GLOB '*fts4*' THEN 4 ELSE 5 END"
c3c4ef1… stephan 1952 " FROM repository.sqlite_schema WHERE name='ftsidx'"
c3c4ef1… stephan 1953 );
c3c4ef1… stephan 1954 }
c3c4ef1… stephan 1955 return idxType;
a00a140… drh 1956 }
a00a140… drh 1957
a00a140… drh 1958 /*
a00a140… drh 1959 ** Fill the FTSDOCS table with unindexed entries for everything
a00a140… drh 1960 ** in the repository. This uses INSERT OR IGNORE so entries already
a00a140… drh 1961 ** in FTSDOCS are unchanged.
a00a140… drh 1962 */
a00a140… drh 1963 void search_fill_index(void){
a00a140… drh 1964 if( !search_index_exists() ) return;
a00a140… drh 1965 search_sql_setup(g.db);
a00a140… drh 1966 db_multi_exec(
a00a140… drh 1967 "INSERT OR IGNORE INTO ftsdocs(type,rid,idxed)"
a00a140… drh 1968 " SELECT 'c', objid, 0 FROM event WHERE type='ci';"
a00a140… drh 1969 );
a00a140… drh 1970 db_multi_exec(
a00a140… drh 1971 "WITH latest_wiki(rid,name,mtime) AS ("
a00a140… drh 1972 " SELECT tagxref.rid, substr(tag.tagname,6), max(tagxref.mtime)"
a00a140… drh 1973 " FROM tag, tagxref"
a00a140… drh 1974 " WHERE tag.tagname GLOB 'wiki-*'"
a00a140… drh 1975 " AND tagxref.tagid=tag.tagid"
a00a140… drh 1976 " AND tagxref.value>0"
a00a140… drh 1977 " GROUP BY 2"
a00a140… drh 1978 ") INSERT OR IGNORE INTO ftsdocs(type,rid,name,idxed)"
a00a140… drh 1979 " SELECT 'w', rid, name, 0 FROM latest_wiki;"
a00a140… drh 1980 );
a00a140… drh 1981 db_multi_exec(
a00a140… drh 1982 "INSERT OR IGNORE INTO ftsdocs(type,rid,idxed)"
a00a140… drh 1983 " SELECT 't', tkt_id, 0 FROM ticket;"
a00a140… drh 1984 );
a551d50… andygoth 1985 db_multi_exec(
a551d50… andygoth 1986 "INSERT OR IGNORE INTO ftsdocs(type,rid,name,idxed)"
99fcc43… drh 1987 " SELECT type, objid, comment, 0 FROM event WHERE type IN ('e','f');"
a551d50… andygoth 1988 );
a00a140… drh 1989 }
a00a140… drh 1990
a00a140… drh 1991 /*
a00a140… drh 1992 ** The document described by cType,rid,zName is about to be added or
a00a140… drh 1993 ** updated. If the document has already been indexed, then unindex it
a00a140… drh 1994 ** now while we still have access to the old content. Add the document
a00a140… drh 1995 ** to the queue of documents that need to be indexed or reindexed.
a00a140… drh 1996 */
a00a140… drh 1997 void search_doc_touch(char cType, int rid, const char *zName){
5e28feb… drh 1998 if( search_index_exists() && !content_is_private(rid) ){
a00a140… drh 1999 char zType[2];
a00a140… drh 2000 zType[0] = cType;
a00a140… drh 2001 zType[1] = 0;
a611ea3… drh 2002 search_sql_setup(g.db);
a00a140… drh 2003 db_multi_exec(
c3c4ef1… stephan 2004 "DELETE FROM ftsidx WHERE rowid IN"
a00a140… drh 2005 " (SELECT rowid FROM ftsdocs WHERE type=%Q AND rid=%d AND idxed)",
a00a140… drh 2006 zType, rid
a00a140… drh 2007 );
a00a140… drh 2008 db_multi_exec(
a00a140… drh 2009 "REPLACE INTO ftsdocs(type,rid,name,idxed)"
a00a140… drh 2010 " VALUES(%Q,%d,%Q,0)",
a00a140… drh 2011 zType, rid, zName
a00a140… drh 2012 );
a551d50… andygoth 2013 if( cType=='w' || cType=='e' ){
a00a140… drh 2014 db_multi_exec(
c3c4ef1… stephan 2015 "DELETE FROM ftsidx WHERE rowid IN"
a551d50… andygoth 2016 " (SELECT rowid FROM ftsdocs WHERE type='%c' AND name=%Q AND idxed)",
a551d50… andygoth 2017 cType, zName
a00a140… drh 2018 );
a00a140… drh 2019 db_multi_exec(
a551d50… andygoth 2020 "DELETE FROM ftsdocs WHERE type='%c' AND name=%Q AND rid!=%d",
a551d50… andygoth 2021 cType, zName, rid
a00a140… drh 2022 );
a00a140… drh 2023 }
99fcc43… drh 2024 /* All forum posts are always indexed */
a00a140… drh 2025 }
a00a140… drh 2026 }
a00a140… drh 2027
a00a140… drh 2028 /*
a00a140… drh 2029 ** If the doc-glob and doc-br settings are valid for document search
5260fbf… jan.nijtmans 2030 ** and if the latest check-in on doc-br is in the unindexed set of
a00a140… drh 2031 ** check-ins, then update all 'd' entries in FTSDOCS that have
a00a140… drh 2032 ** changed.
a00a140… drh 2033 */
a00a140… drh 2034 static void search_update_doc_index(void){
ca0d66b… danield 2035 const char *zMainBranch = db_main_branch();
3a6dd83… drh 2036 const char *zDocBranches = db_get("doc-branch", zMainBranch);
40d0b36… stephan 2037 int i;
40d0b36… stephan 2038 Glob * pGlob = glob_create(zDocBranches)
40d0b36… stephan 2039 /* We're misusing a Glob as a list of comma-/space-delimited
40d0b36… stephan 2040 ** tokens. We're not actually doing glob matches here. */;
40d0b36… stephan 2041 if( !pGlob ) return;
a00a140… drh 2042 db_multi_exec(
a00a140… drh 2043 "CREATE TEMP TABLE current_docs(rid INTEGER PRIMARY KEY, name);"
a00a140… drh 2044 "CREATE VIRTUAL TABLE IF NOT EXISTS temp.foci USING files_of_checkin;"
40d0b36… stephan 2045 );
40d0b36… stephan 2046 for( i = 0; i < pGlob->nPattern; ++i ){
40d0b36… stephan 2047 const char *zDocBr = pGlob->azPattern[i];
40d0b36… stephan 2048 int ckid = symbolic_name_to_rid(zDocBr,"ci");
40d0b36… stephan 2049 double rTime;
40d0b36… stephan 2050 if( !db_exists("SELECT 1 FROM ftsdocs WHERE type='c' AND rid=%d"
40d0b36… stephan 2051 " AND NOT idxed", ckid) ) continue;
40d0b36… stephan 2052 /* If we get this far, it means that changes to 'd' entries are
40d0b36… stephan 2053 ** required. */
40d0b36… stephan 2054 rTime = db_double(0.0, "SELECT mtime FROM event WHERE objid=%d", ckid);
40d0b36… stephan 2055 db_multi_exec(
40d0b36… stephan 2056 "INSERT OR IGNORE INTO current_docs(rid, name)"
40d0b36… stephan 2057 " SELECT blob.rid, foci.filename FROM foci, blob"
40d0b36… stephan 2058 " WHERE foci.checkinID=%d AND blob.uuid=foci.uuid"
40d0b36… stephan 2059 " AND %z",
40d0b36… stephan 2060 ckid, glob_expr("foci.filename", db_get("doc-glob",""))
40d0b36… stephan 2061 );
40d0b36… stephan 2062 db_multi_exec(
40d0b36… stephan 2063 "DELETE FROM ftsidx WHERE rowid IN"
40d0b36… stephan 2064 " (SELECT rowid FROM ftsdocs WHERE type='d'"
40d0b36… stephan 2065 " AND rid NOT IN (SELECT rid FROM current_docs))"
40d0b36… stephan 2066 );
40d0b36… stephan 2067 db_multi_exec(
40d0b36… stephan 2068 "DELETE FROM ftsdocs WHERE type='d'"
40d0b36… stephan 2069 " AND rid NOT IN (SELECT rid FROM current_docs)"
40d0b36… stephan 2070 );
40d0b36… stephan 2071 db_multi_exec(
40d0b36… stephan 2072 "INSERT OR IGNORE INTO ftsdocs(type,rid,name,idxed,label,bx,url,mtime)"
40d0b36… stephan 2073 " SELECT 'd', rid, name, 0,"
40d0b36… stephan 2074 " title('d',rid,name),"
40d0b36… stephan 2075 " body('d',rid,name),"
40d0b36… stephan 2076 " printf('/doc/%T/%%s',urlencode(name)),"
40d0b36… stephan 2077 " %.17g"
40d0b36… stephan 2078 " FROM current_docs",
40d0b36… stephan 2079 zDocBr, rTime
40d0b36… stephan 2080 );
40d0b36… stephan 2081 db_multi_exec(
40d0b36… stephan 2082 "INSERT INTO ftsidx(rowid,title,body)"
40d0b36… stephan 2083 " SELECT rowid, label, bx FROM ftsdocs WHERE type='d' AND NOT idxed"
40d0b36… stephan 2084 );
40d0b36… stephan 2085 db_multi_exec(
40d0b36… stephan 2086 "UPDATE ftsdocs SET"
40d0b36… stephan 2087 " idxed=1,"
40d0b36… stephan 2088 " bx=NULL,"
40d0b36… stephan 2089 " label='Document: '||label"
40d0b36… stephan 2090 " WHERE type='d' AND NOT idxed"
40d0b36… stephan 2091 );
40d0b36… stephan 2092 }
40d0b36… stephan 2093 glob_free(pGlob);
a00a140… drh 2094 }
a00a140… drh 2095
a00a140… drh 2096 /*
5260fbf… jan.nijtmans 2097 ** Deal with all of the unindexed 'c' terms in FTSDOCS
a00a140… drh 2098 */
a00a140… drh 2099 static void search_update_checkin_index(void){
a00a140… drh 2100 db_multi_exec(
c3c4ef1… stephan 2101 "INSERT INTO ftsidx(rowid,title,body)"
0f96ffb… drh 2102 " SELECT rowid, '', body('c',rid,NULL) FROM ftsdocs"
a00a140… drh 2103 " WHERE type='c' AND NOT idxed;"
a00a140… drh 2104 );
a00a140… drh 2105 db_multi_exec(
bc21e0a… drh 2106 "UPDATE ftsdocs SET idxed=1, name=NULL,"
bc21e0a… drh 2107 " (label,url,mtime) = "
bc21e0a… drh 2108 " (SELECT printf('Check-in [%%.16s] on %%s',blob.uuid,"
bc21e0a… drh 2109 " datetime(event.mtime)),"
bc21e0a… drh 2110 " printf('/timeline?y=ci&c=%%.20s',blob.uuid),"
bc21e0a… drh 2111 " event.mtime"
bc21e0a… drh 2112 " FROM event, blob"
bc21e0a… drh 2113 " WHERE event.objid=ftsdocs.rid"
bc21e0a… drh 2114 " AND blob.rid=ftsdocs.rid)"
bc21e0a… drh 2115 "WHERE ftsdocs.type='c' AND NOT ftsdocs.idxed"
a00a140… drh 2116 );
a00a140… drh 2117 }
a00a140… drh 2118
a00a140… drh 2119 /*
5260fbf… jan.nijtmans 2120 ** Deal with all of the unindexed 't' terms in FTSDOCS
a00a140… drh 2121 */
a00a140… drh 2122 static void search_update_ticket_index(void){
a00a140… drh 2123 db_multi_exec(
c3c4ef1… stephan 2124 "INSERT INTO ftsidx(rowid,title,body)"
0f96ffb… drh 2125 " SELECT rowid, title('t',rid,NULL), body('t',rid,NULL) FROM ftsdocs"
a00a140… drh 2126 " WHERE type='t' AND NOT idxed;"
a00a140… drh 2127 );
a00a140… drh 2128 if( db_changes()==0 ) return;
a00a140… drh 2129 db_multi_exec(
bc21e0a… drh 2130 "UPDATE ftsdocs SET idxed=1, name=NULL,"
bc21e0a… drh 2131 " (label,url,mtime) ="
bc21e0a… drh 2132 " (SELECT printf('Ticket: %%s (%%s)',title('t',tkt_id,null),"
bc21e0a… drh 2133 " datetime(tkt_mtime)),"
bc21e0a… drh 2134 " printf('/tktview/%%.20s',tkt_uuid),"
bc21e0a… drh 2135 " tkt_mtime"
bc21e0a… drh 2136 " FROM ticket"
bc21e0a… drh 2137 " WHERE tkt_id=ftsdocs.rid)"
bc21e0a… drh 2138 "WHERE ftsdocs.type='t' AND NOT ftsdocs.idxed"
a00a140… drh 2139 );
a00a140… drh 2140 }
a00a140… drh 2141
a00a140… drh 2142 /*
5260fbf… jan.nijtmans 2143 ** Deal with all of the unindexed 'w' terms in FTSDOCS
a00a140… drh 2144 */
a00a140… drh 2145 static void search_update_wiki_index(void){
a00a140… drh 2146 db_multi_exec(
c3c4ef1… stephan 2147 "INSERT INTO ftsidx(rowid,title,body)"
0f96ffb… drh 2148 " SELECT rowid, title('w',rid,NULL),body('w',rid,NULL) FROM ftsdocs"
a00a140… drh 2149 " WHERE type='w' AND NOT idxed;"
a00a140… drh 2150 );
a00a140… drh 2151 if( db_changes()==0 ) return;
a00a140… drh 2152 db_multi_exec(
bc21e0a… drh 2153 "UPDATE ftsdocs SET idxed=1,"
bc21e0a… drh 2154 " (name,label,url,mtime) = "
bc21e0a… drh 2155 " (SELECT ftsdocs.name,"
bc21e0a… drh 2156 " 'Wiki: '||ftsdocs.name,"
bc21e0a… drh 2157 " '/wiki?name='||urlencode(ftsdocs.name),"
bc21e0a… drh 2158 " tagxref.mtime"
bc21e0a… drh 2159 " FROM tagxref WHERE tagxref.rid=ftsdocs.rid)"
bc21e0a… drh 2160 " WHERE ftsdocs.type='w' AND NOT ftsdocs.idxed"
bc21e0a… drh 2161 );
bc21e0a… drh 2162 }
bc21e0a… drh 2163
bc21e0a… drh 2164 /*
99fcc43… drh 2165 ** Deal with all of the unindexed 'f' terms in FTSDOCS
99fcc43… drh 2166 */
99fcc43… drh 2167 static void search_update_forum_index(void){
99fcc43… drh 2168 db_multi_exec(
c3c4ef1… stephan 2169 "INSERT INTO ftsidx(rowid,title,body)"
99fcc43… drh 2170 " SELECT rowid, title('f',rid,NULL),body('f',rid,NULL) FROM ftsdocs"
99fcc43… drh 2171 " WHERE type='f' AND NOT idxed;"
99fcc43… drh 2172 );
99fcc43… drh 2173 if( db_changes()==0 ) return;
99fcc43… drh 2174 db_multi_exec(
99fcc43… drh 2175 "UPDATE ftsdocs SET idxed=1, name=NULL,"
99fcc43… drh 2176 " (label,url,mtime) = "
99fcc43… drh 2177 " (SELECT 'Forum '||event.comment,"
99fcc43… drh 2178 " '/forumpost/'||blob.uuid,"
99fcc43… drh 2179 " event.mtime"
99fcc43… drh 2180 " FROM event, blob"
99fcc43… drh 2181 " WHERE event.objid=ftsdocs.rid"
99fcc43… drh 2182 " AND blob.rid=ftsdocs.rid)"
99fcc43… drh 2183 "WHERE ftsdocs.type='f' AND NOT ftsdocs.idxed"
99fcc43… drh 2184 );
99fcc43… drh 2185 }
99fcc43… drh 2186
99fcc43… drh 2187 /*
a551d50… andygoth 2188 ** Deal with all of the unindexed 'e' terms in FTSDOCS
a551d50… andygoth 2189 */
a551d50… andygoth 2190 static void search_update_technote_index(void){
a551d50… andygoth 2191 db_multi_exec(
c3c4ef1… stephan 2192 "INSERT INTO ftsidx(rowid,title,body)"
a551d50… andygoth 2193 " SELECT rowid, title('e',rid,NULL),body('e',rid,NULL) FROM ftsdocs"
a551d50… andygoth 2194 " WHERE type='e' AND NOT idxed;"
a551d50… andygoth 2195 );
a551d50… andygoth 2196 if( db_changes()==0 ) return;
a551d50… andygoth 2197 db_multi_exec(
a551d50… andygoth 2198 "UPDATE ftsdocs SET idxed=1,"
a551d50… andygoth 2199 " (name,label,url,mtime) = "
a551d50… andygoth 2200 " (SELECT ftsdocs.name,"
a551d50… andygoth 2201 " 'Tech Note: '||ftsdocs.name,"
a551d50… andygoth 2202 " '/technote/'||substr(tag.tagname,7),"
a551d50… andygoth 2203 " tagxref.mtime"
a551d50… andygoth 2204 " FROM tagxref, tag USING (tagid)"
a551d50… andygoth 2205 " WHERE tagxref.rid=ftsdocs.rid"
a551d50… andygoth 2206 " AND tagname GLOB 'event-*')"
a551d50… andygoth 2207 " WHERE ftsdocs.type='e' AND NOT ftsdocs.idxed"
a551d50… andygoth 2208 );
a551d50… andygoth 2209 }
a551d50… andygoth 2210
a551d50… andygoth 2211 /*
a00a140… drh 2212 ** Deal with all of the unindexed entries in the FTSDOCS table - that
a00a140… drh 2213 ** is to say, all the entries with FTSDOCS.IDXED=0. Add them to the
a00a140… drh 2214 ** index.
a00a140… drh 2215 */
a00a140… drh 2216 void search_update_index(unsigned int srchFlags){
a00a140… drh 2217 if( !search_index_exists() ) return;
a00a140… drh 2218 if( !db_exists("SELECT 1 FROM ftsdocs WHERE NOT idxed") ) return;
a00a140… drh 2219 search_sql_setup(g.db);
8e85d6c… drh 2220 db_unprotect(PROTECT_READONLY);
a00a140… drh 2221 if( srchFlags & (SRCH_CKIN|SRCH_DOC) ){
a00a140… drh 2222 search_update_doc_index();
a00a140… drh 2223 search_update_checkin_index();
a00a140… drh 2224 }
a00a140… drh 2225 if( srchFlags & SRCH_TKT ){
a00a140… drh 2226 search_update_ticket_index();
a00a140… drh 2227 }
a00a140… drh 2228 if( srchFlags & SRCH_WIKI ){
a00a140… drh 2229 search_update_wiki_index();
a00a140… drh 2230 }
a551d50… andygoth 2231 if( srchFlags & SRCH_TECHNOTE ){
a551d50… andygoth 2232 search_update_technote_index();
a551d50… andygoth 2233 }
99fcc43… drh 2234 if( srchFlags & SRCH_FORUM ){
99fcc43… drh 2235 search_update_forum_index();
99fcc43… drh 2236 }
8e85d6c… drh 2237 db_protect_pop();
18c0ab6… drh 2238 }
18c0ab6… drh 2239
18c0ab6… drh 2240 /*
18c0ab6… drh 2241 ** Construct, prepopulate, and then update the full-text index.
18c0ab6… drh 2242 */
18c0ab6… drh 2243 void search_rebuild_index(void){
18c0ab6… drh 2244 fossil_print("rebuilding the search index...");
18c0ab6… drh 2245 fflush(stdout);
18c0ab6… drh 2246 search_create_index();
18c0ab6… drh 2247 search_fill_index();
2e9e369… drh 2248 search_update_index(search_restrict(SRCH_ALL));
fc85382… stephan 2249 if( db_table_exists("repository","chat") ){
fc85382… stephan 2250 chat_rebuild_index(1);
fc85382… stephan 2251 }
18c0ab6… drh 2252 fossil_print(" done\n");
a00a140… drh 2253 }
a00a140… drh 2254
a00a140… drh 2255 /*
3e7c7e2… drh 2256 ** COMMAND: fts-config* abbrv-subcom
7b6adfb… drh 2257 **
7b6adfb… drh 2258 ** Usage: fossil fts-config ?SUBCOMMAND? ?ARGUMENT?
7b6adfb… drh 2259 **
7b6adfb… drh 2260 ** The "fossil fts-config" command configures the full-text search capabilities
7b6adfb… drh 2261 ** of the repository. Subcommands:
7b6adfb… drh 2262 **
62cb8ea… drh 2263 ** reindex Rebuild the search index. This is a no-op if
62cb8ea… drh 2264 ** index search is disabled
62cb8ea… drh 2265 **
62cb8ea… drh 2266 ** index (on|off) Turn the search index on or off
62cb8ea… drh 2267 **
62cb8ea… drh 2268 ** enable TYPE .. Enable search for TYPE. TYPE is one of:
62cb8ea… drh 2269 ** check-in, document, ticket, wiki, technote,
62cb8ea… drh 2270 ** forum, help, or all
62cb8ea… drh 2271 **
62cb8ea… drh 2272 ** disable TYPE ... Disable search for TYPE
62cb8ea… drh 2273 **
62cb8ea… drh 2274 ** tokenizer VALUE Select a tokenizer for indexed search. VALUE
62cb8ea… drh 2275 ** may be one of (porter, on, off, trigram, unicode61),
62cb8ea… drh 2276 ** and "on" is equivalent to "porter". Unindexed
62cb8ea… drh 2277 ** search never uses tokenization or stemming.
7b6adfb… drh 2278 **
7b6adfb… drh 2279 ** The current search settings are displayed after any changes are applied.
7b6adfb… drh 2280 ** Run this command with no arguments to simply see the settings.
a00a140… drh 2281 */
b1b4eaa… drh 2282 void fts_config_cmd(void){
c3c4ef1… stephan 2283 static const struct {
53d1f05… drh 2284 int iCmd;
53d1f05… drh 2285 const char *z;
53d1f05… drh 2286 } aCmd[] = {
7b6adfb… drh 2287 { 1, "reindex" },
7b6adfb… drh 2288 { 2, "index" },
7b6adfb… drh 2289 { 3, "disable" },
7b6adfb… drh 2290 { 4, "enable" },
0e5d27f… stephan 2291 { 5, "tokenizer"},
c9db8c7… mistachkin 2292 };
53d1f05… drh 2293 static const struct {
53d1f05… drh 2294 const char *zSetting;
53d1f05… drh 2295 const char *zName;
53d1f05… drh 2296 const char *zSw;
53d1f05… drh 2297 } aSetng[] = {
6d9f0f5… drh 2298 { "search-ci", "check-in search:", "c" },
6d9f0f5… drh 2299 { "search-doc", "document search:", "d" },
6d9f0f5… drh 2300 { "search-tkt", "ticket search:", "t" },
6d9f0f5… drh 2301 { "search-wiki", "wiki search:", "w" },
6d9f0f5… drh 2302 { "search-technote", "technote search:", "e" },
6d9f0f5… drh 2303 { "search-forum", "forum search:", "f" },
6d9f0f5… drh 2304 { "search-help", "built-in help search:", "h" },
c9db8c7… mistachkin 2305 };
c9db8c7… mistachkin 2306 char *zSubCmd = 0;
7b6adfb… drh 2307 int i, j, n;
7b6adfb… drh 2308 int iCmd = 0;
7b6adfb… drh 2309 int iAction = 0;
a00a140… drh 2310 db_find_and_open_repository(0, 0);
7b6adfb… drh 2311 if( g.argc>2 ){
7b6adfb… drh 2312 zSubCmd = g.argv[2];
7b6adfb… drh 2313 n = (int)strlen(zSubCmd);
3cb9ba4… andygoth 2314 for(i=0; i<count(aCmd); i++){
7b6adfb… drh 2315 if( fossil_strncmp(aCmd[i].z, zSubCmd, n)==0 ) break;
7b6adfb… drh 2316 }
3cb9ba4… andygoth 2317 if( i>=count(aCmd) ){
7b6adfb… drh 2318 Blob all;
7b6adfb… drh 2319 blob_init(&all,0,0);
3cb9ba4… andygoth 2320 for(i=0; i<count(aCmd); i++) blob_appendf(&all, " %s", aCmd[i].z);
f25da6e… wyoung 2321 fossil_fatal("unknown \"%s\" - should be one of:%s",
7b6adfb… drh 2322 zSubCmd, blob_str(&all));
7b6adfb… drh 2323 return;
7b6adfb… drh 2324 }
7b6adfb… drh 2325 iCmd = aCmd[i].iCmd;
7b6adfb… drh 2326 }
b1b4eaa… drh 2327 g.perm.Read = 1;
b1b4eaa… drh 2328 g.perm.RdTkt = 1;
b1b4eaa… drh 2329 g.perm.RdWiki = 1;
7b6adfb… drh 2330 if( iCmd==1 ){
0f96ffb… drh 2331 if( search_index_exists() ) iAction = 2;
7b6adfb… drh 2332 }
7b6adfb… drh 2333 if( iCmd==2 ){
7b6adfb… drh 2334 if( g.argc<3 ) usage("index (on|off)");
7b6adfb… drh 2335 iAction = 1 + is_truth(g.argv[3]);
a00a140… drh 2336 }
e1962ef… drh 2337 db_begin_transaction();
7b6adfb… drh 2338
7b6adfb… drh 2339 /* Adjust search settings */
7b6adfb… drh 2340 if( iCmd==3 || iCmd==4 ){
6d9f0f5… drh 2341 int k;
7b6adfb… drh 2342 const char *zCtrl;
6d9f0f5… drh 2343 for(k=2; k<g.argc; k++){
6d9f0f5… drh 2344 if( k==2 ){
6d9f0f5… drh 2345 if( g.argc<4 ){
6d9f0f5… drh 2346 zCtrl = "all";
6d9f0f5… drh 2347 }else{
6d9f0f5… drh 2348 zCtrl = g.argv[3];
6d9f0f5… drh 2349 k++;
6d9f0f5… drh 2350 }
6d9f0f5… drh 2351 }else{
6d9f0f5… drh 2352 zCtrl = g.argv[k];
6d9f0f5… drh 2353 }
6d9f0f5… drh 2354 if( fossil_strcmp(zCtrl,"all")==0 ){
6d9f0f5… drh 2355 zCtrl = "cdtwefh";
6d9f0f5… drh 2356 }
6d9f0f5… drh 2357 if( strlen(zCtrl)>=4 ){
6d9f0f5… drh 2358 /* If the argument to "enable" or "disable" is a string of at least
6d9f0f5… drh 2359 ** 4 characters which matches part of any aSetng.zName, then use that
6d9f0f5… drh 2360 ** one aSetng value only. */
6d9f0f5… drh 2361 char *zGlob = mprintf("*%s*", zCtrl);
6d9f0f5… drh 2362 for(j=0; j<count(aSetng); j++){
6d9f0f5… drh 2363 if( sqlite3_strglob(zGlob, aSetng[j].zName)==0 ){
6d9f0f5… drh 2364 db_set_int(aSetng[j].zSetting/*works-like:"x"*/, iCmd-3, 0);
6d9f0f5… drh 2365 zCtrl = 0;
6d9f0f5… drh 2366 break;
6d9f0f5… drh 2367 }
6d9f0f5… drh 2368 }
6d9f0f5… drh 2369 fossil_free(zGlob);
6d9f0f5… drh 2370 }
6d9f0f5… drh 2371 if( zCtrl ){
6d9f0f5… drh 2372 for(j=0; j<count(aSetng); j++){
6d9f0f5… drh 2373 if( strchr(zCtrl, aSetng[j].zSw[0])!=0 ){
6d9f0f5… drh 2374 db_set_int(aSetng[j].zSetting/*works-like:"x"*/, iCmd-3, 0);
6d9f0f5… drh 2375 }
6d9f0f5… drh 2376 }
7b6adfb… drh 2377 }
0f96ffb… drh 2378 }
0e5d27f… stephan 2379 }else if( iCmd==5 ){
0e5d27f… stephan 2380 int iOldTokenizer, iNewTokenizer;
9965e1d… wyoung 2381 if( g.argc<4 ) usage("tokenizer porter|on|off|trigram|unicode61");
0e5d27f… stephan 2382 iOldTokenizer = search_tokenizer_type(0);
0e5d27f… stephan 2383 db_set("search-tokenizer",
0e5d27f… stephan 2384 search_tokenizer_for_string(g.argv[3]), 0);
0e5d27f… stephan 2385 iNewTokenizer = search_tokenizer_type(1);
0e5d27f… stephan 2386 if( iOldTokenizer!=iNewTokenizer ){
0e5d27f… stephan 2387 /* Drop or rebuild index if tokenizer changes. */
0e5d27f… stephan 2388 iAction = 1 + ((iOldTokenizer && iNewTokenizer)
0e5d27f… stephan 2389 ? 1 : (iNewTokenizer ? 1 : 0));
0e5d27f… stephan 2390 }
7b6adfb… drh 2391 }
7b6adfb… drh 2392
7b6adfb… drh 2393 /* destroy or rebuild the index, if requested */
7b6adfb… drh 2394 if( iAction>=1 ){
7b6adfb… drh 2395 search_drop_index();
7b6adfb… drh 2396 }
7b6adfb… drh 2397 if( iAction>=2 ){
18c0ab6… drh 2398 search_rebuild_index();
7b6adfb… drh 2399 }
7b6adfb… drh 2400
7b6adfb… drh 2401 /* Always show the status before ending */
3cb9ba4… andygoth 2402 for(i=0; i<count(aSetng); i++){
6d9f0f5… drh 2403 fossil_print("%-21s %s\n", aSetng[i].zName,
7b6adfb… drh 2404 db_get_boolean(aSetng[i].zSetting,0) ? "on" : "off");
7b6adfb… drh 2405 }
6d9f0f5… drh 2406 fossil_print("%-21s %s\n", "tokenizer:",
0e5d27f… stephan 2407 search_tokenizer_for_string(0));
7b6adfb… drh 2408 if( search_index_exists() ){
5c5e549… drh 2409 int pgsz = db_int64(0, "PRAGMA repository.page_size;");
5c5e549… drh 2410 i64 nTotal = db_int64(0, "PRAGMA repository.page_count;")*pgsz;
5c5e549… drh 2411 i64 nFts = db_int64(0, "SELECT count(*) FROM dbstat"
5c5e549… drh 2412 " WHERE schema='repository'"
5c5e549… drh 2413 " AND name LIKE 'fts%%'")*pgsz;
5c5e549… drh 2414 char zSize[50];
6d9f0f5… drh 2415 fossil_print("%-21s FTS%d\n", "full-text index:", search_index_type(1));
6d9f0f5… drh 2416 fossil_print("%-21s %d\n", "documents:",
7b6adfb… drh 2417 db_int(0, "SELECT count(*) FROM ftsdocs"));
5c5e549… drh 2418 approxSizeName(sizeof(zSize), zSize, nFts);
6d9f0f5… drh 2419 fossil_print("%-21s %s (%.1f%% of repository)\n", "space used",
5c5e549… drh 2420 zSize, 100.0*((double)nFts/(double)nTotal));
7b6adfb… drh 2421 }else{
6d9f0f5… drh 2422 fossil_print("%-21s disabled\n", "full-text index:");
a00a140… drh 2423 }
a00a140… drh 2424 db_end_transaction(0);
375bc71… drh 2425 }
375bc71… drh 2426
375bc71… drh 2427 /*
375bc71… drh 2428 ** WEBPAGE: test-ftsdocs
375bc71… drh 2429 **
375bc71… drh 2430 ** Show a table of all documents currently in the search index.
375bc71… drh 2431 */
375bc71… drh 2432 void search_data_page(void){
375bc71… drh 2433 Stmt q;
375bc71… drh 2434 const char *zId = P("id");
375bc71… drh 2435 const char *zType = P("y");
375bc71… drh 2436 const char *zIdxed = P("ixed");
375bc71… drh 2437 int id;
53d1f05… drh 2438 int cnt1 = 0, cnt2 = 0, cnt3 = 0;
375bc71… drh 2439 login_check_credentials();
375bc71… drh 2440 if( !g.perm.Admin ){ login_needed(0); return; }
112c713… drh 2441 style_set_current_feature("test");
375bc71… drh 2442 if( !search_index_exists() ){
375bc71… drh 2443 @ <p>Indexed search is disabled
112c713… drh 2444 style_finish_page();
375bc71… drh 2445 return;
375bc71… drh 2446 }
53d1f05… drh 2447 search_sql_setup(g.db);
53d1f05… drh 2448 style_submenu_element("Setup","%R/srchsetup");
375bc71… drh 2449 if( zId!=0 && (id = atoi(zId))>0 ){
375bc71… drh 2450 /* Show information about a single ftsdocs entry */
375bc71… drh 2451 style_header("Information about ftsdoc entry %d", id);
53d1f05… drh 2452 style_submenu_element("Summary","%R/test-ftsdocs");
375bc71… drh 2453 db_prepare(&q,
375bc71… drh 2454 "SELECT type||rid, name, idxed, label, url, datetime(mtime)"
375bc71… drh 2455 " FROM ftsdocs WHERE rowid=%d", id
375bc71… drh 2456 );
375bc71… drh 2457 if( db_step(&q)==SQLITE_ROW ){
375bc71… drh 2458 const char *zUrl = db_column_text(&q,4);
53d1f05… drh 2459 const char *zDocId = db_column_text(&q,0);
53d1f05… drh 2460 char *zName;
53d1f05… drh 2461 char *z;
375bc71… drh 2462 @ <table border=0>
c3c4ef1… stephan 2463 @ <tr><td align='right'>rowid:<td>&nbsp;&nbsp;<td>%d(id)
53d1f05… drh 2464 @ <tr><td align='right'>id:<td><td>%s(zDocId)
375bc71… drh 2465 @ <tr><td align='right'>name:<td><td>%h(db_column_text(&q,1))
375bc71… drh 2466 @ <tr><td align='right'>idxed:<td><td>%d(db_column_int(&q,2))
375bc71… drh 2467 @ <tr><td align='right'>label:<td><td>%h(db_column_text(&q,3))
375bc71… drh 2468 @ <tr><td align='right'>url:<td><td>
375bc71… drh 2469 @ <a href='%R%s(zUrl)'>%h(zUrl)</a>
375bc71… drh 2470 @ <tr><td align='right'>mtime:<td><td>%s(db_column_text(&q,5))
c3c4ef1… stephan 2471 z = db_text(0, "SELECT title FROM ftsidx WHERE rowid=%d",id);
53d1f05… drh 2472 if( z && z[0] ){
53d1f05… drh 2473 @ <tr><td align="right">title:<td><td>%h(z)
53d1f05… drh 2474 fossil_free(z);
53d1f05… drh 2475 }
c3c4ef1… stephan 2476 z = db_text(0, "SELECT body FROM ftsidx WHERE rowid=%d",id);
53d1f05… drh 2477 if( z && z[0] ){
53d1f05… drh 2478 @ <tr><td align="right" valign="top">body:<td><td>%h(z)
53d1f05… drh 2479 fossil_free(z);
53d1f05… drh 2480 }
375bc71… drh 2481 @ </table>
53d1f05… drh 2482 zName = mprintf("Indexed '%c' docs",zDocId[0]);
53d1f05… drh 2483 style_submenu_element(zName,"%R/test-ftsdocs?y=%c&ixed=1",zDocId[0]);
53d1f05… drh 2484 zName = mprintf("Unindexed '%c' docs",zDocId[0]);
53d1f05… drh 2485 style_submenu_element(zName,"%R/test-ftsdocs?y=%c&ixed=0",zDocId[0]);
375bc71… drh 2486 }
375bc71… drh 2487 db_finalize(&q);
112c713… drh 2488 style_finish_page();
375bc71… drh 2489 return;
375bc71… drh 2490 }
375bc71… drh 2491 if( zType!=0 && zType[0]!=0 && zType[1]==0 &&
375bc71… drh 2492 zIdxed!=0 && (zIdxed[0]=='1' || zIdxed[0]=='0') && zIdxed[1]==0
375bc71… drh 2493 ){
375bc71… drh 2494 int ixed = zIdxed[0]=='1';
53d1f05… drh 2495 char *zName;
375bc71… drh 2496 style_header("List of '%c' documents that are%s indexed",
375bc71… drh 2497 zType[0], ixed ? "" : " not");
53d1f05… drh 2498 style_submenu_element("Summary","%R/test-ftsdocs");
53d1f05… drh 2499 if( ixed==0 ){
53d1f05… drh 2500 zName = mprintf("Indexed '%c' docs",zType[0]);
53d1f05… drh 2501 style_submenu_element(zName,"%R/test-ftsdocs?y=%c&ixed=1",zType[0]);
53d1f05… drh 2502 }else{
53d1f05… drh 2503 zName = mprintf("Unindexed '%c' docs",zType[0]);
53d1f05… drh 2504 style_submenu_element(zName,"%R/test-ftsdocs?y=%c&ixed=0",zType[0]);
53d1f05… drh 2505 }
375bc71… drh 2506 db_prepare(&q,
375bc71… drh 2507 "SELECT rowid, type||rid ||' '|| coalesce(label,'')"
375bc71… drh 2508 " FROM ftsdocs WHERE type='%c' AND %s idxed",
375bc71… drh 2509 zType[0], ixed ? "" : "NOT"
375bc71… drh 2510 );
375bc71… drh 2511 @ <ul>
375bc71… drh 2512 while( db_step(&q)==SQLITE_ROW ){
375bc71… drh 2513 @ <li> <a href='test-ftsdocs?id=%d(db_column_int(&q,0))'>
375bc71… drh 2514 @ %h(db_column_text(&q,1))</a>
375bc71… drh 2515 }
375bc71… drh 2516 @ </ul>
375bc71… drh 2517 db_finalize(&q);
112c713… drh 2518 style_finish_page();
375bc71… drh 2519 return;
375bc71… drh 2520 }
375bc71… drh 2521 style_header("Summary of ftsdocs");
375bc71… drh 2522 db_prepare(&q,
53d1f05… drh 2523 "SELECT type, sum(idxed IS TRUE), sum(idxed IS FALSE), count(*)"
53d1f05… drh 2524 " FROM ftsdocs"
53d1f05… drh 2525 " GROUP BY 1 ORDER BY 4 DESC"
375bc71… drh 2526 );
375bc71… drh 2527 @ <table border=1 cellpadding=3 cellspacing=0>
375bc71… drh 2528 @ <thead>
53d1f05… drh 2529 @ <tr><th>Type<th>Indexed<th>Unindexed<th>Total
375bc71… drh 2530 @ </thead>
375bc71… drh 2531 @ <tbody>
375bc71… drh 2532 while( db_step(&q)==SQLITE_ROW ){
375bc71… drh 2533 const char *zType = db_column_text(&q,0);
53d1f05… drh 2534 int nIndexed = db_column_int(&q, 1);
53d1f05… drh 2535 int nUnindexed = db_column_int(&q, 2);
53d1f05… drh 2536 int nTotal = db_column_int(&q, 3);
53d1f05… drh 2537 @ <tr><td>%h(zType)
53d1f05… drh 2538 if( nIndexed>0 ){
53d1f05… drh 2539 @ <td align="right"><a href='%R/test-ftsdocs?y=%s(zType)&ixed=1'>\
53d1f05… drh 2540 @ %d(nIndexed)</a>
53d1f05… drh 2541 }else{
53d1f05… drh 2542 @ <td align="right">0
53d1f05… drh 2543 }
53d1f05… drh 2544 if( nUnindexed>0 ){
53d1f05… drh 2545 @ <td align="right"><a href='%R/test-ftsdocs?y=%s(zType)&ixed=0'>\
53d1f05… drh 2546 @ %d(nUnindexed)</a>
53d1f05… drh 2547 }else{
53d1f05… drh 2548 @ <td align="right">0
53d1f05… drh 2549 }
53d1f05… drh 2550 @ <td align="right">%d(nTotal)
375bc71… drh 2551 @ </tr>
53d1f05… drh 2552 cnt1 += nIndexed;
53d1f05… drh 2553 cnt2 += nUnindexed;
53d1f05… drh 2554 cnt3 += nTotal;
375bc71… drh 2555 }
53d1f05… drh 2556 db_finalize(&q);
375bc71… drh 2557 @ </tbody><tfooter>
53d1f05… drh 2558 @ <tr><th>Total<th align="right">%d(cnt1)<th align="right">%d(cnt2)
53d1f05… drh 2559 @ <th align="right">%d(cnt3)
375bc71… drh 2560 @ </tfooter>
375bc71… drh 2561 @ </table>
112c713… drh 2562 style_finish_page();
c3c4ef1… stephan 2563 }
c3c4ef1… stephan 2564
c3c4ef1… stephan 2565
c3c4ef1… stephan 2566 /*
c3c4ef1… stephan 2567 ** The Fts5MatchinfoCtx bits were all taken verbatim from:
c3c4ef1… stephan 2568 **
c3c4ef1… stephan 2569 ** https://sqlite.org/src/finfo?name=ext/fts5/fts5_test_mi.c
c3c4ef1… stephan 2570 */
c3c4ef1… stephan 2571
c3c4ef1… stephan 2572 typedef struct Fts5MatchinfoCtx Fts5MatchinfoCtx;
c3c4ef1… stephan 2573
f82caec… drh 2574 #if INTERFACE
c3c4ef1… stephan 2575 #ifndef SQLITE_AMALGAMATION
c3c4ef1… stephan 2576 typedef unsigned int u32;
f82caec… drh 2577 #endif
c3c4ef1… stephan 2578 #endif
c3c4ef1… stephan 2579
c3c4ef1… stephan 2580 struct Fts5MatchinfoCtx {
c3c4ef1… stephan 2581 int nCol; /* Number of cols in FTS5 table */
c3c4ef1… stephan 2582 int nPhrase; /* Number of phrases in FTS5 query */
c3c4ef1… stephan 2583 char *zArg; /* nul-term'd copy of 2nd arg */
c3c4ef1… stephan 2584 int nRet; /* Number of elements in aRet[] */
c3c4ef1… stephan 2585 u32 *aRet; /* Array of 32-bit unsigned ints to return */
c3c4ef1… stephan 2586 };
c3c4ef1… stephan 2587
c3c4ef1… stephan 2588
c3c4ef1… stephan 2589 /*
c3c4ef1… stephan 2590 ** Return a pointer to the fts5_api pointer for database connection db.
c3c4ef1… stephan 2591 ** If an error occurs, return NULL and leave an error in the database
c3c4ef1… stephan 2592 ** handle (accessible using sqlite3_errcode()/errmsg()).
c3c4ef1… stephan 2593 */
c3c4ef1… stephan 2594 static int fts5_api_from_db(sqlite3 *db, fts5_api **ppApi){
c3c4ef1… stephan 2595 sqlite3_stmt *pStmt = 0;
c3c4ef1… stephan 2596 int rc;
c3c4ef1… stephan 2597
c3c4ef1… stephan 2598 *ppApi = 0;
c3c4ef1… stephan 2599 rc = sqlite3_prepare(db, "SELECT fts5(?1)", -1, &pStmt, 0);
c3c4ef1… stephan 2600 if( rc==SQLITE_OK ){
c3c4ef1… stephan 2601 sqlite3_bind_pointer(pStmt, 1, (void*)ppApi, "fts5_api_ptr", 0);
c3c4ef1… stephan 2602 (void)sqlite3_step(pStmt);
c3c4ef1… stephan 2603 rc = sqlite3_finalize(pStmt);
c3c4ef1… stephan 2604 }
c3c4ef1… stephan 2605
c3c4ef1… stephan 2606 return rc;
c3c4ef1… stephan 2607 }
c3c4ef1… stephan 2608
c3c4ef1… stephan 2609
c3c4ef1… stephan 2610 /*
c3c4ef1… stephan 2611 ** Argument f should be a flag accepted by matchinfo() (a valid character
275da70… danield 2612 ** in the string passed as the second argument). If it is not, -1 is
c3c4ef1… stephan 2613 ** returned. Otherwise, if f is a valid matchinfo flag, the value returned
c3c4ef1… stephan 2614 ** is the number of 32-bit integers added to the output array if the
c3c4ef1… stephan 2615 ** table has nCol columns and the query nPhrase phrases.
c3c4ef1… stephan 2616 */
c3c4ef1… stephan 2617 static int fts5MatchinfoFlagsize(int nCol, int nPhrase, char f){
c3c4ef1… stephan 2618 int ret = -1;
c3c4ef1… stephan 2619 switch( f ){
c3c4ef1… stephan 2620 case 'p': ret = 1; break;
c3c4ef1… stephan 2621 case 'c': ret = 1; break;
c3c4ef1… stephan 2622 case 'x': ret = 3 * nCol * nPhrase; break;
c3c4ef1… stephan 2623 case 'y': ret = nCol * nPhrase; break;
c3c4ef1… stephan 2624 case 'b': ret = ((nCol + 31) / 32) * nPhrase; break;
c3c4ef1… stephan 2625 case 'n': ret = 1; break;
c3c4ef1… stephan 2626 case 'a': ret = nCol; break;
c3c4ef1… stephan 2627 case 'l': ret = nCol; break;
c3c4ef1… stephan 2628 case 's': ret = nCol; break;
c3c4ef1… stephan 2629 }
c3c4ef1… stephan 2630 return ret;
c3c4ef1… stephan 2631 }
c3c4ef1… stephan 2632
c3c4ef1… stephan 2633 static int fts5MatchinfoIter(
c3c4ef1… stephan 2634 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
c3c4ef1… stephan 2635 Fts5Context *pFts, /* First arg to pass to pApi functions */
c3c4ef1… stephan 2636 Fts5MatchinfoCtx *p,
c3c4ef1… stephan 2637 int(*x)(const Fts5ExtensionApi*,Fts5Context*,Fts5MatchinfoCtx*,char,u32*)
c3c4ef1… stephan 2638 ){
c3c4ef1… stephan 2639 int i;
c3c4ef1… stephan 2640 int n = 0;
c3c4ef1… stephan 2641 int rc = SQLITE_OK;
c3c4ef1… stephan 2642 char f;
c3c4ef1… stephan 2643 for(i=0; (f = p->zArg[i]); i++){
c3c4ef1… stephan 2644 rc = x(pApi, pFts, p, f, &p->aRet[n]);
c3c4ef1… stephan 2645 if( rc!=SQLITE_OK ) break;
c3c4ef1… stephan 2646 n += fts5MatchinfoFlagsize(p->nCol, p->nPhrase, f);
c3c4ef1… stephan 2647 }
c3c4ef1… stephan 2648 return rc;
c3c4ef1… stephan 2649 }
c3c4ef1… stephan 2650
c3c4ef1… stephan 2651 static int fts5MatchinfoXCb(
c3c4ef1… stephan 2652 const Fts5ExtensionApi *pApi,
c3c4ef1… stephan 2653 Fts5Context *pFts,
c3c4ef1… stephan 2654 void *pUserData
c3c4ef1… stephan 2655 ){
c3c4ef1… stephan 2656 Fts5PhraseIter iter;
c3c4ef1… stephan 2657 int iCol, iOff;
c3c4ef1… stephan 2658 u32 *aOut = (u32*)pUserData;
c3c4ef1… stephan 2659 int iPrev = -1;
c3c4ef1… stephan 2660
c3c4ef1… stephan 2661 for(pApi->xPhraseFirst(pFts, 0, &iter, &iCol, &iOff);
c3c4ef1… stephan 2662 iCol>=0;
c3c4ef1… stephan 2663 pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
c3c4ef1… stephan 2664 ){
c3c4ef1… stephan 2665 aOut[iCol*3+1]++;
c3c4ef1… stephan 2666 if( iCol!=iPrev ) aOut[iCol*3 + 2]++;
c3c4ef1… stephan 2667 iPrev = iCol;
c3c4ef1… stephan 2668 }
c3c4ef1… stephan 2669
c3c4ef1… stephan 2670 return SQLITE_OK;
c3c4ef1… stephan 2671 }
c3c4ef1… stephan 2672
c3c4ef1… stephan 2673 static int fts5MatchinfoGlobalCb(
c3c4ef1… stephan 2674 const Fts5ExtensionApi *pApi,
c3c4ef1… stephan 2675 Fts5Context *pFts,
c3c4ef1… stephan 2676 Fts5MatchinfoCtx *p,
c3c4ef1… stephan 2677 char f,
c3c4ef1… stephan 2678 u32 *aOut
c3c4ef1… stephan 2679 ){
c3c4ef1… stephan 2680 int rc = SQLITE_OK;
c3c4ef1… stephan 2681 switch( f ){
c3c4ef1… stephan 2682 case 'p':
c3c4ef1… stephan 2683 aOut[0] = p->nPhrase;
c3c4ef1… stephan 2684 break;
c3c4ef1… stephan 2685
c3c4ef1… stephan 2686 case 'c':
c3c4ef1… stephan 2687 aOut[0] = p->nCol;
c3c4ef1… stephan 2688 break;
c3c4ef1… stephan 2689
c3c4ef1… stephan 2690 case 'x': {
c3c4ef1… stephan 2691 int i;
c3c4ef1… stephan 2692 for(i=0; i<p->nPhrase && rc==SQLITE_OK; i++){
c3c4ef1… stephan 2693 void *pPtr = (void*)&aOut[i * p->nCol * 3];
c3c4ef1… stephan 2694 rc = pApi->xQueryPhrase(pFts, i, pPtr, fts5MatchinfoXCb);
c3c4ef1… stephan 2695 }
c3c4ef1… stephan 2696 break;
c3c4ef1… stephan 2697 }
c3c4ef1… stephan 2698
c3c4ef1… stephan 2699 case 'n': {
c3c4ef1… stephan 2700 sqlite3_int64 nRow;
c3c4ef1… stephan 2701 rc = pApi->xRowCount(pFts, &nRow);
c3c4ef1… stephan 2702 aOut[0] = (u32)nRow;
c3c4ef1… stephan 2703 break;
c3c4ef1… stephan 2704 }
c3c4ef1… stephan 2705
c3c4ef1… stephan 2706 case 'a': {
c3c4ef1… stephan 2707 sqlite3_int64 nRow = 0;
c3c4ef1… stephan 2708 rc = pApi->xRowCount(pFts, &nRow);
c3c4ef1… stephan 2709 if( nRow==0 ){
c3c4ef1… stephan 2710 memset(aOut, 0, sizeof(u32) * p->nCol);
c3c4ef1… stephan 2711 }else{
c3c4ef1… stephan 2712 int i;
c3c4ef1… stephan 2713 for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
c3c4ef1… stephan 2714 sqlite3_int64 nToken;
c3c4ef1… stephan 2715 rc = pApi->xColumnTotalSize(pFts, i, &nToken);
c3c4ef1… stephan 2716 if( rc==SQLITE_OK){
c3c4ef1… stephan 2717 aOut[i] = (u32)((2*nToken + nRow) / (2*nRow));
c3c4ef1… stephan 2718 }
c3c4ef1… stephan 2719 }
c3c4ef1… stephan 2720 }
c3c4ef1… stephan 2721 break;
c3c4ef1… stephan 2722 }
c3c4ef1… stephan 2723
c3c4ef1… stephan 2724 }
c3c4ef1… stephan 2725 return rc;
c3c4ef1… stephan 2726 }
c3c4ef1… stephan 2727
c3c4ef1… stephan 2728 static int fts5MatchinfoLocalCb(
c3c4ef1… stephan 2729 const Fts5ExtensionApi *pApi,
c3c4ef1… stephan 2730 Fts5Context *pFts,
c3c4ef1… stephan 2731 Fts5MatchinfoCtx *p,
c3c4ef1… stephan 2732 char f,
c3c4ef1… stephan 2733 u32 *aOut
c3c4ef1… stephan 2734 ){
c3c4ef1… stephan 2735 int i;
c3c4ef1… stephan 2736 int rc = SQLITE_OK;
c3c4ef1… stephan 2737
c3c4ef1… stephan 2738 switch( f ){
c3c4ef1… stephan 2739 case 'b': {
c3c4ef1… stephan 2740 int iPhrase;
c3c4ef1… stephan 2741 int nInt = ((p->nCol + 31) / 32) * p->nPhrase;
c3c4ef1… stephan 2742 for(i=0; i<nInt; i++) aOut[i] = 0;
c3c4ef1… stephan 2743
c3c4ef1… stephan 2744 for(iPhrase=0; iPhrase<p->nPhrase; iPhrase++){
c3c4ef1… stephan 2745 Fts5PhraseIter iter;
c3c4ef1… stephan 2746 int iCol;
c3c4ef1… stephan 2747 for(pApi->xPhraseFirstColumn(pFts, iPhrase, &iter, &iCol);
c3c4ef1… stephan 2748 iCol>=0;
c3c4ef1… stephan 2749 pApi->xPhraseNextColumn(pFts, &iter, &iCol)
c3c4ef1… stephan 2750 ){
c3c4ef1… stephan 2751 aOut[iPhrase * ((p->nCol+31)/32) + iCol/32] |= ((u32)1 << iCol%32);
c3c4ef1… stephan 2752 }
c3c4ef1… stephan 2753 }
c3c4ef1… stephan 2754
c3c4ef1… stephan 2755 break;
c3c4ef1… stephan 2756 }
c3c4ef1… stephan 2757
c3c4ef1… stephan 2758 case 'x':
c3c4ef1… stephan 2759 case 'y': {
c3c4ef1… stephan 2760 int nMul = (f=='x' ? 3 : 1);
c3c4ef1… stephan 2761 int iPhrase;
c3c4ef1… stephan 2762
c3c4ef1… stephan 2763 for(i=0; i<(p->nCol*p->nPhrase); i++) aOut[i*nMul] = 0;
c3c4ef1… stephan 2764
c3c4ef1… stephan 2765 for(iPhrase=0; iPhrase<p->nPhrase; iPhrase++){
c3c4ef1… stephan 2766 Fts5PhraseIter iter;
c3c4ef1… stephan 2767 int iOff, iCol;
c3c4ef1… stephan 2768 for(pApi->xPhraseFirst(pFts, iPhrase, &iter, &iCol, &iOff);
c3c4ef1… stephan 2769 iOff>=0;
c3c4ef1… stephan 2770 pApi->xPhraseNext(pFts, &iter, &iCol, &iOff)
c3c4ef1… stephan 2771 ){
c3c4ef1… stephan 2772 aOut[nMul * (iCol + iPhrase * p->nCol)]++;
c3c4ef1… stephan 2773 }
c3c4ef1… stephan 2774 }
c3c4ef1… stephan 2775
c3c4ef1… stephan 2776 break;
c3c4ef1… stephan 2777 }
c3c4ef1… stephan 2778
c3c4ef1… stephan 2779 case 'l': {
c3c4ef1… stephan 2780 for(i=0; rc==SQLITE_OK && i<p->nCol; i++){
c3c4ef1… stephan 2781 int nToken;
c3c4ef1… stephan 2782 rc = pApi->xColumnSize(pFts, i, &nToken);
c3c4ef1… stephan 2783 aOut[i] = (u32)nToken;
c3c4ef1… stephan 2784 }
c3c4ef1… stephan 2785 break;
c3c4ef1… stephan 2786 }
c3c4ef1… stephan 2787
c3c4ef1… stephan 2788 case 's': {
c3c4ef1… stephan 2789 int nInst;
c3c4ef1… stephan 2790
c3c4ef1… stephan 2791 memset(aOut, 0, sizeof(u32) * p->nCol);
c3c4ef1… stephan 2792
c3c4ef1… stephan 2793 rc = pApi->xInstCount(pFts, &nInst);
c3c4ef1… stephan 2794 for(i=0; rc==SQLITE_OK && i<nInst; i++){
c3c4ef1… stephan 2795 int iPhrase, iOff, iCol = 0;
c3c4ef1… stephan 2796 int iNextPhrase;
c3c4ef1… stephan 2797 int iNextOff;
c3c4ef1… stephan 2798 u32 nSeq = 1;
c3c4ef1… stephan 2799 int j;
c3c4ef1… stephan 2800
c3c4ef1… stephan 2801 rc = pApi->xInst(pFts, i, &iPhrase, &iCol, &iOff);
c3c4ef1… stephan 2802 iNextPhrase = iPhrase+1;
c3c4ef1… stephan 2803 iNextOff = iOff+pApi->xPhraseSize(pFts, 0);
c3c4ef1… stephan 2804 for(j=i+1; rc==SQLITE_OK && j<nInst; j++){
c3c4ef1… stephan 2805 int ip, ic, io;
c3c4ef1… stephan 2806 rc = pApi->xInst(pFts, j, &ip, &ic, &io);
c3c4ef1… stephan 2807 if( ic!=iCol || io>iNextOff ) break;
c3c4ef1… stephan 2808 if( ip==iNextPhrase && io==iNextOff ){
c3c4ef1… stephan 2809 nSeq++;
c3c4ef1… stephan 2810 iNextPhrase = ip+1;
c3c4ef1… stephan 2811 iNextOff = io + pApi->xPhraseSize(pFts, ip);
c3c4ef1… stephan 2812 }
c3c4ef1… stephan 2813 }
c3c4ef1… stephan 2814
c3c4ef1… stephan 2815 if( nSeq>aOut[iCol] ) aOut[iCol] = nSeq;
c3c4ef1… stephan 2816 }
c3c4ef1… stephan 2817
c3c4ef1… stephan 2818 break;
c3c4ef1… stephan 2819 }
c3c4ef1… stephan 2820 }
c3c4ef1… stephan 2821 return rc;
c3c4ef1… stephan 2822 }
c3c4ef1… stephan 2823
c3c4ef1… stephan 2824 static Fts5MatchinfoCtx *fts5MatchinfoNew(
c3c4ef1… stephan 2825 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
c3c4ef1… stephan 2826 Fts5Context *pFts, /* First arg to pass to pApi functions */
c3c4ef1… stephan 2827 sqlite3_context *pCtx, /* Context for returning error message */
c3c4ef1… stephan 2828 const char *zArg /* Matchinfo flag string */
c3c4ef1… stephan 2829 ){
c3c4ef1… stephan 2830 Fts5MatchinfoCtx *p;
c3c4ef1… stephan 2831 int nCol;
c3c4ef1… stephan 2832 int nPhrase;
c3c4ef1… stephan 2833 int i;
c3c4ef1… stephan 2834 int nInt;
c3c4ef1… stephan 2835 sqlite3_int64 nByte;
c3c4ef1… stephan 2836 int rc;
c3c4ef1… stephan 2837
c3c4ef1… stephan 2838 nCol = pApi->xColumnCount(pFts);
c3c4ef1… stephan 2839 nPhrase = pApi->xPhraseCount(pFts);
c3c4ef1… stephan 2840
c3c4ef1… stephan 2841 nInt = 0;
c3c4ef1… stephan 2842 for(i=0; zArg[i]; i++){
c3c4ef1… stephan 2843 int n = fts5MatchinfoFlagsize(nCol, nPhrase, zArg[i]);
c3c4ef1… stephan 2844 if( n<0 ){
c3c4ef1… stephan 2845 char *zErr = sqlite3_mprintf("unrecognized matchinfo flag: %c", zArg[i]);
c3c4ef1… stephan 2846 sqlite3_result_error(pCtx, zErr, -1);
c3c4ef1… stephan 2847 sqlite3_free(zErr);
c3c4ef1… stephan 2848 return 0;
c3c4ef1… stephan 2849 }
c3c4ef1… stephan 2850 nInt += n;
c3c4ef1… stephan 2851 }
c3c4ef1… stephan 2852
c3c4ef1… stephan 2853 nByte = sizeof(Fts5MatchinfoCtx) /* The struct itself */
c3c4ef1… stephan 2854 + sizeof(u32) * nInt /* The p->aRet[] array */
c3c4ef1… stephan 2855 + (i+1); /* The p->zArg string */
c3c4ef1… stephan 2856 p = (Fts5MatchinfoCtx*)sqlite3_malloc64(nByte);
c3c4ef1… stephan 2857 if( p==0 ){
c3c4ef1… stephan 2858 sqlite3_result_error_nomem(pCtx);
c3c4ef1… stephan 2859 return 0;
c3c4ef1… stephan 2860 }
c3c4ef1… stephan 2861 memset(p, 0, nByte);
c3c4ef1… stephan 2862
c3c4ef1… stephan 2863 p->nCol = nCol;
c3c4ef1… stephan 2864 p->nPhrase = nPhrase;
c3c4ef1… stephan 2865 p->aRet = (u32*)&p[1];
c3c4ef1… stephan 2866 p->nRet = nInt;
c3c4ef1… stephan 2867 p->zArg = (char*)&p->aRet[nInt];
c3c4ef1… stephan 2868 memcpy(p->zArg, zArg, i);
c3c4ef1… stephan 2869
c3c4ef1… stephan 2870 rc = fts5MatchinfoIter(pApi, pFts, p, fts5MatchinfoGlobalCb);
c3c4ef1… stephan 2871 if( rc!=SQLITE_OK ){
c3c4ef1… stephan 2872 sqlite3_result_error_code(pCtx, rc);
c3c4ef1… stephan 2873 sqlite3_free(p);
c3c4ef1… stephan 2874 p = 0;
c3c4ef1… stephan 2875 }
c3c4ef1… stephan 2876
c3c4ef1… stephan 2877 return p;
c3c4ef1… stephan 2878 }
c3c4ef1… stephan 2879
c3c4ef1… stephan 2880 static void fts5MatchinfoFunc(
c3c4ef1… stephan 2881 const Fts5ExtensionApi *pApi, /* API offered by current FTS version */
c3c4ef1… stephan 2882 Fts5Context *pFts, /* First arg to pass to pApi functions */
c3c4ef1… stephan 2883 sqlite3_context *pCtx, /* Context for returning result/error */
c3c4ef1… stephan 2884 int nVal, /* Number of values in apVal[] array */
c3c4ef1… stephan 2885 sqlite3_value **apVal /* Array of trailing arguments */
c3c4ef1… stephan 2886 ){
c3c4ef1… stephan 2887 const char *zArg;
c3c4ef1… stephan 2888 Fts5MatchinfoCtx *p;
c3c4ef1… stephan 2889 int rc = SQLITE_OK;
c3c4ef1… stephan 2890
c3c4ef1… stephan 2891 if( nVal>0 ){
c3c4ef1… stephan 2892 zArg = (const char*)sqlite3_value_text(apVal[0]);
c3c4ef1… stephan 2893 }else{
c3c4ef1… stephan 2894 zArg = "pcx";
c3c4ef1… stephan 2895 }
c3c4ef1… stephan 2896
c3c4ef1… stephan 2897 p = (Fts5MatchinfoCtx*)pApi->xGetAuxdata(pFts, 0);
c3c4ef1… stephan 2898 if( p==0 || sqlite3_stricmp(zArg, p->zArg) ){
c3c4ef1… stephan 2899 p = fts5MatchinfoNew(pApi, pFts, pCtx, zArg);
c3c4ef1… stephan 2900 if( p==0 ){
c3c4ef1… stephan 2901 rc = SQLITE_NOMEM;
c3c4ef1… stephan 2902 }else{
c3c4ef1… stephan 2903 rc = pApi->xSetAuxdata(pFts, p, sqlite3_free);
c3c4ef1… stephan 2904 }
c3c4ef1… stephan 2905 }
c3c4ef1… stephan 2906
c3c4ef1… stephan 2907 if( rc==SQLITE_OK ){
c3c4ef1… stephan 2908 rc = fts5MatchinfoIter(pApi, pFts, p, fts5MatchinfoLocalCb);
c3c4ef1… stephan 2909 }
c3c4ef1… stephan 2910 if( rc!=SQLITE_OK ){
c3c4ef1… stephan 2911 sqlite3_result_error_code(pCtx, rc);
c3c4ef1… stephan 2912 }else{
e2bdc10… danield 2913 /* No error has occurred, so return a copy of the array of integers. */
c3c4ef1… stephan 2914 int nByte = p->nRet * sizeof(u32);
c3c4ef1… stephan 2915 sqlite3_result_blob(pCtx, (void*)p->aRet, nByte, SQLITE_TRANSIENT);
c3c4ef1… stephan 2916 }
c3c4ef1… stephan 2917 }
c3c4ef1… stephan 2918
c3c4ef1… stephan 2919 int db_register_fts5(sqlite3 *db){
c3c4ef1… stephan 2920 int rc; /* Return code */
c3c4ef1… stephan 2921 fts5_api *pApi; /* FTS5 API functions */
c3c4ef1… stephan 2922
c3c4ef1… stephan 2923 /* Extract the FTS5 API pointer from the database handle. The
c3c4ef1… stephan 2924 ** fts5_api_from_db() function above is copied verbatim from the
c3c4ef1… stephan 2925 ** FTS5 documentation. Refer there for details. */
c3c4ef1… stephan 2926 rc = fts5_api_from_db(db, &pApi);
c3c4ef1… stephan 2927 if( rc!=SQLITE_OK ) return rc;
c3c4ef1… stephan 2928
c3c4ef1… stephan 2929 /* If fts5_api_from_db() returns NULL, then either FTS5 is not registered
c3c4ef1… stephan 2930 ** with this database handle, or an error (OOM perhaps?) has occurred.
c3c4ef1… stephan 2931 **
c3c4ef1… stephan 2932 ** Also check that the fts5_api object is version 2 or newer.
c3c4ef1… stephan 2933 */
c3c4ef1… stephan 2934 if( pApi==0 || pApi->iVersion<2 ){
c3c4ef1… stephan 2935 return SQLITE_ERROR;
c3c4ef1… stephan 2936 }
c3c4ef1… stephan 2937
c3c4ef1… stephan 2938 /* Register the implementation of matchinfo() */
c3c4ef1… stephan 2939 rc = pApi->xCreateFunction(pApi, "matchinfo", 0, fts5MatchinfoFunc, 0);
c3c4ef1… stephan 2940
c3c4ef1… stephan 2941 return rc;
232d10b… drh 2942 }

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button