File: logic.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 250,636 kB
  • sloc: xml: 915,934; ansic: 261,349; cpp: 92,628; javascript: 32,542; sh: 4,915; makefile: 586; php: 69
file content (547 lines) | stat: -rw-r--r-- 18,907 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
/*
 Copyright (©) 2003-2025 Teus Benschop.
 
 This program is free software; you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation; either version 3 of the License, or
 (at your option) any later version.
 
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with this program; if not, write to the Free Software
 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */


#include <search/logic.h>
#include <filter/url.h>
#include <filter/string.h>
#include <filter/usfm.h>
#include <filter/text.h>
#include <config/globals.h>
#include <database/bibles.h>
#include <database/config/bible.h>
#include <database/logic.h>


std::string search_logic_index_folder ()
{
  return filter_url_create_root_path ({database_logic_databases (), "search"});
}


std::string search_logic_bible_fragment (std::string bible)
{
  return filter_url_create_path ({search_logic_index_folder (), bible + "_"});
}


std::string search_logic_book_fragment (std::string bible, int book)
{
  return search_logic_bible_fragment (bible) + std::to_string (book) + "_";
}


std::string search_logic_chapter_file (std::string bible, int book, int chapter)
{
  return search_logic_book_fragment (bible, book) + std::to_string (chapter);
}


std::string search_logic_verse_separator ()
{
  return "v#e#r#s#e#s#e#p#a#r#a#t#o#r";
}


std::string search_logic_index_separator ()
{
  return "i#n#d#e#x#s#e#p#a#r#a#t#o#r";
}


#define USFM_RAW 1
#define USFM_LOWER 2
#define PLAIN_RAW 3
#define PLAIN_LOWER 4


// Indexes a $bible $book $chapter for searching.
void search_logic_index_chapter (std::string bible, int book, int chapter)
{
  std::string usfm = database::bibles::get_chapter (bible, book, chapter);
  const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);

  std::vector <std::string> index;
  
  std::set <std::string> already_processed;
  
  std::vector <int> verses = filter::usfm::get_verse_numbers (usfm);
  
  for (auto verse : verses) {

    std::string raw_usfm = filter::strings::trim (filter::usfm::get_verse_text (usfm, verse));

    // In case of combined verses, the bit of USFM may have been indexed already.
    // Skip it in that case.
    if (already_processed.find (raw_usfm) != already_processed.end ()) continue;
    already_processed.insert (raw_usfm);

    index.push_back (search_logic_verse_separator ());
    index.push_back (std::to_string (verse));
    index.push_back (search_logic_index_separator ());

    index.push_back (raw_usfm);
    
    std::string usfm_lower = filter::strings::unicode_string_casefold (raw_usfm);

    index.push_back (search_logic_index_separator ());

    index.push_back (usfm_lower);
    
    // Text filter for getting the plain text.
    Filter_Text filter_text = Filter_Text (bible);
    filter_text.text_text = new Text_Text ();
    filter_text.initializeHeadingsAndTextPerVerse (true);
    filter_text.add_usfm_code (raw_usfm);
    filter_text.run (stylesheet);

    std::string raw_plain;
    // Add the clean verse texts.
    std::map <int, std::string> texts = filter_text.getVersesText ();
    for (auto & element : texts) {
      raw_plain.append (element.second + "\n");
    }
    // Add any clean headings.
    std::map <int, std::string> headings = filter_text.verses_headings;
    for (auto & element : headings) {
      raw_plain.append (element.second + "\n");
    }
    // Add any footnotes.
    raw_plain.append (filter_text.text_text->getnote ());
    // Clean up.
    raw_plain = filter::strings::trim (raw_plain);
    
    index.push_back (search_logic_index_separator ());

    index.push_back (raw_plain);
    
    std::string plain_lower = filter::strings::unicode_string_casefold (raw_plain);

    index.push_back (search_logic_index_separator ());

    index.push_back (plain_lower);
  }
  
  index.push_back (search_logic_index_separator ());
  
  // Store everything.
  std::string path = search_logic_chapter_file (bible, book, chapter);
  filter_url_file_put_contents (path, filter::strings::implode (index, "\n"));
}


