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. 70 * If the schema doesn't exist, then the returned JSON will have id 0. 71 * 72 * @param string $schemaName 73 * @return string The json string 74 */ 75 public function getCurrentSchemaJSON($schemaName) { 76 $schemaName = new Schema($schemaName); 77 return $schemaName->toJSON(); 78 } 79 80 /** 81 * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new 82 * schema will be created. 83 * 84 * @param string $schemaName The name of the schema 85 * @param string $schemaJSON The structure of the schema as exportet by structs export functionality 86 * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used. 87 * @return bool|int the id of the new schema version or false on error. 88 * 89 * @throws dokuwiki\plugin\struct\meta\StructException 90 */ 91 public function importSchema($schemaName, $schemaJSON, $user = null) { 92 $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON); 93 if (!blank($user)) { 94 $importer->setUser($user); 95 } 96 $ok = $importer->build(); 97 return $ok; 98 } 99 100} 101