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\Assignments; 11use dokuwiki\plugin\struct\meta\Schema; 12 13if(!defined('DOKU_INC')) die(); 14 15class helper_plugin_struct_imexport extends DokuWiki_Plugin { 16 17 private $sqlite; 18 19 20 /** 21 * this possibly duplicates @see helper_plugin_struct::getSchema() 22 */ 23 public function getAllSchemasList() { 24 return Schema::getAll(); 25 } 26 27 /** 28 * Delete all existing assignment patterns of a schema and replace them with the provided ones. 29 * 30 * @param string $schemaName 31 * @param string[] $patterns 32 */ 33 public function replaceSchemaAssignmentPatterns($schemaName, $patterns) { 34 /** @var \helper_plugin_struct_db $helper */ 35 $helper = plugin_load('helper', 'struct_db'); 36 $this->sqlite = $helper->getDB(false); 37 if(!$this->sqlite) return; 38 39 $schemaName = $this->sqlite->escape_string($schemaName); 40 $sql = array(); 41 $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'"; 42 $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'"; 43 foreach ($patterns as $pattern) { 44 $pattern = $this->sqlite->escape_string($pattern); 45 $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')"; 46 } 47 48 $this->sqlite->doTransaction($sql); 49 $assignments = Assignments::getInstance(); 50 $assignments->propagatePageAssignments($schemaName); 51 } 52 53 /** 54 * Returns array of patterns for the given Schema 55 * 56 * @param string $schemaName 57 * @return string[] 58 */ 59 public function getSchemaAssignmentPatterns($schemaName) { 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 $res = $this->sqlite->query($sql, $schemaName); 67 $patterns = $this->sqlite->res2arr($res); 68 $this->sqlite->res_close($res); 69 return array_map(function($elem){return $elem['pattern'];},$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 $schema = new Schema($schemaName); 80 if ($schema->getId() == 0) { 81 return false; 82 } 83 return $schema->toJSON(); 84 } 85 86 /** 87 * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new 88 * schema will be created. 89 * 90 * @param string $schemaName The name of the schema 91 * @param string $schemaJSON The structure of the schema as exportet by structs export functionality 92 * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used. 93 * @return bool|int the id of the new schema version or false on error. 94 * 95 * @throws dokuwiki\plugin\struct\meta\StructException 96 */ 97 public function importSchema($schemaName, $schemaJSON, $user = null) { 98 $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON); 99 if (!blank($user)) { 100 $importer->setUser($user); 101 } 102 $ok = $importer->build(); 103 return $ok; 104 } 105 106} 107