1308cc83fSAndreas Gohr<?php 2308cc83fSAndreas Gohr 3308cc83fSAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4308cc83fSAndreas Gohr 5308cc83fSAndreas Gohr/** 6308cc83fSAndreas Gohr * Class AccessTableData 7308cc83fSAndreas Gohr * @package dokuwiki\plugin\struct\meta 8308cc83fSAndreas Gohr * 9308cc83fSAndreas Gohr * This class is for accessing the data stored for a page in a schema 10308cc83fSAndreas Gohr * 11308cc83fSAndreas Gohr */ 12308cc83fSAndreas Gohrclass AccessTablePage extends AccessTable 13308cc83fSAndreas Gohr{ 14308cc83fSAndreas Gohr 15308cc83fSAndreas Gohr const DEFAULT_PAGE_RID = 0; 16308cc83fSAndreas Gohr 17308cc83fSAndreas Gohr public function __construct($schema, $pid, $ts = 0, $rid = 0) 18308cc83fSAndreas Gohr { 19308cc83fSAndreas Gohr $ts = $ts ?: time(); 20308cc83fSAndreas Gohr parent::__construct($schema, $pid, $ts, $rid); 21308cc83fSAndreas Gohr } 22308cc83fSAndreas Gohr 23308cc83fSAndreas Gohr /** 24308cc83fSAndreas Gohr * adds an empty data set for this schema and page 25308cc83fSAndreas Gohr * 26308cc83fSAndreas Gohr * This is basically a delete for the schema fields of a page 27308cc83fSAndreas Gohr * 28308cc83fSAndreas Gohr * @return bool 29308cc83fSAndreas Gohr */ 30308cc83fSAndreas Gohr public function clearData() 31308cc83fSAndreas Gohr { 32308cc83fSAndreas Gohr $data = array(); 33308cc83fSAndreas Gohr 34308cc83fSAndreas Gohr foreach ($this->schema->getColumns() as $col) { 35308cc83fSAndreas Gohr if ($col->isMulti()) { 36308cc83fSAndreas Gohr $data[$col->getLabel()] = array(); 37308cc83fSAndreas Gohr } else { 38308cc83fSAndreas Gohr $data[$col->getLabel()] = null; 39308cc83fSAndreas Gohr } 40308cc83fSAndreas Gohr } 41308cc83fSAndreas Gohr 42308cc83fSAndreas Gohr return $this->saveData($data); 43308cc83fSAndreas Gohr } 44308cc83fSAndreas Gohr 45308cc83fSAndreas Gohr /** 46*25e910deSAnna Dabrowska * @return int|bool 47308cc83fSAndreas Gohr */ 48308cc83fSAndreas Gohr protected function getLastRevisionTimestamp() 49308cc83fSAndreas Gohr { 50308cc83fSAndreas Gohr $table = 'data_' . $this->schema->getTable(); 51308cc83fSAndreas Gohr $where = "WHERE pid = ?"; 52*25e910deSAnna Dabrowska $opts = [$this->pid]; 53308cc83fSAndreas Gohr if ($this->ts) { 54*25e910deSAnna Dabrowska $where .= " AND REV > 0 AND rev <= ?"; 55308cc83fSAndreas Gohr $opts[] = $this->ts; 56308cc83fSAndreas Gohr } 57308cc83fSAndreas Gohr 58308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 59308cc83fSAndreas Gohr $sql = "SELECT rev FROM $table $where ORDER BY rev DESC LIMIT 1"; 60308cc83fSAndreas Gohr $res = $this->sqlite->query($sql, $opts); 61*25e910deSAnna Dabrowska $ret = $this->sqlite->res2single($res); 62308cc83fSAndreas Gohr $this->sqlite->res_close($res); 63*25e910deSAnna Dabrowska // make sure we don't cast empty result to 0 (serial data has rev = 0) 64*25e910deSAnna Dabrowska if ($ret !== false) $ret = (int) $ret; 65308cc83fSAndreas Gohr return $ret; 66308cc83fSAndreas Gohr } 67308cc83fSAndreas Gohr 68308cc83fSAndreas Gohr /** 69308cc83fSAndreas Gohr * @inheritDoc 70308cc83fSAndreas Gohr */ 71308cc83fSAndreas Gohr protected function validateTypeData($data) 72308cc83fSAndreas Gohr { 73308cc83fSAndreas Gohr if ($this->ts == 0) { 74308cc83fSAndreas Gohr throw new StructException("Saving with zero timestamp does not work."); 75308cc83fSAndreas Gohr } 76308cc83fSAndreas Gohr return true; 77308cc83fSAndreas Gohr } 78308cc83fSAndreas Gohr 79308cc83fSAndreas Gohr /** 809e5ba324SAnna Dabrowska * Remove latest status from previous page data 81308cc83fSAndreas Gohr */ 82308cc83fSAndreas Gohr protected function beforeSave() 83308cc83fSAndreas Gohr { 84308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 85308cc83fSAndreas Gohr $ok = $this->sqlite->query( 869e5ba324SAnna Dabrowska "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0", 87308cc83fSAndreas Gohr [$this->pid] 88308cc83fSAndreas Gohr ); 89308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 90308cc83fSAndreas Gohr return $ok && $this->sqlite->query( 919e5ba324SAnna Dabrowska "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0", 92308cc83fSAndreas Gohr [$this->pid] 93308cc83fSAndreas Gohr ); 94308cc83fSAndreas Gohr } 95308cc83fSAndreas Gohr 96308cc83fSAndreas Gohr /** 97308cc83fSAndreas Gohr * @inheritDoc 98308cc83fSAndreas Gohr */ 99308cc83fSAndreas Gohr protected function getSingleNoninputCols() 100308cc83fSAndreas Gohr { 101308cc83fSAndreas Gohr return ['rid, pid, rev, latest']; 102308cc83fSAndreas Gohr } 103308cc83fSAndreas Gohr 104308cc83fSAndreas Gohr /** 105308cc83fSAndreas Gohr * @inheritDoc 106308cc83fSAndreas Gohr */ 107308cc83fSAndreas Gohr protected function getSingleNoninputValues() 108308cc83fSAndreas Gohr { 109308cc83fSAndreas Gohr return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1]; 110308cc83fSAndreas Gohr } 111308cc83fSAndreas Gohr 112308cc83fSAndreas Gohr /** 113308cc83fSAndreas Gohr * @inheritDoc 114308cc83fSAndreas Gohr */ 115308cc83fSAndreas Gohr protected function getMultiSql() 116308cc83fSAndreas Gohr { 117308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 118308cc83fSAndreas Gohr return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)"; 119308cc83fSAndreas Gohr } 120308cc83fSAndreas Gohr 121308cc83fSAndreas Gohr /** 122308cc83fSAndreas Gohr * @inheritDoc 123308cc83fSAndreas Gohr */ 124308cc83fSAndreas Gohr protected function getMultiNoninputValues() 125308cc83fSAndreas Gohr { 126308cc83fSAndreas Gohr return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID]; 127308cc83fSAndreas Gohr } 128308cc83fSAndreas Gohr} 129