xref: /dokuwiki/inc/Remote/Response/Page.php (revision 58ae4747de65e434e2a4f2d10822e363198c89b8)
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 */
21*58ae4747SAndreas 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
27*58ae4747SAndreas Gohr    /** @var string The file path to this page revision */
28*58ae4747SAndreas Gohr    protected $file;
29*58ae4747SAndreas Gohr
30*58ae4747SAndreas Gohr    /**
31*58ae4747SAndreas Gohr     * Page constructor.
32*58ae4747SAndreas Gohr     *
33*58ae4747SAndreas Gohr     * @param string $id The page ID
34*58ae4747SAndreas Gohr     * @param int $revision The page revision 0 for current
35*58ae4747SAndreas Gohr     * @param int $mtime Last modified timestamp
36*58ae4747SAndreas Gohr     * @param string $title The page title
37*58ae4747SAndreas Gohr     * @param int|null $size The page size in bytes
38*58ae4747SAndreas Gohr     * @param int|null $perms The current user's permissions for this page
39*58ae4747SAndreas Gohr     * @param string $hash MD5 sum over the page's content
40*58ae4747SAndreas Gohr     * @param string $author The author of this page revision
41*58ae4747SAndreas Gohr     */
42*58ae4747SAndreas Gohr    public function __construct(
43*58ae4747SAndreas Gohr        $id,
44*58ae4747SAndreas Gohr        $revision = 0,
45*58ae4747SAndreas Gohr        $mtime = 0,
46*58ae4747SAndreas Gohr        $title = '',
47*58ae4747SAndreas Gohr        $size = null,
48*58ae4747SAndreas Gohr        $perms = null,
49*58ae4747SAndreas Gohr        $hash = '',
50*58ae4747SAndreas Gohr        $author = ''
51*58ae4747SAndreas Gohr    )
528ddd9b69SAndreas Gohr    {
53*58ae4747SAndreas Gohr        $this->id = $id;
54*58ae4747SAndreas Gohr        $this->file = wikiFN($this->id, $revision);
55*58ae4747SAndreas Gohr        $this->revision = $revision ?: $mtime ?: @filemtime($this->file);
56*58ae4747SAndreas Gohr        $this->size = $size ?? @filesize($this->file);
57*58ae4747SAndreas Gohr        $this->permission = $perms ?? auth_quickaclcheck($this->id);
58*58ae4747SAndreas Gohr        $this->hash = $hash;
59*58ae4747SAndreas Gohr        $this->author = $author;
60*58ae4747SAndreas 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    {
90*58ae4747SAndreas 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    }
1048ddd9b69SAndreas Gohr}
105