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.
 
 
 
 
 

108 lines
2.9 KiB

  1. <?php
  2. namespace dokuwiki\Remote\Response;
  3. use dokuwiki\ChangeLog\PageChangeLog;
  4. /**
  5. * Represents a single page revision in the wiki.
  6. */
  7. class Page extends ApiResponse
  8. {
  9. /** @var string The page ID */
  10. public $id;
  11. /** @var int The page revision aka last modified timestamp */
  12. public $revision;
  13. /** @var int The page size in bytes */
  14. public $size;
  15. /** @var string The page title */
  16. public $title;
  17. /** @var int The current user's permissions for this page */
  18. public $permission;
  19. /** @var string MD5 sum over the page's content (if available and requested) */
  20. public $hash;
  21. /** @var string The author of this page revision (if available and requested) */
  22. public $author;
  23. /** @var string The file path to this page revision */
  24. protected $file;
  25. /**
  26. * Page constructor.
  27. *
  28. * @param string $id The page ID
  29. * @param int $revision The page revision 0 for current
  30. * @param int $mtime Last modified timestamp
  31. * @param string $title The page title
  32. * @param int|null $size The page size in bytes
  33. * @param int|null $perms The current user's permissions for this page
  34. * @param string $hash MD5 sum over the page's content
  35. * @param string $author The author of this page revision
  36. */
  37. public function __construct(
  38. $id,
  39. $revision = 0,
  40. $mtime = 0,
  41. $title = '',
  42. $size = null,
  43. $perms = null,
  44. $hash = '',
  45. $author = ''
  46. ) {
  47. $this->id = $id;
  48. $this->file = wikiFN($this->id, $revision);
  49. $this->revision = $revision ?: $mtime ?: @filemtime($this->file);
  50. $this->size = $size ?? @filesize($this->file);
  51. $this->permission = $perms ?? auth_quickaclcheck($this->id);
  52. $this->hash = $hash;
  53. $this->author = $author;
  54. $this->title = $title ?: $this->retrieveTitle();
  55. }
  56. /**
  57. * Get the title for the page
  58. *
  59. * Honors $conf['useheading']
  60. *
  61. * @return string
  62. */
  63. protected function retrieveTitle()
  64. {
  65. global $conf;
  66. if ($conf['useheading']) {
  67. $title = p_get_first_heading($this->id);
  68. if ($title) {
  69. return $title;
  70. }
  71. }
  72. return $this->id;
  73. }
  74. /**
  75. * Calculate the hash for this page
  76. *
  77. * This is a heavy operation and should only be called when needed.
  78. */
  79. public function calculateHash()
  80. {
  81. $this->hash = md5(io_readFile($this->file));
  82. }
  83. /**
  84. * Retrieve the author of this page
  85. */
  86. public function retrieveAuthor()
  87. {
  88. $pagelog = new PageChangeLog($this->id, 1024);
  89. $info = $pagelog->getRevisionInfo($this->revision);
  90. $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : '';
  91. }
  92. /** @inheritdoc */
  93. public function __toString()
  94. {
  95. return $this->id . '@' . $this->revision;
  96. }
  97. }