xref: /plugin/struct/meta/PageMeta.php (revision f5df16ddb9f8ba8bd1c70590453952f130624475)
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        /** @var \helper_plugin_struct_db $helper */
20        $helper = plugin_load('helper', 'struct_db');
21        $this->sqlite = $helper->getDB();
22        $this->pid = $pid;
23    }
24
25    /**
26     * If data was explicitly set, then save it to the database if that hasn't happened yet.
27     */
28    public function __destruct() {
29        if ($this->saveNeeded) {
30            $this->savePageData();
31        }
32    }
33
34    /**
35     * Save title, last editor and revision timestamp to database
36     */
37    public function savePageData() {
38        $sql = "REPLACE INTO titles (pid, title, lasteditor, lastrev, lastsummary) VALUES (?,?,?,?,?)";
39        $this->sqlite->query($sql, array($this->pid, $this->title, $this->lasteditor, $this->lastrev, $this->lastsummary));
40        $this->saveNeeded = false;
41    }
42
43    /**
44     * Sets a new title
45     *
46     * @param string|null $title set null to derive from PID
47     */
48    public function setTitle($title) {
49        if($title === null) {
50            $title = noNS($this->pid);
51        }
52
53        $this->title = $title;
54        $this->saveNeeded = true;
55    }
56
57    /**
58     * Sets the last editor
59     *
60     * @param string|null $lastEditor
61     */
62    public function setLastEditor($lastEditor) {
63        if($lastEditor === null) {
64            $lastEditor = '';
65        }
66
67        $this->lasteditor = $lastEditor;
68        $this->saveNeeded = true;
69    }
70
71    /**
72     * Sets the revision timestamp
73     *
74     * @param int|null $lastrev
75     */
76    public function setLastRevision($lastrev) {
77        if($lastrev === null) {
78            $lastrev = 0;
79        }
80
81        $this->lastrev = $lastrev;
82        $this->saveNeeded = true;
83    }
84
85    /**
86     * Sets the last summary
87     *
88     * @param int|null $lastsummary
89     */
90    public function setLastSummary($lastsummary) {
91        if($lastsummary === null) {
92            $lastsummary = '';
93        }
94
95        $this->lastsummary = $lastsummary;
96        $this->saveNeeded = true;
97    }
98
99    /**
100     * @return string the page's ID
101     */
102    public function getPid() {
103        return $this->pid;
104    }
105}
106