/** * Schema constructor * * @param string $table The table this schema is for * @param int $ts The timestamp for when this schema was valid, 0 for current * @param bool $islookup only used when creating a new schema, makes the new schema a lookup */ public function __construct($table, $ts = 0, $islookup = false) { /** @var \helper_plugin_struct_db $helper */ $helper = plugin_load('helper', 'struct_db'); $info = $helper->getInfo(); $this->structversion = $info['date']; $this->sqlite = $helper->getDB(); if (!$this->sqlite) { return; } $table = self::cleanTableName($table); $this->table = $table; $this->ts = $ts; // load info about the schema itself if ($ts) { $sql = "SELECT *\n FROM schemas\n WHERE tbl = ?\n AND ts <= ?\n ORDER BY ts DESC\n LIMIT 1"; $opt = array($table, $ts); } else { $sql = "SELECT *\n FROM schemas\n WHERE tbl = ?\n ORDER BY ts DESC\n LIMIT 1"; $opt = array($table); } $res = $this->sqlite->query($sql, $opt); if ($this->sqlite->res2count($res)) { $schema = $this->sqlite->res2arr($res); $result = array_shift($schema); $this->id = $result['id']; $this->user = $result['user']; $this->chksum = $result['chksum']; $this->islookup = $result['islookup']; $this->ts = $result['ts']; $this->editors = $result['editors']; } else { $this->islookup = $islookup; } $this->sqlite->res_close($res); if (!$this->id) { return; } // load existing columns $sql = "SELECT SC.*, T.*\n FROM schema_cols SC,\n types T\n WHERE SC.sid = ?\n AND SC.tid = T.id\n ORDER BY SC.sort"; $res = $this->sqlite->query($sql, $this->id); $rows = $this->sqlite->res2arr($res); $this->sqlite->res_close($res); foreach ($rows as $row) { if ($row['class'] == 'Integer') { $row['class'] = 'Decimal'; } $class = 'dokuwiki\\plugin\\struct\\types\\' . $row['class']; if (!class_exists($class)) { // This usually never happens, except during development msg('Unknown type "' . hsc($row['class']) . '" falling back to Text', -1); $class = 'dokuwiki\\plugin\\struct\\types\\Text'; } $config = json_decode($row['config'], true); /** @var AbstractBaseType $type */ $type = new $class($config, $row['label'], $row['ismulti'], $row['tid']); $column = new Column($row['sort'], $type, $row['colref'], $row['enabled'], $table); $type->setContext($column); $this->columns[] = $column; if ($row['sort'] > $this->maxsort) { $this->maxsort = $row['sort']; } } }