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); 55*7234bfb1Ssplitbrain $cols = implode(',', $cols); 56*7234bfb1Ssplitbrain 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 78308cc83fSAndreas Gohr $isempty = array_reduce($data, function ($isempty, $cell) { 79308cc83fSAndreas Gohr return $isempty && ($cell === '' || $cell === [] || $cell === null); 80308cc83fSAndreas Gohr }, true); 81308cc83fSAndreas Gohr 82308cc83fSAndreas Gohr return !$isempty; 83308cc83fSAndreas Gohr } 84308cc83fSAndreas Gohr 85308cc83fSAndreas Gohr /** 86308cc83fSAndreas Gohr * @inheritDoc 87308cc83fSAndreas Gohr */ 88308cc83fSAndreas Gohr protected function getSingleNoninputCols() 89308cc83fSAndreas Gohr { 90308cc83fSAndreas Gohr return ['pid', 'rev', 'latest']; 91308cc83fSAndreas Gohr } 92308cc83fSAndreas Gohr 93308cc83fSAndreas Gohr /** 94308cc83fSAndreas Gohr * @inheritDoc 95308cc83fSAndreas Gohr */ 96308cc83fSAndreas Gohr protected function getSingleNoninputValues() 97308cc83fSAndreas Gohr { 98308cc83fSAndreas Gohr return [$this->pid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST]; 99308cc83fSAndreas Gohr } 100308cc83fSAndreas Gohr 101308cc83fSAndreas Gohr /** 102308cc83fSAndreas Gohr * @inheritDoc 103308cc83fSAndreas Gohr */ 104308cc83fSAndreas Gohr protected function getMultiNoninputValues() 105308cc83fSAndreas Gohr { 106308cc83fSAndreas Gohr return [$this->pid, $this->rid, AccessTable::DEFAULT_REV, AccessTable::DEFAULT_LATEST]; 107308cc83fSAndreas Gohr } 108308cc83fSAndreas Gohr 109308cc83fSAndreas Gohr /** 110308cc83fSAndreas Gohr * Set new rid if this is a new insert 111308cc83fSAndreas Gohr * @return bool 112308cc83fSAndreas Gohr */ 113308cc83fSAndreas Gohr protected function afterSingleSave() 114308cc83fSAndreas Gohr { 115308cc83fSAndreas Gohr $ok = true; 116308cc83fSAndreas Gohr if (!$this->rid) { 11779b29326SAnna Dabrowska $this->rid = $this->sqlite->queryValue("SELECT rid FROM $this->stable WHERE ROWID = last_insert_rowid()"); 118308cc83fSAndreas Gohr if (!$this->rid) { 119308cc83fSAndreas Gohr $ok = false; 120308cc83fSAndreas Gohr } 121308cc83fSAndreas Gohr } 122a09ff24aSAnna Dabrowska 123a09ff24aSAnna Dabrowska // FIXME this might replace handleEmptyMulti() but would it always be safe? in remote API context? 124a09ff24aSAnna Dabrowska if (!empty($this->multiValues)) { 125a09ff24aSAnna Dabrowska $ok = $ok && $this->clearMulti(); 126a09ff24aSAnna Dabrowska } 127a09ff24aSAnna Dabrowska 128308cc83fSAndreas Gohr return $ok; 129308cc83fSAndreas Gohr } 130d680cb37SAnna Dabrowska 131d680cb37SAnna Dabrowska /** 132d680cb37SAnna Dabrowska * Add an optional query to clear any previous multi values if the first one is empty. 133d680cb37SAnna Dabrowska * Allows for deleting multi values from the inline editor. 134d680cb37SAnna Dabrowska * 135d680cb37SAnna Dabrowska * @param string $pid 136d680cb37SAnna Dabrowska * @param int $rid 137d680cb37SAnna Dabrowska * @param int $colref 138d680cb37SAnna Dabrowska */ 139d680cb37SAnna Dabrowska protected function handleEmptyMulti($pid, $rid, $colref) 140d680cb37SAnna Dabrowska { 141438a804cSAnna Dabrowska $table = 'multi_' . $this->schema->getTable(); 142d680cb37SAnna Dabrowska $this->optQueries[] = [ 143438a804cSAnna Dabrowska "DELETE FROM $table WHERE pid = ? AND rid = ? AND colref = ?", 144438a804cSAnna Dabrowska $pid, $rid, $colref 145d680cb37SAnna Dabrowska ]; 146d680cb37SAnna Dabrowska } 147308cc83fSAndreas Gohr} 148