File: compare.cpp

package info (click to toggle)
bibledit 5.1.036-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 252,000 kB
  • sloc: xml: 915,984; ansic: 261,349; cpp: 92,794; javascript: 32,542; sh: 4,527; makefile: 514; php: 69
file content (250 lines) | stat: -rw-r--r-- 10,365 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
/*
 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 <compare/compare.h>
#include <filter/string.h>
#include <filter/url.h>
#include <filter/roles.h>
#include <filter/usfm.h>
#include <filter/text.h>
#include <filter/diff.h>
#include <filter/passage.h>
#include <webserver/request.h>
#include <locale/translate.h>
#include <access/bible.h>
#include <tasks/logic.h>
#include <database/jobs.h>
#include <database/logs.h>
#include <database/bibles.h>
#include <database/usfmresources.h>
#include <database/books.h>
#include <database/config/bible.h>
#include <jobs/index.h>


// Compare the $bible with another Bible, passed through $compare.
void compare_compare (const std::string& bible, const std::string& compare, const int job_id)
{
  Database_Logs::log (translate("Comparing Bibles") + " " + bible + " " + translate ("and") + " " + compare, roles::consultant);

  
  Database_Jobs database_jobs = Database_Jobs ();
  Database_UsfmResources database_usfmresources = Database_UsfmResources ();

  const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);
  

  database_jobs.set_progress (job_id, translate("The Bibles are being compared..."));
  

  // The results of the comparison. Will be displayed to the user.
  std::vector <std::string> result;
  {
    std::stringstream ss {};
    ss << translate("Bible") << " " << std::quoted(bible) << " " << translate ("has been compared with") << " " << std::quoted(compare) << ".";
    result.push_back (ss.str());
  }
  result.push_back (translate("Additions are in bold.") + " " + translate ("Removed words are in strikethrough."));
  result.push_back (std::string());
  
  
  // Get the combined distinct books in both Bibles / Resources.
  const std::vector <int> bible_books = database::bibles::get_books (bible);
  const std::vector <int> compare_books = database::bibles::get_books (compare);
  const std::vector <int> resource_books = database_usfmresources.getBooks (compare);
  std::vector <int> books;
  {
    std::set <int> bookset;
    bookset.insert (bible_books.begin(), bible_books.end());
    bookset.insert (compare_books.begin(), compare_books.end());
    bookset.insert (resource_books.begin(), resource_books.end());
    books.assign (bookset.begin(), bookset.end ());
    std::sort (books.begin(), books.end());
  }
  
  
  // Results of comparison of raw USFM.
  std::vector <std::string> raw;
  
  
  // Absent books / chapters.
  std::vector <std::string> absent;
  
  
  // The new verses as in the $bible.
  std::vector <std::string> new_verses;
  
  
  for (const auto& book : books) {

    
    const std::string book_name = database::books::get_english_from_id (static_cast<book_id>(book));
    database_jobs.set_progress (job_id, book_name);
    
    
    if (std::find (bible_books.begin(), bible_books.end(), book) == bible_books.end()) {
      std::stringstream ss {};
      ss << translate("Bible") << " " << std::quoted(bible) << " " << translate ("does not contain") << " " << book_name << ".";
      absent.push_back (ss.str());
      continue;
    }
    
    if (std::find (compare_books.begin(), compare_books.end(), book) == compare_books.end()) {
      if (std::find (resource_books.begin(), resource_books.end(), book) == resource_books.end ()) {
        std::stringstream ss{};
        ss << translate("Bible/Resource") << " " << std::quoted(compare) << " " << translate ("does not contain") << " " << book_name << ".";
        absent.push_back (ss.str());
        continue;
      }
    }
    
    
    // Get the combined distinct chapters in both Bibles / Resources.
    const std::vector <int> bible_chapters = database::bibles::get_chapters (bible, book);
    const std::vector <int> compare_chapters = database::bibles::get_chapters (compare, book);
    const std::vector <int> resource_chapters = database_usfmresources.getChapters (compare, book);
    std::vector <int> chapters;
    {
      std::set <int> chapterset;
      chapterset.insert (bible_chapters.begin(), bible_chapters.end());
      chapterset.insert (compare_chapters.begin(), compare_chapters.end());
      chapterset.insert (resource_chapters.begin(), resource_chapters.end());
      chapters.assign (chapterset.begin(), chapterset.end ());
      std::sort (chapters.begin(), chapters.end());
    }


    for (const auto& chapter : chapters) {

      
      // Look for, report, and skip missing chapters in the source Bible.
      if (std::find (bible_chapters.begin(), bible_chapters.end(), chapter) == bible_chapters.end ()) {
        std::stringstream ss {};
        ss << translate("Bible") << " " << std::quoted(bible) << " " << translate ("does not contain") << " " << book_name << " " << chapter << ".";
        absent.push_back (ss.str());
        continue;
      }

      
      // Look for, report, and skip missing chapters in the comparison USFM data.
      if (std::find (compare_chapters.begin(), compare_chapters.end(), chapter) == compare_chapters.end()) {
        if (std::find (resource_chapters.begin(), resource_chapters.end(), chapter) == resource_chapters.end()) {
          std::stringstream ss {};
          ss << translate("Bible/Resource") << " " << std::quoted(compare) << " " << translate ("does not contain") << " " << book_name << " " << chapter << ".";
          absent.push_back (ss.str());
          continue;
        }
      }
      

      // Get source and compare USFM, and skip them if they are equal.
      const std::string bible_chapter_usfm = database::bibles::get_chapter (bible, book, chapter);
      std::string compare_chapter_usfm = database::bibles::get_chapter (compare, book, chapter);
      if (compare_chapter_usfm.empty()) {
        compare_chapter_usfm = database_usfmresources.getUsfm (compare, book, chapter);
      }
      if (bible_chapter_usfm == compare_chapter_usfm) 
        continue;
      
      
      // Get the sorted combined set of distinct verses in the chapter of the Bible and of the USFM to compare with.
      std::vector <int> verses {};
      {
        const std::vector <int> bible_verse_numbers = filter::usfm::get_verse_numbers (bible_chapter_usfm);
        const std::vector <int> compare_verse_numbers = filter::usfm::get_verse_numbers (compare_chapter_usfm);
        std::set <int> verse_set {};
        verse_set.insert (bible_verse_numbers.begin(), bible_verse_numbers.end());
        verse_set.insert (compare_verse_numbers.begin(), compare_verse_numbers.end());
        verses.assign (verse_set.begin(), verse_set.end ());
        std::sort (verses.begin(), verses.end());
      }
      
      
      for (const int& verse : verses) {
 

        // Get the USFM of verse of the Bible and comparison USFM, and skip it if both are the same.
        const std::string bible_verse_usfm = filter::usfm::get_verse_text (bible_chapter_usfm, verse);
        const std::string compare_verse_usfm = filter::usfm::get_verse_text (compare_chapter_usfm, verse);
        if (bible_verse_usfm == compare_verse_usfm)
          continue;
        
        Filter_Text filter_text_bible = Filter_Text (bible);
        Filter_Text filter_text_compare = Filter_Text (compare);
        filter_text_bible.html_text_standard = new HtmlText (std::string());
        filter_text_compare.html_text_standard = new HtmlText (std::string());
        filter_text_bible.text_text = new Text_Text ();
        filter_text_compare.text_text = new Text_Text ();
        filter_text_bible.add_usfm_code (bible_verse_usfm);
        filter_text_compare.add_usfm_code (compare_verse_usfm);
        filter_text_bible.run (stylesheet);
        filter_text_compare.run (stylesheet);
        const std::string bible_html = filter_text_bible.html_text_standard->get_inner_html ();
        const std::string compare_html = filter_text_compare.html_text_standard->get_inner_html ();
        const std::string bible_text = filter_text_bible.text_text->get ();
        const std::string compare_text = filter_text_compare.text_text->get ();
        if (bible_text != compare_text) {
          const std::string modification = filter_diff_diff (compare_text, bible_text);
          result.push_back (filter_passage_display (book, chapter, std::to_string (verse)) + " " + modification);
          new_verses.push_back (filter_passage_display (book, chapter, std::to_string (verse)) + " " + bible_text);
        }
        const std::string modification = filter_diff_diff (compare_verse_usfm, bible_verse_usfm);
        raw.push_back (filter_passage_display (book, chapter, std::to_string (verse)) + " " + modification);
      }
    }
  }

  
  // Add the absent books / chapters to the comparison.
  if (!absent.empty ()) {
    result.push_back (std::string());
    result.insert (result.end (), absent.begin(), absent.end());
  }

  
  // Add any differences in the raw USFM to the comparison.
  if (!raw.empty ()) {
    result.push_back (std::string());
    result.insert (result.end (), raw.begin(), raw.end());
  }
 
  
  // Add the text of the new verses, as they are in the $bible.
  if (!new_verses.empty ()) {
    result.push_back (std::string());
    result.push_back (translate("The texts as they are in the Bible") + " " + bible);
    result.push_back (std::string());
    result.insert (result.end(), new_verses.begin(), new_verses.end());
  }
  
  
  // Format and store the result of the comparison.
  for (auto& line : result) {
    if (line.empty()) {
      line = "<br>";
    } else {
      line.insert (0, "<p>");
      line.append ("</p>");
    }
  }
  database_jobs.set_result (job_id, filter::strings::implode (result, "\n"));
  
  
  Database_Logs::log (translate("Comparison is ready"), roles::consultant);
}