xref: /plugin/struct/meta/PageMeta.php (revision d6d97f6064c3b0f90310be8341edc9585520ee54)
13aad9612SMichael Grosse<?php
23aad9612SMichael Grosse
33aad9612SMichael Grossenamespace dokuwiki\plugin\struct\meta;
43aad9612SMichael Grosse
5*d6d97f60SAnna Dabrowskaclass PageMeta
6*d6d97f60SAnna Dabrowska{
73aad9612SMichael Grosse
83aad9612SMichael Grosse    /** @var \helper_plugin_sqlite */
93aad9612SMichael Grosse    protected $sqlite;
103aad9612SMichael Grosse
113aad9612SMichael Grosse    protected $pid;
123aad9612SMichael Grosse    protected $title = null;
133aad9612SMichael Grosse    protected $lasteditor = null;
143aad9612SMichael Grosse    protected $lastrev = null;
1588b58a21SSzymon Olewniczak    protected $lastsummary = null;
163aad9612SMichael Grosse
173aad9612SMichael Grosse    protected $saveNeeded = false;
183aad9612SMichael Grosse
19*d6d97f60SAnna Dabrowska    public function __construct($pid)
20*d6d97f60SAnna Dabrowska    {
213aad9612SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
223aad9612SMichael Grosse        $helper = plugin_load('helper', 'struct_db');
233aad9612SMichael Grosse        $this->sqlite = $helper->getDB();
243aad9612SMichael Grosse        $this->pid = $pid;
253aad9612SMichael Grosse    }
263aad9612SMichael Grosse
273aad9612SMichael Grosse    /**
283aad9612SMichael Grosse     * If data was explicitly set, then save it to the database if that hasn't happened yet.
293aad9612SMichael Grosse     */
30*d6d97f60SAnna Dabrowska    public function __destruct()
31*d6d97f60SAnna Dabrowska    {
323aad9612SMichael Grosse        if ($this->saveNeeded) {
333aad9612SMichael Grosse            $this->savePageData();
343aad9612SMichael Grosse        }
353aad9612SMichael Grosse    }
363aad9612SMichael Grosse
373aad9612SMichael Grosse    /**
383aad9612SMichael Grosse     * Save title, last editor and revision timestamp to database
393aad9612SMichael Grosse     */
40*d6d97f60SAnna Dabrowska    public function savePageData()
41*d6d97f60SAnna Dabrowska    {
4288b58a21SSzymon Olewniczak        $sql = "REPLACE INTO titles (pid, title, lasteditor, lastrev, lastsummary) VALUES (?,?,?,?,?)";
4388b58a21SSzymon Olewniczak        $this->sqlite->query($sql, array($this->pid, $this->title, $this->lasteditor, $this->lastrev, $this->lastsummary));
443aad9612SMichael Grosse        $this->saveNeeded = false;
453aad9612SMichael Grosse    }
463aad9612SMichael Grosse
473aad9612SMichael Grosse    /**
483aad9612SMichael Grosse     * Sets a new title
493aad9612SMichael Grosse     *
503aad9612SMichael Grosse     * @param string|null $title set null to derive from PID
513aad9612SMichael Grosse     */
52*d6d97f60SAnna Dabrowska    public function setTitle($title)
53*d6d97f60SAnna Dabrowska    {
543aad9612SMichael Grosse        if ($title === null) {
553aad9612SMichael Grosse            $title = noNS($this->pid);
563aad9612SMichael Grosse        }
573aad9612SMichael Grosse
583aad9612SMichael Grosse        $this->title = $title;
593aad9612SMichael Grosse        $this->saveNeeded = true;
603aad9612SMichael Grosse    }
613aad9612SMichael Grosse
623aad9612SMichael Grosse    /**
633aad9612SMichael Grosse     * Sets the last editor
643aad9612SMichael Grosse     *
653aad9612SMichael Grosse     * @param string|null $lastEditor
663aad9612SMichael Grosse     */
67*d6d97f60SAnna Dabrowska    public function setLastEditor($lastEditor)
68*d6d97f60SAnna Dabrowska    {
693aad9612SMichael Grosse        if ($lastEditor === null) {
703aad9612SMichael Grosse            $lastEditor = '';
713aad9612SMichael Grosse        }
723aad9612SMichael Grosse
733aad9612SMichael Grosse        $this->lasteditor = $lastEditor;
743aad9612SMichael Grosse        $this->saveNeeded = true;
753aad9612SMichael Grosse    }
763aad9612SMichael Grosse
773aad9612SMichael Grosse    /**
783aad9612SMichael Grosse     * Sets the revision timestamp
793aad9612SMichael Grosse     *
803aad9612SMichael Grosse     * @param int|null $lastrev
813aad9612SMichael Grosse     */
82*d6d97f60SAnna Dabrowska    public function setLastRevision($lastrev)
83*d6d97f60SAnna Dabrowska    {
843aad9612SMichael Grosse        if ($lastrev === null) {
853aad9612SMichael Grosse            $lastrev = 0;
863aad9612SMichael Grosse        }
873aad9612SMichael Grosse
883aad9612SMichael Grosse        $this->lastrev = $lastrev;
893aad9612SMichael Grosse        $this->saveNeeded = true;
903aad9612SMichael Grosse    }
913aad9612SMichael Grosse
923aad9612SMichael Grosse    /**
9388b58a21SSzymon Olewniczak     * Sets the last summary
9488b58a21SSzymon Olewniczak     *
9588b58a21SSzymon Olewniczak     * @param int|null $lastsummary
9688b58a21SSzymon Olewniczak     */
97*d6d97f60SAnna Dabrowska    public function setLastSummary($lastsummary)
98*d6d97f60SAnna Dabrowska    {
9988b58a21SSzymon Olewniczak        if ($lastsummary === null) {
10088b58a21SSzymon Olewniczak            $lastsummary = '';
10188b58a21SSzymon Olewniczak        }
10288b58a21SSzymon Olewniczak
10388b58a21SSzymon Olewniczak        $this->lastsummary = $lastsummary;
10488b58a21SSzymon Olewniczak        $this->saveNeeded = true;
10588b58a21SSzymon Olewniczak    }
10688b58a21SSzymon Olewniczak
10788b58a21SSzymon Olewniczak    /**
1083aad9612SMichael Grosse     * @return string the page's ID
1093aad9612SMichael Grosse     */
110*d6d97f60SAnna Dabrowska    public function getPid()
111*d6d97f60SAnna Dabrowska    {
1123aad9612SMichael Grosse        return $this->pid;
1133aad9612SMichael Grosse    }
1143aad9612SMichael Grosse}
115