1<?php 2 3/** 4 * DokuWiki Plugin struct (Helper Component) 5 * 6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 7 * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 8 */ 9 10use dokuwiki\plugin\struct\meta\Assignments; 11use dokuwiki\plugin\struct\meta\Schema; 12 13class helper_plugin_struct_imexport extends DokuWiki_Plugin 14{ 15 private $sqlite; 16 17 /** 18 * this possibly duplicates @see helper_plugin_struct::getSchema() 19 */ 20 public function getAllSchemasList() 21 { 22 return Schema::getAll(); 23 } 24 25 /** 26 * Delete all existing assignment patterns of a schema and replace them with the provided ones. 27 * 28 * @param string $schemaName 29 * @param string[] $patterns 30 */ 31 public function replaceSchemaAssignmentPatterns($schemaName, $patterns) 32 { 33 /** @var \helper_plugin_struct_db $helper */ 34 $helper = plugin_load('helper', 'struct_db'); 35 $this->sqlite = $helper->getDB(false); 36 if (!$this->sqlite) return; 37 38 $schemaName = $this->sqlite->escape_string($schemaName); 39 $sql = array(); 40 $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'"; 41 $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'"; 42 foreach ($patterns as $pattern) { 43 $pattern = $this->sqlite->escape_string($pattern); 44 $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')"; 45 } 46 47 $this->sqlite->doTransaction($sql); 48 $assignments = Assignments::getInstance(); 49 $assignments->propagatePageAssignments($schemaName); 50 } 51 52 /** 53 * Returns array of patterns for the given Schema 54 * 55 * @param string $schemaName 56 * @return string[] 57 */ 58 public function getSchemaAssignmentPatterns($schemaName) 59 { 60 /** @var \helper_plugin_struct_db $helper */ 61 $helper = plugin_load('helper', 'struct_db'); 62 $this->sqlite = $helper->getDB(false); 63 if (!$this->sqlite) return array(); 64 65 $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?'; 66 $patterns = $this->sqlite->queryAll($sql, $schemaName); 67 return array_map(function ($elem) { 68 return $elem['pattern']; 69 }, $patterns); 70 } 71 72 /** 73 * Get the json of the current version of the given schema or false if the schema doesn't exist. 74 * 75 * @param string $schemaName 76 * @return string|bool The json string or false if the schema doesn't exist 77 */ 78 public function getCurrentSchemaJSON($schemaName) 79 { 80 $schema = new Schema($schemaName); 81 if ($schema->getId() == 0) { 82 return false; 83 } 84 return $schema->toJSON(); 85 } 86 87 /** 88 * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new 89 * schema will be created. 90 * 91 * @param string $schemaName The name of the schema 92 * @param string $schemaJSON The structure of the schema as exportet by structs export functionality 93 * @param string $user optional, the user that should be set in the schemas history. 94 * If blank, the current user is used. 95 * @return bool|int the id of the new schema version or false on error. 96 * 97 * @throws dokuwiki\plugin\struct\meta\StructException 98 */ 99 public function importSchema($schemaName, $schemaJSON, $user = null) 100 { 101 $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON); 102 if (!blank($user)) { 103 $importer->setUser($user); 104 } 105 $ok = $importer->build(); 106 return $ok; 107 } 108} 109