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