Revisions.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. namespace dokuwiki\Ui;
  3. use dokuwiki\ChangeLog\ChangeLog;
  4. /**
  5. * DokuWiki Revisions Interface
  6. * parent class of PageRevisions and MediaRevisions
  7. *
  8. * Note: navigation starts from -1, not 0. This is because our Revision management starts old revisions at 0 and
  9. * will return the current revision only if the revisions starting at -1 are requested.
  10. *
  11. * @package dokuwiki\Ui
  12. */
  13. abstract class Revisions extends Ui
  14. {
  15. /* @var string */
  16. protected $id; // page id or media id
  17. /* @var ChangeLog */
  18. protected $changelog; // PageChangeLog or MediaChangeLog object
  19. /**
  20. * Revisions Ui constructor
  21. *
  22. * @param string $id page id or media id
  23. */
  24. public function __construct($id)
  25. {
  26. $this->id = $id;
  27. $this->setChangeLog();
  28. }
  29. /**
  30. * set class property changelog
  31. */
  32. abstract protected function setChangeLog();
  33. /**
  34. * Get revisions, and set correct pagination parameters (first, hasNext)
  35. *
  36. * @param int $first
  37. * @param bool $hasNext
  38. * @return array revisions to be shown in a paginated list
  39. * @see also https://www.dokuwiki.org/devel:changelog
  40. */
  41. protected function getRevisions(&$first, &$hasNext)
  42. {
  43. global $conf;
  44. $changelog =& $this->changelog;
  45. $revisions = [];
  46. $currentRevInfo = $changelog->getCurrentRevisionInfo();
  47. if (!$currentRevInfo) return $revisions;
  48. $num = $conf['recent'];
  49. /* we need to get one additional log entry to be able to
  50. * decide if this is the last page or is there another one.
  51. * see also Ui\Recent::getRecents()
  52. */
  53. $revlist = $changelog->getRevisions($first, $num + 1);
  54. if (count($revlist) == 0 && $first > -1) {
  55. // resets to zero if $first requested a too high number
  56. $first = -1;
  57. return $this->getRevisions($first, $hasNext);
  58. }
  59. // decide if this is the last page or is there another one
  60. $hasNext = false;
  61. if (count($revlist) > $num) {
  62. $hasNext = true;
  63. array_pop($revlist); // remove one additional log entry
  64. }
  65. // append each revision info array to the revisions
  66. foreach ($revlist as $rev) {
  67. $revisions[] = $changelog->getRevisionInfo($rev);
  68. }
  69. return $revisions;
  70. }
  71. /**
  72. * Navigation buttons for Pagination (prev/next)
  73. *
  74. * @param int $first
  75. * @param bool $hasNext
  76. * @param callable $callback returns array of hidden fields for the form button
  77. * @return string html
  78. */
  79. protected function navigation($first, $hasNext, $callback)
  80. {
  81. global $conf;
  82. $html = '<div class="pagenav">';
  83. $last = $first + $conf['recent'];
  84. if ($first > -1) {
  85. $first = max($first - $conf['recent'], -1);
  86. $html .= '<div class="pagenav-prev">';
  87. $html .= html_btn('newer', $this->id, "p", $callback($first));
  88. $html .= '</div>';
  89. }
  90. if ($hasNext) {
  91. $html .= '<div class="pagenav-next">';
  92. $html .= html_btn('older', $this->id, "n", $callback($last));
  93. $html .= '</div>';
  94. }
  95. $html .= '</div>';
  96. return $html;
  97. }
  98. }