xref: /plugin/struct/meta/AccessTableGlobal.php (revision 84ce8119d0320eb3f7194fe801fa96fb60fe42f1)
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 */
27*84ce8119SAndreas Gohr        $sql = 'DELETE FROM data_' . $this->schema->getTable() . ' WHERE rid = ?';
28*84ce8119SAndreas Gohr        $this->sqlite->query($sql, $this->rid);
29*84ce8119SAndreas Gohr        $sql = 'DELETE FROM multi_' . $this->schema->getTable() . ' WHERE rid = ?';
30*84ce8119SAndreas Gohr        $this->sqlite->query($sql, $this->rid);
31308cc83fSAndreas Gohr    }
32308cc83fSAndreas Gohr
33*84ce8119SAndreas Gohr    /**
34*84ce8119SAndreas Gohr     * @inheritDoc
35*84ce8119SAndreas 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);
55308cc83fSAndreas Gohr        $cols = join(',', $cols);
56308cc83fSAndreas Gohr        $vals = array_merge($this->getSingleNoninputValues(), $this->singleValues);
57308cc83fSAndreas Gohr        $rid = $this->getRid() ?: "(SELECT (COALESCE(MAX(rid), 0 ) + 1) FROM $this->stable)";
58308cc83fSAndreas Gohr
5917a3a578SAndreas Gohr        return "REPLACE INTO $this->stable (rid, $cols)
6017a3a578SAndreas Gohr                      VALUES ($rid," . trim(str_repeat('?,', count($vals)), ',') . ');';
61308cc83fSAndreas Gohr    }
62308cc83fSAndreas Gohr
63308cc83fSAndreas Gohr    /**
64308cc83fSAndreas Gohr     * @inheritDoc
65308cc83fSAndreas Gohr     */
66308cc83fSAndreas Gohr    protected function getMultiSql()
67308cc83fSAndreas Gohr    {
68308cc83fSAndreas Gohr        return "REPLACE INTO $this->mtable (pid, rid, rev, latest, colref, row, value) VALUES (?,?,?,?,?,?,?)";
69308cc83fSAndreas Gohr    }
70308cc83fSAndreas Gohr
71308cc83fSAndreas Gohr    /**
72308cc83fSAndreas Gohr     * @inheritDoc
73308cc83fSAndreas Gohr     */
74308cc83fSAndreas Gohr    protected function validateTypeData($data)
75308cc83fSAndreas Gohr    {
76308cc83fSAndreas Gohr        // we do not store completely empty rows
77308cc83fSAndreas Gohr        $isempty = array_reduce($data, function ($isempty, $cell) {
78308cc83fSAndreas Gohr            return $isempty && ($cell === '' || $cell === [] || $cell === null);
79308cc83fSAndreas Gohr        }, true);
80308cc83fSAndreas Gohr
81308cc83fSAndreas Gohr        return !$isempty;
82308cc83fSAndreas Gohr    }
83308cc83fSAndreas Gohr
84308cc83fSAndreas Gohr    /**
85308cc83fSAndreas Gohr     * @inheritDoc
86308cc83fSAndreas Gohr     */
87308cc83fSAndreas Gohr    protected function getSingleNoninputCols()
88308cc83fSAndreas Gohr    {
89308cc83fSAndreas Gohr        return ['pid', 'rev', 'latest'];
90308cc83fSAndreas Gohr    }
91308cc83fSAndreas Gohr
92308cc83fSAndreas Gohr    /**
93308cc83fSAndreas Gohr     * @inheritDoc
94308cc83fSAndreas Gohr     */
95308cc83fSAndreas Gohr    protected function getSingleNoninputValues()
96308cc83fSAndreas Gohr    {
97308cc83fSAndreas Gohr        return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST];
98308cc83fSAndreas Gohr    }
99308cc83fSAndreas Gohr
100308cc83fSAndreas Gohr    /**
101308cc83fSAndreas Gohr     * @inheritDoc
102308cc83fSAndreas Gohr     */
103308cc83fSAndreas Gohr    protected function getMultiNoninputValues()
104308cc83fSAndreas Gohr    {
105308cc83fSAndreas Gohr        return [$this->pid, $this->rid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST];
106308cc83fSAndreas Gohr    }
107308cc83fSAndreas Gohr
108308cc83fSAndreas Gohr    /**
109308cc83fSAndreas Gohr     * Set new rid if this is a new insert
110308cc83fSAndreas Gohr     * @return bool
111308cc83fSAndreas Gohr     */
112308cc83fSAndreas Gohr    protected function afterSingleSave()
113308cc83fSAndreas Gohr    {
114308cc83fSAndreas Gohr        $ok = true;
115308cc83fSAndreas Gohr        if (!$this->rid) {
11679b29326SAnna Dabrowska            $this->rid = $this->sqlite->queryValue("SELECT rid FROM $this->stable WHERE ROWID = last_insert_rowid()");
117308cc83fSAndreas Gohr            if (!$this->rid) {
118308cc83fSAndreas Gohr                $ok = false;
119308cc83fSAndreas Gohr            }
120308cc83fSAndreas Gohr        }
121a09ff24aSAnna Dabrowska
122a09ff24aSAnna Dabrowska        // FIXME this might replace handleEmptyMulti() but would it always be safe? in remote API context?
123a09ff24aSAnna Dabrowska        if (!empty($this->multiValues)) {
124a09ff24aSAnna Dabrowska            $ok = $ok && $this->clearMulti();
125a09ff24aSAnna Dabrowska        }
126a09ff24aSAnna Dabrowska
127308cc83fSAndreas Gohr        return $ok;
128308cc83fSAndreas Gohr    }
129d680cb37SAnna Dabrowska
130d680cb37SAnna Dabrowska    /**
131d680cb37SAnna Dabrowska     * Add an optional query to clear any previous multi values if the first one is empty.
132d680cb37SAnna Dabrowska     * Allows for deleting multi values from the inline editor.
133d680cb37SAnna Dabrowska     *
134d680cb37SAnna Dabrowska     * @param string $pid
135d680cb37SAnna Dabrowska     * @param int $rid
136d680cb37SAnna Dabrowska     * @param int $colref
137d680cb37SAnna Dabrowska     */
138d680cb37SAnna Dabrowska    protected function handleEmptyMulti($pid, $rid, $colref)
139d680cb37SAnna Dabrowska    {
140438a804cSAnna Dabrowska        $table = 'multi_' . $this->schema->getTable();
141d680cb37SAnna Dabrowska        $this->optQueries[] = [
142438a804cSAnna Dabrowska            "DELETE FROM $table WHERE pid = ? AND rid = ? AND colref = ?",
143438a804cSAnna Dabrowska            $pid, $rid, $colref
144d680cb37SAnna Dabrowska        ];
145d680cb37SAnna Dabrowska    }
146308cc83fSAndreas Gohr}
147