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 5517a3a578SAndreas Gohr return "REPLACE INTO $this->stable (rid, $cols) 5617a3a578SAndreas Gohr VALUES ($rid," . trim(str_repeat('?,', count($vals)), ',') . ');'; 57308cc83fSAndreas Gohr } 58308cc83fSAndreas Gohr 59308cc83fSAndreas Gohr /** 60308cc83fSAndreas Gohr * @inheritDoc 61308cc83fSAndreas Gohr */ 62308cc83fSAndreas Gohr protected function getMultiSql() 63308cc83fSAndreas Gohr { 64308cc83fSAndreas Gohr return "REPLACE INTO $this->mtable (pid, rid, rev, latest, colref, row, value) VALUES (?,?,?,?,?,?,?)"; 65308cc83fSAndreas Gohr } 66308cc83fSAndreas Gohr 67308cc83fSAndreas Gohr /** 68308cc83fSAndreas Gohr * @inheritDoc 69308cc83fSAndreas Gohr */ 70308cc83fSAndreas Gohr protected function validateTypeData($data) 71308cc83fSAndreas Gohr { 72308cc83fSAndreas Gohr // we do not store completely empty rows 73308cc83fSAndreas Gohr $isempty = array_reduce($data, function ($isempty, $cell) { 74308cc83fSAndreas Gohr return $isempty && ($cell === '' || $cell === [] || $cell === null); 75308cc83fSAndreas Gohr }, true); 76308cc83fSAndreas Gohr 77308cc83fSAndreas Gohr return !$isempty; 78308cc83fSAndreas Gohr } 79308cc83fSAndreas Gohr 80308cc83fSAndreas Gohr /** 81308cc83fSAndreas Gohr * @inheritDoc 82308cc83fSAndreas Gohr */ 83308cc83fSAndreas Gohr protected function getSingleNoninputCols() 84308cc83fSAndreas Gohr { 85308cc83fSAndreas Gohr return ['pid', 'rev', 'latest']; 86308cc83fSAndreas Gohr } 87308cc83fSAndreas Gohr 88308cc83fSAndreas Gohr /** 89308cc83fSAndreas Gohr * @inheritDoc 90308cc83fSAndreas Gohr */ 91308cc83fSAndreas Gohr protected function getSingleNoninputValues() 92308cc83fSAndreas Gohr { 93308cc83fSAndreas Gohr return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST]; 94308cc83fSAndreas Gohr } 95308cc83fSAndreas Gohr 96308cc83fSAndreas Gohr /** 97308cc83fSAndreas Gohr * @inheritDoc 98308cc83fSAndreas Gohr */ 99308cc83fSAndreas Gohr protected function getMultiNoninputValues() 100308cc83fSAndreas Gohr { 101308cc83fSAndreas Gohr return [$this->pid, $this->rid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST]; 102308cc83fSAndreas Gohr } 103308cc83fSAndreas Gohr 104308cc83fSAndreas Gohr /** 105308cc83fSAndreas Gohr * Set new rid if this is a new insert 106308cc83fSAndreas Gohr * @return bool 107308cc83fSAndreas Gohr */ 108308cc83fSAndreas Gohr protected function afterSingleSave() 109308cc83fSAndreas Gohr { 110308cc83fSAndreas Gohr $ok = true; 111308cc83fSAndreas Gohr if (!$this->rid) { 112*79b29326SAnna Dabrowska $this->rid = $this->sqlite->queryValue("SELECT rid FROM $this->stable WHERE ROWID = last_insert_rowid()"); 113308cc83fSAndreas Gohr if (!$this->rid) { 114308cc83fSAndreas Gohr $ok = false; 115308cc83fSAndreas Gohr } 116308cc83fSAndreas Gohr } 117a09ff24aSAnna Dabrowska 118a09ff24aSAnna Dabrowska // FIXME this might replace handleEmptyMulti() but would it always be safe? in remote API context? 119a09ff24aSAnna Dabrowska if (!empty($this->multiValues)) { 120a09ff24aSAnna Dabrowska $ok = $ok && $this->clearMulti(); 121a09ff24aSAnna Dabrowska } 122a09ff24aSAnna Dabrowska 123308cc83fSAndreas Gohr return $ok; 124308cc83fSAndreas Gohr } 125d680cb37SAnna Dabrowska 126d680cb37SAnna Dabrowska /** 127d680cb37SAnna Dabrowska * Add an optional query to clear any previous multi values if the first one is empty. 128d680cb37SAnna Dabrowska * Allows for deleting multi values from the inline editor. 129d680cb37SAnna Dabrowska * 130d680cb37SAnna Dabrowska * @param string $pid 131d680cb37SAnna Dabrowska * @param int $rid 132d680cb37SAnna Dabrowska * @param int $colref 133d680cb37SAnna Dabrowska */ 134d680cb37SAnna Dabrowska protected function handleEmptyMulti($pid, $rid, $colref) 135d680cb37SAnna Dabrowska { 136d680cb37SAnna Dabrowska $this->optQueries[] = [ 137d680cb37SAnna Dabrowska "DELETE FROM ? WHERE pid = ? AND rid = ? AND colref = ?", 138d680cb37SAnna Dabrowska 'multi_' . $this->schema->getTable(), $pid, $rid, $colref 139d680cb37SAnna Dabrowska ]; 140d680cb37SAnna Dabrowska } 141308cc83fSAndreas Gohr} 142