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