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