xref: /dokuwiki/inc/Remote/Response/Media.php (revision 8268b284d42dee1f36da2a61bd2c69ee9fa8aa33)
16cce3332SAndreas Gohr<?php
26cce3332SAndreas Gohr
36cce3332SAndreas Gohrnamespace dokuwiki\Remote\Response;
46cce3332SAndreas Gohr
56cce3332SAndreas Gohr/**
66cce3332SAndreas Gohr * Represents a single media revision in the wiki.
76cce3332SAndreas Gohr */
86cce3332SAndreas Gohrclass Media extends ApiResponse
96cce3332SAndreas Gohr{
106cce3332SAndreas Gohr    /** @var string The media ID */
116cce3332SAndreas Gohr    public $id;
126cce3332SAndreas Gohr    /** @var int The media revision aka last modified timestamp */
136cce3332SAndreas Gohr    public $revision;
146cce3332SAndreas Gohr    /** @var int The page size in bytes */
156cce3332SAndreas Gohr    public $size;
166cce3332SAndreas Gohr    /** @var int The current user's permissions for this file */
1758ae4747SAndreas Gohr    public $permission;
186cce3332SAndreas Gohr    /** @var bool Wether this is an image file */
196cce3332SAndreas Gohr    public $isimage;
206cce3332SAndreas Gohr    /** @var string MD5 sum over the file's content (if available and requested) */
216cce3332SAndreas Gohr    public $hash;
226cce3332SAndreas Gohr
2358ae4747SAndreas Gohr    /** @var string The file path to this media revision */
2458ae4747SAndreas Gohr    protected $file;
2558ae4747SAndreas Gohr
2658ae4747SAndreas Gohr    /**
2758ae4747SAndreas Gohr     * Media constructor.
2858ae4747SAndreas Gohr     *
2958ae4747SAndreas Gohr     * @param string $id The media ID
3058ae4747SAndreas Gohr     * @param int $revision The media revision aka last modified timestamp
3158ae4747SAndreas Gohr     * @param int $mtime The media revision aka last modified timestamp
3258ae4747SAndreas Gohr     * @param int|null $size The page size in bytes
3358ae4747SAndreas Gohr     * @param int|null $perms The current user's permissions for this file
3458ae4747SAndreas Gohr     * @param bool|null $isimage Wether this is an image file
3558ae4747SAndreas Gohr     * @param string $hash MD5 sum over the file's content
3658ae4747SAndreas Gohr     */
3758ae4747SAndreas Gohr    public function __construct(
3858ae4747SAndreas Gohr        $id,
3958ae4747SAndreas Gohr        $revision = 0,
4058ae4747SAndreas Gohr        $mtime = 0,
4158ae4747SAndreas Gohr        $size = null,
4258ae4747SAndreas Gohr        $perms = null,
4358ae4747SAndreas Gohr        $isimage = null,
4458ae4747SAndreas Gohr        $hash = ''
4558ae4747SAndreas Gohr    )
466cce3332SAndreas Gohr    {
4758ae4747SAndreas Gohr        $this->id = $id;
4858ae4747SAndreas Gohr        $this->file = mediaFN($this->id, $revision);
4958ae4747SAndreas Gohr        $this->revision = $revision ?: $mtime ?: filemtime($this->file);
5058ae4747SAndreas Gohr        $this->size = $size ?? filesize($this->file);
5158ae4747SAndreas Gohr        $this->permission = $perms ?? auth_quickaclcheck($this->id);;
5258ae4747SAndreas Gohr        $this->isimage = (bool)($isimage ?? false);
5358ae4747SAndreas Gohr        $this->hash = $hash;
546cce3332SAndreas Gohr    }
556cce3332SAndreas Gohr
566cce3332SAndreas Gohr    /**
576cce3332SAndreas Gohr     * Calculate the hash for this page
586cce3332SAndreas Gohr     *
596cce3332SAndreas Gohr     * This is a heavy operation and should only be called when needed.
606cce3332SAndreas Gohr     */
616cce3332SAndreas Gohr    public function calculateHash()
626cce3332SAndreas Gohr    {
6358ae4747SAndreas Gohr        $this->hash = md5(io_readFile($this->file, false));
646cce3332SAndreas Gohr    }
65*8268b284SAndreas Gohr
66*8268b284SAndreas Gohr    /** @inheritdoc */
67*8268b284SAndreas Gohr    public function __toString()
68*8268b284SAndreas Gohr    {
69*8268b284SAndreas Gohr        return $this->id . '@' . $this->revision;
70*8268b284SAndreas Gohr    }
716cce3332SAndreas Gohr}
72