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