1<?php
2
3namespace dokuwiki\plugin\struct\meta;
4
5class PageMeta
6{
7    /** @var \helper_plugin_sqlite */
8    protected $sqlite;
9
10    protected $pid;
11    protected $title = null;
12    protected $lasteditor = null;
13    protected $lastrev = null;
14    protected $lastsummary = null;
15
16    protected $saveNeeded = false;
17
18    public function __construct($pid)
19    {
20        /** @var \helper_plugin_struct_db $helper */
21        $helper = plugin_load('helper', 'struct_db');
22        $this->sqlite = $helper->getDB();
23        $this->pid = $pid;
24    }
25
26    /**
27     * If data was explicitly set, then save it to the database if that hasn't happened yet.
28     */
29    public function __destruct()
30    {
31        if ($this->saveNeeded) {
32            $this->savePageData();
33        }
34    }
35
36    /**
37     * Get latest page metadata from database
38     */
39    public function getPageData()
40    {
41        $sql = "SELECT pid, title, lasteditor, lastrev, lastsummary FROM titles WHERE pid = ?";
42        $res = $this->sqlite->query($sql, $this->pid);
43        $data = $this->sqlite->res2row($res);
44        $this->sqlite->res_close($res);
45        return $data;
46    }
47
48    /**
49     * Save title, last editor and revision timestamp to database
50     */
51    public function savePageData()
52    {
53        $sql = "REPLACE INTO titles (pid, title, lasteditor, lastrev, lastsummary) VALUES (?,?,?,?,?)";
54        $this->sqlite->query($sql, [$this->pid, $this->title, $this->lasteditor, $this->lastrev, $this->lastsummary]);
55        $this->saveNeeded = false;
56    }
57
58    /**
59     * Sets a new title
60     *
61     * @param string|null $title set null to derive from PID
62     */
63    public function setTitle($title)
64    {
65        if ($title === null) {
66            $title = noNS($this->pid);
67        }
68
69        $this->title = $title;
70        $this->saveNeeded = true;
71    }
72
73    /**
74     * Sets the last editor
75     *
76     * @param string|null $lastEditor
77     */
78    public function setLastEditor($lastEditor)
79    {
80        if ($lastEditor === null) {
81            $lastEditor = '';
82        }
83
84        $this->lasteditor = $lastEditor;
85        $this->saveNeeded = true;
86    }
87
88    /**
89     * Sets the revision timestamp
90     *
91     * @param int|null $lastrev
92     */
93    public function setLastRevision($lastrev)
94    {
95        if ($lastrev === null) {
96            $lastrev = 0;
97        }
98
99        $this->lastrev = $lastrev;
100        $this->saveNeeded = true;
101    }
102
103    /**
104     * Sets the last summary
105     *
106     * @param int|null $lastsummary
107     */
108    public function setLastSummary($lastsummary)
109    {
110        if ($lastsummary === null) {
111            $lastsummary = '';
112        }
113
114        $this->lastsummary = $lastsummary;
115        $this->saveNeeded = true;
116    }
117
118    /**
119     * @return string the page's ID
120     */
121    public function getPid()
122    {
123        return $this->pid;
124    }
125}
126