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 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
|
/*
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 <bb/logic.h>
#include <filter/string.h>
#include <filter/url.h>
#include <filter/roles.h>
#include <filter/git.h>
#include <filter/diff.h>
#include <filter/usfm.h>
#include <filter/text.h>
#include <filter/date.h>
#include <database/bibles.h>
#include <database/modifications.h>
#include <database/bibleactions.h>
#include <database/logs.h>
#include <database/versifications.h>
#include <database/books.h>
#include <database/config/general.h>
#include <database/cache.h>
#include <database/privileges.h>
#include <database/config/bible.h>
#include <client/logic.h>
#include <demo/logic.h>
#include <sync/resources.h>
#include <sword/logic.h>
#include <resource/logic.h>
#include <search/logic.h>
#include <locale/translate.h>
#include <email/send.h>
#include <developer/logic.h>
#include <locale/translate.h>
#include <editor/html2format.h>
#include <book/create.h>
void bible_logic::store_chapter (const std::string& bible, int book, int chapter, const std::string& usfm)
{
// Record data of the chapter to be stored prior to storing the new version.
// Both client and cloud follow this order.
#ifdef HAVE_CLIENT
// Client stores Bible action.
const std::string oldusfm = database::bibles::get_chapter (bible, book, chapter);
database::bible_actions::record (bible, book, chapter, oldusfm);
// Kick the unsent-data timeout mechanism.
bible_logic::kick_unsent_data_timer ();
#endif
#ifdef HAVE_CLOUD
// Server stores diff data.
database::modifications::storeTeamDiff (bible, book, chapter);
#endif
// Store the chapter in the database.
database::bibles::store_chapter (bible, book, chapter, usfm);
}
void bible_logic::delete_chapter (const std::string& bible, int book, int chapter)
{
// Cloud and client record data of the chapter to be deleted prior to deletion.
#ifdef HAVE_CLIENT
// Client stores Bible action.
const std::string usfm = database::bibles::get_chapter (bible, book, chapter);
database::bible_actions::record (bible, book, chapter, usfm);
// Kick the unsent-data timeout mechanism.
bible_logic::kick_unsent_data_timer ();
#endif
#ifdef HAVE_CLOUD
// Server stores diff data.
database::modifications::storeTeamDiff (bible, book, chapter);
#endif
// Delete the chapter from the database.
database::bibles::delete_chapter (bible, book, chapter);
}
void bible_logic::delete_book (const std::string& bible, int book)
{
// Both client and cloud record data of the book to be deleted prior to deletion.
#ifdef HAVE_CLIENT
// Client stores Bible actions.
const std::vector <int> chapters = database::bibles::get_chapters (bible, book);
for (const auto& chapter : chapters) {
const std::string usfm = database::bibles::get_chapter (bible, book, chapter);
database::bible_actions::record (bible, book, chapter, usfm);
}
// Kick the unsent-data timeout mechanism.
bible_logic::kick_unsent_data_timer ();
#endif
#ifdef HAVE_CLOUD
// Server stores diff data.
database::modifications::storeTeamDiffBook (bible, book);
#endif
// Delete the book from the database.
database::bibles::delete_book (bible, book);
}
void bible_logic::delete_bible (const std::string& bible)
{
// The client and the cloud record data of the Bible to be deleted prior to deletion.
#ifdef HAVE_CLIENT
// Client stores Bible actions.
const std::vector <int> books = database::bibles::get_books (bible);
for (const auto book : books) {
const std::vector <int> chapters = database::bibles::get_chapters (bible, book);
for (const auto chapter : chapters) {
const std::string usfm = database::bibles::get_chapter (bible, book, chapter);
database::bible_actions::record (bible, book, chapter, usfm);
}
}
// Kick the unsent-data timeout mechanism.
bible_logic::kick_unsent_data_timer ();
#endif
#ifdef HAVE_CLOUD
// Server stores diff data.
database::modifications::storeTeamDiffBible (bible);
// Possible git repository.
const std::string gitdirectory = filter_git_directory (bible);
if (file_or_dir_exists (gitdirectory)) {
filter_url_rmdir (gitdirectory);
}
#endif
// Delete the Bible from the database.
database::bibles::delete_bible (bible);
// Delete the search index.
search_logic_delete_bible (bible);
// Delete associated settings and privileges.
DatabasePrivileges::remove_bible (bible);
database::config::bible::remove (bible);
}
void bible_logic::import_resource (std::string bible, std::string resource)
{
Database_Logs::log ("Starting to import resource " + resource + " into Bible " + bible);
Database_Versifications database_versifications {};
Webserver_Request webserver_request {};
const std::vector <int> books = database_versifications.getMaximumBooks ();
for (const auto book : books) {
const std::string bookName = database::books::get_english_from_id (static_cast<book_id>(book));
const std::vector <int> chapters = database_versifications.getMaximumChapters (book);
for (const auto chapter : chapters) {
const std::string message = "Importing " + resource + " " + bookName + " chapter " + std::to_string (chapter);
Database_Logs::log (message, roles::translator);
std::vector <std::string> usfm {};
if (chapter == 0) usfm.push_back ("\\id " + database::books::get_usfm_from_id (static_cast<book_id>(book)));
if (chapter) {
usfm.push_back ("\\c " + std::to_string (chapter));
usfm.push_back ("\\p");
}
const std::vector <int> verses = database_versifications.getMaximumVerses (book, chapter);
for (const auto verse : verses) {
if (verse == 0) continue;
// Fetch the text for the passage.
bool server_is_installing_module = false;
int wait_iterations = 0;
std::string html;
do {
// Fetch this resource from the server.
html = resource_logic_get_verse (webserver_request, resource, book, chapter, verse);
server_is_installing_module = (html == sword_logic_installing_module_text ());
if (server_is_installing_module) {
Database_Logs::log (translate ("Waiting while Bibledit Cloud installs the requested SWORD module"));
std::this_thread::sleep_for (std::chrono::seconds (60));
wait_iterations++;
}
} while (server_is_installing_module && (wait_iterations < 5));
// Remove all html markup.
html = filter::strings::html2text (html);
html = filter::strings::replace ("\n", " ", html);
// Add the verse to the USFM.
usfm.push_back ("\\v " + std::to_string (verse) + " " + filter::strings::trim (html));
}
bible_logic::store_chapter (bible, book, chapter, filter::strings::implode (usfm, "\n"));
}
}
Database_Logs::log ("Completed importing resource " + resource + " into Bible " + bible);
}
// This logs the change in the Bible text.
// When $force is given, it records the change on clients also.
void bible_logic::log_change (const std::string& bible,
int book, int chapter,
const std::string& usfm,
std::string user,
const std::string& summary,
[[maybe_unused]] bool force)
{
#ifdef HAVE_CLIENT
if (!force) return;
#endif
const std::string existing_usfm = database::bibles::get_chapter (bible, book, chapter);
// It used to calculate the percentage difference, but this took a relatively long time.
// In particular on low-power devices and on Windows, the time it took was excessive.
const std::string bookname = database::books::get_english_from_id (static_cast<book_id>(book));
const std::string passage = bible + " " + bookname + " " + std::to_string (chapter);
const std::string stylesheet = database::config::bible::get_export_stylesheet (bible);
const std::vector <int> existing_verse_numbers = filter::usfm::get_verse_numbers (existing_usfm);
const std::vector <int> verse_numbers = filter::usfm::get_verse_numbers (usfm);
std::vector <int> verses = existing_verse_numbers;
verses.insert (verses.end (), verse_numbers.begin (), verse_numbers.end ());
verses = filter::strings::array_unique (verses);
sort (verses.begin (), verses.end ());
std::vector <std::string> body;
body.push_back ("Changes:");
for (const auto verse : verses) {
const std::string existing_verse_usfm = filter::usfm::get_verse_text (existing_usfm, verse);
const std::string verse_usfm = filter::usfm::get_verse_text (usfm, verse);
if (existing_verse_usfm != verse_usfm) {
Filter_Text filter_text_old = Filter_Text (bible);
Filter_Text filter_text_new = Filter_Text (bible);
filter_text_old.text_text = new Text_Text ();
filter_text_new.text_text = new Text_Text ();
filter_text_old.add_usfm_code (existing_verse_usfm);
filter_text_new.add_usfm_code (verse_usfm);
filter_text_old.run (stylesheet);
filter_text_new.run (stylesheet);
const std::string old_text = filter_text_old.text_text->get ();
const std::string new_text = filter_text_new.text_text->get ();
if (old_text != new_text) {
body.push_back (std::string());
body.push_back (filter_passage_display (book, chapter, std::to_string (verse)));
body.push_back ("Old: " + old_text);
body.push_back ("New: " + new_text);
}
}
}
body.push_back (std::string());
body.push_back ("Old USFM:");
body.push_back (existing_usfm);
body.push_back (std::string());
body.push_back ("New USFM:");
body.push_back (usfm);
if (!user.empty ()) user.append (" - ");
Database_Logs::log (user + summary + " - " + passage, filter::strings::implode (body, "\n"));
}
// This logs the change in the Bible text as a result of a merge operation.
// This is mostly for diagnostics as at times there's queries on how the merge was done.
void bible_logic::log_merge (const std::string& user, const std::string& bible, int book, int chapter,
const std::string& base, const std::string& change,
const std::string& prioritized_change, const std::string& result)
{
const std::string bookname = database::books::get_english_from_id (static_cast<book_id>(book));
const std::string passage = bible + " " + bookname + " " + std::to_string (chapter);
std::vector <std::string> body {};
body.push_back ("This is a record of a merge operation after receiving an update on a chapter from a client.");
body.push_back (std::string());
body.push_back ("Base:");
body.push_back (base);
body.push_back (std::string());
body.push_back ("Change:");
body.push_back (change);
body.push_back (std::string());
body.push_back ("Existing:");
body.push_back (prioritized_change);
body.push_back (std::string());
body.push_back ("Result:");
body.push_back (result);
Database_Logs::log (user + " - merge record - " + passage, filter::strings::implode (body, "\n"));
}
void bible_logic::kick_unsent_data_timer ()
{
// The timer contains the oldest age of Bible data on a client not yet sent to the Cloud.
// If the timer has been set already, bail out.
if (database::config::general::get_unsent_bible_data_time () != 0) return;
// Stamp with the current time.
database::config::general::set_unsent_bible_data_time (filter::date::seconds_since_epoch ());
}
void bible_logic::kick_unreceived_data_timer ()
{
database::config::general::set_unreceived_bible_data_time (filter::date::seconds_since_epoch ());
}
// This returns a warning in case there's old Bible data not yet sent to the Cloud,
// or in case it has not received data from the Cloud for some days.
std::string bible_logic::unsent_unreceived_data_warning ()
{
std::string warning {};
#ifdef HAVE_CLIENT
// Time-stamp for oldest unreceived Bible data.
int data_time = database::config::general::get_unreceived_bible_data_time ();
// A value of 0 means that it is not relevant.
if (data_time) {
const int now = filter::date::seconds_since_epoch ();
// Unreceived data warning after four days.
data_time += (4 * 24 * 3600);
if (now > data_time) {
warning.clear ();
warning.append (translate ("It has been some time ago that changes in the Bible text were received from the Cloud."));
warning.append (" ");
warning.append (translate ("This will right itself in a little time if you are connected to the Internet."));
warning.append (" ");
warning.append (translate ("If not please do a Send/receive action."));
}
}
// Time-stamp for oldest unsent Bible data.
data_time = database::config::general::get_unsent_bible_data_time ();
// A value of 0 means that there's no pending data.
if (data_time) {
const int now = filter::date::seconds_since_epoch ();
// Unsent data warning after four days.
data_time += (4 * 24 * 3600);
if (now > data_time) {
warning.clear ();
warning.append (translate ("There are pending changes in the Bible text that have not yet been sent to the Cloud for some time."));
warning.append (" ");
warning.append (translate ("This will right itself in a little time if you are connected to the Internet."));
warning.append (" ");
warning.append (translate ("If not please do a Send/receive action."));
}
}
#endif
return warning;
}
void bible_logic::merge_irregularity_mail (const std::vector <std::string>& users,
const std::vector <Merge_Conflict>& conflicts)
{
if (conflicts.empty ()) return;
for (const auto& conflict : conflicts) {
// Add the passage to the subject.
std::string newsubject = conflict.subject;
if (conflict.book) newsubject.append (" | " + filter_passage_display (conflict.book, conflict.chapter, std::string()));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node = document.append_child ("h3");
node.text ().set (newsubject.c_str());
// Storage of the changes the user sent, and the result that was saved, in raw USFM, per verse.
std::vector <std::string> change_usfm {};
std::vector <std::string> result_usfm {};
// Go through all verses available in the USFM,
// and make a record for each verse,
// where the USFM differs between the change that the user made and the result that was saved.
const std::vector <int> verses = filter::usfm::get_verse_numbers (conflict.result);
for (const auto verse : verses) {
const std::string change = filter::usfm::get_verse_text (conflict.change, verse);
const std::string result = filter::usfm::get_verse_text (conflict.result, verse);
// When there's no change in the verse, skip it.
if (change == result) continue;
// Record the difference.
change_usfm.push_back (change);
result_usfm.push_back (result);
}
// Add some information for the user.
node = document.append_child ("p");
node.text ().set ("You sent changes to the Cloud. The changes were merged with other changes already in the Cloud. The crossed out parts below could not be merged. They were replaced with the bold text below. You may want to check the changes or resend them.");
// Go over each verse where the change differs from the resulting text that was saved.
// For each verse, outline the difference between the change and the result.
// Clearly maark it up for the user.
// The purpose is that the user immediately can see what happened,
// and whether and how to correct it.
for (size_t i = 0; i < change_usfm.size(); i++) {
node = document.append_child ("p");
const std::string modification = filter_diff_diff (change_usfm[i], result_usfm[i]);
// Add raw html to the email's text buffer.
node.append_buffer (modification.c_str (), modification.size ());
}
// Add some information for the user.
document.append_child ("hr");
document.append_child ("br");
pugi::xml_node div_node {};
div_node = document.append_child ("div");
// Disabled: https://github.com/bibledit/cloud/issues/401
// div_node.append_attribute ("style") = "font-size: 30%";
node = div_node.append_child ("p");
node.text ().set ("Full details follow below.");
// Add the base text.
div_node.append_child ("br");
node = div_node.append_child ("p");
node.text ().set ("Base text loaded in your editor");
node = div_node.append_child ("pre");
node.text ().set (conflict.base.c_str ());
// Add the changed text.
div_node.append_child ("br");
node = div_node.append_child ("p");
node.text ().set ("Changed text by you");
node = div_node.append_child ("pre");
node.text ().set (conflict.change.c_str ());
// Add the existing text.
div_node.append_child ("br");
node = div_node.append_child ("p");
node.text ().set ("Existing text in the Cloud");
node = div_node.append_child ("pre");
node.text ().set (conflict.prioritized_change.c_str ());
// Add the merge result.
div_node.append_child ("br");
node = div_node.append_child ("p");
node.text ().set ("The text that was actually saved to the chapter in the Cloud");
node = div_node.append_child ("pre");
node.text ().set (conflict.result.c_str ());
// Convert the document to a string.
std::stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user(s).
for (const auto & user : users) {
email::schedule (user, newsubject, html);
}
}
}
void bible_logic::unsafe_save_mail (std::string subject, const std::string& explanation,
const std::string& user,
const std::string& usfm,
int book, int chapter)
{
if (subject.empty ()) return;
// Add the passage to the subject for extra clarity.
// https://github.com/bibledit/cloud/issues/676
subject.append (" | " + filter_passage_display (book, chapter, std::string()));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node {};
node = document.append_child ("h3");
node.text ().set (subject.c_str());
// Add some information for the user.
node = document.append_child ("p");
node.text ().set (explanation.c_str ());
node = document.append_child ("p");
node.text ().set ("Failed to save the text below.");
// Add the base text.
document.append_child ("br");
node = document.append_child ("pre");
node.text ().set (usfm.c_str ());
// Convert the document to a string.
std::stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user.
email::schedule (user, subject, html);
}
// This function sends an email
// if the USFM received from the client
// does not match the USFM that gets stored on the server.
void bible_logic::client_receive_merge_mail (const std::string& bible, int book, int chapter,
const std::string& user,
const std::string& client_old,
const std::string& client_new,
const std::string& server)
{
// No difference: Done.
if (client_old == server) return;
std::vector <std::string> client_diff {};
std::vector <std::string> server_diff {};
// Go through all verses from the client,
// and make a record for each verse,
// where the USFM differs between client and server.
const std::vector <int> verses = filter::usfm::get_verse_numbers (client_old);
for (const auto verse : verses) {
const std::string client_old_verse = filter::usfm::get_verse_text (client_old, verse);
const std::string client_new_verse = filter::usfm::get_verse_text (client_new, verse);
// When there's no change in the verse as sent by the client, skip further checks.
if (client_old_verse == client_new_verse) continue;
// Check whether the client's change made it to the server.
const std::string server_verse = filter::usfm::get_verse_text (server, verse);
if (client_new_verse == server_verse) continue;
// Record the difference.
client_diff.push_back (client_new_verse);
server_diff.push_back (server_verse);
}
// No differences found: Done.
if (client_diff.empty ()) return;
// Add the passage to the subject.
std::string subject = "Saved Bible text was merged";
subject.append (" | " + filter_passage_display (book, chapter, std::string()));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node {};
node = document.append_child ("h3");
node.text ().set (subject.c_str());
// Add some information for the user.
node = document.append_child ("p");
std::string information {};
information.append (translate ("The Cloud already had some changes in this chapter."));
information.append (" ");
information.append (translate ("You sent some changes for this chapter too."));
information.append (" ");
information.append (translate ("The Cloud merged the two."));
information.append (" ");
information.append (translate ("Most likely this is okay."));
information.append (" ");
information.append (translate ("You may want to check the result of the merge, whether it is correct."));
node.text ().set (information.c_str());
node = document.append_child ("p");
std::string location = bible + " " + filter_passage_display (book, chapter, "") + ".";
node.text ().set (location.c_str ());
for (unsigned int i = 0; i < client_diff.size(); i++) {
document.append_child ("br");
node = document.append_child ("p");
node.text ().set ("You sent:");
node = document.append_child ("p");
node.text ().set (client_diff[i].c_str ());
node = document.append_child ("p");
node.text ().set ("The Cloud now has:");
node = document.append_child ("p");
node.text ().set (server_diff[i].c_str ());
node = document.append_child ("p");
node.text ().set ("The difference is:");
node = document.append_child ("p");
const std::string difference = filter_diff_diff (client_diff[i], server_diff[i]);
node.append_buffer (difference.c_str (), difference.size ());
}
// Convert the document to a string.
std::stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user.
email::schedule (user, subject, html);
}
// This emails pending Bible updates to the user.
void bible_logic::client_mail_pending_bible_updates (const std::string& user)
{
// Iterate over all the actions stored for all Bible data ready for sending to the Cloud.
const std::vector <std::string> bibles = database::bible_actions::get_bibles ();
for (const auto& bible : bibles) {
// Skip the Sample Bible, for less clutter.
if (bible == demo_sample_bible_name ()) continue;
const std::vector <int> books = database::bible_actions::get_books (bible);
for (const int book : books) {
const std::vector <int> chapters = database::bible_actions::get_chapters (bible, book);
for (const int chapter : chapters) {
// Get old and new USFM for this chapter.
const std::string oldusfm = database::bible_actions::get_usfm (bible, book, chapter);
const std::string newusfm = database::bibles::get_chapter (bible, book, chapter);
// If old USFM and new USFM are the same, or the new USFM is empty, skip it.
if (newusfm == oldusfm) continue;
if (newusfm.empty ()) continue;
// Add the passage to the subject.
std::string subject = "Discarded text update";
subject.append (" | " + filter_passage_display (book, chapter, std::string()));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node {};
node = document.append_child ("h3");
node.text ().set (subject.c_str());
// Add some information for the user.
node = document.append_child ("p");
std::string information {};
information.append (translate ("You have now connected to Bibledit Cloud."));
information.append (" ");
information.append (translate ("While you were disconnected from Bibledit Cloud, you made some changes in the Bible text on your device."));
information.append (" ");
information.append (translate ("These changes will not be saved to Bibledit Cloud."));
information.append (" ");
information.append (translate ("The unsaved text is below."));
node.text ().set (information.c_str());
// Add the passage.
node = document.append_child ("p");
std::string location = bible + " " + filter_passage_display (book, chapter, "") + ".";
node.text ().set (location.c_str ());
// Add the text.
document.append_child ("br");
node = document.append_child ("pre");
node.text ().set (newusfm.c_str ());
// Convert the document to a string.
std::stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user.
email::schedule (user, subject, html);
}
}
}
}
void bible_logic::client_no_write_access_mail (const std::string& bible, int book, int chapter,
const std::string& user,
const std::string& oldusfm, const std::string& newusfm)
{
// No difference: Done.
if (oldusfm == newusfm) return;
std::vector <std::string> client_new_diff {};
std::vector <std::string> client_old_diff {};
// Go through all verses from the client,
// and make a record for each verse,
// where the USFM differs between client and server.
const std::vector <int> verses = filter::usfm::get_verse_numbers (oldusfm);
for (const auto verse : verses) {
const std::string client_old_verse = filter::usfm::get_verse_text (oldusfm, verse);
const std::string client_new_verse = filter::usfm::get_verse_text (newusfm, verse);
// When there's no change in the verse as sent by the client, skip further checks.
if (client_old_verse == client_new_verse) continue;
// Record the difference.
client_new_diff.push_back (client_new_verse);
client_old_diff.push_back (client_old_verse);
}
// No differences found: Done.
if (client_new_diff.empty ()) return;
// Add the passage to the subject.
std::string subject = "No write access while sending Bible text";
subject.append (" | " + filter_passage_display (book, chapter, ""));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node {};
node = document.append_child ("h3");
node.text ().set (subject.c_str());
// Add some information for the user.
node = document.append_child ("p");
std::string information {};
information.append (translate ("While sending Bible text to Bibledit Cloud, you did not have write access to this chapter."));
information.append (" ");
information.append (translate ("You may want to check whether this is correct."));
node.text ().set (information.c_str());
node = document.append_child ("p");
const std::string location = bible + " " + filter_passage_display (book, chapter, "") + ".";
node.text ().set (location.c_str ());
for (unsigned int i = 0; i < client_new_diff.size(); i++) {
document.append_child ("br");
node = document.append_child ("p");
node.text ().set ("You sent:");
node = document.append_child ("p");
node.text ().set (client_new_diff[i].c_str ());
node = document.append_child ("p");
node.text ().set ("The difference is:");
node = document.append_child ("p");
const std::string difference = filter_diff_diff (client_old_diff[i], client_new_diff[i]);
node.append_buffer (difference.c_str (), difference.size ());
}
// Convert the document to a string.
std::stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user.
email::schedule (user, subject, html);
}
void bible_logic::recent_save_email (const std::string& bible,
int book, int chapter,
const std::string& user,
const std::string& old_usfm, const std::string& new_usfm)
{
std::vector <std::string> old_verses {};
std::vector <std::string> new_verses {};
// Go through all verses available in the USFM,
// and make a record for each verse,
// where the USFM differs between the old and the new USFM.
const std::vector <int> verses = filter::usfm::get_verse_numbers (new_usfm);
for (const auto verse : verses) {
const std::string old_verse = filter::usfm::get_verse_text (old_usfm, verse);
const std::string new_verse = filter::usfm::get_verse_text (new_usfm, verse);
// When there's no change in the verse, skip further checks.
if (old_verse == new_verse) continue;
// Record the difference.
old_verses.push_back (old_verse);
new_verses.push_back (new_verse);
}
// No differences found: Done.
if (new_verses.empty ()) return;
// Add the passage to the subject.
std::string subject = translate ("Check whether Bible text was saved");
subject.append (" | " + filter_passage_display (book, chapter, ""));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node {};
node = document.append_child ("h3");
node.text ().set (subject.c_str());
// Add some information for the user.
node = document.append_child ("p");
std::string information {};
information.append (translate ("Bibledit saved the Bible text below."));
information.append (" ");
information.append (translate ("But Bibledit is not entirely sure that all went well."));
information.append (" ");
information.append (translate ("You may want to check whether the Bible text was saved correctly."));
information.append (" ");
information.append (translate ("The text in bold was added."));
information.append (" ");
information.append (translate ("The text in strikethrough was removed."));
node.text ().set (information.c_str());
node = document.append_child ("p");
const std::string location = bible + " " + filter_passage_display (book, chapter, "") + ".";
node.text ().set (location.c_str ());
bool differences_found {false};
for (unsigned int i = 0; i < new_verses.size(); i++) {
Filter_Text filter_text_old = Filter_Text (bible);
Filter_Text filter_text_new = Filter_Text (bible);
filter_text_old.html_text_standard = new HtmlText (translate("Bible"));
filter_text_new.html_text_standard = new HtmlText (translate("Bible"));
filter_text_old.text_text = new Text_Text ();
filter_text_new.text_text = new Text_Text ();
filter_text_old.add_usfm_code (old_verses[i]);
filter_text_new.add_usfm_code (new_verses[i]);
filter_text_old.run (stylesv2::standard_sheet());
filter_text_new.run (stylesv2::standard_sheet());
const std::string old_text = filter_text_old.text_text->get ();
const std::string new_text = filter_text_new.text_text->get ();
if (old_text != new_text) {
node = document.append_child ("p");
const std::string modification = filter_diff_diff (old_text, new_text);
const std::string fragment = /* filter::strings::convert_to_string (verse) + " " + */ modification;
node.append_buffer (fragment.c_str (), fragment.size ());
differences_found = true;
}
}
// If no differences were found, bail out.
// This also handles differences in spacing.
// If the differences consist of whitespace only, bail out here.
// See issue https://github.com/bibledit/cloud/issues/413
if (!differences_found) return;
// Convert the document to a string.
std:: stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user.
email::schedule (user, subject, html);
}
void bible_logic::optional_merge_irregularity_email (const std::string& bible, int book, int chapter,
const std::string& user,
const std::string& ancestor_usfm,
const std::string& edited_usfm,
const std::string& merged_usfm)
{
// If the merged edited USFM is the same as the edited USFM,
// that means that the user's changes will get saved to the chapter.
if (edited_usfm == merged_usfm) return;
// But if the merged edited USFM differs from the original edited USFM,
// more checks need to be done to be sure that the user's edits made it.
// Add the passage to the subject.
std::string subject = translate ("Check whether Bible text was saved");
subject.append (" | " + filter_passage_display (book, chapter, ""));
// Create the body of the email.
pugi::xml_document document {};
pugi::xml_node node {};
node = document.append_child ("h3");
node.text ().set (subject.c_str());
// Add some information for the user.
node = document.append_child ("p");
std::string information {};
information.append (translate ("Bibledit saved the Bible text below."));
information.append (" ");
information.append (translate ("But Bibledit is not entirely sure that all went well."));
information.append (" ");
information.append (translate ("You may want to check whether the Bible text was saved correctly."));
information.append (" ");
information.append (translate ("The text in bold was added."));
information.append (" ");
information.append (translate ("The text in strikethrough was removed."));
node.text ().set (information.c_str());
node = document.append_child ("p");
const std::string location = bible + " " + filter_passage_display (book, chapter, "") + ".";
node.text ().set (location.c_str ());
bool anomalies_found {false};
// Go through all verses available in the USFM,
// and check the differences for each verse.
const std::vector <int> verses = filter::usfm::get_verse_numbers (merged_usfm);
for (const auto verse : verses) {
const std::string ancestor_verse_usfm = filter::usfm::get_verse_text (ancestor_usfm, verse);
const std::string edited_verse_usfm = filter::usfm::get_verse_text (edited_usfm, verse);
const std::string merged_verse_usfm = filter::usfm::get_verse_text (merged_usfm, verse);
// There's going to be a check to find out that all the changes the user made,
// are available among the changes resulting from the merge.
// If all the changes are there, all is good.
// If not, then email the user about this.
bool anomaly_found {false};
std::vector <std::string> user_removals, user_additions, merged_removals, merged_additions;
filter_diff_diff (ancestor_verse_usfm, edited_verse_usfm, &user_removals, &user_additions);
filter_diff_diff (ancestor_verse_usfm, merged_verse_usfm, &merged_removals, &merged_additions);
for (const auto& user_removal : user_removals) {
if (!in_array (user_removal, merged_removals)) anomaly_found = true;
}
for (const auto& user_addition : user_additions) {
if (!in_array (user_addition, merged_additions)) anomaly_found = true;
}
if (!anomaly_found) continue;
Filter_Text filter_text_ancestor = Filter_Text (bible);
Filter_Text filter_text_edited = Filter_Text (bible);
Filter_Text filter_text_merged = Filter_Text (bible);
filter_text_ancestor.html_text_standard = new HtmlText (translate("Bible"));
filter_text_edited.html_text_standard = new HtmlText (translate("Bible"));
filter_text_merged.html_text_standard = new HtmlText (translate("Bible"));
filter_text_ancestor.text_text = new Text_Text ();
filter_text_edited.text_text = new Text_Text ();
filter_text_merged.text_text = new Text_Text ();
filter_text_ancestor.add_usfm_code (ancestor_verse_usfm);
filter_text_edited.add_usfm_code (edited_verse_usfm);
filter_text_merged.add_usfm_code (merged_verse_usfm);
filter_text_ancestor.run (stylesv2::standard_sheet());
filter_text_edited.run (stylesv2::standard_sheet());
filter_text_merged.run (stylesv2::standard_sheet());
const std::string ancestor_text = filter_text_ancestor.text_text->get ();
const std::string edited_text = filter_text_edited.text_text->get ();
const std::string merged_text = filter_text_merged.text_text->get ();
std::string modification_edited = filter_diff_diff (ancestor_text, edited_text);
std::string modification_saved = filter_diff_diff (ancestor_text, merged_text);
// If the edited and saved modifications are the same, do not email this.
// This keeps changes in footnotes and cross references out from consideration.
if (modification_saved == modification_edited) continue;
// Add labels to the modifications.
modification_edited.insert(0, translate ("You edited:") + " ");
modification_saved.insert(0, translate ("Bibledit saved:") + " ");
// Add the modifications to the email body.
node = document.append_child ("p");
node.append_buffer (modification_edited.c_str (), modification_edited.size ());
node = document.append_child ("p");
node.append_buffer (modification_saved.c_str (), modification_saved.size ());
anomalies_found = true;
}
// If no differences were found, bail out.
// This also handles differences in spacing.
// If the differences consist of whitespace only, bail out here.
// See issue https://github.com/bibledit/cloud/issues/413
if (!anomalies_found) return;
// Convert the document to a string.
std::stringstream output {};
document.print (output, "", pugi::format_raw);
const std::string html = output.str ();
// Schedule the mail for sending to the user.
email::schedule (user, subject, html);
}
const char * bible_logic::insert_operator ()
{
return "i";
}
const char * bible_logic::delete_operator ()
{
return "d";
}
const char * bible_logic::format_paragraph_operator ()
{
return "p";
}
const char * bible_logic::format_character_operator ()
{
return "c";
}
// There are three containers with updating information.
// The function condenses this updating information.
// This condensed information works better for the Quill editor.
void bible_logic::condense_editor_updates (const std::vector <int>& positions_in,
const std::vector <int>& sizes_in,
const std::vector <bool>& additions_in,
const std::vector <std::string>& content_in,
std::vector <int>& positions_out,
std::vector <int>& sizes_out,
std::vector <std::string>& operators_out,
std::vector <std::string>& content_out)
{
positions_out.clear();
sizes_out.clear();
operators_out.clear();
content_out.clear();
// https://stackoverflow.com/questions/40492414/why-does-stdnumeric-limitslong-longmax-fail
int previous_position = (std::numeric_limits<int>::min)();
bool previous_addition {false};
std::string previous_character {};
for (size_t i = 0; i < positions_in.size(); i++) {
const int position = positions_in[i];
const int size = sizes_in[i];
const bool addition = additions_in[i];
const std::string character = content_in[i].substr (0, 1);
const std::string format = content_in[i].substr (1);
// The following situation occurs when changing the style of a paragraph.
// Like for example changing a paragraph style from "p" to "s".
// The current sequence for this change is:
// 1. Delete a new line at a known position.
// 2. Insert a new line at the same position, with a given format.
// Condense this as follows:
// 1. Apply the given paragraph format to the given position.
// Without condensing this, the following would happen:
// 1. Delete the new line before the second line.
// Result: The first line gets the format of the second line.
// 2. Insert the new line before the second line again.
// 3. Appy formatting to the second line.
// The net result is that the first line remains with the paragraph format of the second line.
const bool newlineflag = addition && !previous_addition && (character == "\n") && (character == previous_character) && (position == previous_position);
if (newlineflag) {
// Remove the previous "delete new line".
positions_out.pop_back();
sizes_out.pop_back();
operators_out.pop_back();
content_out.pop_back();
// Add the paragraph format operation data.
positions_out.push_back(position);
sizes_out.push_back(size);
operators_out.push_back(bible_logic::format_paragraph_operator());
content_out.push_back(format);
} else {
positions_out.push_back(position);
sizes_out.push_back(size);
if (addition) operators_out.push_back(bible_logic::insert_operator());
else operators_out.push_back(bible_logic::delete_operator());
content_out.push_back(character + format);
}
// Store data for the next iteration.
previous_position = position;
previous_addition = addition;
previous_character = character;
}
}
void bible_logic::html_to_editor_updates (const std::string& editor_html,
const std::string& server_html,
std::vector <int>& positions,
std::vector <int>& sizes,
std::vector <std::string>& operators,
std::vector <std::string>& content)
{
// Clear outputs.
positions.clear();
sizes.clear();
operators.clear();
content.clear();
// Convert the html to formatted text.
Editor_Html2Format editor_format {};
Editor_Html2Format server_format {};
editor_format.load (editor_html);
server_format.load (server_html);
editor_format.run ();
server_format.run ();
// Convert the formatted text fragments to formatted UTF-8 characters.
std::vector <std::string> editor_formatted_character_content;
for (size_t i = 0; i < editor_format.texts.size(); i++) {
const std::string& text = editor_format.texts[i];
const std::string& format = editor_format.formats[i];
const size_t length = filter::strings::unicode_string_length (text);
for (size_t pos = 0; pos < length; pos++) {
const std::string utf8_character = filter::strings::unicode_string_substr (text, pos, 1);
editor_formatted_character_content.push_back (utf8_character + format);
}
}
std::vector <std::string> server_formatted_character_content {};
std::vector <std::string> server_utf8_characters {};
for (size_t i = 0; i < server_format.texts.size(); i++) {
const std::string& text = server_format.texts[i];
const std::string& format = server_format.formats[i];
const size_t length = filter::strings::unicode_string_length (text);
for (size_t pos = 0; pos < length; pos++) {
const std::string utf8_character = filter::strings::unicode_string_substr (text, pos, 1);
server_formatted_character_content.push_back (utf8_character + format);
server_utf8_characters.push_back(utf8_character);
}
}
// Find the differences between the two sets of content.
std::vector <int> positions_diff {};
std::vector <int> sizes_diff {};
std::vector <bool> additions_diff {};
std::vector <std::string> content_diff {};
int new_line_diff_count {};
filter_diff_diff_utf16 (editor_formatted_character_content, server_formatted_character_content,
positions_diff, sizes_diff, additions_diff, content_diff, new_line_diff_count);
// Condense the differences a bit and render them to another format.
bible_logic::condense_editor_updates (positions_diff, sizes_diff, additions_diff, content_diff,
positions, sizes, operators, content);
// Problem description:
// User action: Remove the new line at the end of the current paragraph.
// Result: The current paragraph takes the style of the next paragraph.
// User actions: While removing notes, this goes wrong.
// Solution:
// If there's new line(s) added or removed, apply all paragraph styles again.
if (new_line_diff_count) {
int position {0};
for (size_t i = 0; i < server_utf8_characters.size(); i++) {
const int size = static_cast<int>(filter::strings::convert_to_u16string (server_utf8_characters[i]).length());
if (server_utf8_characters[i] == "\n") {
positions.push_back(position);
sizes.push_back(size);
operators.push_back(bible_logic::format_paragraph_operator());
content.push_back(server_formatted_character_content[i].substr (1));
}
position += size;
}
}
}
void bible_logic::create_empty_bible (const std::string& name)
{
Database_Logs::log (translate("Creating Bible") + " " + name);
// Remove and create the empty Bible.
database::bibles::delete_bible (name);
database::bibles::create_bible (name);
// Remove index for the Bible.
search_logic_delete_bible (name);
// Create books with blank verses for the OT and NT.
std::vector <book_id> books = database::books::get_ids ();
for (const auto book : books) {
const book_type type = database::books::get_type (book);
if ((type == book_type::old_testament) || (type == book_type::new_testament)) {
std::vector <std::string> feedback {};
book_create (name, book, -1, feedback);
}
}
Database_Logs::log (translate("Created:") + " " + name);
}
|