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;
12    protected $lasteditor;
13    protected $lastrev;
14    protected $lastsummary;
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        $data = $this->sqlite->queryRecord($sql, [$this->pid]);
43        return $data;
44    }
45
46    /**
47     * Save title, last editor and revision timestamp to database
48     */
49    public function savePageData()
50    {
51        $sql = "REPLACE INTO titles (pid, title, lasteditor, lastrev, lastsummary) VALUES (?,?,?,?,?)";
52        $this->sqlite->query($sql, [$this->pid, $this->title, $this->lasteditor, $this->lastrev, $this->lastsummary]);
53        $this->saveNeeded = false;
54    }
55
56    /**
57     * Sets a new title
58     *
59     * @param string|null $title set null to derive from PID
60     */
61    public function setTitle($title)
62    {
63        if ($title === null) {
64            $title = noNS($this->pid);
65        }
66
67        $this->title = $title;
68        $this->saveNeeded = true;
69    }
70
71    /**
72     * Sets the last editor
73     *
74     * @param string|null $lastEditor
75     */
76    public function setLastEditor($lastEditor)
77    {
78        if ($lastEditor === null) {
79            $lastEditor = '';
80        }
81
82        $this->lasteditor = $lastEditor;
83        $this->saveNeeded = true;
84    }
85
86    /**
87     * Sets the revision timestamp
88     *
89     * @param int|null $lastrev
90     */
91    public function setLastRevision($lastrev)
92    {
93        if ($lastrev === null) {
94            $lastrev = 0;
95        }
96
97        $this->lastrev = $lastrev;
98        $this->saveNeeded = true;
99    }
100
101    /**
102     * Sets the last summary
103     *
104     * @param int|null $lastsummary
105     */
106    public function setLastSummary($lastsummary)
107    {
108        if ($lastsummary === null) {
109            $lastsummary = '';
110        }
111
112        $this->lastsummary = $lastsummary;
113        $this->saveNeeded = true;
114    }
115
116    /**
117     * @return string the page's ID
118     */
119    public function getPid()
120    {
121        return $this->pid;
122    }
123}
124