xref: /dokuwiki/inc/Remote/Response/Media.php (revision 04acbb6fa78aa0b48be52e98501d8a9d113043df)
1<?php
2
3namespace dokuwiki\Remote\Response;
4
5/**
6 * Represents a single media revision in the wiki.
7 */
8class Media extends ApiResponse
9{
10    /** @var string The media ID */
11    public $id;
12    /** @var int The media revision aka last modified timestamp */
13    public $revision;
14    /** @var int The page size in bytes */
15    public $size;
16    /** @var int The current user's permissions for this file */
17    public $permission;
18    /** @var bool Wether this is an image file */
19    public $isimage;
20    /** @var string MD5 sum over the file's content (if available and requested) */
21    public $hash;
22
23    /** @var string The file path to this media revision */
24    protected $file;
25
26    /**
27     * Media constructor.
28     *
29     * @param string $id The media ID
30     * @param int $revision The media revision aka last modified timestamp
31     * @param int $mtime The media revision aka last modified timestamp
32     * @param int|null $size The page size in bytes
33     * @param int|null $perms The current user's permissions for this file
34     * @param bool|null $isimage Wether this is an image file
35     * @param string $hash MD5 sum over the file's content
36     */
37    public function __construct(
38        $id,
39        $revision = 0,
40        $mtime = 0,
41        $size = null,
42        $perms = null,
43        $isimage = null,
44        $hash = ''
45    )
46    {
47        $this->id = $id;
48        $this->file = mediaFN($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->isimage = (bool)($isimage ?? false);
53        $this->hash = $hash;
54    }
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    /** @inheritdoc */
67    public function __toString()
68    {
69        return $this->id . '@' . $this->revision;
70    }
71}
72