// Searches the text of the Bibles.
// Returns an array with matching passages.
// $search: Contains the text to search for.
// $bibles: Array of Bible names to search in.
std::vector <Passage> search_logic_search_text (std::string search, std::vector <std::string> bibles)
{
  std::vector <Passage> passages;
  
  if (search == "") return passages;
  
  search = filter::strings::unicode_string_casefold (search);
  search = filter::strings::replace (",", "", search);
  
  for (auto bible : bibles) {
    std::vector <int> books = database::bibles::get_books (bible);
    for (auto book : books) {
      std::vector <int> chapters = database::bibles::get_chapters (bible, book);
      for (auto chapter : chapters) {
        std::string path = search_logic_chapter_file (bible, book, chapter);
        std::string index = filter_url_file_get_contents (path);
        if (index.find (search) != std::string::npos) {
          std::vector <std::string> lines = filter::strings::explode (index, '\n');
          int index_verse = 0;
          bool read_index_verse = false;
          int index_item = 0;
          for (auto & line : lines) {
            if (read_index_verse) {
              index_verse = filter::strings::convert_to_int (line);
              read_index_verse = false;
            } else if (line == search_logic_verse_separator ()) {
              read_index_verse = true;
              index_item = 0;
            } else if (line == search_logic_index_separator ()) {
              index_item++;
            } else if (index_item == PLAIN_LOWER) {
              if (line.find (search) != std::string::npos) {
                passages.push_back (Passage (bible, book, chapter, std::to_string (index_verse)));
              }
            }
          }
        }
      }
    }
  }

  return passages;
}


// Performs a case-insensitive search of the text of one $bible.
// Returns an array with the matching passages.
// $search: Contains the text to search for.
std::vector <Passage> search_logic_search_bible_text (std::string bible, std::string search)
{
  std::vector <Passage> passages;
  
  if (search == "") return passages;
  
  search = filter::strings::unicode_string_casefold (search);
  
  std::vector <int> books = database::bibles::get_books (bible);
  for (auto book : books) {
    std::vector <int> chapters = database::bibles::get_chapters (bible, book);
    for (auto chapter : chapters) {
      std::string path = search_logic_chapter_file (bible, book, chapter);
      std::string index = filter_url_file_get_contents (path);
      if (index.find (search) != std::string::npos) {
        std::vector <std::string> lines = filter::strings::explode (index, '\n');
        int index_verse = 0;
        bool read_index_verse = false;
        int index_item = 0;
        for (auto & line : lines) {
          if (read_index_verse) {
            index_verse = filter::strings::convert_to_int (line);
            read_index_verse = false;
          } else if (line == search_logic_verse_separator ()) {
            read_index_verse = true;
            index_item = 0;
          } else if (line == search_logic_index_separator ()) {
            index_item++;
          } else if (index_item == PLAIN_LOWER) {
            if (line.find (search) != std::string::npos) {
              passages.push_back (Passage (bible, book, chapter, std::to_string (index_verse)));
            }
          }
        }
      }
    }
  }
  
  return passages;
}


// Performs a case-sensitive search of the text of one $bible.
// Returns an array with the rowid's of matching verses.
// $search: Contains the text to search for.
std::vector <Passage> search_logic_search_bible_text_case_sensitive (std::string bible, std::string search)
{
  std::vector <Passage> passages;
  
  if (search == "") return passages;
  
  std::vector <int> books = database::bibles::get_books (bible);
  for (auto book : books) {
    std::vector <int> chapters = database::bibles::get_chapters (bible, book);
    for (auto chapter : chapters) {
      std::string path = search_logic_chapter_file (bible, book, chapter);
      std::string index = filter_url_file_get_contents (path);
      if (index.find (search) != std::string::npos) {
        std::vector <std::string> lines = filter::strings::explode (index, '\n');
        int index_verse = 0;
        bool read_index_verse = false;
        int index_item = 0;
        for (auto & line : lines) {
          if (read_index_verse) {
            index_verse = filter::strings::convert_to_int (line);
            read_index_verse = false;
          } else if (line == search_logic_verse_separator ()) {
            read_index_verse = true;
            index_item = 0;
          } else if (line == search_logic_index_separator ()) {
            index_item++;
          } else if (index_item == PLAIN_RAW) {
            if (line.find (search) != std::string::npos) {
              passages.push_back (Passage (bible, book, chapter, std::to_string (index_verse)));
            }
          }
        }
      }
    }
  }
  
  return passages;
}


