xref: /dokuwiki/inc/Remote/Response/Page.php (revision 0caa81c70023f1e9557c4b6769a9ff200df17a19)
1<?php
2
3namespace dokuwiki\Remote\Response;
4
5use dokuwiki\ChangeLog\PageChangeLog;
6
7/**
8 * Represents a single page revision in the wiki.
9 */
10class Page extends ApiResponse
11{
12    /** @var string The page ID */
13    public $id;
14    /** @var int The page revision aka last modified timestamp */
15    public $revision;
16    /** @var int The page size in bytes */
17    public $size;
18    /** @var string The page title */
19    public $title;
20    /** @var int The current user's permissions for this page */
21    public $perms;
22    /** @var string MD5 sum over the page's content (if available and requested) */
23    public $hash;
24    /** @var string The author of this page revision (if available and requested) */
25    public $author;
26
27    /** @inheritdoc */
28    public function __construct($data)
29    {
30        $this->id = cleanID($data['id'] ?? '');
31        if ($this->id === '') {
32            throw new \InvalidArgumentException('Missing id');
33        }
34        if (!page_exists($this->id)) {
35            throw new \InvalidArgumentException('Page does not exist');
36        }
37
38        // FIXME this isn't really managing the difference between old and current revs correctly
39
40        $this->revision = (int)($data['rev'] ?? @filemtime(wikiFN($this->id)));
41        $this->size = (int)($data['size'] ?? @filesize(wikiFN($this->id)));
42        $this->title = $data['title'] ?? $this->retrieveTitle();
43        $this->perms = $data['perm'] ?? auth_quickaclcheck($this->id);
44        $this->hash = $data['hash'] ?? '';
45        $this->author = $data['author'] ?? '';
46    }
47
48    /**
49     * Get the title for the page
50     *
51     * Honors $conf['useheading']
52     *
53     * @return string
54     */
55    protected function retrieveTitle()
56    {
57        global $conf;
58
59        if ($conf['useheading']) {
60            $title = p_get_first_heading($this->id);
61            if ($title) {
62                return $title;
63            }
64        }
65        return $this->id;
66    }
67
68    /**
69     * Calculate the hash for this page
70     *
71     * This is a heavy operation and should only be called when needed.
72     */
73    public function calculateHash()
74    {
75        if (!page_exists($this->id)) return;
76        $this->hash = md5(io_readFile(wikiFN($this->id)));
77    }
78
79    /**
80     * Retrieve the author of this page
81     */
82    public function retrieveAuthor()
83    {
84        if (!page_exists($this->id)) return;
85
86        $pagelog = new PageChangeLog($this->id, 1024);
87        $info = $pagelog->getRevisionInfo($this->revision);
88        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : null;
89    }
90}
91