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.
 
 
 
 
 

88 lines
2.6 KiB

  1. <?php
  2. namespace dokuwiki\Remote\Response;
  3. use dokuwiki\ChangeLog\MediaChangeLog;
  4. /**
  5. * Represents a single media revision in the wiki.
  6. */
  7. class Media extends ApiResponse
  8. {
  9. /** @var string The media ID */
  10. public $id;
  11. /** @var int The media revision aka last modified timestamp */
  12. public $revision;
  13. /** @var int The page size in bytes */
  14. public $size;
  15. /** @var int The current user's permissions for this file */
  16. public $permission;
  17. /** @var bool Wether this is an image file */
  18. public $isimage;
  19. /** @var string MD5 sum over the file'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 media revision */
  24. protected $file;
  25. /**
  26. * Media constructor.
  27. *
  28. * @param string $id The media ID
  29. * @param int $revision The media revision aka last modified timestamp
  30. * @param int $mtime The media revision aka last modified timestamp
  31. * @param int|null $size The page size in bytes
  32. * @param int|null $perms The current user's permissions for this file
  33. * @param bool|null $isimage Wether this is an image file
  34. * @param string $hash MD5 sum over the file's content
  35. */
  36. public function __construct(
  37. $id,
  38. $revision = 0,
  39. $mtime = 0,
  40. $size = null,
  41. $perms = null,
  42. $isimage = null,
  43. $hash = '',
  44. $author = ''
  45. ) {
  46. $this->id = $id;
  47. $this->file = mediaFN($this->id, $revision);
  48. $this->revision = $revision ?: $mtime ?: filemtime($this->file);
  49. $this->size = $size ?? filesize($this->file);
  50. $this->permission = $perms ?? auth_quickaclcheck($this->id);
  51. ;
  52. $this->isimage = (bool)($isimage ?? preg_match("/\.(jpe?g|gif|png)$/", $id));
  53. $this->hash = $hash;
  54. $this->author = $author;
  55. }
  56. /**
  57. * Calculate the hash for this page
  58. *
  59. * This is a heavy operation and should only be called when needed.
  60. */
  61. public function calculateHash()
  62. {
  63. $this->hash = md5(io_readFile($this->file, false));
  64. }
  65. /**
  66. * Retrieve the author of this page
  67. */
  68. public function retrieveAuthor()
  69. {
  70. $pagelog = new MediaChangeLog($this->id, 1024);
  71. $info = $pagelog->getRevisionInfo($this->revision);
  72. $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : '';
  73. }
  74. /** @inheritdoc */
  75. public function __toString()
  76. {
  77. return $this->id . '@' . $this->revision;
  78. }
  79. }