xref: /dokuwiki/inc/Remote/Response/Page.php (revision d48c2b252a339bed693da46fae768d93f4b3fe41)
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 = ''
51*d48c2b25SAndreas Gohr    ) {
5258ae4747SAndreas Gohr        $this->id = $id;
5358ae4747SAndreas Gohr        $this->file = wikiFN($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);
5758ae4747SAndreas Gohr        $this->hash = $hash;
5858ae4747SAndreas Gohr        $this->author = $author;
5958ae4747SAndreas Gohr        $this->title = $title ?: $this->retrieveTitle();
608ddd9b69SAndreas Gohr    }
618ddd9b69SAndreas Gohr
628ddd9b69SAndreas Gohr    /**
638ddd9b69SAndreas Gohr     * Get the title for the page
648ddd9b69SAndreas Gohr     *
658ddd9b69SAndreas Gohr     * Honors $conf['useheading']
668ddd9b69SAndreas Gohr     *
678ddd9b69SAndreas Gohr     * @return string
688ddd9b69SAndreas Gohr     */
698ddd9b69SAndreas Gohr    protected function retrieveTitle()
708ddd9b69SAndreas Gohr    {
718ddd9b69SAndreas Gohr        global $conf;
728ddd9b69SAndreas Gohr
738ddd9b69SAndreas Gohr        if ($conf['useheading']) {
748ddd9b69SAndreas Gohr            $title = p_get_first_heading($this->id);
758ddd9b69SAndreas Gohr            if ($title) {
768ddd9b69SAndreas Gohr                return $title;
778ddd9b69SAndreas Gohr            }
788ddd9b69SAndreas Gohr        }
798ddd9b69SAndreas Gohr        return $this->id;
808ddd9b69SAndreas Gohr    }
818ddd9b69SAndreas Gohr
826cce3332SAndreas Gohr    /**
836cce3332SAndreas Gohr     * Calculate the hash for this page
846cce3332SAndreas Gohr     *
856cce3332SAndreas Gohr     * This is a heavy operation and should only be called when needed.
866cce3332SAndreas Gohr     */
876cce3332SAndreas Gohr    public function calculateHash()
886cce3332SAndreas Gohr    {
8958ae4747SAndreas Gohr        $this->hash = md5(io_readFile($this->file));
906cce3332SAndreas Gohr    }
916cce3332SAndreas Gohr
926cce3332SAndreas Gohr    /**
936cce3332SAndreas Gohr     * Retrieve the author of this page
946cce3332SAndreas Gohr     */
956cce3332SAndreas Gohr    public function retrieveAuthor()
966cce3332SAndreas Gohr    {
976cce3332SAndreas Gohr        $pagelog = new PageChangeLog($this->id, 1024);
986cce3332SAndreas Gohr        $info = $pagelog->getRevisionInfo($this->revision);
99d1f06eb4SAndreas Gohr        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : '';
1006cce3332SAndreas Gohr    }
1018268b284SAndreas Gohr
1028268b284SAndreas Gohr    /** @inheritdoc */
1038268b284SAndreas Gohr    public function __toString()
1048268b284SAndreas Gohr    {
1058268b284SAndreas Gohr        return $this->id . '@' . $this->revision;
1068268b284SAndreas Gohr    }
1078ddd9b69SAndreas Gohr}
108