1<?php 2 3namespace dokuwiki\plugin\struct\meta; 4 5/** 6 * Class AccessTableData 7 * @package dokuwiki\plugin\struct\meta 8 * 9 * This class is for accessing the data stored for a page in a schema 10 * 11 */ 12class AccessTablePage extends AccessTable 13{ 14 15 const DEFAULT_PAGE_RID = 0; 16 17 public function __construct($schema, $pid, $ts = 0, $rid = 0) 18 { 19 $ts = $ts ?: time(); 20 parent::__construct($schema, $pid, $ts, $rid); 21 } 22 23 /** 24 * adds an empty data set for this schema and page 25 * 26 * This is basically a delete for the schema fields of a page 27 * 28 * @return bool 29 */ 30 public function clearData() 31 { 32 $data = array(); 33 34 foreach ($this->schema->getColumns() as $col) { 35 if ($col->isMulti()) { 36 $data[$col->getLabel()] = array(); 37 } else { 38 $data[$col->getLabel()] = null; 39 } 40 } 41 42 return $this->saveData($data); 43 } 44 45 /** 46 * @return int 47 */ 48 protected function getLastRevisionTimestamp() 49 { 50 $table = 'data_' . $this->schema->getTable(); 51 $where = "WHERE pid = ?"; 52 $opts = array($this->pid); 53 if ($this->ts) { 54 $where .= " AND rev <= ?"; 55 $opts[] = $this->ts; 56 } 57 58 /** @noinspection SqlResolve */ 59 $sql = "SELECT rev FROM $table $where ORDER BY rev DESC LIMIT 1"; 60 $res = $this->sqlite->query($sql, $opts); 61 $ret = (int) $this->sqlite->res2single($res); 62 $this->sqlite->res_close($res); 63 return $ret; 64 } 65 66 /** 67 * @inheritDoc 68 */ 69 protected function validateTypeData($data) 70 { 71 if ($this->ts == 0) { 72 throw new StructException("Saving with zero timestamp does not work."); 73 } 74 return true; 75 } 76 77 /** 78 * Remove latest status from previous data 79 */ 80 protected function beforeSave() 81 { 82 /** @noinspection SqlResolve */ 83 $ok = $this->sqlite->query( 84 "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ?", 85 [$this->pid] 86 ); 87 /** @noinspection SqlResolve */ 88 return $ok && $this->sqlite->query( 89 "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ?", 90 [$this->pid] 91 ); 92 } 93 94 /** 95 * @inheritDoc 96 */ 97 protected function getSingleNoninputCols() 98 { 99 return ['rid, pid, rev, latest']; 100 } 101 102 /** 103 * @inheritDoc 104 */ 105 protected function getSingleNoninputValues() 106 { 107 return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1]; 108 } 109 110 /** 111 * @inheritDoc 112 */ 113 protected function getMultiSql() 114 { 115 /** @noinspection SqlResolve */ 116 return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)"; 117 } 118 119 /** 120 * @inheritDoc 121 */ 122 protected function getMultiNoninputValues() 123 { 124 return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID]; 125 } 126} 127