xref: /dokuwiki/inc/Remote/Response/Page.php (revision d1f06eb4f0e4febc5434c97e319fce6d0253e533)
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    {
53        $this->id = $id;
54        $this->file = wikiFN($this->id, $revision);
55        $this->revision = $revision ?: $mtime ?: @filemtime($this->file);
56        $this->size = $size ?? @filesize($this->file);
57        $this->permission = $perms ?? auth_quickaclcheck($this->id);
58        $this->hash = $hash;
59        $this->author = $author;
60        $this->title = $title ?: $this->retrieveTitle();
61    }
62
63    /**
64     * Get the title for the page
65     *
66     * Honors $conf['useheading']
67     *
68     * @return string
69     */
70    protected function retrieveTitle()
71    {
72        global $conf;
73
74        if ($conf['useheading']) {
75            $title = p_get_first_heading($this->id);
76            if ($title) {
77                return $title;
78            }
79        }
80        return $this->id;
81    }
82
83    /**
84     * Calculate the hash for this page
85     *
86     * This is a heavy operation and should only be called when needed.
87     */
88    public function calculateHash()
89    {
90        $this->hash = md5(io_readFile($this->file));
91    }
92
93    /**
94     * Retrieve the author of this page
95     */
96    public function retrieveAuthor()
97    {
98        $pagelog = new PageChangeLog($this->id, 1024);
99        $info = $pagelog->getRevisionInfo($this->revision);
100        $this->author = is_array($info) ? ($info['user'] ?: $info['ip']) : '';
101    }
102
103    /** @inheritdoc */
104    public function __toString()
105    {
106        return $this->id . '@' . $this->revision;
107    }
108}
109