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 */ 14*d6d97f60SAnna Dabrowskaclass SchemaImporter extends SchemaBuilder 15*d6d97f60SAnna Dabrowska{ 168ddf87afSAndreas Gohr 178ddf87afSAndreas Gohr /** 188ddf87afSAndreas Gohr * Import a schema using JSON 198ddf87afSAndreas Gohr * 208ddf87afSAndreas Gohr * @todo sanity checking of the input data should be added 218ddf87afSAndreas Gohr * 228ddf87afSAndreas Gohr * @param string $table 238ddf87afSAndreas Gohr * @param string $json 248ddf87afSAndreas Gohr */ 25*d6d97f60SAnna Dabrowska public function __construct($table, $json) 26*d6d97f60SAnna Dabrowska { 277f803aa8SAnna Dabrowska parent::__construct($table, []); 288ddf87afSAndreas Gohr 298ddf87afSAndreas Gohr // number of existing columns 308ddf87afSAndreas Gohr $existing = count($this->oldschema->getColumns()); 318ddf87afSAndreas Gohr 328ddf87afSAndreas Gohr $input = json_decode($json, true); 33f2e141a0SAndreas Gohr if ($input === null) { 34f2e141a0SAndreas Gohr switch (json_last_error()) { 35f2e141a0SAndreas Gohr case JSON_ERROR_NONE: 36f2e141a0SAndreas Gohr $error = 'No errors'; 37f2e141a0SAndreas Gohr break; 38f2e141a0SAndreas Gohr case JSON_ERROR_DEPTH: 39f2e141a0SAndreas Gohr $error = 'Maximum stack depth exceeded'; 40f2e141a0SAndreas Gohr break; 41f2e141a0SAndreas Gohr case JSON_ERROR_STATE_MISMATCH: 42f2e141a0SAndreas Gohr $error = 'Underflow or the modes mismatch'; 43f2e141a0SAndreas Gohr break; 44f2e141a0SAndreas Gohr case JSON_ERROR_CTRL_CHAR: 45f2e141a0SAndreas Gohr $error = 'Unexpected control character found'; 46f2e141a0SAndreas Gohr break; 47f2e141a0SAndreas Gohr case JSON_ERROR_SYNTAX: 48f2e141a0SAndreas Gohr $error = 'Syntax error, malformed JSON'; 49f2e141a0SAndreas Gohr break; 50f2e141a0SAndreas Gohr case JSON_ERROR_UTF8: 51f2e141a0SAndreas Gohr $error = 'Malformed UTF-8 characters, possibly incorrectly encoded'; 52f2e141a0SAndreas Gohr break; 53f2e141a0SAndreas Gohr default: 54f2e141a0SAndreas Gohr $error = 'Unknown error'; 55f2e141a0SAndreas Gohr break; 56f2e141a0SAndreas Gohr } 57f2e141a0SAndreas Gohr 58f2e141a0SAndreas Gohr throw new StructException('JSON couldn\'t be decoded: ' . $error); 59f2e141a0SAndreas Gohr } 60127d6bacSMichael Große $config = isset($input['config']) ? $input['config'] : array(); 618ddf87afSAndreas Gohr $data = array( 62127d6bacSMichael Große 'config' => json_encode($config), 638ddf87afSAndreas Gohr 'cols' => array(), 640845722bSAndreas Gohr 'new' => array(), 658ddf87afSAndreas Gohr ); 668ddf87afSAndreas Gohr 678ddf87afSAndreas Gohr foreach ($input['columns'] as $column) { 688ddf87afSAndreas Gohr // config has to stay json 698ddf87afSAndreas Gohr $column['config'] = json_encode($column['config'], JSON_PRETTY_PRINT); 708ddf87afSAndreas Gohr 718ddf87afSAndreas Gohr if (!empty($column['colref']) && $column['colref'] <= $existing) { 728ddf87afSAndreas Gohr // update existing column 738ddf87afSAndreas Gohr $data['cols'][$column['colref']] = $column; 748ddf87afSAndreas Gohr } else { 758ddf87afSAndreas Gohr // add new column 768ddf87afSAndreas Gohr $data['new'][] = $column; 778ddf87afSAndreas Gohr } 788ddf87afSAndreas Gohr } 798ddf87afSAndreas Gohr 808ddf87afSAndreas Gohr $this->data = $data; 818ddf87afSAndreas Gohr } 828ddf87afSAndreas Gohr} 83