File: mappings.cpp

package info (click to toggle)
bibledit-cloud 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: 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 (369 lines) | stat: -rw-r--r-- 12,928 bytes parent folder | download | duplicates (5)
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
/*
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 <database/mappings.h>
#include <filter/url.h>
#include <filter/string.h>
#include <database/sqlite.h>
#include <database/books.h>


// This is a database for the verse mapping systems.
// Resilience: It is normally not written to, so corruption is unlikely.


constexpr const auto mappings {"mappings"};


Database_Mappings::Database_Mappings ()
{
}


Database_Mappings::~Database_Mappings ()
{
}


void Database_Mappings::create1 ()
{
  SqliteDatabase sql (mappings);
  sql.set_sql ("CREATE TABLE IF NOT EXISTS maps ("
               "name text,"
               "book integer,"
               "chapter integer,"
               "verse integer,"
               "origbook integer,"
               "origchapter integer,"
               "origverse integer"
               ");");
  sql.execute ();
  sql.set_sql ("DROP INDEX IF EXISTS bible;");
  sql.execute ();
  sql.set_sql ("DROP INDEX IF EXISTS original;");
  sql.execute ();
}


void Database_Mappings::create2 ()
{
  SqliteDatabase sql (mappings);
  sql.set_sql ("CREATE INDEX IF NOT EXISTS bible ON maps (name, book, chapter, verse);");
  sql.execute ();
  sql.set_sql ("CREATE INDEX IF NOT EXISTS original ON maps (name, book, chapter, verse);");
  sql.execute ();
}


// Import the default mappings that come with Bibledit.
void Database_Mappings::defaults ()
{
  const std::string folder = filter_url_create_root_path ({"mapping"});
  const std::vector <std::string> files = filter_url_scandir (folder);
  for (const auto& file : files) {
    std::string name (file);
    const std::string extension = filter_url_get_extension (name);
    if (extension != "txt")
      continue;
    name = name.substr (0, strlen (name.c_str()) - 4);
    name = filter::strings::replace ("_", " ", name);
    const std::string path = filter_url_create_path ({folder, file});
    const std::string data = filter_url_file_get_contents (path);
    import (name, data);
  }
}


void Database_Mappings::optimize ()
{
  SqliteDatabase sql (mappings);
  sql.set_sql ("VACUUM;");
  sql.execute();
}


void Database_Mappings::import (const std::string& name, const std::string& data)
{
  // Delete existing mapping with this name.
  erase (name);

  SqliteDatabase sql (mappings);

  // Begin a transaction for better speed.
  sql.set_sql ("BEGIN;");
  sql.execute();

  const std::vector <std::string> lines = filter::strings::explode (data, '\n');
  for (std::string line : lines) {
    
    // Each line looks like this: 
    // Haggai 2:15 = Haggai 2:14
    // At the left is the passage in this versification.
    // At the right is the passage in the original Hebrew and Greek versification.
    line = filter::strings::trim (line);
    if (line.empty ()) continue;

    // Cut the line into two: The two passages.
    std::vector <std::string> entry = filter::strings::explode (line, '=');
    if (entry.size() != 2) continue;
    
    std::string passage_string = filter::strings::trim (entry [0]);
    std::string original_string = filter::strings::trim (entry [1]);

    // Storage for further interpretation.
    std::vector <std::string> bits;
    
    // Split the passage entry on the colon (:) to get the verse.
    bits = filter::strings::explode(passage_string, ':');
    if (bits.size() != 2) continue;
    int passage_verse = filter::strings::convert_to_int(bits[1]);
    // Split the first bit on the spaces and get the last item as the chapter.
    bits = filter::strings::explode(bits[0], ' ');
    if (bits.size() < 2) continue;
    int passage_chapter = filter::strings::convert_to_int(bits[bits.size()-1]);
    // Remove the last bit so it remains with the book, and get that book.
    bits.pop_back();
    std::string passage_book_string = filter::strings::implode(bits, " ");
    int passage_book = static_cast<int>(database::books::get_id_from_english(passage_book_string));

    // Split the original entry on the colon (:) to get the verse.
    bits = filter::strings::explode(original_string, ':');
    if (bits.size() != 2) continue;
    int original_verse = filter::strings::convert_to_int(bits[1]);
    // Split the first bit on the spaces and get the last item as the chapter.
    bits = filter::strings::explode(bits[0], ' ');
    if (bits.size() < 2) continue;
    int original_chapter = filter::strings::convert_to_int(bits[bits.size()-1]);
    // Remove the last bit so it remains with the book, and get that book.
    bits.pop_back();
    std::string original_book_string = filter::strings::implode(bits, " ");
    int original_book = static_cast<int>(database::books::get_id_from_english(original_book_string));

    // Store it in the database.
    sql.clear();
    sql.add ("INSERT INTO maps VALUES (");
    sql.add (name);
    sql.add (",");
    sql.add (passage_book);
    sql.add (",");
    sql.add (passage_chapter);
    sql.add (",");
    sql.add (passage_verse);
    sql.add (",");
    sql.add (original_book);
    sql.add (",");
    sql.add (original_chapter);
    sql.add (",");
    sql.add (original_verse);
    sql.add (");");
    sql.execute ();
  }

  // Commit the transaction.
  sql.set_sql ("COMMIT;");
  sql.execute();
}


// Exports a mapping.
std::string Database_Mappings::output (const std::string& name)
{
  std::vector <std::string> data;
  SqliteDatabase sql (mappings);
  sql.add ("SELECT * FROM maps WHERE name =");
  sql.add (name);
  sql.add ("ORDER BY book ASC, chapter ASC, verse ASC;");
  std::map <std::string, std::vector <std::string> > result = sql.query ();
  const std::vector <std::string> books = result ["book"];
  const std::vector <std::string> chapters = result ["chapter"];
  const std::vector <std::string> verses = result ["verse"];
  const std::vector <std::string> origbooks = result ["origbook"];
  const std::vector <std::string> origchapters = result ["origchapter"];
  const std::vector <std::string> origverses = result ["origverse"];

  for (unsigned int i = 0; i < books.size (); i++) {
    const int book = filter::strings::convert_to_int (books [i]);
    const std::string bookname = database::books::get_english_from_id (static_cast<book_id>(book));
    const std::string chapter = chapters [i];
    const std::string verse = verses [i];
    const int origbook = filter::strings::convert_to_int (origbooks[i]);
    const std::string origbookname = database::books::get_english_from_id (static_cast<book_id>(origbook));
    const std::string origchapter = origchapters[i];
    const std::string origverse = origverses [i];
    const std::string item = bookname + " " + chapter + ":" + verse + " = " + origbookname + " " + origchapter + ":" + origverse;
    data.push_back (item);
  }
  return filter::strings::implode (data, "\n");
}


void Database_Mappings::create (const std::string& name)
{
  // Insert one entry, so the $name makes it into the database.
  SqliteDatabase sql (mappings);
  sql.add ("INSERT INTO maps VALUES (");
  sql.add (name);
  sql.add (", 1, 1, 1, 1, 1, 1);");
  sql.execute ();
}


void Database_Mappings::erase (const std::string& name)
{
  SqliteDatabase sql (mappings);
  sql.add ("DELETE FROM maps WHERE name =");
  sql.add (name);
  sql.add (";");
  sql.execute ();
}


// Returns the mapping names in the database.
std::vector <std::string> Database_Mappings::names ()
{
  // Get the names from the database.
  SqliteDatabase sql (mappings);
  sql.set_sql ("SELECT DISTINCT name FROM maps;");
  std::vector <std::string> names = sql.query ()["name"];

  // Ensure the original mapping is there too.
  if (find (names.begin (), names.end (), original ()) == names.end()) {
    names.push_back (original ());
  }

  sort (names.begin (), names.end());

  return names;
}


std::string Database_Mappings::original ()
{
  return "Hebrew Greek";
}


// This function translates a $book, $chapter, and $verse 
// in the $input versification to a $book, $chapter and $verse 
// in the $output versification.
// It returns an array with one passage in most cases.
// When the verses in the $input and $output versifications overlap,
// it may return an array with two passages.
std::vector <Passage> Database_Mappings::translate (const std::string& input, const std::string& output, int book, int chapter, int verse)
{
  // Care for situation that the input and output are the same.
  if (input == output) {
    Passage passage = Passage ("", book, chapter, std::to_string (verse));
    return {passage};
  }

  // Get the $input mapping for the passage from the database.
  // This maps the $input to the Hebrew/Greek versification system.
  // Skip this phase if the $input mapping is Hebrew / Greek.
  std::vector <Passage> origpassage;
  if (input != original ()) {
    SqliteDatabase sql (mappings);
    sql.add ("SELECT origbook, origchapter, origverse FROM maps WHERE name =");
    sql.add (input);
    sql.add ("AND book =");
    sql.add (book);
    sql.add ("AND chapter =");
    sql.add (chapter);
    sql.add ("AND verse =");
    sql.add (verse);
    sql.add (";");
    std::map <std::string, std::vector <std::string> > result = sql.query ();
    const std::vector <std::string> origbooks = result ["origbook"];
    const std::vector <std::string> origchapters = result ["origchapter"];
    const std::vector <std::string> origverses = result ["origverse"];
    for (unsigned int i = 0; i < origbooks.size (); i++) {
      const Passage passage ("", filter::strings::convert_to_int (origbooks [i]), filter::strings::convert_to_int (origchapters [i]), origverses [i]);
      origpassage.push_back (passage);
    }
  }
  
  // Check that the search yields a passage.
  // If there is none, it means that the $input passage is the same as in Hebrew/Greek.
  if (origpassage.empty ()) {
    Passage passage = Passage ("", book, chapter, std::to_string (verse));
    origpassage.push_back (passage);
  }

  // If the $output mapping is Hebrew/Greek, then we're done.
  if (output == original ()) {
    return origpassage;
  }
  
  // Get the $output mapping for the passage or two passages from the database.
  // This is a translation from Hebrew/Greek to the $output system.
  std::vector <Passage> targetpassage;
  for (const Passage& passage : origpassage) {
    const int origbook = passage.m_book;
    const int origchapter = passage.m_chapter;
    const int origverse = filter::strings::convert_to_int (passage.m_verse);
    SqliteDatabase sql (mappings);
    sql.add ("SELECT book, chapter, verse FROM maps WHERE name =");
    sql.add (output);
    sql.add ("AND origbook =");
    sql.add (origbook);
    sql.add ("AND origchapter =");
    sql.add (origchapter);
    sql.add ("AND origverse =");
    sql.add (origverse);
    sql.add (";");
    std::map <std::string, std::vector <std::string> > result = sql.query ();
    const std::vector <std::string> books = result ["book"];
    const std::vector <std::string> chapters = result ["chapter"];
    const std::vector <std::string> verses = result ["verse"];
    for (unsigned int i = 0; i < books.size (); i++) {
      Passage passage2 = Passage (std::string(), filter::strings::convert_to_int (books [i]), filter::strings::convert_to_int (chapters [i]), verses [i]);
      bool passageExists = false;
      for (auto& existingpassage : targetpassage) {
        if (existingpassage.equal (passage2)) passageExists = true;
      }
      if (!passageExists) targetpassage.push_back (passage2);
    }
  }
  
  // Check that the search yields a passage.
  // If none, it means that the $output passage is the same as in Hebrew/Greek.
  if (targetpassage.empty()) {
    targetpassage = origpassage;
  }
  
  // Result.
  return targetpassage;
}


/*

 To not overwrite updated verse mappings, 
 there could be one database with the defaults, 
 and another one on the system with the modifications.
 The updated mappings will be stored in the second database. 
 That means that if the name of a mapping occurs in a second database, 
 that it won't read it from the first database. 
 This enables the first database to be always updated, 
 while the system keeps reading from the second database.

 Another way of doing is to keep it as it is, and make the clients download updated mappings from the Cloud.
 And then to preserver existing mappings in the Cloud on update.
 
*/