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

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