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