/** * Creates book template with ID $book in Bible $bible. * If a $chapter is given instead of NULL, it creates that chapter only. */ public static function create($bible, $book, $chapter, &$feedback) { $database_bibles = Database_Bibles::getInstance(); $database_versifications = Database_Versifications::getInstance(); $database_books = Database_Books::getInstance(); $database_logs = Database_Logs::getInstance(); $database_config_bible = Database_Config_Bible::getInstance(); $bible_id = $database_bibles->getID($bible); if ($bible_id == 0) { $feedback[] = Locale_Translate::_("Bible {$bible} does not exist: Cannot create book"); return false; } if ($book == 0) { $feedback[] = Locale_Translate::_("Invalid book while creating a book template"); return false; } // The chapters that have been created. $chaptersCreated = array(); // Chapter 0. if (!isset($chapter) || $chapter == 0) { $data = "\\id " . $database_books->getUsfmFromId($book) . "\n"; $data .= "\\h " . $database_books->getEnglishFromId($book) . "\n"; $data .= "\\toc2 " . $database_books->getEnglishFromId($book) . "\n"; Bible_Logic::storeChapter($bible, $book, 0, $data); $chaptersCreated[] = 0; } // Subsequent chapters. $versification = $database_config_bible->getVersificationSystem($bible); $versification_data = $database_versifications->getBooksChaptersVerses($versification); foreach ($versification_data as $row) { if ($book == $row["book"]) { $ch = $row["chapter"]; $verse = $row["verse"]; if (!isset($chapter) || $chapter == $ch) { $data = "\\c {$ch}\n"; $data .= "\\p\n"; for ($i = 1; $i <= $verse; $i++) { $data .= "\\v {$i}\n"; } Bible_Logic::storeChapter($bible, $book, $ch, $data); $chaptersCreated[] = $ch; } } } // Done. if (count($chaptersCreated) == 0) { $feedback[] = Locale_Translate::_("No chapters have been craeted"); return false; } $chaptersCreated = implode(" ", $chaptersCreated); $feedback[] = Locale_Translate::_("The following chapters have been created:") . " " . $chaptersCreated; return true; }
public function setUp() { $database_bibles = Database_Bibles::getInstance(); $database_modifications = Database_Modifications::getInstance(); $this->tearDown(); Filter_Client::set(false); $this->temporary_folder = uniqid(sys_get_temp_dir() . "/"); mkdir($this->temporary_folder); $database_bibles->createBible("phpunit"); Bible_Logic::storeChapter("phpunit", 1, 2, "old chapter text"); $database_modifications->truncateTeams(); Bible_Logic::storeChapter("phpunit", 1, 2, "new chapter text"); }
public static function safeStoreChapter($bible, $book, $chapter, $usfm) { $database_bibles = Database_Bibles::getInstance(); $database_logs = Database_Logs::getInstance(); $database_books = Database_Books::getInstance(); // Existing chapter contents. $existing = $database_bibles->getChapter($bible, $book, $chapter); // Bail out if the existing chapter equals the USFM to be saved. if ($usfm == $existing) { return true; } // The length of the new USFM code should not differ more than 20% from the existing USFM code. $existingLength = strlen($existing); $newLength = strlen($usfm); $percentage = 100 * ($newLength - $existingLength) / $existingLength; $percentage = abs($percentage); $percentage = round($percentage); if ($percentage > 20) { $database_logs->log("The chapter was not saved for safety reasons. The length differs {$percentage}% from the existing chapter. Make minor changes and save often."); $database_logs->log("{$bible} " . $database_books->getEnglishFromId($book) . " {$chapter}"); $database_logs->log($usfm); return false; } // The text of the new chapter should not differ more than 20% from the existing text. similar_text($existing, $usfm, $percentage); $percentage = abs($percentage); $percentage = 100 - $percentage; $percentage = round($percentage); if ($percentage > 20) { $database_logs->log("The chapter was not saved for safety reasons. The new text differs {$percentage}% from the existing text. Make minor changes and save often."); $database_logs->log("{$bible} " . $database_books->getEnglishFromId($book) . " {$chapter}"); $database_logs->log($usfm); return false; } // Safety checks have passed: Save chapter. Bible_Logic::storeChapter($bible, $book, $chapter, $usfm); return true; }
// Restore the Bible action for this chapter. $database_bibleactions->delete($bible, $book, $chapter); $database_bibleactions->record($bible, $book, $chapter, $oldusfm); } else { if ($response != "") { // The first line of the response can contain a message as a string. // This is what the server says to the client in case of an error. // Normally the first line of the response contains a numerical value. // This value is the checksum of the data that the server returns in the subsequent lines. // This data would be the updated USFM for the chapter. $response = explode("\n", $response); $checksum_message = array_shift($response); if (is_numeric($checksum_message)) { $response = implode("\n", $response); if (Checksum_Logic::get($response) == $checksum_message) { Bible_Logic::storeChapter($bible, $book, $chapter, $response); } else { // Checksum error. // The chapter will not be stored on the client. // That is fine for just now, because the pending sync action will do it. $database_logs->log("Checksum error while receiving chapter from server", Filter_Roles::TRANSLATOR_LEVEL); } } else { $database_logs->log("The server says: {$checksum_message}", Filter_Roles::TRANSLATOR_LEVEL); } } else { // If the server sends no response, // then the server decided that there is no need to send the updated chapter back to the client, // as it considers the client as already up to date. } }
if (isset($destination)) { $bibles = $database_bibles->getBibles(); if (in_array($destination, $bibles)) { $error_message = Locale_Translate::_("Cannot copy Bible because the destination Bible already exists."); } else { ignore_user_abort(true); set_time_limit(0); // User needs read access to the original. if (Access_Bible::read($origin)) { $database_bibles->createBible($destination); $books = $database_bibles->getBooks($origin); foreach ($books as $book) { $chapters = $database_bibles->getChapters($origin, $book); foreach ($chapters as $chapter) { $data = $database_bibles->getChapter($origin, $book, $chapter); Bible_Logic::storeChapter($destination, $book, $chapter, $data); } } $success_message = Locale_Translate::_("The Bible was copied."); // Check / grant access to destination Bible. if (!Access_Bible::write($destination)) { $database_users->grantAccess2Bible($session_logic->currentUser(), $destination); } } } } } @($view->view->success_message = $success_message); @($view->view->error_message = $error_message); $bibles = Access_Bible::bibles(); $view->view->bibles = $bibles;
} else { if ($newusfm != $serverusfm) { // Do a merge in case the client sends USFM that differs from what's on the server. $mergedusfm = Filter_Merge::run($oldusfm, $newusfm, $serverusfm); if ($mergedusfm == $serverusfm) { // When the merged USFM is the same as what's already on the server, then it means there was a merge conflict. // Email the user with the details, so the user can resolve the conflicts. $subject = "Problem sending chapter to server"; $body = "<p>While sending {$bible} {$bookname} {$chapter} to the server, the server didn't manage to merge it.</p>"; $body .= "<p>Please re-enter your changes as you see fit.</p>"; $body .= "<p>Here is the chapter you sent to the server:</p>"; $body .= "<pre>{$newusfm}</pre>"; $database_mail->send($username, $subject, $body); } else { // Update the server with the new chapter data. Bible_Logic::storeChapter($bible, $book, $chapter, $mergedusfm); } } } // If text was saved, record it as a change entered by the user. $new_id = $database_bibles->getChapterId($bible, $book, $chapter); if ($new_id != $old_id) { $database_modifications = Database_Modifications::getInstance(); $database_modifications->recordUserSave($username, $bible, $book, $chapter, $old_id, $old_text, $new_id, $new_text); } // Send the updated chapter back to the client. // Do this only in case the updated chapter USFM is different from the new USFM the client sent. // This means that in most cases, nothing will be sent back. // That saves bandwidth. // And it allows the user on the client to keep editing without the returned chapter to overwrite the changes the user made. $serverusfm = $database_bibles->getChapter($bible, $book, $chapter);
if ($usfmReplacementCount != $standardReplacementCount) { $replacementOkay = false; } if ($updatedPlainText != $standardPlainText) { $replacementOkay = false; } } // Generate success or failure icon. if ($replacementOkay) { $icon = '<span class="success">✔</span>'; } else { $icon = '<span class="error">✗</span>'; } // Store the new chapter in the database on success. if ($replacementOkay) { Bible_Logic::storeChapter($bible, $book, $chapter, $updatedUsfm); } // Mark the new plain text. if ($replacewith != "") { if ($searchplain) { $updatedPlainText = Filter_Markup::words(array($replacewith), $updatedPlainText); } } // Clickable passage. $link = Filter_Books::linkForOpeningEditorAt($book, $chapter, $verse); // Success or failure message. if ($replacementOkay) { if ($searchplain) { $msg = $updatedPlainText; } else { $msg = $updatedUsfm;
public static function syncGitChapter2Bible($git, $bible, $book, $chapter) { // The databases. $database_bibles = Database_Bibles::getInstance(); $database_books = Database_Books::getInstance(); $database_logs = Database_Logs::getInstance(); // Filename for the chapter. $bookname = $database_books->getEnglishFromId($book); $filename = "{$git}/{$bookname}/{$chapter}/data"; if (file_exists($filename)) { // Store chapter in database. $usfm = file_get_contents($filename); Bible_Logic::storeChapter($bible, $book, $chapter, $usfm); $database_logs->log(Locale_Translate::_("A collaborator updated") . " {$bible} {$bookname} {$chapter}"); } else { // Delete chapter from database. Bible_Logic::deleteChapter($bible, $book, $chapter); $database_logs->log(Locale_Translate::_("A collaborator deleted chapter") . " {$bible} {$bookname} {$chapter}"); } }
\v 22 And I beseech you, brethren, suffer the word of exhortation: for I have written a letter unto you in few words. \v 23 Know ye that \add our\add* brother Timothy is set at liberty; with whom, if he come shortly, I will see you. \p \v 24 Salute all them that have the rule over you, and all the saints. They of Italy salute you. \p \v 25 Grace \add be\add* with you all. Amen. EOD; $data[] = $usfm; foreach ($data as $usfm) { $book_chapter_text = Filter_Usfm::import($usfm, "Standard"); foreach ($book_chapter_text as $data) { $book_number = $data[0]; $chapter_number = $data[1]; $chapter_data = $data[2]; if ($book_number > 0) { Bible_Logic::storeChapter("KJV", $book_number, $chapter_number, $chapter_data); } } } // Clean out nearly empty chapters from the Bibles. $bibles = $database_bibles->getBibles(); foreach ($bibles as $bible) { $books = $database_bibles->getBooks($bible); foreach ($books as $book) { $chapters = $database_bibles->getChapters($bible, $book); foreach ($chapters as $chapter) { // Remove chapters, other than 0, that are rather short, as these chapters likely contain no text, but USFM markers only. if ($chapter == 0) { continue; } $usfm = $database_bibles->getChapter($bible, $book, $chapter);
if ($xref['verse'] == $verse) { $xrefs[] = array($xref['offset'], $xref['text']); } } if (empty($xrefs)) { continue; } // Get the USFM for the current verse in the target Bible, if any. if (!isset($usfmArray[$verse])) { continue; } $usfm = $usfmArray[$verse]; // Get the length of the text of the verse in the source Bible without the xrefs. // Get the ratio for positioning the xrefs by comparing the lengths of source and target verse text. $sourceUsfm = $database_bibles->getChapter($sourceBible, $book, $chapter); $sourceUsfm = Filter_Usfm::getVerseText($sourceUsfm, $verse); $sourceUsfm = Filter_Usfm::removeNotes($sourceUsfm, array("x")); $sourceLength = mb_strlen($sourceUsfm); $targetLength = mb_strlen($usfm); $ratio = $targetLength / $sourceLength; // Insert the notes. $usfm = Filter_Usfm::insertNotes($usfm, $xrefs, $ratio); $usfmArray[$verse] = $usfm; } $usfm = implode("\n", $usfmArray); Bible_Logic::storeChapter($targetBible, $book, $chapter, $usfm); $header = new Assets_Header(Locale_Translate::_("Cross references")); $header->run(); $view = new Assets_View(__FILE__); $view->render("insert.php"); Assets_Page::footer();
public function testGetTeamDiffCount() { $database_bibles = Database_Bibles::getInstance(); $database_modifications = Database_Modifications::getInstance(); Bible_Logic::storeChapter("phpunit", 3, 1, "chapter text"); Bible_Logic::storeChapter("phpunit2", 3, 3, "chapter text"); Bible_Logic::storeChapter("phpunit2", 3, 5, "chapter text"); $count = $database_modifications->getTeamDiffCount("phpunit"); $this->assertEquals(1, $count); $count = $database_modifications->getTeamDiffCount("phpunit2"); $this->assertEquals(2, $count); $count = $database_modifications->getTeamDiffCount("phpunit3"); $this->assertEquals(0, $count); }
$database_logs = Database_Logs::getInstance(); foreach ($files as $file) { $database_logs->log("Examining file for import: {$file}", true); $success_message = ""; $error_message = ""; $data = file_get_contents($file); if ($data !== false) { if ($data != "") { if (Validate_Utf8::valid($data)) { $book_chapter_text = Filter_Usfm::import($data, $stylesheet); foreach ($book_chapter_text as $data) { $book_number = $data[0]; $chapter_number = $data[1]; $chapter_data = $data[2]; if ($book_number > 0) { Bible_Logic::storeChapter($bible, $book_number, $chapter_number, $chapter_data); $book_name = $database_books->getUsfmFromId($book_number); $database_logs->log("Imported {$book_name} {$chapter_number}", Filter_Roles::MANAGER_LEVEL); } else { $database_logs->log("Could not import this data: " . substr($chapter_data, 0, 1000), Filter_Roles::MANAGER_LEVEL); } } } else { $database_logs->log("The file does not contain valid Unicode UTF-8 text.", true); } } else { $database_logs->log("Nothing was imported.", true); } } } $database_logs->log("Import Bible data has finished", true);