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 $permission;
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    /** @var string The file path to this page revision */
28    protected $file;
29
30    /**
31     * Page constructor.
32     *
33     * @param string $id The page ID
34     * @param int $revision The page revision 0 for current
35     * @param int $mtime Last modified timestamp
36     * @param string $title The page title
37     * @param int|null $size The page size in bytes
38     * @param int|null $perms The current user's permissions for this page
39     * @param string $hash MD5 sum over the page's content
40     * @param string $author The author of this page revision
41     */
42    public function __construct(
43        $id,
44        $revision = 0,
45        $mtime = 0,
46        $title = '',
47        $size = null,
48        $perms = null,
49        $hash = '',
50        $author = ''
51    ) {
52        $this->id = $id;
53        $this->file = wikiFN($this->id, $revision);
54        $this->revision = $revision ?: $mtime ?: @filemtime($this->file);
55        $this->size = $size ?? @filesize($this->file);
56        $this->permission = $perms ?? auth_quickaclcheck($this->id);
57        $this->hash = $hash;
58        $this->author = $author;
59        $this->title = $title ?: $this->retrieveTitle();
60    }
61
62    /**
63     * Get the title for the page
64     *
65     * Honors $conf['useheading']
66     *
67     * @return string
68     */
69    protected function retrieveTitle()
70    {
71        global $conf;
72
73        if ($conf['useheading']) {
74            $title = p_get_first_heading($this->id);
75            if ($title) {
76                return $title;
77            }
78        }
79        return $this->id;
80    }
81
82    /**
83     * Calculate the hash for this page
84     *
85     * This is a heavy operation and should only be called when needed.
86     */
87    public function calculateHash()
88    {
89        $this->hash = md5(trim(io_readFile($this->file)));
90    }
91
92    /**
93     * Retrieve the author of this page
94     */
95    public function retrieveAuthor()
96    {
97        $pagelog = new PageChangeLog($this->id, 1024);
98        $info = $pagelog->getRevisionInfo($this->revision);
99        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : '';
100    }
101
102    /** @inheritdoc */
103    public function __toString()
104    {
105        return $this->id . '@' . $this->revision;
106    }
107}
108