xref: /plugin/struct/meta/SchemaImporter.php (revision 7f803aa873894c400484b80e8b543a49fdf1f9d5)
18ddf87afSAndreas Gohr<?php
28ddf87afSAndreas Gohr
3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta;
48ddf87afSAndreas Gohr
545560cc7SAndreas Gohrif(!defined('JSON_PRETTY_PRINT')) define('JSON_PRETTY_PRINT', 0); // PHP 5.3 compatibility
645560cc7SAndreas Gohr
78ddf87afSAndreas Gohr/**
88ddf87afSAndreas Gohr * Class SchemaImporter
98ddf87afSAndreas Gohr *
108ddf87afSAndreas Gohr * This works just like the schema builder, except that it expects a JSON structure as input
118ddf87afSAndreas Gohr *
12ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta
138ddf87afSAndreas Gohr */
148ddf87afSAndreas Gohrclass SchemaImporter extends SchemaBuilder {
158ddf87afSAndreas Gohr
168ddf87afSAndreas Gohr    /**
178ddf87afSAndreas Gohr     * Import a schema using JSON
188ddf87afSAndreas Gohr     *
198ddf87afSAndreas Gohr     * @todo sanity checking of the input data should be added
208ddf87afSAndreas Gohr     *
218ddf87afSAndreas Gohr     * @param string $table
228ddf87afSAndreas Gohr     * @param string $json
238ddf87afSAndreas Gohr     */
24*7f803aa8SAnna Dabrowska    public function __construct($table, $json) {
25*7f803aa8SAnna Dabrowska        parent::__construct($table, []);
268ddf87afSAndreas Gohr
278ddf87afSAndreas Gohr        // number of existing columns
288ddf87afSAndreas Gohr        $existing = count($this->oldschema->getColumns());
298ddf87afSAndreas Gohr
308ddf87afSAndreas Gohr        $input = json_decode($json, true);
31f2e141a0SAndreas Gohr        if($input === null) {
32f2e141a0SAndreas Gohr            switch(json_last_error()) {
33f2e141a0SAndreas Gohr                case JSON_ERROR_NONE:
34f2e141a0SAndreas Gohr                    $error = 'No errors';
35f2e141a0SAndreas Gohr                    break;
36f2e141a0SAndreas Gohr                case JSON_ERROR_DEPTH:
37f2e141a0SAndreas Gohr                    $error = 'Maximum stack depth exceeded';
38f2e141a0SAndreas Gohr                    break;
39f2e141a0SAndreas Gohr                case JSON_ERROR_STATE_MISMATCH:
40f2e141a0SAndreas Gohr                    $error = 'Underflow or the modes mismatch';
41f2e141a0SAndreas Gohr                    break;
42f2e141a0SAndreas Gohr                case JSON_ERROR_CTRL_CHAR:
43f2e141a0SAndreas Gohr                    $error = 'Unexpected control character found';
44f2e141a0SAndreas Gohr                    break;
45f2e141a0SAndreas Gohr                case JSON_ERROR_SYNTAX:
46f2e141a0SAndreas Gohr                    $error = 'Syntax error, malformed JSON';
47f2e141a0SAndreas Gohr                    break;
48f2e141a0SAndreas Gohr                case JSON_ERROR_UTF8:
49f2e141a0SAndreas Gohr                    $error = 'Malformed UTF-8 characters, possibly incorrectly encoded';
50f2e141a0SAndreas Gohr                    break;
51f2e141a0SAndreas Gohr                default:
52f2e141a0SAndreas Gohr                    $error = 'Unknown error';
53f2e141a0SAndreas Gohr                    break;
54f2e141a0SAndreas Gohr            }
55f2e141a0SAndreas Gohr
56f2e141a0SAndreas Gohr            throw new StructException('JSON couldn\'t be decoded: ' . $error);
57f2e141a0SAndreas Gohr        }
58127d6bacSMichael Große        $config = isset($input['config']) ? $input['config'] : array();
598ddf87afSAndreas Gohr        $data = array(
60127d6bacSMichael Große            'config' => json_encode($config),
618ddf87afSAndreas Gohr            'cols' => array(),
620845722bSAndreas Gohr            'new' => array(),
638ddf87afSAndreas Gohr        );
648ddf87afSAndreas Gohr
658ddf87afSAndreas Gohr        foreach($input['columns'] as $column) {
668ddf87afSAndreas Gohr            // config has to stay json
678ddf87afSAndreas Gohr            $column['config'] = json_encode($column['config'], JSON_PRETTY_PRINT);
688ddf87afSAndreas Gohr
698ddf87afSAndreas Gohr            if(!empty($column['colref']) && $column['colref'] <= $existing) {
708ddf87afSAndreas Gohr                // update existing column
718ddf87afSAndreas Gohr                $data['cols'][$column['colref']] = $column;
728ddf87afSAndreas Gohr            } else {
738ddf87afSAndreas Gohr                // add new column
748ddf87afSAndreas Gohr                $data['new'][] = $column;
758ddf87afSAndreas Gohr            }
768ddf87afSAndreas Gohr        }
778ddf87afSAndreas Gohr
788ddf87afSAndreas Gohr        $this->data = $data;
798ddf87afSAndreas Gohr    }
808ddf87afSAndreas Gohr
818ddf87afSAndreas Gohr}
82