Diff.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. namespace dokuwiki\Ui;
  3. use dokuwiki\ChangeLog\ChangeLog;
  4. /**
  5. * DokuWiki Diff Interface
  6. * parent class of PageDiff and MediaDiff
  7. *
  8. * @package dokuwiki\Ui
  9. */
  10. abstract class Diff extends Ui
  11. {
  12. /* @var string */
  13. protected $id; // page id or media id
  14. /* @var int|false */
  15. protected $rev1; // timestamp of older revision
  16. /* @var int|false */
  17. protected $rev2; // timestamp of newer revision
  18. /* @var array */
  19. protected $preference = [];
  20. /* @var ChangeLog */
  21. protected $changelog; // PageChangeLog or MediaChangeLog object
  22. /**
  23. * Diff Ui constructor
  24. *
  25. * @param string $id page id or media id
  26. */
  27. public function __construct($id)
  28. {
  29. $this->id = $id;
  30. $this->setChangeLog();
  31. }
  32. /**
  33. * set class property changelog
  34. */
  35. abstract protected function setChangeLog();
  36. /**
  37. * Prepare revision info of comparison pair
  38. */
  39. abstract protected function preProcess();
  40. /**
  41. * Set a pair of revisions to be compared
  42. *
  43. * @param int $rev1 older revision
  44. * @param int $rev2 newer revision
  45. * @return $this
  46. */
  47. public function compare($rev1, $rev2)
  48. {
  49. if ($rev2 < $rev1) [$rev1, $rev2] = [$rev2, $rev1];
  50. $this->rev1 = (int)$rev1;
  51. $this->rev2 = (int)$this->changelog->traceCurrentRevision($rev2);
  52. return $this;
  53. }
  54. /**
  55. * Gets or Sets preference of the Ui\Diff object
  56. *
  57. * @param string|array $prefs a key name or key-value pair(s)
  58. * @param mixed $value value used when the first args is string
  59. * @return array|$this
  60. */
  61. public function preference($prefs = null, $value = null)
  62. {
  63. // set
  64. if (is_string($prefs) && isset($value)) {
  65. $this->preference[$prefs] = $value;
  66. return $this;
  67. } elseif (is_array($prefs)) {
  68. foreach ($prefs as $name => $value) {
  69. $this->preference[$name] = $value;
  70. }
  71. return $this;
  72. }
  73. // get
  74. return $this->preference;
  75. }
  76. /**
  77. * Handle requested revision(s)
  78. *
  79. * @return void
  80. */
  81. protected function handle()
  82. {
  83. global $INPUT;
  84. // diff link icon click, eg. &do=diff&rev=#
  85. if ($INPUT->has('rev')) {
  86. $this->rev1 = $INPUT->int('rev');
  87. $this->rev2 = $this->changelog->currentRevision();
  88. if ($this->rev2 <= $this->rev1) {
  89. // fallback to compare previous with current
  90. unset($this->rev1, $this->rev2);
  91. }
  92. }
  93. // submit button with two checked boxes, eg. &do=diff&rev2[0]=#&rev2[1]=#
  94. $revs = $INPUT->arr('rev2', []);
  95. if (count($revs) > 1) {
  96. list($rev1, $rev2) = $revs;
  97. if ($rev2 < $rev1) [$rev1, $rev2] = [$rev2, $rev1];
  98. $this->rev1 = (int)$rev1;
  99. $this->rev2 = (int)$this->changelog->traceCurrentRevision($rev2);
  100. }
  101. // no revision was given, compare previous to current
  102. if (!isset($this->rev1, $this->rev2)) {
  103. $rev2 = $this->changelog->currentRevision();
  104. if ($rev2 > $this->changelog->lastRevision()) {
  105. $rev1 = $this->changelog->lastRevision();
  106. } else {
  107. $revs = $this->changelog->getRevisions(0, 1);
  108. $rev1 = count($revs) ? $revs[0] : false;
  109. }
  110. $this->rev1 = $rev1;
  111. $this->rev2 = $rev2;
  112. }
  113. }
  114. }