xref: /plugin/struct/meta/Schema.php (revision fa7b96aac3c296355e7757cc16519768d467d548)
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
27*fa7b96aaSMichael Grosse    /** @var string the user who last edited this schema */
28*fa7b96aaSMichael Grosse    protected $user = '';
29*fa7b96aaSMichael Grosse
30083afc55SAndreas Gohr    /** @var string name of the associated table */
31083afc55SAndreas Gohr    protected $table = '';
32083afc55SAndreas 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
55083afc55SAndreas Gohr     */
56083afc55SAndreas Gohr    public function __construct($table, $ts = 0) {
57083afc55SAndreas Gohr        /** @var \helper_plugin_struct_db $helper */
58*fa7b96aaSMichael Grosse        $helper = plugin_load('helper', 'struct_db', true);
59d486d6d7SAndreas Gohr        $info = $helper->getInfo();
60d486d6d7SAndreas Gohr        $this->structversion = $info['date'];
61083afc55SAndreas Gohr        $this->sqlite = $helper->getDB();
62083afc55SAndreas Gohr        if(!$this->sqlite) return;
63083afc55SAndreas Gohr
64083afc55SAndreas Gohr        $table = self::cleanTableName($table);
65083afc55SAndreas Gohr        $this->table = $table;
66250c83c2SAndreas Gohr        $this->ts = $ts;
67083afc55SAndreas Gohr
68083afc55SAndreas Gohr        // load info about the schema itself
69083afc55SAndreas Gohr        if($ts) {
70083afc55SAndreas Gohr            $sql = "SELECT *
71083afc55SAndreas Gohr                      FROM schemas
72083afc55SAndreas Gohr                     WHERE tbl = ?
73083afc55SAndreas Gohr                       AND ts <= ?
74083afc55SAndreas Gohr                  ORDER BY ts DESC
75083afc55SAndreas Gohr                     LIMIT 1";
76083afc55SAndreas Gohr            $opt = array($table, $ts);
77083afc55SAndreas Gohr        } else {
78083afc55SAndreas Gohr            $sql = "SELECT *
79083afc55SAndreas Gohr                      FROM schemas
80083afc55SAndreas Gohr                     WHERE tbl = ?
81083afc55SAndreas Gohr                  ORDER BY ts DESC
82083afc55SAndreas Gohr                     LIMIT 1";
83083afc55SAndreas Gohr            $opt = array($table);
84083afc55SAndreas Gohr        }
85083afc55SAndreas Gohr        $res = $this->sqlite->query($sql, $opt);
86083afc55SAndreas Gohr        if($this->sqlite->res2count($res)) {
874e2abec0SMichael Große            $schema = $this->sqlite->res2arr($res);
884e2abec0SMichael Große            $result = array_shift($schema);
89083afc55SAndreas Gohr            $this->id = $result['id'];
90*fa7b96aaSMichael Grosse            $this->user = $result['user'];
91083afc55SAndreas Gohr            $this->chksum = $result['chksum'];
92083afc55SAndreas Gohr        }
93083afc55SAndreas Gohr        $this->sqlite->res_close($res);
94083afc55SAndreas Gohr        if(!$this->id) return;
95083afc55SAndreas Gohr
96083afc55SAndreas Gohr        // load existing columns
97083afc55SAndreas Gohr        $sql = "SELECT SC.*, T.*
98083afc55SAndreas Gohr                  FROM schema_cols SC,
99083afc55SAndreas Gohr                       types T
1001c502704SAndreas Gohr                 WHERE SC.sid = ?
1011c502704SAndreas Gohr                   AND SC.tid = T.id
102083afc55SAndreas Gohr              ORDER BY SC.sort";
1031c502704SAndreas Gohr        $res = $this->sqlite->query($sql, $this->id);
104083afc55SAndreas Gohr        $rows = $this->sqlite->res2arr($res);
105083afc55SAndreas Gohr        $this->sqlite->res_close($res);
106083afc55SAndreas Gohr
107083afc55SAndreas Gohr        foreach($rows as $row) {
108ba766201SAndreas Gohr            $class = 'dokuwiki\\plugin\\struct\\types\\' . $row['class'];
10998eaa57dSAndreas Gohr            if(!class_exists($class)) {
11098eaa57dSAndreas Gohr                // This usually never happens, except during development
11198eaa57dSAndreas Gohr                msg('Unknown type "' . hsc($row['class']) . '" falling back to Text', -1);
112ba766201SAndreas Gohr                $class = 'dokuwiki\\plugin\\struct\\types\\Text';
11398eaa57dSAndreas Gohr            }
11498eaa57dSAndreas Gohr
115083afc55SAndreas Gohr            $config = json_decode($row['config'], true);
116bbf3d6aaSAndreas Gohr            /** @var AbstractBaseType $type */
117bbf3d6aaSAndreas Gohr            $type = new $class($config, $row['label'], $row['ismulti'], $row['tid']);
118bbf3d6aaSAndreas Gohr            $column = new Column(
1191c502704SAndreas Gohr                $row['sort'],
120bbf3d6aaSAndreas Gohr                $type,
1211c502704SAndreas Gohr                $row['colref'],
12263d51bbfSAndreas Gohr                $row['enabled'],
12363d51bbfSAndreas Gohr                $table
1241c502704SAndreas Gohr            );
125bbf3d6aaSAndreas Gohr            $type->setContext($column);
1261c502704SAndreas Gohr
1277629557eSAndreas Gohr            $this->columns[] = $column;
128083afc55SAndreas Gohr            if($row['sort'] > $this->maxsort) $this->maxsort = $row['sort'];
129083afc55SAndreas Gohr        }
130083afc55SAndreas Gohr    }
131083afc55SAndreas Gohr
132083afc55SAndreas Gohr    /**
133083afc55SAndreas Gohr     * Cleans any unwanted stuff from table names
134083afc55SAndreas Gohr     *
135083afc55SAndreas Gohr     * @param string $table
136083afc55SAndreas Gohr     * @return string
137083afc55SAndreas Gohr     */
138083afc55SAndreas Gohr    static public function cleanTableName($table) {
1392af472dcSAndreas Gohr        $table = strtolower($table);
140083afc55SAndreas Gohr        $table = preg_replace('/[^a-z0-9_]+/', '', $table);
141083afc55SAndreas Gohr        $table = preg_replace('/^[0-9_]+/', '', $table);
142083afc55SAndreas Gohr        $table = trim($table);
143083afc55SAndreas Gohr        return $table;
144083afc55SAndreas Gohr    }
145083afc55SAndreas Gohr
146083afc55SAndreas Gohr    /**
147097f4a53SAndreas Gohr     * Gets a list of all available schemas
148097f4a53SAndreas Gohr     *
149097f4a53SAndreas Gohr     * @return string[]
150097f4a53SAndreas Gohr     */
151097f4a53SAndreas Gohr    static public function getAll() {
152097f4a53SAndreas Gohr        /** @var \helper_plugin_struct_db $helper */
153097f4a53SAndreas Gohr        $helper = plugin_load('helper', 'struct_db');
154097f4a53SAndreas Gohr        $db = $helper->getDB();
155097f4a53SAndreas Gohr        if(!$db) return array();
156097f4a53SAndreas Gohr
157097f4a53SAndreas Gohr        $res = $db->query("SELECT DISTINCT tbl FROM schemas ORDER BY tbl");
158097f4a53SAndreas Gohr        $tables = $db->res2arr($res);
159097f4a53SAndreas Gohr        $db->res_close($res);
160097f4a53SAndreas Gohr
161097f4a53SAndreas Gohr        $result = array();
162097f4a53SAndreas Gohr        foreach($tables as $row) {
163097f4a53SAndreas Gohr            $result[] = $row['tbl'];
164097f4a53SAndreas Gohr        }
165097f4a53SAndreas Gohr        return $result;
166097f4a53SAndreas Gohr    }
167097f4a53SAndreas Gohr
168097f4a53SAndreas Gohr    /**
169d5a1a6dcSAndreas Gohr     * Delete all data associated with this schema
170d5a1a6dcSAndreas Gohr     *
171d5a1a6dcSAndreas Gohr     * This is really all data ever! Be careful!
172d5a1a6dcSAndreas Gohr     */
173d5a1a6dcSAndreas Gohr    public function delete() {
174d5a1a6dcSAndreas Gohr        if(!$this->id) throw new StructException('can not delete unsaved schema');
175d5a1a6dcSAndreas Gohr
176d5a1a6dcSAndreas Gohr        $this->sqlite->query('BEGIN TRANSACTION');
177d5a1a6dcSAndreas Gohr
178d5a1a6dcSAndreas Gohr        $sql = "DROP TABLE ?";
179d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, 'data_'.$this->table);
180d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, 'multi_'.$this->table);
181d5a1a6dcSAndreas Gohr
182d5a1a6dcSAndreas Gohr        $sql = "DELETE FROM schema_assignments WHERE tbl = ?";
183d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, $this->table);
184d5a1a6dcSAndreas Gohr
185d5a1a6dcSAndreas Gohr        $sql = "DELETE FROM schema_assignments_patterns WHERE tbl = ?";
186d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, $this->table);
187d5a1a6dcSAndreas Gohr
188d5a1a6dcSAndreas Gohr        $sql = "SELECT T.id
189d5a1a6dcSAndreas Gohr                  FROM types T, schema_cols SC, schemas S
190d5a1a6dcSAndreas Gohr                 WHERE T.id = SC.tid
191d5a1a6dcSAndreas Gohr                   AND SC.sid = S.id
192d5a1a6dcSAndreas Gohr                   AND S.tbl = ?";
193d5a1a6dcSAndreas Gohr        $sql = "DELETE FROM types WHERE id IN ($sql)";
194d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, $this->table);
195d5a1a6dcSAndreas Gohr
196d5a1a6dcSAndreas Gohr        $sql = "SELECT id
197d5a1a6dcSAndreas Gohr                  FROM schemas
198d5a1a6dcSAndreas Gohr                 WHERE tbl = ?";
199d5a1a6dcSAndreas Gohr        $sql = "DELETE FROM schema_cols WHERE sid IN ($sql)";
200d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, $this->table);
201d5a1a6dcSAndreas Gohr
202d5a1a6dcSAndreas Gohr        $sql = "DELETE FROM schemas WHERE tbl = ?";
203d5a1a6dcSAndreas Gohr        $this->sqlite->query($sql, $this->table);
204d5a1a6dcSAndreas Gohr
205d5a1a6dcSAndreas Gohr        $this->sqlite->query('COMMIT TRANSACTION');
206d5a1a6dcSAndreas Gohr        $this->sqlite->query('VACUUM');
207f9f13d8cSAndreas Gohr
208f9f13d8cSAndreas Gohr        // a deleted schema should not be used anymore, but let's make sure it's somewhat sane anyway
209f9f13d8cSAndreas Gohr        $this->id = 0;
210f9f13d8cSAndreas Gohr        $this->chksum = '';
211f9f13d8cSAndreas Gohr        $this->columns = array();
212f9f13d8cSAndreas Gohr        $this->maxsort = 0;
213f9f13d8cSAndreas Gohr        $this->ts = 0;
214d5a1a6dcSAndreas Gohr    }
215d5a1a6dcSAndreas Gohr
216d5a1a6dcSAndreas Gohr    /**
2171c502704SAndreas Gohr     * @return string
2181c502704SAndreas Gohr     */
2191c502704SAndreas Gohr    public function getChksum() {
2201c502704SAndreas Gohr        return $this->chksum;
2211c502704SAndreas Gohr    }
2221c502704SAndreas Gohr
2231c502704SAndreas Gohr    /**
2241c502704SAndreas Gohr     * @return int
2251c502704SAndreas Gohr     */
2261c502704SAndreas Gohr    public function getId() {
2271c502704SAndreas Gohr        return $this->id;
2281c502704SAndreas Gohr    }
2291c502704SAndreas Gohr
2301c502704SAndreas Gohr    /**
231*fa7b96aaSMichael Grosse     * @return string
232*fa7b96aaSMichael Grosse     */
233*fa7b96aaSMichael Grosse    public function getUser() {
234*fa7b96aaSMichael Grosse        return $this->user;
235*fa7b96aaSMichael Grosse    }
236*fa7b96aaSMichael Grosse
237*fa7b96aaSMichael Grosse    /**
238ce206ec7SAndreas Gohr     * Returns a list of columns in this schema
239ce206ec7SAndreas Gohr     *
240ce206ec7SAndreas Gohr     * @param bool $withDisabled if false, disabled columns will not be returned
241ce206ec7SAndreas Gohr     * @return Column[]
2421c502704SAndreas Gohr     */
243ce206ec7SAndreas Gohr    public function getColumns($withDisabled = true) {
244ce206ec7SAndreas Gohr        if(!$withDisabled) {
245ce206ec7SAndreas Gohr            return array_filter(
246ce206ec7SAndreas Gohr                $this->columns,
247ce206ec7SAndreas Gohr                function (Column $col) {
248ce206ec7SAndreas Gohr                    return $col->isEnabled();
249ce206ec7SAndreas Gohr                }
250ce206ec7SAndreas Gohr            );
251ce206ec7SAndreas Gohr        }
252ce206ec7SAndreas Gohr
2531c502704SAndreas Gohr        return $this->columns;
2541c502704SAndreas Gohr    }
2551c502704SAndreas Gohr
256ae697e1fSAndreas Gohr    /**
2575742aea9SAndreas Gohr     * Find a column in the schema by its label
2585742aea9SAndreas Gohr     *
2595742aea9SAndreas Gohr     * Only enabled columns are returned!
2605742aea9SAndreas Gohr     *
2615742aea9SAndreas Gohr     * @param $name
2625742aea9SAndreas Gohr     * @return bool|Column
2635742aea9SAndreas Gohr     */
2645742aea9SAndreas Gohr    public function findColumn($name) {
2655742aea9SAndreas Gohr        foreach($this->columns as $col) {
2665742aea9SAndreas Gohr            if($col->isEnabled() && utf8_strtolower($col->getLabel()) == utf8_strtolower($name)) {
2675742aea9SAndreas Gohr                return $col;
2685742aea9SAndreas Gohr            }
2695742aea9SAndreas Gohr        }
2705742aea9SAndreas Gohr        return false;
2715742aea9SAndreas Gohr    }
2725742aea9SAndreas Gohr
2735742aea9SAndreas Gohr    /**
274ae697e1fSAndreas Gohr     * @return string
275ae697e1fSAndreas Gohr     */
276ae697e1fSAndreas Gohr    public function getTable() {
277ae697e1fSAndreas Gohr        return $this->table;
278ae697e1fSAndreas Gohr    }
2791c502704SAndreas Gohr
280ae697e1fSAndreas Gohr    /**
281ae697e1fSAndreas Gohr     * @return int the highest sort number used in this schema
282ae697e1fSAndreas Gohr     */
283ae697e1fSAndreas Gohr    public function getMaxsort() {
284ae697e1fSAndreas Gohr        return $this->maxsort;
285ae697e1fSAndreas Gohr    }
2861c502704SAndreas Gohr
287d486d6d7SAndreas Gohr    /**
288d486d6d7SAndreas Gohr     * @return string the JSON representing this schema
289d486d6d7SAndreas Gohr     */
290d486d6d7SAndreas Gohr    public function toJSON() {
291d486d6d7SAndreas Gohr        $data = array(
292d486d6d7SAndreas Gohr            'structversion' => $this->structversion,
293d486d6d7SAndreas Gohr            'schema' => $this->getTable(),
294d486d6d7SAndreas Gohr            'id' => $this->getId(),
295*fa7b96aaSMichael Grosse            'user' => $this->getUser(),
296d486d6d7SAndreas Gohr            'columns' => array()
297d486d6d7SAndreas Gohr        );
298d486d6d7SAndreas Gohr
299d486d6d7SAndreas Gohr        foreach($this->columns as $column) {
300d486d6d7SAndreas Gohr            $data['columns'][] = array(
301d486d6d7SAndreas Gohr                'colref' => $column->getColref(),
302d486d6d7SAndreas Gohr                'ismulti' => $column->isMulti(),
303d486d6d7SAndreas Gohr                'isenabled' => $column->isEnabled(),
304d486d6d7SAndreas Gohr                'sort' => $column->getSort(),
305d486d6d7SAndreas Gohr                'label' => $column->getLabel(),
306d486d6d7SAndreas Gohr                'class' => $column->getType()->getClass(),
307d486d6d7SAndreas Gohr                'config' => $column->getType()->getConfig(),
308d486d6d7SAndreas Gohr            );
309d486d6d7SAndreas Gohr        }
310d486d6d7SAndreas Gohr
311d486d6d7SAndreas Gohr        return json_encode($data, JSON_PRETTY_PRINT);
312d486d6d7SAndreas Gohr    }
313083afc55SAndreas Gohr}
314