xref: /dokuwiki/inc/Remote/Response/Page.php (revision 8268b284d42dee1f36da2a61bd2c69ee9fa8aa33)
18ddd9b69SAndreas Gohr<?php
28ddd9b69SAndreas Gohr
38ddd9b69SAndreas Gohrnamespace dokuwiki\Remote\Response;
48ddd9b69SAndreas Gohr
56cce3332SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog;
66cce3332SAndreas Gohr
78ddd9b69SAndreas Gohr/**
88ddd9b69SAndreas Gohr * Represents a single page revision in the wiki.
98ddd9b69SAndreas Gohr */
108ddd9b69SAndreas Gohrclass Page extends ApiResponse
118ddd9b69SAndreas Gohr{
128ddd9b69SAndreas Gohr    /** @var string The page ID */
138ddd9b69SAndreas Gohr    public $id;
148ddd9b69SAndreas Gohr    /** @var int The page revision aka last modified timestamp */
158ddd9b69SAndreas Gohr    public $revision;
168ddd9b69SAndreas Gohr    /** @var int The page size in bytes */
178ddd9b69SAndreas Gohr    public $size;
188ddd9b69SAndreas Gohr    /** @var string The page title */
198ddd9b69SAndreas Gohr    public $title;
208ddd9b69SAndreas Gohr    /** @var int The current user's permissions for this page */
2158ae4747SAndreas Gohr    public $permission;
226cce3332SAndreas Gohr    /** @var string MD5 sum over the page's content (if available and requested) */
238ddd9b69SAndreas Gohr    public $hash;
246cce3332SAndreas Gohr    /** @var string The author of this page revision (if available and requested) */
256cce3332SAndreas Gohr    public $author;
268ddd9b69SAndreas Gohr
2758ae4747SAndreas Gohr    /** @var string The file path to this page revision */
2858ae4747SAndreas Gohr    protected $file;
2958ae4747SAndreas Gohr
3058ae4747SAndreas Gohr    /**
3158ae4747SAndreas Gohr     * Page constructor.
3258ae4747SAndreas Gohr     *
3358ae4747SAndreas Gohr     * @param string $id The page ID
3458ae4747SAndreas Gohr     * @param int $revision The page revision 0 for current
3558ae4747SAndreas Gohr     * @param int $mtime Last modified timestamp
3658ae4747SAndreas Gohr     * @param string $title The page title
3758ae4747SAndreas Gohr     * @param int|null $size The page size in bytes
3858ae4747SAndreas Gohr     * @param int|null $perms The current user's permissions for this page
3958ae4747SAndreas Gohr     * @param string $hash MD5 sum over the page's content
4058ae4747SAndreas Gohr     * @param string $author The author of this page revision
4158ae4747SAndreas Gohr     */
4258ae4747SAndreas Gohr    public function __construct(
4358ae4747SAndreas Gohr        $id,
4458ae4747SAndreas Gohr        $revision = 0,
4558ae4747SAndreas Gohr        $mtime = 0,
4658ae4747SAndreas Gohr        $title = '',
4758ae4747SAndreas Gohr        $size = null,
4858ae4747SAndreas Gohr        $perms = null,
4958ae4747SAndreas Gohr        $hash = '',
5058ae4747SAndreas Gohr        $author = ''
5158ae4747SAndreas Gohr    )
528ddd9b69SAndreas Gohr    {
5358ae4747SAndreas Gohr        $this->id = $id;
5458ae4747SAndreas Gohr        $this->file = wikiFN($this->id, $revision);
5558ae4747SAndreas Gohr        $this->revision = $revision ?: $mtime ?: @filemtime($this->file);
5658ae4747SAndreas Gohr        $this->size = $size ?? @filesize($this->file);
5758ae4747SAndreas Gohr        $this->permission = $perms ?? auth_quickaclcheck($this->id);
5858ae4747SAndreas Gohr        $this->hash = $hash;
5958ae4747SAndreas Gohr        $this->author = $author;
6058ae4747SAndreas Gohr        $this->title = $title ?: $this->retrieveTitle();
618ddd9b69SAndreas Gohr    }
628ddd9b69SAndreas Gohr
638ddd9b69SAndreas Gohr    /**
648ddd9b69SAndreas Gohr     * Get the title for the page
658ddd9b69SAndreas Gohr     *
668ddd9b69SAndreas Gohr     * Honors $conf['useheading']
678ddd9b69SAndreas Gohr     *
688ddd9b69SAndreas Gohr     * @return string
698ddd9b69SAndreas Gohr     */
708ddd9b69SAndreas Gohr    protected function retrieveTitle()
718ddd9b69SAndreas Gohr    {
728ddd9b69SAndreas Gohr        global $conf;
738ddd9b69SAndreas Gohr
748ddd9b69SAndreas Gohr        if ($conf['useheading']) {
758ddd9b69SAndreas Gohr            $title = p_get_first_heading($this->id);
768ddd9b69SAndreas Gohr            if ($title) {
778ddd9b69SAndreas Gohr                return $title;
788ddd9b69SAndreas Gohr            }
798ddd9b69SAndreas Gohr        }
808ddd9b69SAndreas Gohr        return $this->id;
818ddd9b69SAndreas Gohr    }
828ddd9b69SAndreas Gohr
836cce3332SAndreas Gohr    /**
846cce3332SAndreas Gohr     * Calculate the hash for this page
856cce3332SAndreas Gohr     *
866cce3332SAndreas Gohr     * This is a heavy operation and should only be called when needed.
876cce3332SAndreas Gohr     */
886cce3332SAndreas Gohr    public function calculateHash()
896cce3332SAndreas Gohr    {
9058ae4747SAndreas Gohr        $this->hash = md5(io_readFile($this->file));
916cce3332SAndreas Gohr    }
926cce3332SAndreas Gohr
936cce3332SAndreas Gohr    /**
946cce3332SAndreas Gohr     * Retrieve the author of this page
956cce3332SAndreas Gohr     */
966cce3332SAndreas Gohr    public function retrieveAuthor()
976cce3332SAndreas Gohr    {
986cce3332SAndreas Gohr        if (!page_exists($this->id)) return;
996cce3332SAndreas Gohr
1006cce3332SAndreas Gohr        $pagelog = new PageChangeLog($this->id, 1024);
1016cce3332SAndreas Gohr        $info = $pagelog->getRevisionInfo($this->revision);
1026cce3332SAndreas Gohr        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : null;
1036cce3332SAndreas Gohr    }
104*8268b284SAndreas Gohr
105*8268b284SAndreas Gohr    /** @inheritdoc */
106*8268b284SAndreas Gohr    public function __toString()
107*8268b284SAndreas Gohr    {
108*8268b284SAndreas Gohr        return $this->id . '@' . $this->revision;
109*8268b284SAndreas Gohr    }
1108ddd9b69SAndreas Gohr}
111