17c5fd8d6SMichael Grosse<?php 2*d6d97f60SAnna Dabrowska 37c5fd8d6SMichael Grosse/** 47c5fd8d6SMichael Grosse * DokuWiki Plugin struct (Helper Component) 57c5fd8d6SMichael Grosse * 67c5fd8d6SMichael Grosse * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 77c5fd8d6SMichael Grosse * @author Andreas Gohr, Michael Große <dokuwiki@cosmocode.de> 87c5fd8d6SMichael Grosse */ 97c5fd8d6SMichael Grosse 107c5fd8d6SMichael Grosse// must be run within Dokuwiki 117cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments; 12aeca15adSMichael Grosseuse dokuwiki\plugin\struct\meta\Schema; 13aeca15adSMichael Grosse 147c5fd8d6SMichael Grosseif (!defined('DOKU_INC')) die(); 157c5fd8d6SMichael Grosse 16*d6d97f60SAnna Dabrowskaclass helper_plugin_struct_imexport extends DokuWiki_Plugin 17*d6d97f60SAnna Dabrowska{ 187c5fd8d6SMichael Grosse 197c5fd8d6SMichael Grosse private $sqlite; 207c5fd8d6SMichael Grosse 21fa7b96aaSMichael Grosse 22fa7b96aaSMichael Grosse /** 23fa7b96aaSMichael Grosse * this possibly duplicates @see helper_plugin_struct::getSchema() 24fa7b96aaSMichael Grosse */ 25*d6d97f60SAnna Dabrowska public function getAllSchemasList() 26*d6d97f60SAnna Dabrowska { 27aeca15adSMichael Grosse return Schema::getAll(); 287c5fd8d6SMichael Grosse } 297c5fd8d6SMichael Grosse 307c5fd8d6SMichael Grosse /** 31c4ce7499SMichael Grosse * Delete all existing assignment patterns of a schema and replace them with the provided ones. 32c4ce7499SMichael Grosse * 33c4ce7499SMichael Grosse * @param string $schemaName 34aeca15adSMichael Grosse * @param string[] $patterns 357c5fd8d6SMichael Grosse */ 36*d6d97f60SAnna Dabrowska public function replaceSchemaAssignmentPatterns($schemaName, $patterns) 37*d6d97f60SAnna Dabrowska { 387c5fd8d6SMichael Grosse /** @var \helper_plugin_struct_db $helper */ 39aeca15adSMichael Grosse $helper = plugin_load('helper', 'struct_db'); 407cbcfbdbSAndreas Gohr $this->sqlite = $helper->getDB(false); 417cbcfbdbSAndreas Gohr if (!$this->sqlite) return; 427cbcfbdbSAndreas Gohr 43c4ce7499SMichael Grosse $schemaName = $this->sqlite->escape_string($schemaName); 447c5fd8d6SMichael Grosse $sql = array(); 45c4ce7499SMichael Grosse $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'"; 46c4ce7499SMichael Grosse $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'"; 477c5fd8d6SMichael Grosse foreach ($patterns as $pattern) { 481bd268d2SMichael Grosse $pattern = $this->sqlite->escape_string($pattern); 49c4ce7499SMichael Grosse $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')"; 507c5fd8d6SMichael Grosse } 517c5fd8d6SMichael Grosse 527c5fd8d6SMichael Grosse $this->sqlite->doTransaction($sql); 537cbcfbdbSAndreas Gohr $assignments = Assignments::getInstance(); 54c4ce7499SMichael Grosse $assignments->propagatePageAssignments($schemaName); 557c5fd8d6SMichael Grosse } 567c5fd8d6SMichael Grosse 57c4ce7499SMichael Grosse /** 58c4ce7499SMichael Grosse * Returns array of patterns for the given Schema 59c4ce7499SMichael Grosse * 60c4ce7499SMichael Grosse * @param string $schemaName 61c4ce7499SMichael Grosse * @return string[] 62c4ce7499SMichael Grosse */ 63*d6d97f60SAnna Dabrowska public function getSchemaAssignmentPatterns($schemaName) 64*d6d97f60SAnna Dabrowska { 657c5fd8d6SMichael Grosse /** @var \helper_plugin_struct_db $helper */ 66aeca15adSMichael Grosse $helper = plugin_load('helper', 'struct_db'); 677cbcfbdbSAndreas Gohr $this->sqlite = $helper->getDB(false); 687cbcfbdbSAndreas Gohr if (!$this->sqlite) return array(); 697c5fd8d6SMichael Grosse 707c5fd8d6SMichael Grosse $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?'; 71c4ce7499SMichael Grosse $res = $this->sqlite->query($sql, $schemaName); 727c5fd8d6SMichael Grosse $patterns = $this->sqlite->res2arr($res); 737c5fd8d6SMichael Grosse $this->sqlite->res_close($res); 74*d6d97f60SAnna Dabrowska return array_map(function ($elem) { 75*d6d97f60SAnna Dabrowska return $elem['pattern']; 76*d6d97f60SAnna Dabrowska }, $patterns); 777c5fd8d6SMichael Grosse } 787c5fd8d6SMichael Grosse 79c4ce7499SMichael Grosse /** 8026d59c4aSMichael Grosse * Get the json of the current version of the given schema or false if the schema doesn't exist. 81c4ce7499SMichael Grosse * 82c4ce7499SMichael Grosse * @param string $schemaName 8326d59c4aSMichael Grosse * @return string|bool The json string or false if the schema doesn't exist 84c4ce7499SMichael Grosse */ 85*d6d97f60SAnna Dabrowska public function getCurrentSchemaJSON($schemaName) 86*d6d97f60SAnna Dabrowska { 8726d59c4aSMichael Grosse $schema = new Schema($schemaName); 8826d59c4aSMichael Grosse if ($schema->getId() == 0) { 8926d59c4aSMichael Grosse return false; 9026d59c4aSMichael Grosse } 9126d59c4aSMichael Grosse return $schema->toJSON(); 92fa7b96aaSMichael Grosse } 93fa7b96aaSMichael Grosse 94c4ce7499SMichael Grosse /** 95c4ce7499SMichael Grosse * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new 96c4ce7499SMichael Grosse * schema will be created. 97c4ce7499SMichael Grosse * 98c4ce7499SMichael Grosse * @param string $schemaName The name of the schema 99c4ce7499SMichael Grosse * @param string $schemaJSON The structure of the schema as exportet by structs export functionality 100c4ce7499SMichael Grosse * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used. 101c4ce7499SMichael Grosse * @return bool|int the id of the new schema version or false on error. 10257920350SMichael Grosse * 10357920350SMichael Grosse * @throws dokuwiki\plugin\struct\meta\StructException 104c4ce7499SMichael Grosse */ 105*d6d97f60SAnna Dabrowska public function importSchema($schemaName, $schemaJSON, $user = null) 106*d6d97f60SAnna Dabrowska { 107c4ce7499SMichael Grosse $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON); 10878bff02fSMichael Grosse if (!blank($user)) { 10978bff02fSMichael Grosse $importer->setUser($user); 11078bff02fSMichael Grosse } 11178bff02fSMichael Grosse $ok = $importer->build(); 112fa7b96aaSMichael Grosse return $ok; 113fa7b96aaSMichael Grosse } 1147c5fd8d6SMichael Grosse} 115