BioTorrents.de’s version of Gazelle
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

compare.php 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. // Diff function by Leto of StC
  3. function diff($OldText, $NewText)
  4. {
  5. $LineArrayOld = explode("\n", $OldText);
  6. $LineArrayNew = explode("\n", $NewText);
  7. $LineOffset = 0;
  8. $Result = [];
  9. foreach ($LineArrayOld as $OldLine => $OldString) {
  10. $Key = $OldLine + $LineOffset;
  11. if ($Key < 0) {
  12. $Key = 0;
  13. }
  14. $Found = -1;
  15. while ($Key < count($LineArrayNew)) {
  16. if ($OldString !== $LineArrayNew[$Key]) {
  17. $Key++;
  18. } elseif ($OldString === $LineArrayNew[$Key]) {
  19. $Found = $Key;
  20. break;
  21. }
  22. }
  23. if ($Found === '-1') { // We never found the old line in the new array
  24. $Result[] = '<span class="line_deleted">&larr; '.$OldString.'</span><br />';
  25. $LineOffset = $LineOffset - 1;
  26. } elseif ($Found === $OldLine + $LineOffset) {
  27. $Result[] = '<span class="line_unchanged">&#8597; '.$OldString.'</span><br />';
  28. } elseif ($Found !== $OldLine + $LineOffset) {
  29. if ($Found < $OldLine + $LineOffset) {
  30. $Result[] = '<span class="line_moved">&#8676; '.$OldString.'</span><br />';
  31. } else {
  32. $Result[] = '<span class="line_moved">&larr; '.$OldString.'</span><br />';
  33. $Key = $OldLine + $LineOffset;
  34. while ($Key < $Found) {
  35. $Result[] = '<span class="line_new">&rarr; '.$LineArrayNew[$Key].'</span><br />';
  36. $Key++;
  37. }
  38. $Result[] = '<span class="line_moved">&rarr; '.$OldString.'</span><br />';
  39. }
  40. $LineOffset = $Found - $OldLine;
  41. }
  42. }
  43. if (count($LineArrayNew) > count($LineArrayOld) + $LineOffset) {
  44. $Key = count($LineArrayOld) + $LineOffset;
  45. while ($Key < count($LineArrayNew)) {
  46. $Result[] = '<span class="line_new">&rarr; '.$LineArrayNew[$Key].'</span><br />';
  47. $Key++;
  48. }
  49. }
  50. return $Result;
  51. }
  52. function get_body($ID, $Rev)
  53. {
  54. # $Rev is a str, $Revision an int
  55. global $DB, $Revision, $Body;
  56. if ((int) $Rev === $Revision) {
  57. $Str = $Body;
  58. } else {
  59. $DB->query("
  60. SELECT Body
  61. FROM wiki_revisions
  62. WHERE ID = '$ID'
  63. AND Revision = '$Rev'");
  64. if (!$DB->has_results()) {
  65. error(404);
  66. }
  67. list($Str) = $DB->next_record();
  68. }
  69. return $Str;
  70. }
  71. if (!isset($_GET['old'])
  72. || !isset($_GET['new'])
  73. || !isset($_GET['id'])
  74. || !is_number($_GET['old'])
  75. || !is_number($_GET['new'])
  76. || !is_number($_GET['id'])
  77. || $_GET['old'] > $_GET['new']
  78. ) {
  79. error(0);
  80. }
  81. $ArticleID = (int) $_GET['id'];
  82. $Article = Wiki::get_article($ArticleID);
  83. list($Revision, $Title, $Body, $Read, $Edit, $Date, $AuthorID, $AuthorName) = array_shift($Article);
  84. if ($Edit > $LoggedUser['EffectiveClass']) {
  85. error(404);
  86. }
  87. View::show_header('Compare Article Revisions');
  88. $Diff2 = get_body($ArticleID, $_GET['new']);
  89. $Diff1 = get_body($ArticleID, $_GET['old']);
  90. ?>
  91. <div>
  92. <div class="header">
  93. <h2>Compare <a
  94. href="wiki.php?action=article&amp;id=<?=$ArticleID?>"><?=$Title?></a> Revisions</h2>
  95. </div>
  96. <div class="box center_revision" id="center">
  97. <div class="body">
  98. <?php
  99. foreach (diff($Diff1, $Diff2) as $Line) {
  100. echo $Line;
  101. } ?>
  102. </div>
  103. </div>
  104. </div>
  105. <?php
  106. View::show_footer();