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 230845722bSAndreas Gohr * @param bool $islookup 248ddf87afSAndreas Gohr */ 257e98290dSAndreas Gohr public function __construct($table, $json, $islookup=false) { 260845722bSAndreas Gohr parent::__construct($table, array('islookup' => $islookup)); 278ddf87afSAndreas Gohr 288ddf87afSAndreas Gohr // number of existing columns 298ddf87afSAndreas Gohr $existing = count($this->oldschema->getColumns()); 308ddf87afSAndreas Gohr 318ddf87afSAndreas Gohr $input = json_decode($json, true); 32f2e141a0SAndreas Gohr if($input === null) { 33f2e141a0SAndreas Gohr switch(json_last_error()) { 34f2e141a0SAndreas Gohr case JSON_ERROR_NONE: 35f2e141a0SAndreas Gohr $error = 'No errors'; 36f2e141a0SAndreas Gohr break; 37f2e141a0SAndreas Gohr case JSON_ERROR_DEPTH: 38f2e141a0SAndreas Gohr $error = 'Maximum stack depth exceeded'; 39f2e141a0SAndreas Gohr break; 40f2e141a0SAndreas Gohr case JSON_ERROR_STATE_MISMATCH: 41f2e141a0SAndreas Gohr $error = 'Underflow or the modes mismatch'; 42f2e141a0SAndreas Gohr break; 43f2e141a0SAndreas Gohr case JSON_ERROR_CTRL_CHAR: 44f2e141a0SAndreas Gohr $error = 'Unexpected control character found'; 45f2e141a0SAndreas Gohr break; 46f2e141a0SAndreas Gohr case JSON_ERROR_SYNTAX: 47f2e141a0SAndreas Gohr $error = 'Syntax error, malformed JSON'; 48f2e141a0SAndreas Gohr break; 49f2e141a0SAndreas Gohr case JSON_ERROR_UTF8: 50f2e141a0SAndreas Gohr $error = 'Malformed UTF-8 characters, possibly incorrectly encoded'; 51f2e141a0SAndreas Gohr break; 52f2e141a0SAndreas Gohr default: 53f2e141a0SAndreas Gohr $error = 'Unknown error'; 54f2e141a0SAndreas Gohr break; 55f2e141a0SAndreas Gohr } 56f2e141a0SAndreas Gohr 57f2e141a0SAndreas Gohr throw new StructException('JSON couldn\'t be decoded: ' . $error); 58f2e141a0SAndreas Gohr } 59*127d6bacSMichael Große $config = isset($input['config']) ? $input['config'] : array(); 608ddf87afSAndreas Gohr $data = array( 61*127d6bacSMichael Große 'config' => json_encode($config), 628ddf87afSAndreas Gohr 'cols' => array(), 630845722bSAndreas Gohr 'new' => array(), 648ddf87afSAndreas Gohr ); 658ddf87afSAndreas Gohr 668ddf87afSAndreas Gohr foreach($input['columns'] as $column) { 678ddf87afSAndreas Gohr // config has to stay json 688ddf87afSAndreas Gohr $column['config'] = json_encode($column['config'], JSON_PRETTY_PRINT); 698ddf87afSAndreas Gohr 708ddf87afSAndreas Gohr if(!empty($column['colref']) && $column['colref'] <= $existing) { 718ddf87afSAndreas Gohr // update existing column 728ddf87afSAndreas Gohr $data['cols'][$column['colref']] = $column; 738ddf87afSAndreas Gohr } else { 748ddf87afSAndreas Gohr // add new column 758ddf87afSAndreas Gohr $data['new'][] = $column; 768ddf87afSAndreas Gohr } 778ddf87afSAndreas Gohr } 788ddf87afSAndreas Gohr 798ddf87afSAndreas Gohr $this->data = $data; 808ddf87afSAndreas Gohr } 818ddf87afSAndreas Gohr 828ddf87afSAndreas Gohr} 83