xref: /dokuwiki/inc/Remote/Response/Page.php (revision 6cce3332fbc12c1e250ec7e6adbad6d4dc2c74e8)
18ddd9b69SAndreas Gohr<?php
28ddd9b69SAndreas Gohr
38ddd9b69SAndreas Gohrnamespace dokuwiki\Remote\Response;
48ddd9b69SAndreas Gohr
5*6cce3332SAndreas Gohruse dokuwiki\ChangeLog\PageChangeLog;
6*6cce3332SAndreas 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 */
218ddd9b69SAndreas Gohr    public $perms;
22*6cce3332SAndreas Gohr    /** @var string MD5 sum over the page's content (if available and requested) */
238ddd9b69SAndreas Gohr    public $hash;
24*6cce3332SAndreas Gohr    /** @var string The author of this page revision (if available and requested) */
25*6cce3332SAndreas Gohr    public $author;
268ddd9b69SAndreas Gohr
278ddd9b69SAndreas Gohr    /** @inheritdoc */
288ddd9b69SAndreas Gohr    public function __construct($data)
298ddd9b69SAndreas Gohr    {
308ddd9b69SAndreas Gohr        $this->id = cleanID($data['id'] ?? '');
318ddd9b69SAndreas Gohr        if ($this->id === '') {
328ddd9b69SAndreas Gohr            throw new \InvalidArgumentException('Missing id');
338ddd9b69SAndreas Gohr        }
34*6cce3332SAndreas Gohr        if (!page_exists($this->id)) {
35*6cce3332SAndreas Gohr            throw new \InvalidArgumentException('Page does not exist');
36*6cce3332SAndreas Gohr        }
378ddd9b69SAndreas Gohr
38*6cce3332SAndreas Gohr        // FIXME this isn't really managing the difference between old and current revs correctly
39*6cce3332SAndreas Gohr
40*6cce3332SAndreas Gohr        $this->revision = (int)($data['rev'] ?? @filemtime(wikiFN($this->id)));
418ddd9b69SAndreas Gohr        $this->size = (int)($data['size'] ?? @filesize(wikiFN($this->id)));
428ddd9b69SAndreas Gohr        $this->title = $data['title'] ?? $this->retrieveTitle();
438ddd9b69SAndreas Gohr        $this->perms = $data['perm'] ?? auth_quickaclcheck($this->id);
448ddd9b69SAndreas Gohr        $this->hash = $data['hash'] ?? '';
45*6cce3332SAndreas Gohr        $this->author = $data['author'] ?? '';
468ddd9b69SAndreas Gohr    }
478ddd9b69SAndreas Gohr
488ddd9b69SAndreas Gohr    /**
498ddd9b69SAndreas Gohr     * Get the title for the page
508ddd9b69SAndreas Gohr     *
518ddd9b69SAndreas Gohr     * Honors $conf['useheading']
528ddd9b69SAndreas Gohr     *
538ddd9b69SAndreas Gohr     * @return string
548ddd9b69SAndreas Gohr     */
558ddd9b69SAndreas Gohr    protected function retrieveTitle()
568ddd9b69SAndreas Gohr    {
578ddd9b69SAndreas Gohr        global $conf;
588ddd9b69SAndreas Gohr
598ddd9b69SAndreas Gohr        if ($conf['useheading']) {
608ddd9b69SAndreas Gohr            $title = p_get_first_heading($this->id);
618ddd9b69SAndreas Gohr            if ($title) {
628ddd9b69SAndreas Gohr                return $title;
638ddd9b69SAndreas Gohr            }
648ddd9b69SAndreas Gohr        }
658ddd9b69SAndreas Gohr        return $this->id;
668ddd9b69SAndreas Gohr    }
678ddd9b69SAndreas Gohr
68*6cce3332SAndreas Gohr    /**
69*6cce3332SAndreas Gohr     * Calculate the hash for this page
70*6cce3332SAndreas Gohr     *
71*6cce3332SAndreas Gohr     * This is a heavy operation and should only be called when needed.
72*6cce3332SAndreas Gohr     */
73*6cce3332SAndreas Gohr    public function calculateHash()
74*6cce3332SAndreas Gohr    {
75*6cce3332SAndreas Gohr        if (!page_exists($this->id)) return;
76*6cce3332SAndreas Gohr        $this->hash = md5(io_readFile(wikiFN($this->id)));
77*6cce3332SAndreas Gohr    }
78*6cce3332SAndreas Gohr
79*6cce3332SAndreas Gohr    /**
80*6cce3332SAndreas Gohr     * Retrieve the author of this page
81*6cce3332SAndreas Gohr     */
82*6cce3332SAndreas Gohr    public function retrieveAuthor()
83*6cce3332SAndreas Gohr    {
84*6cce3332SAndreas Gohr        if (!page_exists($this->id)) return;
85*6cce3332SAndreas Gohr
86*6cce3332SAndreas Gohr        $pagelog = new PageChangeLog($this->id, 1024);
87*6cce3332SAndreas Gohr        $info = $pagelog->getRevisionInfo($this->revision);
88*6cce3332SAndreas Gohr        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : null;
89*6cce3332SAndreas Gohr    }
908ddd9b69SAndreas Gohr}
91