$database_bibles->deleteChapter($bible, $book, $chapter); $database_logs->log(Locale_Translate::_("Deleting chapter because the server does not have it") . ": {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); } // The client goes through all chapters in the book. // It compares the checksum of that chapter locally with the same checksum on the server. // If the checksums differ, then the client requests the updated chapter from the server and stores it locally. // As the user on the client may continue editing the Bible while the sync operations runs, // the edits made by the user should not be overwritten by downloading the newest chapter from the server, // because in this case the newest chapter is not on the server, but on the client. // The solution is this: // When the Bible actions database contains an action for this particular chapter, // it means that chapter is being edited, and therefore the new chapter from the server should be merged with what is on the client. foreach ($server_chapters as $chapter) { // Get checksum for the chapter on client and on server. // If both are the same, it means the USFM in both is the same, and we're done. $client_checksum = Checksum_Logic::getChapter($bible, $book, $chapter); $post = array("b" => $bible, "bk" => $book, "c" => $chapter, "a" => "chapter"); $server_checksum = Sync_Logic::post($post, $url); if ($server_checksum === false) { $database_logs->log(Locale_Translate::_("Failure getting checksum:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); continue; } if ($client_checksum == $server_checksum) { continue; } // Different checksums: Get the USFM for the chapter as it is on the server. $database_logs->log(Locale_Translate::_("Getting chapter:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL); $post = array("b" => $bible, "bk" => $book, "c" => $chapter, "a" => "get"); $server_usfm = Sync_Logic::post($post, $url); if ($server_usfm === false) { $database_logs->log(Locale_Translate::_("Failure getting chapter:") . " {$bible} {$book_name} {$chapter}", Filter_Roles::TRANSLATOR_LEVEL);
} else { if ($action == "book") { // The server responds with the checksum of the whole book. $server_checksum = Checksum_Logic::getBook($bible, $book); echo $server_checksum; } else { if ($action == "chapters") { // The server responds with the list of books in the Bible book. $server_chapters = $database_bibles->getChapters($bible, $book); $server_chapters = implode("\n", $server_chapters); $checksum = Checksum_Logic::get($server_chapters); echo "{$checksum}\n{$server_chapters}"; } else { if ($action == "chapter") { // The server responds with the checksum of the whole chapter. $server_checksum = Checksum_Logic::getChapter($bible, $book, $chapter); echo $server_checksum; } else { if ($action == "get") { // The server responds with the USFM of the chapter, prefixed by a checksum. $usfm = $database_bibles->getChapter($bible, $book, $chapter); $checksum = Checksum_Logic::get($usfm); echo "{$checksum}\n{$usfm}"; } } } } } } } }
public function testGetChapter2() { $checksum = Checksum_Logic::getChapter("phpunit2", 2, 6); $this->assertEquals(md5(""), $checksum); }