xref: /plugin/struct/meta/AccessTableGlobal.php (revision a09ff24ac13f25af7394128ac2058efadce2a366)
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 */
27308cc83fSAndreas Gohr        $sql = 'DELETE FROM ? WHERE rid = ?';
28308cc83fSAndreas Gohr        $this->sqlite->query($sql, 'data_' . $this->schema->getTable(), $this->rid);
29308cc83fSAndreas Gohr        $this->sqlite->query($sql, 'multi_' . $this->schema->getTable(), $this->rid);
30308cc83fSAndreas Gohr    }
31308cc83fSAndreas Gohr
32308cc83fSAndreas Gohr    protected function getLastRevisionTimestamp()
33308cc83fSAndreas Gohr    {
34308cc83fSAndreas Gohr        return 0;
35308cc83fSAndreas Gohr    }
36308cc83fSAndreas Gohr
37308cc83fSAndreas Gohr    /**
38308cc83fSAndreas Gohr     * @inheritDoc
39308cc83fSAndreas Gohr     */
40308cc83fSAndreas Gohr    protected function buildGetDataSQL($idColumn = 'rid')
41308cc83fSAndreas Gohr    {
42308cc83fSAndreas Gohr        return parent::buildGetDataSQL($idColumn);
43308cc83fSAndreas Gohr    }
44308cc83fSAndreas Gohr
45308cc83fSAndreas Gohr    /**
46308cc83fSAndreas Gohr     * @inheritDoc
47308cc83fSAndreas Gohr     */
48308cc83fSAndreas Gohr    protected function getSingleSql()
49308cc83fSAndreas Gohr    {
50308cc83fSAndreas Gohr        $cols = array_merge($this->getSingleNoninputCols(), $this->singleCols);
51308cc83fSAndreas Gohr        $cols = join(',', $cols);
52308cc83fSAndreas Gohr        $vals = array_merge($this->getSingleNoninputValues(), $this->singleValues);
53308cc83fSAndreas Gohr        $rid = $this->getRid() ?: "(SELECT (COALESCE(MAX(rid), 0 ) + 1) FROM $this->stable)";
54308cc83fSAndreas Gohr
55308cc83fSAndreas Gohr        return "REPLACE INTO $this->stable (rid, $cols) VALUES ($rid," . trim(str_repeat('?,', count($vals)), ',') . ');';
56308cc83fSAndreas Gohr    }
57308cc83fSAndreas Gohr
58308cc83fSAndreas Gohr    /**
59308cc83fSAndreas Gohr     * @inheritDoc
60308cc83fSAndreas Gohr     */
61308cc83fSAndreas Gohr    protected function getMultiSql()
62308cc83fSAndreas Gohr    {
63308cc83fSAndreas Gohr        return "REPLACE INTO $this->mtable (pid, rid, rev, latest, colref, row, value) VALUES (?,?,?,?,?,?,?)";
64308cc83fSAndreas Gohr    }
65308cc83fSAndreas Gohr
66308cc83fSAndreas Gohr    /**
67308cc83fSAndreas Gohr     * @inheritDoc
68308cc83fSAndreas Gohr     */
69308cc83fSAndreas Gohr    protected function validateTypeData($data)
70308cc83fSAndreas Gohr    {
71308cc83fSAndreas Gohr        // we do not store completely empty rows
72308cc83fSAndreas Gohr        $isempty = array_reduce($data, function ($isempty, $cell) {
73308cc83fSAndreas Gohr            return $isempty && ($cell === '' || $cell === [] || $cell === null);
74308cc83fSAndreas Gohr        }, true);
75308cc83fSAndreas Gohr
76308cc83fSAndreas Gohr        return !$isempty;
77308cc83fSAndreas Gohr    }
78308cc83fSAndreas Gohr
79308cc83fSAndreas Gohr    /**
80308cc83fSAndreas Gohr     * @inheritDoc
81308cc83fSAndreas Gohr     */
82308cc83fSAndreas Gohr    protected function getSingleNoninputCols()
83308cc83fSAndreas Gohr    {
84308cc83fSAndreas Gohr        return ['pid', 'rev', 'latest'];
85308cc83fSAndreas Gohr    }
86308cc83fSAndreas Gohr
87308cc83fSAndreas Gohr    /**
88308cc83fSAndreas Gohr     * @inheritDoc
89308cc83fSAndreas Gohr     */
90308cc83fSAndreas Gohr    protected function getSingleNoninputValues()
91308cc83fSAndreas Gohr    {
92308cc83fSAndreas Gohr        return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST];
93308cc83fSAndreas Gohr    }
94308cc83fSAndreas Gohr
95308cc83fSAndreas Gohr    /**
96308cc83fSAndreas Gohr     * @inheritDoc
97308cc83fSAndreas Gohr     */
98308cc83fSAndreas Gohr    protected function getMultiNoninputValues()
99308cc83fSAndreas Gohr    {
100308cc83fSAndreas Gohr        return [$this->pid, $this->rid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST];
101308cc83fSAndreas Gohr    }
102308cc83fSAndreas Gohr
103308cc83fSAndreas Gohr    /**
104308cc83fSAndreas Gohr     * Set new rid if this is a new insert
105308cc83fSAndreas Gohr
106308cc83fSAndreas Gohr     * @return bool
107308cc83fSAndreas Gohr     */
108308cc83fSAndreas Gohr    protected function afterSingleSave()
109308cc83fSAndreas Gohr    {
110308cc83fSAndreas Gohr        $ok = true;
111308cc83fSAndreas Gohr        if (!$this->rid) {
112308cc83fSAndreas Gohr            $res = $this->sqlite->query("SELECT rid FROM $this->stable WHERE ROWID = last_insert_rowid()");
113308cc83fSAndreas Gohr            $this->rid = $this->sqlite->res2single($res);
114308cc83fSAndreas Gohr            $this->sqlite->res_close($res);
115308cc83fSAndreas Gohr            if (!$this->rid) {
116308cc83fSAndreas Gohr                $ok = false;
117308cc83fSAndreas Gohr            }
118308cc83fSAndreas Gohr        }
119*a09ff24aSAnna Dabrowska
120*a09ff24aSAnna Dabrowska        // FIXME this might replace handleEmptyMulti() but would it always be safe? in remote API context?
121*a09ff24aSAnna Dabrowska        if (!empty($this->multiValues)) {
122*a09ff24aSAnna Dabrowska            $ok = $ok && $this->clearMulti();
123*a09ff24aSAnna Dabrowska        }
124*a09ff24aSAnna Dabrowska
125308cc83fSAndreas Gohr        return $ok;
126308cc83fSAndreas Gohr    }
127d680cb37SAnna Dabrowska
128d680cb37SAnna Dabrowska    /**
129d680cb37SAnna Dabrowska     * Add an optional query to clear any previous multi values if the first one is empty.
130d680cb37SAnna Dabrowska     * Allows for deleting multi values from the inline editor.
131d680cb37SAnna Dabrowska     *
132d680cb37SAnna Dabrowska     * @param string $pid
133d680cb37SAnna Dabrowska     * @param int $rid
134d680cb37SAnna Dabrowska     * @param int $colref
135d680cb37SAnna Dabrowska     */
136d680cb37SAnna Dabrowska    protected function handleEmptyMulti($pid, $rid, $colref)
137d680cb37SAnna Dabrowska    {
138d680cb37SAnna Dabrowska        $this->optQueries[] = [
139d680cb37SAnna Dabrowska            "DELETE FROM ? WHERE pid = ? AND rid = ? AND colref = ?",
140d680cb37SAnna Dabrowska            'multi_' . $this->schema->getTable(), $pid, $rid, $colref
141d680cb37SAnna Dabrowska        ];
142d680cb37SAnna Dabrowska    }
143308cc83fSAndreas Gohr}
144