// Performs a case-insensitive search of the USFM of one bible.
// Returns an array with the rowid's of matching verses.
// search: Contains the text to search for.
std::vector <Passage> search_logic_search_bible_usfm (std::string bible, std::string search)
{
  std::vector <Passage> passages;
  
  if (search == "") return passages;
  
  search = filter::strings::unicode_string_casefold (search);
  
  std::vector <int> books = database::bibles::get_books (bible);
  for (auto book : books) {
    std::vector <int> chapters = database::bibles::get_chapters (bible, book);
    for (auto chapter : chapters) {
      std::string path = search_logic_chapter_file (bible, book, chapter);
      std::string index = filter_url_file_get_contents (path);
      if (index.find (search) != std::string::npos) {
        std::vector <std::string> lines = filter::strings::explode (index, '\n');
        int index_verse = 0;
        bool read_index_verse = false;
        int index_item = 0;
        for (auto & line : lines) {
          if (read_index_verse) {
            index_verse = filter::strings::convert_to_int (line);
            read_index_verse = false;
          } else if (line == search_logic_verse_separator ()) {
            read_index_verse = true;
            index_item = 0;
          } else if (line == search_logic_index_separator ()) {
            index_item++;
          } else if (index_item == USFM_LOWER) {
            if (line.find (search) != std::string::npos) {
              passages.push_back (Passage (bible, book, chapter, std::to_string (index_verse)));
            }
          }
        }
      }
    }
  }
  
  return passages;
}


// Performs a case-sensitive search of the USFM of one $bible.
// Returns an array with the rowid's of matching verses.
// $search: Contains the text to search for.
std::vector <Passage> search_logic_search_bible_usfm_case_sensitive (std::string bible, std::string search)
{
  std::vector <Passage> passages;
  
  if (search == "") return passages;
  
  std::vector <int> books = database::bibles::get_books (bible);
  for (auto book : books) {
    std::vector <int> chapters = database::bibles::get_chapters (bible, book);
    for (auto chapter : chapters) {
      std::string path = search_logic_chapter_file (bible, book, chapter);
      std::string index = filter_url_file_get_contents (path);
      if (index.find (search) != std::string::npos) {
        std::vector <std::string> lines = filter::strings::explode (index, '\n');
        int index_verse = 0;
        bool read_index_verse = false;
        int index_item = 0;
        for (auto & line : lines) {
          if (read_index_verse) {
            index_verse = filter::strings::convert_to_int (line);
            read_index_verse = false;
          } else if (line == search_logic_verse_separator ()) {
            read_index_verse = true;
            index_item = 0;
          } else if (line == search_logic_index_separator ()) {
            index_item++;
          } else if (index_item == USFM_RAW) {
            if (line.find (search) != std::string::npos) {
              passages.push_back (Passage (bible, book, chapter, std::to_string (index_verse)));
            }
          }
        }
      }
    }
  }
  
  return passages;
}


// Gets the plain raw text for the bible and passage given.
std::string search_logic_get_bible_verse_text (std::string bible, int book, int chapter, int verse)
{
  std::vector <std::string> texts;
  std::string path = search_logic_chapter_file (bible, book, chapter);
  std::string index = filter_url_file_get_contents (path);
  std::vector <std::string> lines = filter::strings::explode (index, '\n');
  int index_verse = 0;
  bool read_index_verse = false;
  int index_item = 0;
  for (auto & line : lines) {
    if (read_index_verse) {
      index_verse = filter::strings::convert_to_int (line);
      read_index_verse = false;
    } else if (line == search_logic_verse_separator ()) {
      read_index_verse = true;
      index_item = 0;
    } else if (line == search_logic_index_separator ()) {
      index_item++;
    } else if (index_item == PLAIN_RAW) {
      if (verse == index_verse) {
        texts.push_back (line);
      }
    }
  }
  return filter::strings::implode (texts, "\n");
}


// Gets the raw USFM for the bible and passage given.
std::string search_logic_get_bible_verse_usfm (std::string bible, int book, int chapter, int verse)
{
  std::vector <std::string> texts;
  std::string path = search_logic_chapter_file (bible, book, chapter);
  std::string index = filter_url_file_get_contents (path);
  std::vector <std::string> lines = filter::strings::explode (index, '\n');
  int index_verse = 0;
  bool read_index_verse = false;
  int index_item = 0;
  for (auto & line : lines) {
    if (read_index_verse) {
      index_verse = filter::strings::convert_to_int (line);
      read_index_verse = false;
    } else if (line == search_logic_verse_separator ()) {
      read_index_verse = true;
      index_item = 0;
    } else if (line == search_logic_index_separator ()) {
      index_item++;
    } else if (index_item == USFM_RAW) {
      if (verse == index_verse) {
        texts.push_back (line);
      }
    }
  }
  return filter::strings::implode (texts, "\n");
}


