1083afc55SAndreas Gohr<?php 2083afc55SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4d486d6d7SAndreas Gohr 5ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\AbstractBaseType; 6083afc55SAndreas Gohr 745560cc7SAndreas Gohrif(!defined('JSON_PRETTY_PRINT')) define('JSON_PRETTY_PRINT', 0); // PHP 5.3 compatibility 845560cc7SAndreas 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 * 17ba766201SAndreas Gohr * @package dokuwiki\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 3088b7d2aaSAndreas Gohr /** @var bool is this a lookup schema? */ 3188b7d2aaSAndreas Gohr protected $islookup = false; 3288b7d2aaSAndreas Gohr 33083afc55SAndreas Gohr /** 34083afc55SAndreas Gohr * @var string the current checksum of this schema 35083afc55SAndreas Gohr */ 36083afc55SAndreas Gohr protected $chksum = ''; 37083afc55SAndreas Gohr 381c502704SAndreas Gohr /** @var Column[] all the colums */ 39083afc55SAndreas Gohr protected $columns = array(); 40083afc55SAndreas Gohr 41083afc55SAndreas Gohr /** @var int */ 42083afc55SAndreas Gohr protected $maxsort = 0; 43083afc55SAndreas Gohr 44250c83c2SAndreas Gohr /** @var int */ 45250c83c2SAndreas Gohr protected $ts = 0; 46250c83c2SAndreas Gohr 47d486d6d7SAndreas Gohr /** @var string struct version info */ 48d486d6d7SAndreas Gohr protected $structversion = '?'; 49d486d6d7SAndreas Gohr 50083afc55SAndreas Gohr /** 51083afc55SAndreas Gohr * Schema constructor 527182938bSAndreas Gohr * 53083afc55SAndreas Gohr * @param string $table The table this schema is for 54083afc55SAndreas Gohr * @param int $ts The timestamp for when this schema was valid, 0 for current 5588b7d2aaSAndreas Gohr * @param bool $islookup only used when creating a new schema, makes the new schema a lookup 56083afc55SAndreas Gohr */ 5788b7d2aaSAndreas Gohr public function __construct($table, $ts = 0, $islookup = false) { 58083afc55SAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 59083afc55SAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 60d486d6d7SAndreas Gohr $info = $helper->getInfo(); 61d486d6d7SAndreas Gohr $this->structversion = $info['date']; 62083afc55SAndreas Gohr $this->sqlite = $helper->getDB(); 63083afc55SAndreas Gohr if(!$this->sqlite) return; 64083afc55SAndreas Gohr 65083afc55SAndreas Gohr $table = self::cleanTableName($table); 66083afc55SAndreas Gohr $this->table = $table; 67250c83c2SAndreas Gohr $this->ts = $ts; 68083afc55SAndreas Gohr 69083afc55SAndreas Gohr // load info about the schema itself 70083afc55SAndreas Gohr if($ts) { 71083afc55SAndreas Gohr $sql = "SELECT * 72083afc55SAndreas Gohr FROM schemas 73083afc55SAndreas Gohr WHERE tbl = ? 74083afc55SAndreas Gohr AND ts <= ? 75083afc55SAndreas Gohr ORDER BY ts DESC 76083afc55SAndreas Gohr LIMIT 1"; 77083afc55SAndreas Gohr $opt = array($table, $ts); 78083afc55SAndreas Gohr } else { 79083afc55SAndreas Gohr $sql = "SELECT * 80083afc55SAndreas Gohr FROM schemas 81083afc55SAndreas Gohr WHERE tbl = ? 82083afc55SAndreas Gohr ORDER BY ts DESC 83083afc55SAndreas Gohr LIMIT 1"; 84083afc55SAndreas Gohr $opt = array($table); 85083afc55SAndreas Gohr } 86083afc55SAndreas Gohr $res = $this->sqlite->query($sql, $opt); 87083afc55SAndreas Gohr if($this->sqlite->res2count($res)) { 884e2abec0SMichael Große $schema = $this->sqlite->res2arr($res); 894e2abec0SMichael Große $result = array_shift($schema); 90083afc55SAndreas Gohr $this->id = $result['id']; 91083afc55SAndreas Gohr $this->chksum = $result['chksum']; 9288b7d2aaSAndreas Gohr $this->islookup = $result['islookup']; 93587e314dSAndreas Gohr $this->ts = $result['ts']; 9488b7d2aaSAndreas Gohr } else { 9588b7d2aaSAndreas Gohr $this->islookup = $islookup; 96083afc55SAndreas Gohr } 97083afc55SAndreas Gohr $this->sqlite->res_close($res); 98083afc55SAndreas Gohr if(!$this->id) return; 99083afc55SAndreas Gohr 100083afc55SAndreas Gohr // load existing columns 101083afc55SAndreas Gohr $sql = "SELECT SC.*, T.* 102083afc55SAndreas Gohr FROM schema_cols SC, 103083afc55SAndreas Gohr types T 1041c502704SAndreas Gohr WHERE SC.sid = ? 1051c502704SAndreas Gohr AND SC.tid = T.id 106083afc55SAndreas Gohr ORDER BY SC.sort"; 1071c502704SAndreas Gohr $res = $this->sqlite->query($sql, $this->id); 108083afc55SAndreas Gohr $rows = $this->sqlite->res2arr($res); 109083afc55SAndreas Gohr $this->sqlite->res_close($res); 110083afc55SAndreas Gohr 111083afc55SAndreas Gohr foreach($rows as $row) { 112328db41bSAndreas Gohr if($row['class'] == 'Integer') { 113328db41bSAndreas Gohr $row['class'] = 'Decimal'; 114328db41bSAndreas Gohr } 115328db41bSAndreas Gohr 116ba766201SAndreas Gohr $class = 'dokuwiki\\plugin\\struct\\types\\' . $row['class']; 11798eaa57dSAndreas Gohr if(!class_exists($class)) { 11898eaa57dSAndreas Gohr // This usually never happens, except during development 11998eaa57dSAndreas Gohr msg('Unknown type "' . hsc($row['class']) . '" falling back to Text', -1); 120ba766201SAndreas Gohr $class = 'dokuwiki\\plugin\\struct\\types\\Text'; 12198eaa57dSAndreas Gohr } 12298eaa57dSAndreas Gohr 123083afc55SAndreas Gohr $config = json_decode($row['config'], true); 124bbf3d6aaSAndreas Gohr /** @var AbstractBaseType $type */ 125bbf3d6aaSAndreas Gohr $type = new $class($config, $row['label'], $row['ismulti'], $row['tid']); 126bbf3d6aaSAndreas Gohr $column = new Column( 1271c502704SAndreas Gohr $row['sort'], 128bbf3d6aaSAndreas Gohr $type, 1291c502704SAndreas Gohr $row['colref'], 13063d51bbfSAndreas Gohr $row['enabled'], 13163d51bbfSAndreas Gohr $table 1321c502704SAndreas Gohr ); 133bbf3d6aaSAndreas Gohr $type->setContext($column); 1341c502704SAndreas Gohr 1357629557eSAndreas Gohr $this->columns[] = $column; 136083afc55SAndreas Gohr if($row['sort'] > $this->maxsort) $this->maxsort = $row['sort']; 137083afc55SAndreas Gohr } 138083afc55SAndreas Gohr } 139083afc55SAndreas Gohr 140083afc55SAndreas Gohr /** 141*67641668SAndreas Gohr * @return string identifer for debugging purposes 142*67641668SAndreas Gohr */ 143*67641668SAndreas Gohr function __toString() { 144*67641668SAndreas Gohr return __CLASS__.' '.$this->table.' ('.$this->id.') '.($this->islookup ? 'LOOKUP' : 'DATA'); 145*67641668SAndreas Gohr } 146*67641668SAndreas Gohr 147*67641668SAndreas Gohr /** 148083afc55SAndreas Gohr * Cleans any unwanted stuff from table names 149083afc55SAndreas Gohr * 150083afc55SAndreas Gohr * @param string $table 151083afc55SAndreas Gohr * @return string 152083afc55SAndreas Gohr */ 153083afc55SAndreas Gohr static public function cleanTableName($table) { 1542af472dcSAndreas Gohr $table = strtolower($table); 155083afc55SAndreas Gohr $table = preg_replace('/[^a-z0-9_]+/', '', $table); 156083afc55SAndreas Gohr $table = preg_replace('/^[0-9_]+/', '', $table); 157083afc55SAndreas Gohr $table = trim($table); 158083afc55SAndreas Gohr return $table; 159083afc55SAndreas Gohr } 160083afc55SAndreas Gohr 161083afc55SAndreas Gohr /** 162097f4a53SAndreas Gohr * Gets a list of all available schemas 163097f4a53SAndreas Gohr * 1647c080d69SAndreas Gohr * @param string $filter either 'page' or 'lookup' 1657c080d69SAndreas Gohr * @return \string[] 166097f4a53SAndreas Gohr */ 1677c080d69SAndreas Gohr static public function getAll($filter = '') { 168097f4a53SAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 169097f4a53SAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 170097f4a53SAndreas Gohr $db = $helper->getDB(); 171097f4a53SAndreas Gohr if(!$db) return array(); 172097f4a53SAndreas Gohr 1737c080d69SAndreas Gohr if($filter == 'page') { 1747c080d69SAndreas Gohr $where = 'islookup = 0'; 1757c080d69SAndreas Gohr } elseif($filter == 'lookup') { 1767c080d69SAndreas Gohr $where = 'islookup = 1'; 1777c080d69SAndreas Gohr } else { 1787c080d69SAndreas Gohr $where = '1 = 1'; 1797c080d69SAndreas Gohr } 1807c080d69SAndreas Gohr 1817c080d69SAndreas Gohr $res = $db->query("SELECT DISTINCT tbl FROM schemas WHERE $where ORDER BY tbl"); 182097f4a53SAndreas Gohr $tables = $db->res2arr($res); 183097f4a53SAndreas Gohr $db->res_close($res); 184097f4a53SAndreas Gohr 185097f4a53SAndreas Gohr $result = array(); 186097f4a53SAndreas Gohr foreach($tables as $row) { 187097f4a53SAndreas Gohr $result[] = $row['tbl']; 188097f4a53SAndreas Gohr } 189097f4a53SAndreas Gohr return $result; 190097f4a53SAndreas Gohr } 191097f4a53SAndreas Gohr 192097f4a53SAndreas Gohr /** 193d5a1a6dcSAndreas Gohr * Delete all data associated with this schema 194d5a1a6dcSAndreas Gohr * 195d5a1a6dcSAndreas Gohr * This is really all data ever! Be careful! 196d5a1a6dcSAndreas Gohr */ 197d5a1a6dcSAndreas Gohr public function delete() { 198d5a1a6dcSAndreas Gohr if(!$this->id) throw new StructException('can not delete unsaved schema'); 199d5a1a6dcSAndreas Gohr 200d5a1a6dcSAndreas Gohr $this->sqlite->query('BEGIN TRANSACTION'); 201d5a1a6dcSAndreas Gohr 202d5a1a6dcSAndreas Gohr $sql = "DROP TABLE ?"; 203d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, 'data_' . $this->table); 204d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, 'multi_' . $this->table); 205d5a1a6dcSAndreas Gohr 206d5a1a6dcSAndreas Gohr $sql = "DELETE FROM schema_assignments WHERE tbl = ?"; 207d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, $this->table); 208d5a1a6dcSAndreas Gohr 209d5a1a6dcSAndreas Gohr $sql = "DELETE FROM schema_assignments_patterns WHERE tbl = ?"; 210d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, $this->table); 211d5a1a6dcSAndreas Gohr 212d5a1a6dcSAndreas Gohr $sql = "SELECT T.id 213d5a1a6dcSAndreas Gohr FROM types T, schema_cols SC, schemas S 214d5a1a6dcSAndreas Gohr WHERE T.id = SC.tid 215d5a1a6dcSAndreas Gohr AND SC.sid = S.id 216d5a1a6dcSAndreas Gohr AND S.tbl = ?"; 217d5a1a6dcSAndreas Gohr $sql = "DELETE FROM types WHERE id IN ($sql)"; 218d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, $this->table); 219d5a1a6dcSAndreas Gohr 220d5a1a6dcSAndreas Gohr $sql = "SELECT id 221d5a1a6dcSAndreas Gohr FROM schemas 222d5a1a6dcSAndreas Gohr WHERE tbl = ?"; 223d5a1a6dcSAndreas Gohr $sql = "DELETE FROM schema_cols WHERE sid IN ($sql)"; 224d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, $this->table); 225d5a1a6dcSAndreas Gohr 226d5a1a6dcSAndreas Gohr $sql = "DELETE FROM schemas WHERE tbl = ?"; 227d5a1a6dcSAndreas Gohr $this->sqlite->query($sql, $this->table); 228d5a1a6dcSAndreas Gohr 229d5a1a6dcSAndreas Gohr $this->sqlite->query('COMMIT TRANSACTION'); 230d5a1a6dcSAndreas Gohr $this->sqlite->query('VACUUM'); 231f9f13d8cSAndreas Gohr 232f9f13d8cSAndreas Gohr // a deleted schema should not be used anymore, but let's make sure it's somewhat sane anyway 233f9f13d8cSAndreas Gohr $this->id = 0; 234f9f13d8cSAndreas Gohr $this->chksum = ''; 235f9f13d8cSAndreas Gohr $this->columns = array(); 236f9f13d8cSAndreas Gohr $this->maxsort = 0; 237f9f13d8cSAndreas Gohr $this->ts = 0; 238d5a1a6dcSAndreas Gohr } 239d5a1a6dcSAndreas Gohr 240d5a1a6dcSAndreas Gohr /** 2411c502704SAndreas Gohr * @return string 2421c502704SAndreas Gohr */ 2431c502704SAndreas Gohr public function getChksum() { 2441c502704SAndreas Gohr return $this->chksum; 2451c502704SAndreas Gohr } 2461c502704SAndreas Gohr 2471c502704SAndreas Gohr /** 2481c502704SAndreas Gohr * @return int 2491c502704SAndreas Gohr */ 2501c502704SAndreas Gohr public function getId() { 2511c502704SAndreas Gohr return $this->id; 2521c502704SAndreas Gohr } 2531c502704SAndreas Gohr 2541c502704SAndreas Gohr /** 255587e314dSAndreas Gohr * @return int returns the timestamp this Schema was created at 256f411d872SAndreas Gohr */ 257f411d872SAndreas Gohr public function getTimeStamp() { 258f411d872SAndreas Gohr return $this->ts; 259f411d872SAndreas Gohr } 260f411d872SAndreas Gohr 261f411d872SAndreas Gohr /** 26288b7d2aaSAndreas Gohr * @return bool is this a lookup schema? 26388b7d2aaSAndreas Gohr */ 26488b7d2aaSAndreas Gohr public function isLookup() { 26588b7d2aaSAndreas Gohr return $this->islookup; 26688b7d2aaSAndreas Gohr } 26788b7d2aaSAndreas Gohr 26888b7d2aaSAndreas Gohr /** 269ce206ec7SAndreas Gohr * Returns a list of columns in this schema 270ce206ec7SAndreas Gohr * 271ce206ec7SAndreas Gohr * @param bool $withDisabled if false, disabled columns will not be returned 272ce206ec7SAndreas Gohr * @return Column[] 2731c502704SAndreas Gohr */ 274ce206ec7SAndreas Gohr public function getColumns($withDisabled = true) { 275ce206ec7SAndreas Gohr if(!$withDisabled) { 276ce206ec7SAndreas Gohr return array_filter( 277ce206ec7SAndreas Gohr $this->columns, 278ce206ec7SAndreas Gohr function (Column $col) { 279ce206ec7SAndreas Gohr return $col->isEnabled(); 280ce206ec7SAndreas Gohr } 281ce206ec7SAndreas Gohr ); 282ce206ec7SAndreas Gohr } 283ce206ec7SAndreas Gohr 2841c502704SAndreas Gohr return $this->columns; 2851c502704SAndreas Gohr } 2861c502704SAndreas Gohr 287ae697e1fSAndreas Gohr /** 2885742aea9SAndreas Gohr * Find a column in the schema by its label 2895742aea9SAndreas Gohr * 2905742aea9SAndreas Gohr * Only enabled columns are returned! 2915742aea9SAndreas Gohr * 2925742aea9SAndreas Gohr * @param $name 2935742aea9SAndreas Gohr * @return bool|Column 2945742aea9SAndreas Gohr */ 2955742aea9SAndreas Gohr public function findColumn($name) { 2965742aea9SAndreas Gohr foreach($this->columns as $col) { 2975742aea9SAndreas Gohr if($col->isEnabled() && utf8_strtolower($col->getLabel()) == utf8_strtolower($name)) { 2985742aea9SAndreas Gohr return $col; 2995742aea9SAndreas Gohr } 3005742aea9SAndreas Gohr } 3015742aea9SAndreas Gohr return false; 3025742aea9SAndreas Gohr } 3035742aea9SAndreas Gohr 3045742aea9SAndreas Gohr /** 305ae697e1fSAndreas Gohr * @return string 306ae697e1fSAndreas Gohr */ 307ae697e1fSAndreas Gohr public function getTable() { 308ae697e1fSAndreas Gohr return $this->table; 309ae697e1fSAndreas Gohr } 3101c502704SAndreas Gohr 311ae697e1fSAndreas Gohr /** 312ae697e1fSAndreas Gohr * @return int the highest sort number used in this schema 313ae697e1fSAndreas Gohr */ 314ae697e1fSAndreas Gohr public function getMaxsort() { 315ae697e1fSAndreas Gohr return $this->maxsort; 316ae697e1fSAndreas Gohr } 3171c502704SAndreas Gohr 318d486d6d7SAndreas Gohr /** 319d486d6d7SAndreas Gohr * @return string the JSON representing this schema 320d486d6d7SAndreas Gohr */ 321d486d6d7SAndreas Gohr public function toJSON() { 322d486d6d7SAndreas Gohr $data = array( 323d486d6d7SAndreas Gohr 'structversion' => $this->structversion, 324d486d6d7SAndreas Gohr 'schema' => $this->getTable(), 325d486d6d7SAndreas Gohr 'id' => $this->getId(), 326d486d6d7SAndreas Gohr 'columns' => array() 327d486d6d7SAndreas Gohr ); 328d486d6d7SAndreas Gohr 329d486d6d7SAndreas Gohr foreach($this->columns as $column) { 330d486d6d7SAndreas Gohr $data['columns'][] = array( 331d486d6d7SAndreas Gohr 'colref' => $column->getColref(), 332d486d6d7SAndreas Gohr 'ismulti' => $column->isMulti(), 333d486d6d7SAndreas Gohr 'isenabled' => $column->isEnabled(), 334d486d6d7SAndreas Gohr 'sort' => $column->getSort(), 335d486d6d7SAndreas Gohr 'label' => $column->getLabel(), 336d486d6d7SAndreas Gohr 'class' => $column->getType()->getClass(), 337d486d6d7SAndreas Gohr 'config' => $column->getType()->getConfig(), 338d486d6d7SAndreas Gohr ); 339d486d6d7SAndreas Gohr } 340d486d6d7SAndreas Gohr 341d486d6d7SAndreas Gohr return json_encode($data, JSON_PRETTY_PRINT); 342d486d6d7SAndreas Gohr } 343083afc55SAndreas Gohr} 344