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|bool 47 */ 48 protected function getLastRevisionTimestamp() 49 { 50 $table = 'data_' . $this->schema->getTable(); 51 $where = "WHERE pid = ?"; 52 $opts = [$this->pid]; 53 if ($this->ts) { 54 $where .= " AND REV > 0 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 = $this->sqlite->res2single($res); 62 $this->sqlite->res_close($res); 63 // make sure we don't cast empty result to 0 (serial data has rev = 0) 64 if ($ret !== false) $ret = (int) $ret; 65 return $ret; 66 } 67 68 /** 69 * @inheritDoc 70 */ 71 protected function validateTypeData($data) 72 { 73 if ($this->ts == 0) { 74 throw new StructException("Saving with zero timestamp does not work."); 75 } 76 return true; 77 } 78 79 /** 80 * Remove latest status from previous page data 81 */ 82 protected function beforeSave() 83 { 84 /** @noinspection SqlResolve */ 85 $ok = $this->sqlite->query( 86 "UPDATE $this->stable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0", 87 [$this->pid] 88 ); 89 /** @noinspection SqlResolve */ 90 return $ok && $this->sqlite->query( 91 "UPDATE $this->mtable SET latest = 0 WHERE latest = 1 AND pid = ? AND rid = 0", 92 [$this->pid] 93 ); 94 } 95 96 /** 97 * @inheritDoc 98 */ 99 protected function getSingleNoninputCols() 100 { 101 return ['rid, pid, rev, latest']; 102 } 103 104 /** 105 * @inheritDoc 106 */ 107 protected function getSingleNoninputValues() 108 { 109 return [self::DEFAULT_PAGE_RID, $this->pid, $this->ts, 1]; 110 } 111 112 /** 113 * @inheritDoc 114 */ 115 protected function getMultiSql() 116 { 117 /** @noinspection SqlResolve */ 118 return "INSERT INTO $this->mtable (latest, rev, pid, rid, colref, row, value) VALUES (?,?,?,?,?,?,?)"; 119 } 120 121 /** 122 * @inheritDoc 123 */ 124 protected function getMultiNoninputValues() 125 { 126 return [AccessTable::DEFAULT_LATEST, $this->ts, $this->pid, self::DEFAULT_PAGE_RID]; 127 } 128} 129