/** * Get the json of the current version of the given schema or false if the schema doesn't exist. * * @param string $schemaName * @return string|bool The json string or false if the schema doesn't exist */ public function getCurrentSchemaJSON($schemaName) { $schema = new Schema($schemaName); if ($schema->getId() == 0) { return false; } return $schema->toJSON(); }
/** * Should carry out any processing required by the plugin. */ public function handle() { global $INPUT; global $ID; global $config_cascade; $config_file_path = end($config_cascade['main']['local']); // form submit $table = Schema::cleanTableName($INPUT->str('table')); if ($table && $INPUT->bool('save') && checkSecurityToken()) { $builder = new SchemaBuilder($table, $INPUT->arr('schema')); if (!$builder->build()) { msg('something went wrong while saving', -1); } touch($config_file_path); } // export if ($table && $INPUT->bool('export')) { $builder = new Schema($table); header('Content-Type: application/json'); header("Content-Disposition: attachment; filename={$table}.struct.json"); echo $builder->toJSON(); exit; } // import if ($table && $INPUT->bool('import')) { if (isset($_FILES['schemafile']['tmp_name'])) { $json = io_readFile($_FILES['schemafile']['tmp_name'], false); if (!$json) { msg('Something went wrong with the upload', -1); } else { $builder = new SchemaImporter($table, $json, $INPUT->bool('lookup')); if (!$builder->build()) { msg('something went wrong while saving', -1); } touch($config_file_path); } } } // delete if ($table && $INPUT->bool('delete')) { if ($table != $INPUT->str('confirm')) { msg($this->getLang('del_fail'), -1); } else { try { $schema = new Schema($table); $schema->delete(); msg($this->getLang('del_ok'), 1); touch($config_file_path); send_redirect(wl($ID, array('do' => 'admin', 'page' => 'struct_schemas'), true, '&')); } catch (StructException $e) { msg(hsc($e->getMessage()), -1); } } } }
public function test_import_export() { $sb = new meta\SchemaImporter('foobar', file_get_contents(__DIR__ . '/json/schema1.struct.json')); $this->assertTrue((bool) $sb->build()); $schema = new meta\Schema('foobar'); $expect = json_decode(file_get_contents(__DIR__ . '/json/schema1.struct.json'), true); $actual = json_decode($schema->toJSON(), true); // we don't expect this to match unset($expect['structversion']); unset($actual['structversion']); $expect['schema'] = 'foobar'; // we exported the new schema $this->assertEquals($expect, $actual); }