18ddf87afSAndreas Gohr<?php 28ddf87afSAndreas Gohr 3ba766201SAndreas Gohrnamespace dokuwiki\plugin\struct\meta; 48ddf87afSAndreas Gohr 58ddf87afSAndreas Gohr/** 68ddf87afSAndreas Gohr * Class SchemaImporter 78ddf87afSAndreas Gohr * 88ddf87afSAndreas Gohr * This works just like the schema builder, except that it expects a JSON structure as input 98ddf87afSAndreas Gohr * 10ba766201SAndreas Gohr * @package dokuwiki\plugin\struct\meta 118ddf87afSAndreas Gohr */ 12d6d97f60SAnna Dabrowskaclass SchemaImporter extends SchemaBuilder 13d6d97f60SAnna Dabrowska{ 148ddf87afSAndreas Gohr /** 158ddf87afSAndreas Gohr * Import a schema using JSON 168ddf87afSAndreas Gohr * 178ddf87afSAndreas Gohr * @param string $table 188ddf87afSAndreas Gohr * @param string $json 190549dcc5SAndreas Gohr * @todo sanity checking of the input data should be added 200549dcc5SAndreas Gohr * 218ddf87afSAndreas Gohr */ 22d6d97f60SAnna Dabrowska public function __construct($table, $json) 23d6d97f60SAnna Dabrowska { 247f803aa8SAnna Dabrowska parent::__construct($table, []); 258ddf87afSAndreas Gohr 268ddf87afSAndreas Gohr // number of existing columns 278ddf87afSAndreas Gohr $existing = count($this->oldschema->getColumns()); 288ddf87afSAndreas Gohr 298ddf87afSAndreas Gohr $input = json_decode($json, true); 30f2e141a0SAndreas Gohr if ($input === null) { 31f2e141a0SAndreas Gohr switch (json_last_error()) { 32f2e141a0SAndreas Gohr case JSON_ERROR_NONE: 33f2e141a0SAndreas Gohr $error = 'No errors'; 34f2e141a0SAndreas Gohr break; 35f2e141a0SAndreas Gohr case JSON_ERROR_DEPTH: 36f2e141a0SAndreas Gohr $error = 'Maximum stack depth exceeded'; 37f2e141a0SAndreas Gohr break; 38f2e141a0SAndreas Gohr case JSON_ERROR_STATE_MISMATCH: 39f2e141a0SAndreas Gohr $error = 'Underflow or the modes mismatch'; 40f2e141a0SAndreas Gohr break; 41f2e141a0SAndreas Gohr case JSON_ERROR_CTRL_CHAR: 42f2e141a0SAndreas Gohr $error = 'Unexpected control character found'; 43f2e141a0SAndreas Gohr break; 44f2e141a0SAndreas Gohr case JSON_ERROR_SYNTAX: 45f2e141a0SAndreas Gohr $error = 'Syntax error, malformed JSON'; 46f2e141a0SAndreas Gohr break; 47f2e141a0SAndreas Gohr case JSON_ERROR_UTF8: 48f2e141a0SAndreas Gohr $error = 'Malformed UTF-8 characters, possibly incorrectly encoded'; 49f2e141a0SAndreas Gohr break; 50f2e141a0SAndreas Gohr default: 51f2e141a0SAndreas Gohr $error = 'Unknown error'; 52f2e141a0SAndreas Gohr break; 53f2e141a0SAndreas Gohr } 54f2e141a0SAndreas Gohr 55f2e141a0SAndreas Gohr throw new StructException('JSON couldn\'t be decoded: ' . $error); 56f2e141a0SAndreas Gohr } 57*7234bfb1Ssplitbrain $config = $input['config'] ?? []; 58*7234bfb1Ssplitbrain $data = ['config' => json_encode($config), 'cols' => [], 'new' => []]; 598ddf87afSAndreas Gohr 608ddf87afSAndreas Gohr foreach ($input['columns'] as $column) { 618ddf87afSAndreas Gohr // config has to stay json 628ddf87afSAndreas Gohr $column['config'] = json_encode($column['config'], JSON_PRETTY_PRINT); 638ddf87afSAndreas Gohr 648ddf87afSAndreas Gohr if (!empty($column['colref']) && $column['colref'] <= $existing) { 658ddf87afSAndreas Gohr // update existing column 668ddf87afSAndreas Gohr $data['cols'][$column['colref']] = $column; 678ddf87afSAndreas Gohr } else { 688ddf87afSAndreas Gohr // add new column 698ddf87afSAndreas Gohr $data['new'][] = $column; 708ddf87afSAndreas Gohr } 718ddf87afSAndreas Gohr } 728ddf87afSAndreas Gohr 738ddf87afSAndreas Gohr $this->data = $data; 748ddf87afSAndreas Gohr } 758ddf87afSAndreas Gohr} 76