/usr/share/bibledit-gtk/merge.php is in bibledit-gtk-data 4.9-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 | <?php
/*
Copyright (©) 2003-2013 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.
*/
/*
Note: This object should not call other objects within the Bibledit-Web source,
because this object is also called from Bibledit-Gtk, and does not have access
to those other objects.
Calling other objects would result in faral errors that break Bibledit-Gtk.
*/
class Filter_Merge
{
/*
This filter merges files.
$base: Data for the merge base.
$user: Data as modified by the user.
$server: Data as modified by the collaboration server.
The filter uses program "merge" do do a three-way merge.
There should be one unchanged segment (either a line or word) between the modifications.
If necessary it converts the data into a new format with one character per line,
for more fine-grained merging.
In case of a conflict, it favours the edition from the server.
The filter returns the merged data.
*/
public static function run ($base, $user, $server)
{
// The three files for merging.
$mergeBaseFile = uniqid (sys_get_temp_dir () . "/") . "mergebase.txt";
$userModificationFile = uniqid (sys_get_temp_dir () . "/") . "usermodification.txt";
$serverModificationFile = uniqid (sys_get_temp_dir () . "/") . "servermodification.txt";
$success = false;
// Try a standard line-based merge. Should be sufficient for most cases.
file_put_contents ($mergeBaseFile, $base);
file_put_contents ($userModificationFile, $user);
file_put_contents ($serverModificationFile, $server);
$exit_code = Filter_Merge::merge ($mergeBaseFile, $userModificationFile, $serverModificationFile);
$result = file_get_contents ($userModificationFile);
if ($exit_code == 0) $success = true;
if (!$success) {
// Convert the data to one word per line, and try to merge again.
$baseWords = Filter_Merge::lines2words ($base);
$userWords = Filter_Merge::lines2words ($user);
$serverWords = Filter_Merge::lines2words ($server);
file_put_contents ($mergeBaseFile, $baseWords);
file_put_contents ($userModificationFile, $userWords);
file_put_contents ($serverModificationFile, $serverWords);
$exit_code = Filter_Merge::merge ($mergeBaseFile, $userModificationFile, $serverModificationFile);
$result = file_get_contents ($userModificationFile);
$result = Filter_Merge::words2lines ($result);
if ($exit_code == 0) $success = true;
}
if (!$success) {
// Convert the data so it has one grapheme per line, and try again.
$baseGraphemes = Filter_Merge::lines2graphemes ($base);
$userGraphemes = Filter_Merge::lines2graphemes ($user);
$serverGraphemes = Filter_Merge::lines2graphemes ($server);
file_put_contents ($mergeBaseFile, $baseGraphemes);
file_put_contents ($userModificationFile, $userGraphemes);
file_put_contents ($serverModificationFile, $serverGraphemes);
$exit_code = Filter_Merge::merge ($mergeBaseFile, $userModificationFile, $serverModificationFile);
$result = file_get_contents ($userModificationFile);
$result = Filter_Merge::graphemes2lines ($result);
if ($exit_code == 0) $success = true;
}
if (!$success) {
// Everthing failed. Return the server data.
$result = $server;
}
// Clear up.
@unlink ($mergeBaseFile);
@unlink ($userModificationFile);
@unlink ($serverModificationFile);
// Merge result.
return $result;
}
/*
merge - three-way file merge
merge [ options ] file1 file2 file3
merge incorporates all changes that lead from file2 to file3 into file1.
The result ordinarily goes into file1.
merge is useful for combining separate changes to an original.
Suppose file2 is the original, and both file1 and file3 are modifications of file2.
Then merge combines both changes.
Exit status is 0 for no conflicts, 1 for some conflicts, 2 for trouble.
The function returns the exit status.
*/
private static function merge ($base, $user, $server)
{
$command = "merge $user $base $server 2>&1";
exec ($command, $output, $exit_code);
return $exit_code;
}
private static function lines2words ($data)
{
$data = str_replace ("\n", " new__line ", $data);
$data = str_replace (" ", "\n", $data);
return $data;
}
private static function words2lines ($data)
{
$data = str_replace ("\n", " ", $data);
$data = str_replace (" new__line ", "\n", $data);
return $data;
}
private static function lines2graphemes ($data)
{
// Older versions of PHP do not yet have the grapheme_* functions,
// so we use the mb_* versions here.
mb_internal_encoding ("UTF-8");
$data = str_replace ("\n", " new__line ", $data);
$data2 = "";
$count = mb_strlen ($data);
for ($i = 0; $i < $count; $i++) {
$grapheme = mb_substr ($data, $i, 1);
$data2 .= $grapheme;
$data2 .= "\n";
}
return $data2;
}
private static function graphemes2lines ($data)
{
$data = str_replace ("\n", "", $data);
$data = str_replace (" new__line ", "\n", $data);
return $data;
}
}
?>
|