17c5fd8d6SMichael Grosse<?php 27c5fd8d6SMichael Grosse/** 37c5fd8d6SMichael Grosse * DokuWiki Plugin struct (Helper Component) 47c5fd8d6SMichael Grosse * 57c5fd8d6SMichael Grosse * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 67c5fd8d6SMichael Grosse * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 77c5fd8d6SMichael Grosse */ 87c5fd8d6SMichael Grosse 97c5fd8d6SMichael Grosse// must be run within Dokuwiki 10*7cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 11aeca15adSMichael Grosseuse dokuwiki\plugin\struct\meta\Schema; 12aeca15adSMichael Grosse 137c5fd8d6SMichael Grosseif(!defined('DOKU_INC')) die(); 147c5fd8d6SMichael Grosse 157c5fd8d6SMichael Grosseclass helper_plugin_struct_imexport extends DokuWiki_Plugin { 167c5fd8d6SMichael Grosse 177c5fd8d6SMichael Grosse private $sqlite; 187c5fd8d6SMichael Grosse 19fa7b96aaSMichael Grosse 20fa7b96aaSMichael Grosse /** 21fa7b96aaSMichael Grosse * this possibly duplicates @see helper_plugin_struct::getSchema() 22fa7b96aaSMichael Grosse */ 237c5fd8d6SMichael Grosse public function getAllSchemasList() { 24aeca15adSMichael Grosse return Schema::getAll(); 257c5fd8d6SMichael Grosse } 267c5fd8d6SMichael Grosse 277c5fd8d6SMichael Grosse /** 28c4ce7499SMichael Grosse * Delete all existing assignment patterns of a schema and replace them with the provided ones. 29c4ce7499SMichael Grosse * 30c4ce7499SMichael Grosse * @param string $schemaName 31aeca15adSMichael Grosse * @param string[] $patterns 327c5fd8d6SMichael Grosse */ 33c4ce7499SMichael Grosse public function replaceSchemaAssignmentPatterns($schemaName, $patterns) { 347c5fd8d6SMichael Grosse /** @var \helper_plugin_struct_db $helper */ 35aeca15adSMichael Grosse $helper = plugin_load('helper', 'struct_db'); 36*7cbcfbdbSAndreas Gohr $this->sqlite = $helper->getDB(false); 37*7cbcfbdbSAndreas Gohr if(!$this->sqlite) return; 38*7cbcfbdbSAndreas Gohr 39c4ce7499SMichael Grosse $schemaName = $this->sqlite->escape_string($schemaName); 407c5fd8d6SMichael Grosse $sql = array(); 41c4ce7499SMichael Grosse $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'"; 42c4ce7499SMichael Grosse $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'"; 437c5fd8d6SMichael Grosse foreach ($patterns as $pattern) { 441bd268d2SMichael Grosse $pattern = $this->sqlite->escape_string($pattern); 45c4ce7499SMichael Grosse $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')"; 467c5fd8d6SMichael Grosse } 477c5fd8d6SMichael Grosse 487c5fd8d6SMichael Grosse $this->sqlite->doTransaction($sql); 49*7cbcfbdbSAndreas Gohr $assignments = Assignments::getInstance(); 50c4ce7499SMichael Grosse $assignments->propagatePageAssignments($schemaName); 517c5fd8d6SMichael Grosse } 527c5fd8d6SMichael Grosse 53c4ce7499SMichael Grosse /** 54c4ce7499SMichael Grosse * Returns array of patterns for the given Schema 55c4ce7499SMichael Grosse * 56c4ce7499SMichael Grosse * @param string $schemaName 57c4ce7499SMichael Grosse * @return string[] 58c4ce7499SMichael Grosse */ 59c4ce7499SMichael Grosse public function getSchemaAssignmentPatterns($schemaName) { 607c5fd8d6SMichael Grosse /** @var \helper_plugin_struct_db $helper */ 61aeca15adSMichael Grosse $helper = plugin_load('helper', 'struct_db'); 62*7cbcfbdbSAndreas Gohr $this->sqlite = $helper->getDB(false); 63*7cbcfbdbSAndreas Gohr if(!$this->sqlite) return array(); 647c5fd8d6SMichael Grosse 657c5fd8d6SMichael Grosse $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?'; 66c4ce7499SMichael Grosse $res = $this->sqlite->query($sql, $schemaName); 677c5fd8d6SMichael Grosse $patterns = $this->sqlite->res2arr($res); 687c5fd8d6SMichael Grosse $this->sqlite->res_close($res); 697c5fd8d6SMichael Grosse return array_map(function($elem){return $elem['pattern'];},$patterns); 707c5fd8d6SMichael Grosse } 717c5fd8d6SMichael Grosse 72c4ce7499SMichael Grosse /** 7326d59c4aSMichael Grosse * Get the json of the current version of the given schema or false if the schema doesn't exist. 74c4ce7499SMichael Grosse * 75c4ce7499SMichael Grosse * @param string $schemaName 7626d59c4aSMichael Grosse * @return string|bool The json string or false if the schema doesn't exist 77c4ce7499SMichael Grosse */ 78c4ce7499SMichael Grosse public function getCurrentSchemaJSON($schemaName) { 7926d59c4aSMichael Grosse $schema = new Schema($schemaName); 8026d59c4aSMichael Grosse if ($schema->getId() == 0) { 8126d59c4aSMichael Grosse return false; 8226d59c4aSMichael Grosse } 8326d59c4aSMichael Grosse return $schema->toJSON(); 84fa7b96aaSMichael Grosse } 85fa7b96aaSMichael Grosse 86c4ce7499SMichael Grosse /** 87c4ce7499SMichael Grosse * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new 88c4ce7499SMichael Grosse * schema will be created. 89c4ce7499SMichael Grosse * 90c4ce7499SMichael Grosse * @param string $schemaName The name of the schema 91c4ce7499SMichael Grosse * @param string $schemaJSON The structure of the schema as exportet by structs export functionality 92c4ce7499SMichael Grosse * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used. 93c4ce7499SMichael Grosse * @return bool|int the id of the new schema version or false on error. 9457920350SMichael Grosse * 9557920350SMichael Grosse * @throws dokuwiki\plugin\struct\meta\StructException 96c4ce7499SMichael Grosse */ 97c4ce7499SMichael Grosse public function importSchema($schemaName, $schemaJSON, $user = null) { 98c4ce7499SMichael Grosse $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON); 9978bff02fSMichael Grosse if (!blank($user)) { 10078bff02fSMichael Grosse $importer->setUser($user); 10178bff02fSMichael Grosse } 10278bff02fSMichael Grosse $ok = $importer->build(); 103fa7b96aaSMichael Grosse return $ok; 104fa7b96aaSMichael Grosse } 105fa7b96aaSMichael Grosse 1067c5fd8d6SMichael Grosse} 107