1083afc55SAndreas Gohr<?php 2083afc55SAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 4d486d6d7SAndreas Gohr 5*438a804cSAnna Dabrowskause dokuwiki\plugin\sqlite\SQLiteDB; 6ba766201SAndreas Gohruse dokuwiki\plugin\struct\types\AbstractBaseType; 7a91bbca2SAndreas Gohruse dokuwiki\Utf8\PhpString; 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 * 17ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 187182938bSAndreas Gohr */ 19d6d97f60SAnna Dabrowskaclass Schema 20d6d97f60SAnna Dabrowska{ 21f800af69SMichael Große use TranslationUtilities; 22f800af69SMichael Große 23*438a804cSAnna Dabrowska /** @var SQLiteDB|null */ 24083afc55SAndreas Gohr protected $sqlite; 25083afc55SAndreas Gohr 26083afc55SAndreas Gohr /** @var int The ID of this schema */ 27083afc55SAndreas Gohr protected $id = 0; 28083afc55SAndreas Gohr 29fa7b96aaSMichael Grosse /** @var string the user who last edited this schema */ 30fa7b96aaSMichael Grosse protected $user = ''; 31fa7b96aaSMichael Grosse 32083afc55SAndreas Gohr /** @var string name of the associated table */ 33083afc55SAndreas Gohr protected $table = ''; 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 41250c83c2SAndreas Gohr /** @var int */ 42250c83c2SAndreas Gohr protected $ts = 0; 43250c83c2SAndreas Gohr 44d486d6d7SAndreas Gohr /** @var string struct version info */ 45d486d6d7SAndreas Gohr protected $structversion = '?'; 46d486d6d7SAndreas Gohr 47127d6bacSMichael Große /** @var array config array with label translations */ 48127d6bacSMichael Große protected $config = array(); 49e2c90eebSAndreas 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 55083afc55SAndreas Gohr */ 56d6d97f60SAnna Dabrowska public function __construct($table, $ts = 0) 57d6d97f60SAnna Dabrowska { 586c9d1a10SAnna Dabrowska $baseconfig = array('allowed editors' => '', 'internal' => false); 59127d6bacSMichael Große 60083afc55SAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 61083afc55SAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 62d486d6d7SAndreas Gohr $info = $helper->getInfo(); 63d486d6d7SAndreas Gohr $this->structversion = $info['date']; 64083afc55SAndreas Gohr $this->sqlite = $helper->getDB(); 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 } 8679b29326SAnna Dabrowska $schema = $this->sqlite->queryAll($sql, $opt); 87127d6bacSMichael Große $config = array(); 8879b29326SAnna Dabrowska 8979b29326SAnna Dabrowska if (!empty($schema)) { 904e2abec0SMichael Große $result = array_shift($schema); 91083afc55SAndreas Gohr $this->id = $result['id']; 92fa7b96aaSMichael Grosse $this->user = $result['user']; 93587e314dSAndreas Gohr $this->ts = $result['ts']; 94127d6bacSMichael Große $config = json_decode($result['config'], true); 95083afc55SAndreas Gohr } 96127d6bacSMichael Große $this->config = array_merge($baseconfig, $config); 97f800af69SMichael Große $this->initTransConfig(array('label')); 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"; 10779b29326SAnna Dabrowska $rows = $this->sqlite->queryAll($sql, [$this->id]); 108083afc55SAndreas Gohr 109636c8abaSAndreas Gohr $typeclasses = Column::allTypes(); 110083afc55SAndreas Gohr foreach ($rows as $row) { 111328db41bSAndreas Gohr if ($row['class'] == 'Integer') { 112328db41bSAndreas Gohr $row['class'] = 'Decimal'; 113328db41bSAndreas Gohr } 114328db41bSAndreas Gohr 115636c8abaSAndreas Gohr $class = $typeclasses[$row['class']]; 11698eaa57dSAndreas Gohr if (!class_exists($class)) { 11798eaa57dSAndreas Gohr // This usually never happens, except during development 11898eaa57dSAndreas Gohr msg('Unknown type "' . hsc($row['class']) . '" falling back to Text', -1); 119ba766201SAndreas Gohr $class = 'dokuwiki\\plugin\\struct\\types\\Text'; 12098eaa57dSAndreas Gohr } 12198eaa57dSAndreas Gohr 122083afc55SAndreas Gohr $config = json_decode($row['config'], true); 123bbf3d6aaSAndreas Gohr /** @var AbstractBaseType $type */ 124bbf3d6aaSAndreas Gohr $type = new $class($config, $row['label'], $row['ismulti'], $row['tid']); 125bbf3d6aaSAndreas Gohr $column = new Column( 1261c502704SAndreas Gohr $row['sort'], 127bbf3d6aaSAndreas Gohr $type, 1281c502704SAndreas Gohr $row['colref'], 12963d51bbfSAndreas Gohr $row['enabled'], 13063d51bbfSAndreas Gohr $table 1311c502704SAndreas Gohr ); 132bbf3d6aaSAndreas Gohr $type->setContext($column); 1331c502704SAndreas Gohr 1347629557eSAndreas Gohr $this->columns[] = $column; 135083afc55SAndreas Gohr if ($row['sort'] > $this->maxsort) $this->maxsort = $row['sort']; 136083afc55SAndreas Gohr } 137083afc55SAndreas Gohr } 138083afc55SAndreas Gohr 139083afc55SAndreas Gohr /** 14067641668SAndreas Gohr * @return string identifer for debugging purposes 14167641668SAndreas Gohr */ 142748e747fSAnna Dabrowska public function __toString() 143d6d97f60SAnna Dabrowska { 1445b808f9fSAnna Dabrowska return __CLASS__ . ' ' . $this->table . ' (' . $this->id . ') '; 14567641668SAndreas Gohr } 14667641668SAndreas Gohr 14767641668SAndreas Gohr /** 148083afc55SAndreas Gohr * Cleans any unwanted stuff from table names 149083afc55SAndreas Gohr * 150083afc55SAndreas Gohr * @param string $table 151083afc55SAndreas Gohr * @return string 152083afc55SAndreas Gohr */ 153d6d97f60SAnna Dabrowska public static function cleanTableName($table) 154d6d97f60SAnna Dabrowska { 1552af472dcSAndreas Gohr $table = strtolower($table); 156083afc55SAndreas Gohr $table = preg_replace('/[^a-z0-9_]+/', '', $table); 157083afc55SAndreas Gohr $table = preg_replace('/^[0-9_]+/', '', $table); 158083afc55SAndreas Gohr $table = trim($table); 159083afc55SAndreas Gohr return $table; 160083afc55SAndreas Gohr } 161083afc55SAndreas Gohr 162127d6bacSMichael Große 163127d6bacSMichael Große /** 164097f4a53SAndreas Gohr * Gets a list of all available schemas 165097f4a53SAndreas Gohr * 1667c080d69SAndreas Gohr * @return \string[] 167097f4a53SAndreas Gohr */ 1685b808f9fSAnna Dabrowska public static function getAll() 169d6d97f60SAnna Dabrowska { 170097f4a53SAndreas Gohr /** @var \helper_plugin_struct_db $helper */ 171097f4a53SAndreas Gohr $helper = plugin_load('helper', 'struct_db'); 1727cbcfbdbSAndreas Gohr $db = $helper->getDB(false); 173097f4a53SAndreas Gohr if (!$db) return array(); 174097f4a53SAndreas Gohr 17579b29326SAnna Dabrowska $tables = $db->queryAll("SELECT DISTINCT tbl FROM schemas ORDER BY tbl"); 176097f4a53SAndreas Gohr 177097f4a53SAndreas Gohr $result = array(); 178097f4a53SAndreas Gohr foreach ($tables as $row) { 179097f4a53SAndreas Gohr $result[] = $row['tbl']; 180097f4a53SAndreas Gohr } 181097f4a53SAndreas Gohr return $result; 182097f4a53SAndreas Gohr } 183097f4a53SAndreas Gohr 184097f4a53SAndreas Gohr /** 185d5a1a6dcSAndreas Gohr * Delete all data associated with this schema 186d5a1a6dcSAndreas Gohr * 187d5a1a6dcSAndreas Gohr * This is really all data ever! Be careful! 188d5a1a6dcSAndreas Gohr */ 189d6d97f60SAnna Dabrowska public function delete() 190d6d97f60SAnna Dabrowska { 191d5a1a6dcSAndreas Gohr if (!$this->id) throw new StructException('can not delete unsaved schema'); 192d5a1a6dcSAndreas Gohr 193d5a1a6dcSAndreas Gohr $this->sqlite->query('BEGIN TRANSACTION'); 194d5a1a6dcSAndreas Gohr 195*438a804cSAnna Dabrowska $sql = "DROP TABLE "; 196*438a804cSAnna Dabrowska $this->sqlite->query($sql . 'data_' . $this->table); 197*438a804cSAnna Dabrowska $this->sqlite->query($sql . 'multi_' . $this->table); 198d5a1a6dcSAndreas Gohr 199*438a804cSAnna Dabrowska $sql = "DELETE FROM schema_assignments WHERE tbl = '$this->table'"; 200*438a804cSAnna Dabrowska $this->sqlite->query($sql); 201d5a1a6dcSAndreas Gohr 202*438a804cSAnna Dabrowska $sql = "DELETE FROM schema_assignments_patterns WHERE tbl = '$this->table'"; 203*438a804cSAnna Dabrowska $this->sqlite->query($sql); 204d5a1a6dcSAndreas Gohr 205d5a1a6dcSAndreas Gohr $sql = "SELECT T.id 206d5a1a6dcSAndreas Gohr FROM types T, schema_cols SC, schemas S 207d5a1a6dcSAndreas Gohr WHERE T.id = SC.tid 208d5a1a6dcSAndreas Gohr AND SC.sid = S.id 209d5a1a6dcSAndreas Gohr AND S.tbl = ?"; 210d5a1a6dcSAndreas Gohr $sql = "DELETE FROM types WHERE id IN ($sql)"; 211*438a804cSAnna Dabrowska $this->sqlite->query($sql, [$this->table]); 212d5a1a6dcSAndreas Gohr 213d5a1a6dcSAndreas Gohr $sql = "SELECT id 214d5a1a6dcSAndreas Gohr FROM schemas 215d5a1a6dcSAndreas Gohr WHERE tbl = ?"; 216d5a1a6dcSAndreas Gohr $sql = "DELETE FROM schema_cols WHERE sid IN ($sql)"; 217*438a804cSAnna Dabrowska $this->sqlite->query($sql, [$this->table]); 218d5a1a6dcSAndreas Gohr 219d5a1a6dcSAndreas Gohr $sql = "DELETE FROM schemas WHERE tbl = ?"; 220*438a804cSAnna Dabrowska $this->sqlite->query($sql, [$this->table]); 221d5a1a6dcSAndreas Gohr 222d5a1a6dcSAndreas Gohr $this->sqlite->query('COMMIT TRANSACTION'); 223d5a1a6dcSAndreas Gohr $this->sqlite->query('VACUUM'); 224f9f13d8cSAndreas Gohr 225f9f13d8cSAndreas Gohr // a deleted schema should not be used anymore, but let's make sure it's somewhat sane anyway 226f9f13d8cSAndreas Gohr $this->id = 0; 227f9f13d8cSAndreas Gohr $this->columns = array(); 228f9f13d8cSAndreas Gohr $this->maxsort = 0; 229f9f13d8cSAndreas Gohr $this->ts = 0; 230d5a1a6dcSAndreas Gohr } 231d5a1a6dcSAndreas Gohr 23279c83e06SMichael Große 23379c83e06SMichael Große /** 23479c83e06SMichael Große * Clear all data of a schema, but retain the schema itself 23579c83e06SMichael Große */ 236d6d97f60SAnna Dabrowska public function clear() 237d6d97f60SAnna Dabrowska { 23879c83e06SMichael Große if (!$this->id) throw new StructException('can not clear data of unsaved schema'); 23979c83e06SMichael Große 24079c83e06SMichael Große $this->sqlite->query('BEGIN TRANSACTION'); 241*438a804cSAnna Dabrowska $sql = 'DELETE FROM '; 242*438a804cSAnna Dabrowska $this->sqlite->query($sql . 'data_' . $this->table); 243*438a804cSAnna Dabrowska $this->sqlite->query($sql . 'multi_' . $this->table); 24479c83e06SMichael Große $this->sqlite->query('COMMIT TRANSACTION'); 24579c83e06SMichael Große $this->sqlite->query('VACUUM'); 24679c83e06SMichael Große } 24779c83e06SMichael Große 248d5a1a6dcSAndreas Gohr /** 2491c502704SAndreas Gohr * @return int 2501c502704SAndreas Gohr */ 251d6d97f60SAnna Dabrowska public function getId() 252d6d97f60SAnna Dabrowska { 2531c502704SAndreas Gohr return $this->id; 2541c502704SAndreas Gohr } 2551c502704SAndreas Gohr 2561c502704SAndreas Gohr /** 257587e314dSAndreas Gohr * @return int returns the timestamp this Schema was created at 258f411d872SAndreas Gohr */ 259d6d97f60SAnna Dabrowska public function getTimeStamp() 260d6d97f60SAnna Dabrowska { 261f411d872SAndreas Gohr return $this->ts; 262f411d872SAndreas Gohr } 263f411d872SAndreas Gohr 264f411d872SAndreas Gohr /** 265fa7b96aaSMichael Grosse * @return string 266fa7b96aaSMichael Grosse */ 267d6d97f60SAnna Dabrowska public function getUser() 268d6d97f60SAnna Dabrowska { 269fa7b96aaSMichael Grosse return $this->user; 270fa7b96aaSMichael Grosse } 271fa7b96aaSMichael Grosse 272d6d97f60SAnna Dabrowska public function getConfig() 273d6d97f60SAnna Dabrowska { 274127d6bacSMichael Große return $this->config; 275e2c90eebSAndreas Gohr } 276e2c90eebSAndreas Gohr 277fa7b96aaSMichael Grosse /** 278f800af69SMichael Große * Returns the translated label for this schema 279f800af69SMichael Große * 280f800af69SMichael Große * Uses the current language as determined by $conf['lang']. Falls back to english 281f800af69SMichael Große * and then to the Schema label 282f800af69SMichael Große * 283f800af69SMichael Große * @return string 284f800af69SMichael Große */ 285d6d97f60SAnna Dabrowska public function getTranslatedLabel() 286d6d97f60SAnna Dabrowska { 287f800af69SMichael Große return $this->getTranslatedKey('label', $this->table); 288f800af69SMichael Große } 289f800af69SMichael Große 290f800af69SMichael Große /** 2916ebbbb8eSAndreas Gohr * Checks if the current user may edit data in this schema 2926ebbbb8eSAndreas Gohr * 2936ebbbb8eSAndreas Gohr * @return bool 2946ebbbb8eSAndreas Gohr */ 295d6d97f60SAnna Dabrowska public function isEditable() 296d6d97f60SAnna Dabrowska { 2976ebbbb8eSAndreas Gohr global $USERINFO; 298ecf2cba2SAndreas Gohr global $INPUT; 299127d6bacSMichael Große if ($this->config['allowed editors'] === '') return true; 300ecf2cba2SAndreas Gohr if ($INPUT->server->str('REMOTE_USER') === '') return false; 3016ebbbb8eSAndreas Gohr if (auth_isadmin()) return true; 302ecf2cba2SAndreas Gohr return auth_isMember($this->config['allowed editors'], $INPUT->server->str('REMOTE_USER'), $USERINFO['grps']); 3036ebbbb8eSAndreas Gohr } 3046ebbbb8eSAndreas Gohr 3056ebbbb8eSAndreas Gohr /** 3066c9d1a10SAnna Dabrowska * 3076c9d1a10SAnna Dabrowska * @return bool 3086c9d1a10SAnna Dabrowska */ 3096c9d1a10SAnna Dabrowska public function isInternal() 3106c9d1a10SAnna Dabrowska { 3116c9d1a10SAnna Dabrowska return (bool) $this->config['internal']; 3126c9d1a10SAnna Dabrowska } 3136c9d1a10SAnna Dabrowska 3146c9d1a10SAnna Dabrowska /** 315ce206ec7SAndreas Gohr * Returns a list of columns in this schema 316ce206ec7SAndreas Gohr * 317ce206ec7SAndreas Gohr * @param bool $withDisabled if false, disabled columns will not be returned 318ce206ec7SAndreas Gohr * @return Column[] 3191c502704SAndreas Gohr */ 320d6d97f60SAnna Dabrowska public function getColumns($withDisabled = true) 321d6d97f60SAnna Dabrowska { 322ce206ec7SAndreas Gohr if (!$withDisabled) { 323ce206ec7SAndreas Gohr return array_filter( 324ce206ec7SAndreas Gohr $this->columns, 325ce206ec7SAndreas Gohr function (Column $col) { 326ce206ec7SAndreas Gohr return $col->isEnabled(); 327ce206ec7SAndreas Gohr } 328ce206ec7SAndreas Gohr ); 329ce206ec7SAndreas Gohr } 330ce206ec7SAndreas Gohr 3311c502704SAndreas Gohr return $this->columns; 3321c502704SAndreas Gohr } 3331c502704SAndreas Gohr 334ae697e1fSAndreas Gohr /** 3355742aea9SAndreas Gohr * Find a column in the schema by its label 3365742aea9SAndreas Gohr * 3375742aea9SAndreas Gohr * Only enabled columns are returned! 3385742aea9SAndreas Gohr * 3395742aea9SAndreas Gohr * @param $name 3405742aea9SAndreas Gohr * @return bool|Column 3415742aea9SAndreas Gohr */ 342d6d97f60SAnna Dabrowska public function findColumn($name) 343d6d97f60SAnna Dabrowska { 3445742aea9SAndreas Gohr foreach ($this->columns as $col) { 345a91bbca2SAndreas Gohr if ($col->isEnabled() && PhpString::strtolower($col->getLabel()) == PhpString::strtolower($name)) { 3465742aea9SAndreas Gohr return $col; 3475742aea9SAndreas Gohr } 3485742aea9SAndreas Gohr } 3495742aea9SAndreas Gohr return false; 3505742aea9SAndreas Gohr } 3515742aea9SAndreas Gohr 3525742aea9SAndreas Gohr /** 353ae697e1fSAndreas Gohr * @return string 354ae697e1fSAndreas Gohr */ 355d6d97f60SAnna Dabrowska public function getTable() 356d6d97f60SAnna Dabrowska { 357ae697e1fSAndreas Gohr return $this->table; 358ae697e1fSAndreas Gohr } 3591c502704SAndreas Gohr 360ae697e1fSAndreas Gohr /** 361ae697e1fSAndreas Gohr * @return int the highest sort number used in this schema 362ae697e1fSAndreas Gohr */ 363d6d97f60SAnna Dabrowska public function getMaxsort() 364d6d97f60SAnna Dabrowska { 365ae697e1fSAndreas Gohr return $this->maxsort; 366ae697e1fSAndreas Gohr } 3671c502704SAndreas Gohr 368d486d6d7SAndreas Gohr /** 369d486d6d7SAndreas Gohr * @return string the JSON representing this schema 370d486d6d7SAndreas Gohr */ 371d6d97f60SAnna Dabrowska public function toJSON() 372d6d97f60SAnna Dabrowska { 373d486d6d7SAndreas Gohr $data = array( 374d486d6d7SAndreas Gohr 'structversion' => $this->structversion, 375d486d6d7SAndreas Gohr 'schema' => $this->getTable(), 376d486d6d7SAndreas Gohr 'id' => $this->getId(), 37745313413SMichael Grosse 'user' => $this->getUser(), 378127d6bacSMichael Große 'config' => $this->getConfig(), 379d486d6d7SAndreas Gohr 'columns' => array() 380d486d6d7SAndreas Gohr ); 381d486d6d7SAndreas Gohr 382d486d6d7SAndreas Gohr foreach ($this->columns as $column) { 383d486d6d7SAndreas Gohr $data['columns'][] = array( 384d486d6d7SAndreas Gohr 'colref' => $column->getColref(), 385d486d6d7SAndreas Gohr 'ismulti' => $column->isMulti(), 386d486d6d7SAndreas Gohr 'isenabled' => $column->isEnabled(), 387d486d6d7SAndreas Gohr 'sort' => $column->getSort(), 388d486d6d7SAndreas Gohr 'label' => $column->getLabel(), 389d486d6d7SAndreas Gohr 'class' => $column->getType()->getClass(), 390d486d6d7SAndreas Gohr 'config' => $column->getType()->getConfig(), 391d486d6d7SAndreas Gohr ); 392d486d6d7SAndreas Gohr } 393d486d6d7SAndreas Gohr 394d486d6d7SAndreas Gohr return json_encode($data, JSON_PRETTY_PRINT); 395d486d6d7SAndreas Gohr } 396083afc55SAndreas Gohr} 397