xref: /plugin/struct/meta/AccessTableGlobal.php (revision b8d3247dc56feb4b81d8c7df61a4214dd02a662f)
1308cc83fSAndreas Gohr<?php
2308cc83fSAndreas Gohr
3308cc83fSAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
4308cc83fSAndreas Gohr
5308cc83fSAndreas Gohr/**
6308cc83fSAndreas Gohr * Class AccessTableGlobal
7308cc83fSAndreas Gohr *
8308cc83fSAndreas Gohr * Load and (more importantly) save data for Global Schemas
9308cc83fSAndreas Gohr *
10308cc83fSAndreas Gohr * @package dokuwiki\plugin\struct\meta
11308cc83fSAndreas Gohr */
12308cc83fSAndreas Gohrclass AccessTableGlobal extends AccessTable
13308cc83fSAndreas Gohr{
14308cc83fSAndreas Gohr    public function __construct($table, $pid, $ts = 0, $rid = 0)
15308cc83fSAndreas Gohr    {
16308cc83fSAndreas Gohr        parent::__construct($table, $pid, $ts, $rid);
17308cc83fSAndreas Gohr    }
18308cc83fSAndreas Gohr
19308cc83fSAndreas Gohr    /**
20308cc83fSAndreas Gohr     * Remove the current data
21308cc83fSAndreas Gohr     */
22308cc83fSAndreas Gohr    public function clearData()
23308cc83fSAndreas Gohr    {
24308cc83fSAndreas Gohr        if (!$this->rid) return; // no data
25308cc83fSAndreas Gohr
26308cc83fSAndreas Gohr        /** @noinspection SqlResolve */
2784ce8119SAndreas Gohr        $sql = 'DELETE FROM data_' . $this->schema->getTable() . ' WHERE rid = ?';
2884ce8119SAndreas Gohr        $this->sqlite->query($sql, $this->rid);
2984ce8119SAndreas Gohr        $sql = 'DELETE FROM multi_' . $this->schema->getTable() . ' WHERE rid = ?';
3084ce8119SAndreas Gohr        $this->sqlite->query($sql, $this->rid);
31308cc83fSAndreas Gohr    }
32308cc83fSAndreas Gohr
3384ce8119SAndreas Gohr    /**
3484ce8119SAndreas Gohr     * @inheritDoc
3584ce8119SAndreas Gohr     */
36308cc83fSAndreas Gohr    protected function getLastRevisionTimestamp()
37308cc83fSAndreas Gohr    {
38308cc83fSAndreas Gohr        return 0;
39308cc83fSAndreas Gohr    }
40308cc83fSAndreas Gohr
41308cc83fSAndreas Gohr    /**
42308cc83fSAndreas Gohr     * @inheritDoc
43308cc83fSAndreas Gohr     */
44308cc83fSAndreas Gohr    protected function buildGetDataSQL($idColumn = 'rid')
45308cc83fSAndreas Gohr    {
46308cc83fSAndreas Gohr        return parent::buildGetDataSQL($idColumn);
47308cc83fSAndreas Gohr    }
48308cc83fSAndreas Gohr
49308cc83fSAndreas Gohr    /**
50308cc83fSAndreas Gohr     * @inheritDoc
51308cc83fSAndreas Gohr     */
52308cc83fSAndreas Gohr    protected function getSingleSql()
53308cc83fSAndreas Gohr    {
54308cc83fSAndreas Gohr        $cols = array_merge($this->getSingleNoninputCols(), $this->singleCols);
557234bfb1Ssplitbrain        $cols = implode(',', $cols);
567234bfb1Ssplitbrain
57308cc83fSAndreas Gohr        $vals = array_merge($this->getSingleNoninputValues(), $this->singleValues);
58308cc83fSAndreas Gohr        $rid = $this->getRid() ?: "(SELECT (COALESCE(MAX(rid), 0 ) + 1) FROM $this->stable)";
59308cc83fSAndreas Gohr
6017a3a578SAndreas Gohr        return "REPLACE INTO $this->stable (rid, $cols)
6117a3a578SAndreas Gohr                      VALUES ($rid," . trim(str_repeat('?,', count($vals)), ',') . ');';
62308cc83fSAndreas Gohr    }
63308cc83fSAndreas Gohr
64308cc83fSAndreas Gohr    /**
65308cc83fSAndreas Gohr     * @inheritDoc
66308cc83fSAndreas Gohr     */
67308cc83fSAndreas Gohr    protected function getMultiSql()
68308cc83fSAndreas Gohr    {
69308cc83fSAndreas Gohr        return "REPLACE INTO $this->mtable (pid, rid, rev, latest, colref, row, value) VALUES (?,?,?,?,?,?,?)";
70308cc83fSAndreas Gohr    }
71308cc83fSAndreas Gohr
72308cc83fSAndreas Gohr    /**
73308cc83fSAndreas Gohr     * @inheritDoc
74308cc83fSAndreas Gohr     */
75308cc83fSAndreas Gohr    protected function validateTypeData($data)
76308cc83fSAndreas Gohr    {
77308cc83fSAndreas Gohr        // we do not store completely empty rows
78*b8d3247dSAnna Dabrowska        $isempty = array_reduce(
79*b8d3247dSAnna Dabrowska            $data,
80*b8d3247dSAnna Dabrowska            static fn($isempty, $cell) => $isempty && ($cell === '' || $cell === [] || $cell === null),
81*b8d3247dSAnna Dabrowska            true
82*b8d3247dSAnna Dabrowska        );
83308cc83fSAndreas Gohr
84308cc83fSAndreas Gohr        return !$isempty;
85308cc83fSAndreas Gohr    }
86308cc83fSAndreas Gohr
87308cc83fSAndreas Gohr    /**
88308cc83fSAndreas Gohr     * @inheritDoc
89308cc83fSAndreas Gohr     */
90308cc83fSAndreas Gohr    protected function getSingleNoninputCols()
91308cc83fSAndreas Gohr    {
92308cc83fSAndreas Gohr        return ['pid', 'rev', 'latest'];
93308cc83fSAndreas Gohr    }
94308cc83fSAndreas Gohr
95308cc83fSAndreas Gohr    /**
96308cc83fSAndreas Gohr     * @inheritDoc
97308cc83fSAndreas Gohr     */
98308cc83fSAndreas Gohr    protected function getSingleNoninputValues()
99308cc83fSAndreas Gohr    {
100308cc83fSAndreas Gohr        return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST];
101308cc83fSAndreas Gohr    }
102308cc83fSAndreas Gohr
103308cc83fSAndreas Gohr    /**
104308cc83fSAndreas Gohr     * @inheritDoc
105308cc83fSAndreas Gohr     */
106308cc83fSAndreas Gohr    protected function getMultiNoninputValues()
107308cc83fSAndreas Gohr    {
108308cc83fSAndreas Gohr        return [$this->pid, $this->rid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST];
109308cc83fSAndreas Gohr    }
110308cc83fSAndreas Gohr
111308cc83fSAndreas Gohr    /**
112308cc83fSAndreas Gohr     * Set new rid if this is a new insert
113308cc83fSAndreas Gohr     * @return bool
114308cc83fSAndreas Gohr     */
115308cc83fSAndreas Gohr    protected function afterSingleSave()
116308cc83fSAndreas Gohr    {
117308cc83fSAndreas Gohr        $ok = true;
118308cc83fSAndreas Gohr        if (!$this->rid) {
11979b29326SAnna Dabrowska            $this->rid = $this->sqlite->queryValue("SELECT rid FROM $this->stable WHERE ROWID = last_insert_rowid()");
120308cc83fSAndreas Gohr            if (!$this->rid) {
121308cc83fSAndreas Gohr                $ok = false;
122308cc83fSAndreas Gohr            }
123308cc83fSAndreas Gohr        }
124a09ff24aSAnna Dabrowska
125a09ff24aSAnna Dabrowska        // FIXME this might replace handleEmptyMulti() but would it always be safe? in remote API context?
126a09ff24aSAnna Dabrowska        if (!empty($this->multiValues)) {
127a09ff24aSAnna Dabrowska            $ok = $ok && $this->clearMulti();
128a09ff24aSAnna Dabrowska        }
129a09ff24aSAnna Dabrowska
130308cc83fSAndreas Gohr        return $ok;
131308cc83fSAndreas Gohr    }
132d680cb37SAnna Dabrowska
133d680cb37SAnna Dabrowska    /**
134d680cb37SAnna Dabrowska     * Add an optional query to clear any previous multi values if the first one is empty.
135d680cb37SAnna Dabrowska     * Allows for deleting multi values from the inline editor.
136d680cb37SAnna Dabrowska     *
137d680cb37SAnna Dabrowska     * @param string $pid
138d680cb37SAnna Dabrowska     * @param int $rid
139d680cb37SAnna Dabrowska     * @param int $colref
140d680cb37SAnna Dabrowska     */
141d680cb37SAnna Dabrowska    protected function handleEmptyMulti($pid, $rid, $colref)
142d680cb37SAnna Dabrowska    {
143438a804cSAnna Dabrowska        $table = 'multi_' . $this->schema->getTable();
144d680cb37SAnna Dabrowska        $this->optQueries[] = [
145438a804cSAnna Dabrowska            "DELETE FROM $table WHERE pid = ? AND rid = ? AND colref = ?",
146438a804cSAnna Dabrowska            $pid, $rid, $colref
147d680cb37SAnna Dabrowska        ];
148d680cb37SAnna Dabrowska    }
149308cc83fSAndreas Gohr}
150