示例#1
0
 /**
  * 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;
 }
示例#2
0
 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");
 }
示例#3
0
 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;
 }
示例#4
0
     // 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.
     }
 }
示例#5
0
    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;
示例#6
0
} 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);
示例#7
0
    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;
示例#8
0
文件: git.php 项目: alerque/bibledit
 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}");
     }
 }
示例#9
0
        $dialog_books = new Dialog_Books(array("bible"), Locale_Translate::_("Create book"), "", "", "createbook", NULL, $database_bibles->getBooks($bible));
        die;
    } else {
        $feedback = array();
        if ($write_access) {
            Book_Create::create($bible, $createbook, NULL, $feedback);
        }
    }
}
// Book deletion.
@($deletebook = $_GET['deletebook']);
if ($deletebook != "") {
    @($confirm = $_GET['confirm']);
    if ($confirm != "") {
        if ($write_access) {
            Bible_Logic::deleteBook($bible, $deletebook);
        }
    } else {
        $dialog_yes = new Dialog_Yes(array("bible"), Locale_Translate::_("Would you like to delete this book?"), "deletebook");
        die;
    }
}
// Available books.
$book_names = array();
$book_ids = Filter_Books::getOrdered($bible);
foreach ($book_ids as $book) {
    $book_name = $database_books->getEnglishFromId($book);
    $book_name = Locale_Translate::_($book_name);
    $book_names[] = $book_name;
}
$view->view->book_ids = $book_ids;
示例#10
0
    }
}
// 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);
            $length = strlen($usfm);
            if ($length < 1000) {
                Bible_Logic::deleteChapter($bible, $book, $chapter);
            }
        }
        // If a book contains chapter 0 only, remove that entire book.
        $chapters = $database_bibles->getChapters($bible, $book);
        if ($chapters == array(0)) {
            Bible_Logic::deleteBook($bible, $book);
        }
    }
    // If a Bible contains no books, remove that Bible.
    $books = $database_bibles->getBooks($bible);
    if (empty($books)) {
        Bible_Logic::deleteBible($bible);
    }
}
示例#11
0
文件: book.php 项目: alerque/bibledit
$view->view->write_access = $write_access;
// The book.
$book = $_GET['book'];
$view->view->book = Filter_Html::sanitize($book);
$book_name = $database_books->getEnglishFromId($book);
$view->view->book_name = Filter_Html::sanitize($book_name);
// Delete chapter.
@($deletechapter = $_GET['deletechapter']);
if ($deletechapter != "") {
    @($confirm = $_GET['confirm']);
    if ($confirm != "yes") {
        $dialog_yes = new Dialog_Yes(array("bible", "book"), Locale_Translate::_("Would you like to delete this chapter?"), "deletechapter");
        die;
    } else {
        if ($write_access) {
            Bible_Logic::deleteChapter($bible, $book, $deletechapter);
        }
    }
}
// Add chapter.
if (isset($_GET['createchapter'])) {
    $dialog_entry = new Dialog_Entry(array("bible" => $bible, "book" => $book), Locale_Translate::_("Please enter the number for the new chapter"), "", "createchapter", NULL);
    die;
}
if (isset($_POST['createchapter'])) {
    $createchapter = $_POST['entry'];
    $chapters = $database_bibles->getChapters($bible, $book);
    // Only create the chapters if it does not yet exist.
    if (array_search($createchapter, $chapters) === false) {
        $feedback = array();
        if ($write_access) {
示例#12
0
        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();
示例#13
0
 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);
 }
示例#14
0
$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);