void search_logic_delete_bible (std::string bible)
{
  std::string fragment = search_logic_bible_fragment (bible);
  fragment = filter_url_basename (fragment);
  std::vector <std::string> files = filter_url_scandir (search_logic_index_folder ());
  for (auto & file : files) {
    if (file.find (fragment) == 0) {
      std::string path = filter_url_create_path ({search_logic_index_folder (), file});
      filter_url_unlink (path);
    }
  }
}


void search_logic_delete_book (std::string bible, int book)
{
  std::string fragment = search_logic_book_fragment (bible, book);
  fragment = filter_url_basename (fragment);
  std::vector <std::string> files = filter_url_scandir (search_logic_index_folder ());
  for (auto & file : files) {
    if (file.find (fragment) == 0) {
      std::string path = filter_url_create_path ({search_logic_index_folder (), file});
      filter_url_unlink (path);
    }
  }
}


void search_logic_delete_chapter (std::string bible, int book, int chapter)
{
  std::string fragment = search_logic_chapter_file (bible, book, chapter);
  fragment = filter_url_basename (fragment);
  std::vector <std::string> files = filter_url_scandir (search_logic_index_folder ());
  for (auto & file : files) {
    if (file.find (fragment) == 0) {
      std::string path = filter_url_create_path ({search_logic_index_folder (), file});
      filter_url_unlink (path);
    }
  }
}


// Returns the total verse count within a $bible.
int search_logic_get_verse_count (std::string bible)
{
  int verse_count = 0;
  std::vector <int> books = database::bibles::get_books (bible);
  for (auto book : books) {
    std::vector <int> chapters = database::bibles::get_chapters (bible, book);
    for (auto chapter : chapters) {
      std::string path = search_logic_chapter_file (bible, book, chapter);
      std::string index = filter_url_file_get_contents (path);
      std::vector <std::string> lines = filter::strings::explode (index, '\n');
      for (auto & line : lines) {
        if (line == search_logic_verse_separator ()) {
          verse_count++;
        }
      }
    }
  }
  return verse_count;
}


// Copies the search index of Bible $original to Bible $destination.
void search_logic_copy_bible (std::string original, std::string destination)
{
  std::string original_fragment = search_logic_bible_fragment (original);
  original_fragment = filter_url_basename (original_fragment);
  std::string destination_fragment = search_logic_bible_fragment (destination);
  destination_fragment = filter_url_basename (destination_fragment);
  std::vector <std::string> files = filter_url_scandir (search_logic_index_folder ());
  for (const auto& file : files) {
    if (file.find (original_fragment) == 0) {
      const std::string original_path = filter_url_create_path ({search_logic_index_folder (), file});
      const std::string destination_file = destination_fragment + file.substr (original_fragment.length ());
      const std::string destination_path = filter_url_create_path ({search_logic_index_folder (), destination_file});
      filter_url_file_cp (original_path, destination_path);
    }
  }
}


// This generated the plain text that can be used as a reference during a replace operation.
std::string search_logic_plain_replace_verse_text (std::string usfm)
{
  // Text filter for getting the plain text.
  Filter_Text filter_text = Filter_Text ("");
  filter_text.text_text = new Text_Text ();
  filter_text.initializeHeadingsAndTextPerVerse (true);
  filter_text.add_usfm_code (usfm);
  filter_text.run (stylesv2::standard_sheet ());
  
  // The resulting plain text.
  std::string plain_text;

  // Add the clean verse texts.
  std::map <int, std::string> texts = filter_text.getVersesText ();
  for (auto & element : texts) {
    plain_text.append (element.second + "\n");
  }

  // Add any clean headings.
  std::map <int, std::string> headings = filter_text.verses_headings;
  for (auto & element : headings) {
    plain_text.append (element.second + "\n");
  }

  // Add any footnotes.
  plain_text.append (filter_text.text_text->getnote ());
  
  // Clean up.
  plain_text = filter::strings::trim (plain_text);
  
  // Done.
  return plain_text;
}