1083afc55SAndreas Gohr<?php 2083afc55SAndreas Gohr 3083afc55SAndreas Gohrnamespace plugin\struct\meta; 4083afc55SAndreas Gohr 51c502704SAndreas Gohruse dokuwiki\Form\Form; 6083afc55SAndreas Gohruse plugin\struct\types\AbstractBaseType; 7083afc55SAndreas Gohruse plugin\struct\types\Text; 8083afc55SAndreas Gohr 97182938bSAndreas Gohr/** 107182938bSAndreas Gohr * Class Schema 117182938bSAndreas Gohr * 127182938bSAndreas Gohr * Represents the schema of a single data table and all its properties. It defines what can be stored in 137182938bSAndreas Gohr * the represented data table and how those contents are formatted. 147182938bSAndreas Gohr * 157182938bSAndreas Gohr * It can be initialized with a timestamp to access the schema as it looked at that particular point in time. 167182938bSAndreas Gohr * 177182938bSAndreas Gohr * @package plugin\struct\meta 187182938bSAndreas Gohr */ 19083afc55SAndreas Gohrclass Schema { 20083afc55SAndreas Gohr 21083afc55SAndreas Gohr /** @var \helper_plugin_sqlite|null */ 22083afc55SAndreas Gohr protected $sqlite; 23083afc55SAndreas Gohr 24083afc55SAndreas Gohr /** @var int The ID of this schema */ 25083afc55SAndreas Gohr protected $id = 0; 26083afc55SAndreas Gohr 27083afc55SAndreas Gohr /** @var string name of the associated table */ 28083afc55SAndreas Gohr protected $table = ''; 29083afc55SAndreas Gohr 30083afc55SAndreas Gohr /** 31083afc55SAndreas Gohr * @var string the current checksum of this schema 32083afc55SAndreas Gohr */ 33083afc55SAndreas Gohr protected $chksum = ''; 34083afc55SAndreas Gohr 351c502704SAndreas Gohr /** @var Column[] all the colums */ 36083afc55SAndreas Gohr protected $columns = array(); 37083afc55SAndreas Gohr 38083afc55SAndreas Gohr /** @var int */ 39083afc55SAndreas Gohr protected $maxsort = 0; 40083afc55SAndreas Gohr 41083afc55SAndreas Gohr /** 42083afc55SAndreas Gohr * Schema constructor 437182938bSAndreas Gohr * 44083afc55SAndreas Gohr * @param string $table The table this schema is for 45083afc55SAndreas Gohr * @param int $ts The timestamp for when this schema was valid, 0 for current 46083afc55SAndreas Gohr */ 47083afc55SAndreas Gohr public function __construct($table, $ts = 0) { 48083afc55SAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 49083afc55SAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 50083afc55SAndreas Gohr $this->sqlite = $helper->getDB(); 51083afc55SAndreas Gohr if(!$this->sqlite) return; 52083afc55SAndreas Gohr 53083afc55SAndreas Gohr $table = self::cleanTableName($table); 54083afc55SAndreas Gohr $this->table = $table; 55083afc55SAndreas Gohr 56083afc55SAndreas Gohr // load info about the schema itself 57083afc55SAndreas Gohr if($ts) { 58083afc55SAndreas Gohr $sql = "SELECT * 59083afc55SAndreas Gohr FROM schemas 60083afc55SAndreas Gohr WHERE tbl = ? 61083afc55SAndreas Gohr AND ts <= ? 62083afc55SAndreas Gohr ORDER BY ts DESC 63083afc55SAndreas Gohr LIMIT 1"; 64083afc55SAndreas Gohr $opt = array($table, $ts); 65083afc55SAndreas Gohr 66083afc55SAndreas Gohr } else { 67083afc55SAndreas Gohr $sql = "SELECT * 68083afc55SAndreas Gohr FROM schemas 69083afc55SAndreas Gohr WHERE tbl = ? 70083afc55SAndreas Gohr ORDER BY ts DESC 71083afc55SAndreas Gohr LIMIT 1"; 72083afc55SAndreas Gohr $opt = array($table); 73083afc55SAndreas Gohr } 74083afc55SAndreas Gohr $res = $this->sqlite->query($sql, $opt); 75083afc55SAndreas Gohr if($this->sqlite->res2count($res)) { 761c502704SAndreas Gohr $result = array_shift($this->sqlite->res2arr($res)); 77083afc55SAndreas Gohr $this->id = $result['id']; 78083afc55SAndreas Gohr $this->chksum = $result['chksum']; 79083afc55SAndreas Gohr 80083afc55SAndreas Gohr } 81083afc55SAndreas Gohr $this->sqlite->res_close($res); 82083afc55SAndreas Gohr if(!$this->id) return; 83083afc55SAndreas Gohr 84083afc55SAndreas Gohr // load existing columns 85083afc55SAndreas Gohr $sql = "SELECT SC.*, T.* 86083afc55SAndreas Gohr FROM schema_cols SC, 87083afc55SAndreas Gohr types T 881c502704SAndreas Gohr WHERE SC.sid = ? 891c502704SAndreas Gohr AND SC.tid = T.id 90083afc55SAndreas Gohr ORDER BY SC.sort"; 911c502704SAndreas Gohr $res = $this->sqlite->query($sql, $this->id); 92083afc55SAndreas Gohr $rows = $this->sqlite->res2arr($res); 93083afc55SAndreas Gohr $this->sqlite->res_close($res); 94083afc55SAndreas Gohr 95083afc55SAndreas Gohr foreach($rows as $row) { 961c502704SAndreas Gohr $class = 'plugin\\struct\\types\\' . $row['class']; 97083afc55SAndreas Gohr $config = json_decode($row['config'], true); 981c502704SAndreas Gohr $this->columns[$row['colref']] = 991c502704SAndreas Gohr new Column( 1001c502704SAndreas Gohr $row['sort'], 1011c502704SAndreas Gohr new $class($config, $row['label'], $row['ismulti']), 1021c502704SAndreas Gohr $row['tid'], 1031c502704SAndreas Gohr $row['colref'], 1041c502704SAndreas Gohr $row['enabled'] 1051c502704SAndreas Gohr ); 1061c502704SAndreas Gohr 107083afc55SAndreas Gohr if($row['sort'] > $this->maxsort) $this->maxsort = $row['sort']; 108083afc55SAndreas Gohr } 109083afc55SAndreas Gohr } 110083afc55SAndreas Gohr 111083afc55SAndreas Gohr /** 112083afc55SAndreas Gohr * Cleans any unwanted stuff from table names 113083afc55SAndreas Gohr * 114083afc55SAndreas Gohr * @param string $table 115083afc55SAndreas Gohr * @return string 116083afc55SAndreas Gohr */ 117083afc55SAndreas Gohr static public function cleanTableName($table) { 118*2af472dcSAndreas Gohr $table = strtolower($table); 119083afc55SAndreas Gohr $table = preg_replace('/[^a-z0-9_]+/', '', $table); 120083afc55SAndreas Gohr $table = preg_replace('/^[0-9_]+/', '', $table); 121083afc55SAndreas Gohr $table = trim($table); 122083afc55SAndreas Gohr return $table; 123083afc55SAndreas Gohr } 124083afc55SAndreas Gohr 125083afc55SAndreas Gohr /** 1261c502704SAndreas Gohr * @return string 1271c502704SAndreas Gohr */ 1281c502704SAndreas Gohr public function getChksum() { 1291c502704SAndreas Gohr return $this->chksum; 1301c502704SAndreas Gohr } 1311c502704SAndreas Gohr 1321c502704SAndreas Gohr /** 1331c502704SAndreas Gohr * @return int 1341c502704SAndreas Gohr */ 1351c502704SAndreas Gohr public function getId() { 1361c502704SAndreas Gohr return $this->id; 1371c502704SAndreas Gohr } 1381c502704SAndreas Gohr 1391c502704SAndreas Gohr /** 1401c502704SAndreas Gohr * @return \plugin\struct\meta\Column[] 1411c502704SAndreas Gohr */ 1421c502704SAndreas Gohr public function getColumns() { 1431c502704SAndreas Gohr return $this->columns; 1441c502704SAndreas Gohr } 1451c502704SAndreas Gohr 146ae697e1fSAndreas Gohr /** 147ae697e1fSAndreas Gohr * @return string 148ae697e1fSAndreas Gohr */ 149ae697e1fSAndreas Gohr public function getTable() { 150ae697e1fSAndreas Gohr return $this->table; 151ae697e1fSAndreas Gohr } 1521c502704SAndreas Gohr 153ae697e1fSAndreas Gohr /** 154ae697e1fSAndreas Gohr * @return int the highest sort number used in this schema 155ae697e1fSAndreas Gohr */ 156ae697e1fSAndreas Gohr public function getMaxsort() { 157ae697e1fSAndreas Gohr return $this->maxsort; 158ae697e1fSAndreas Gohr } 1591c502704SAndreas Gohr 160083afc55SAndreas Gohr} 161