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 /** 46308cc83fSAndreas Gohr * @return int 47308cc83fSAndreas Gohr */ 48308cc83fSAndreas Gohr protected function getLastRevisionTimestamp() 49308cc83fSAndreas Gohr { 50308cc83fSAndreas Gohr $table = 'data_' . $this->schema->getTable(); 51308cc83fSAndreas Gohr $where = "WHERE pid = ?"; 52308cc83fSAndreas Gohr $opts = array($this->pid); 53308cc83fSAndreas Gohr if ($this->ts) { 54308cc83fSAndreas Gohr $where .= " 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); 61308cc83fSAndreas Gohr $ret = (int) $this->sqlite->res2single($res); 62308cc83fSAndreas Gohr $this->sqlite->res_close($res); 63308cc83fSAndreas Gohr return $ret; 64308cc83fSAndreas Gohr } 65308cc83fSAndreas Gohr 66308cc83fSAndreas Gohr /** 67308cc83fSAndreas Gohr * @inheritDoc 68308cc83fSAndreas Gohr */ 69308cc83fSAndreas Gohr protected function validateTypeData($data) 70308cc83fSAndreas Gohr { 71308cc83fSAndreas Gohr if ($this->ts == 0) { 72308cc83fSAndreas Gohr throw new StructException("Saving with zero timestamp does not work."); 73308cc83fSAndreas Gohr } 74308cc83fSAndreas Gohr return true; 75308cc83fSAndreas Gohr } 76308cc83fSAndreas Gohr 77308cc83fSAndreas Gohr /** 78*9e5ba324SAnna Dabrowska * Remove latest status from previous page data 79308cc83fSAndreas Gohr */ 80308cc83fSAndreas Gohr protected function beforeSave() 81308cc83fSAndreas Gohr { 82308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 83308cc83fSAndreas Gohr $ok = $this->sqlite->query( 84*9e5ba324SAnna Dabrowska "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0", 85308cc83fSAndreas Gohr [$this->pid] 86308cc83fSAndreas Gohr ); 87308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 88308cc83fSAndreas Gohr return $ok && $this->sqlite->query( 89*9e5ba324SAnna Dabrowska "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0", 90308cc83fSAndreas Gohr [$this->pid] 91308cc83fSAndreas Gohr ); 92308cc83fSAndreas Gohr } 93308cc83fSAndreas Gohr 94308cc83fSAndreas Gohr /** 95308cc83fSAndreas Gohr * @inheritDoc 96308cc83fSAndreas Gohr */ 97308cc83fSAndreas Gohr protected function getSingleNoninputCols() 98308cc83fSAndreas Gohr { 99308cc83fSAndreas Gohr return ['rid, pid, rev, latest']; 100308cc83fSAndreas Gohr } 101308cc83fSAndreas Gohr 102308cc83fSAndreas Gohr /** 103308cc83fSAndreas Gohr * @inheritDoc 104308cc83fSAndreas Gohr */ 105308cc83fSAndreas Gohr protected function getSingleNoninputValues() 106308cc83fSAndreas Gohr { 107308cc83fSAndreas Gohr return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1]; 108308cc83fSAndreas Gohr } 109308cc83fSAndreas Gohr 110308cc83fSAndreas Gohr /** 111308cc83fSAndreas Gohr * @inheritDoc 112308cc83fSAndreas Gohr */ 113308cc83fSAndreas Gohr protected function getMultiSql() 114308cc83fSAndreas Gohr { 115308cc83fSAndreas Gohr /** @noinspection SqlResolve */ 116308cc83fSAndreas Gohr return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)"; 117308cc83fSAndreas Gohr } 118308cc83fSAndreas Gohr 119308cc83fSAndreas Gohr /** 120308cc83fSAndreas Gohr * @inheritDoc 121308cc83fSAndreas Gohr */ 122308cc83fSAndreas Gohr protected function getMultiNoninputValues() 123308cc83fSAndreas Gohr { 124308cc83fSAndreas Gohr return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID]; 125308cc83fSAndreas Gohr } 126308cc83fSAndreas Gohr} 127