xref: /dokuwiki/inc/Remote/Response/Media.php (revision d1f06eb4f0e4febc5434c97e319fce6d0253e533)
16cce3332SAndreas Gohr<?php
26cce3332SAndreas Gohr
36cce3332SAndreas Gohrnamespace dokuwiki\Remote\Response;
46cce3332SAndreas Gohr
5*d1f06eb4SAndreas Gohruse dokuwiki\ChangeLog\MediaChangeLog;
6*d1f06eb4SAndreas Gohr
76cce3332SAndreas Gohr/**
86cce3332SAndreas Gohr * Represents a single media revision in the wiki.
96cce3332SAndreas Gohr */
106cce3332SAndreas Gohrclass Media extends ApiResponse
116cce3332SAndreas Gohr{
126cce3332SAndreas Gohr    /** @var string The media ID */
136cce3332SAndreas Gohr    public $id;
146cce3332SAndreas Gohr    /** @var int The media revision aka last modified timestamp */
156cce3332SAndreas Gohr    public $revision;
166cce3332SAndreas Gohr    /** @var int The page size in bytes */
176cce3332SAndreas Gohr    public $size;
186cce3332SAndreas Gohr    /** @var int The current user's permissions for this file */
1958ae4747SAndreas Gohr    public $permission;
206cce3332SAndreas Gohr    /** @var bool Wether this is an image file */
216cce3332SAndreas Gohr    public $isimage;
226cce3332SAndreas Gohr    /** @var string MD5 sum over the file's content (if available and requested) */
236cce3332SAndreas Gohr    public $hash;
24*d1f06eb4SAndreas Gohr    /** @var string The author of this page revision (if available and requested) */
25*d1f06eb4SAndreas Gohr    public $author;
266cce3332SAndreas Gohr
2758ae4747SAndreas Gohr    /** @var string The file path to this media revision */
2858ae4747SAndreas Gohr    protected $file;
2958ae4747SAndreas Gohr
3058ae4747SAndreas Gohr    /**
3158ae4747SAndreas Gohr     * Media constructor.
3258ae4747SAndreas Gohr     *
3358ae4747SAndreas Gohr     * @param string $id The media ID
3458ae4747SAndreas Gohr     * @param int $revision The media revision aka last modified timestamp
3558ae4747SAndreas Gohr     * @param int $mtime The media revision aka last modified timestamp
3658ae4747SAndreas Gohr     * @param int|null $size The page size in bytes
3758ae4747SAndreas Gohr     * @param int|null $perms The current user's permissions for this file
3858ae4747SAndreas Gohr     * @param bool|null $isimage Wether this is an image file
3958ae4747SAndreas Gohr     * @param string $hash MD5 sum over the file's content
4058ae4747SAndreas Gohr     */
4158ae4747SAndreas Gohr    public function __construct(
4258ae4747SAndreas Gohr        $id,
4358ae4747SAndreas Gohr        $revision = 0,
4458ae4747SAndreas Gohr        $mtime = 0,
4558ae4747SAndreas Gohr        $size = null,
4658ae4747SAndreas Gohr        $perms = null,
4758ae4747SAndreas Gohr        $isimage = null,
48*d1f06eb4SAndreas Gohr        $hash = '',
49*d1f06eb4SAndreas Gohr        $author = ''
5058ae4747SAndreas Gohr    )
516cce3332SAndreas Gohr    {
5258ae4747SAndreas Gohr        $this->id = $id;
5358ae4747SAndreas Gohr        $this->file = mediaFN($this->id, $revision);
5458ae4747SAndreas Gohr        $this->revision = $revision ?: $mtime ?: filemtime($this->file);
5558ae4747SAndreas Gohr        $this->size = $size ?? filesize($this->file);
5658ae4747SAndreas Gohr        $this->permission = $perms ?? auth_quickaclcheck($this->id);;
57*d1f06eb4SAndreas Gohr        $this->isimage = (bool)($isimage ?? preg_match("/\.(jpe?g|gif|png)$/", $id));
5858ae4747SAndreas Gohr        $this->hash = $hash;
59*d1f06eb4SAndreas Gohr        $this->author = $author;
606cce3332SAndreas Gohr    }
616cce3332SAndreas Gohr
626cce3332SAndreas Gohr    /**
636cce3332SAndreas Gohr     * Calculate the hash for this page
646cce3332SAndreas Gohr     *
656cce3332SAndreas Gohr     * This is a heavy operation and should only be called when needed.
666cce3332SAndreas Gohr     */
676cce3332SAndreas Gohr    public function calculateHash()
686cce3332SAndreas Gohr    {
6958ae4747SAndreas Gohr        $this->hash = md5(io_readFile($this->file, false));
706cce3332SAndreas Gohr    }
718268b284SAndreas Gohr
72*d1f06eb4SAndreas Gohr    /**
73*d1f06eb4SAndreas Gohr     * Retrieve the author of this page
74*d1f06eb4SAndreas Gohr     */
75*d1f06eb4SAndreas Gohr    public function retrieveAuthor()
76*d1f06eb4SAndreas Gohr    {
77*d1f06eb4SAndreas Gohr        $pagelog = new MediaChangeLog($this->id, 1024);
78*d1f06eb4SAndreas Gohr        $info = $pagelog->getRevisionInfo($this->revision);
79*d1f06eb4SAndreas Gohr        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : '';
80*d1f06eb4SAndreas Gohr    }
81*d1f06eb4SAndreas Gohr
828268b284SAndreas Gohr    /** @inheritdoc */
838268b284SAndreas Gohr    public function __toString()
848268b284SAndreas Gohr    {
858268b284SAndreas Gohr        return $this->id . '@' . $this->revision;
868268b284SAndreas Gohr    }
876cce3332SAndreas Gohr}
88