xref: /plugin/struct/helper/imexport.php (revision 748e747f37aa44250ee32847b5fc3ff1e47f0835)
17c5fd8d6SMichael Grosse<?php
2d6d97f60SAnna 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
107cbcfbdbSAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
11aeca15adSMichael Grosseuse dokuwiki\plugin\struct\meta\Schema;
12aeca15adSMichael Grosse
13d6d97f60SAnna Dabrowskaclass helper_plugin_struct_imexport extends DokuWiki_Plugin
14d6d97f60SAnna Dabrowska{
157c5fd8d6SMichael Grosse
167c5fd8d6SMichael Grosse    private $sqlite;
177c5fd8d6SMichael Grosse
18fa7b96aaSMichael Grosse
19fa7b96aaSMichael Grosse    /**
20fa7b96aaSMichael Grosse     * this possibly duplicates @see helper_plugin_struct::getSchema()
21fa7b96aaSMichael Grosse     */
22d6d97f60SAnna Dabrowska    public function getAllSchemasList()
23d6d97f60SAnna Dabrowska    {
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     */
33d6d97f60SAnna Dabrowska    public function replaceSchemaAssignmentPatterns($schemaName, $patterns)
34d6d97f60SAnna Dabrowska    {
357c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
36aeca15adSMichael Grosse        $helper = plugin_load('helper', 'struct_db');
377cbcfbdbSAndreas Gohr        $this->sqlite = $helper->getDB(false);
387cbcfbdbSAndreas Gohr        if (!$this->sqlite) return;
397cbcfbdbSAndreas Gohr
40c4ce7499SMichael Grosse        $schemaName = $this->sqlite->escape_string($schemaName);
417c5fd8d6SMichael Grosse        $sql = array();
42c4ce7499SMichael Grosse        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'";
43c4ce7499SMichael Grosse        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'";
447c5fd8d6SMichael Grosse        foreach ($patterns as $pattern) {
451bd268d2SMichael Grosse            $pattern = $this->sqlite->escape_string($pattern);
46c4ce7499SMichael Grosse            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')";
477c5fd8d6SMichael Grosse        }
487c5fd8d6SMichael Grosse
497c5fd8d6SMichael Grosse        $this->sqlite->doTransaction($sql);
507cbcfbdbSAndreas Gohr        $assignments = Assignments::getInstance();
51c4ce7499SMichael Grosse        $assignments->propagatePageAssignments($schemaName);
527c5fd8d6SMichael Grosse    }
537c5fd8d6SMichael Grosse
54c4ce7499SMichael Grosse    /**
55c4ce7499SMichael Grosse     * Returns array of patterns for the given Schema
56c4ce7499SMichael Grosse     *
57c4ce7499SMichael Grosse     * @param string $schemaName
58c4ce7499SMichael Grosse     * @return string[]
59c4ce7499SMichael Grosse     */
60d6d97f60SAnna Dabrowska    public function getSchemaAssignmentPatterns($schemaName)
61d6d97f60SAnna Dabrowska    {
627c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
63aeca15adSMichael Grosse        $helper = plugin_load('helper', 'struct_db');
647cbcfbdbSAndreas Gohr        $this->sqlite = $helper->getDB(false);
657cbcfbdbSAndreas Gohr        if (!$this->sqlite) return array();
667c5fd8d6SMichael Grosse
677c5fd8d6SMichael Grosse        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
68c4ce7499SMichael Grosse        $res = $this->sqlite->query($sql, $schemaName);
697c5fd8d6SMichael Grosse        $patterns = $this->sqlite->res2arr($res);
707c5fd8d6SMichael Grosse        $this->sqlite->res_close($res);
71d6d97f60SAnna Dabrowska        return array_map(function ($elem) {
72d6d97f60SAnna Dabrowska            return $elem['pattern'];
73d6d97f60SAnna Dabrowska        }, $patterns);
747c5fd8d6SMichael Grosse    }
757c5fd8d6SMichael Grosse
76c4ce7499SMichael Grosse    /**
7726d59c4aSMichael Grosse     * Get the json of the current version of the given schema or false if the schema doesn't exist.
78c4ce7499SMichael Grosse     *
79c4ce7499SMichael Grosse     * @param string $schemaName
8026d59c4aSMichael Grosse     * @return string|bool The json string or false if the schema doesn't exist
81c4ce7499SMichael Grosse     */
82d6d97f60SAnna Dabrowska    public function getCurrentSchemaJSON($schemaName)
83d6d97f60SAnna Dabrowska    {
8426d59c4aSMichael Grosse        $schema = new Schema($schemaName);
8526d59c4aSMichael Grosse        if ($schema->getId() == 0) {
8626d59c4aSMichael Grosse            return false;
8726d59c4aSMichael Grosse        }
8826d59c4aSMichael Grosse        return $schema->toJSON();
89fa7b96aaSMichael Grosse    }
90fa7b96aaSMichael Grosse
91c4ce7499SMichael Grosse    /**
92c4ce7499SMichael Grosse     * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new
93c4ce7499SMichael Grosse     * schema will be created.
94c4ce7499SMichael Grosse     *
95c4ce7499SMichael Grosse     * @param string $schemaName The name of the schema
96c4ce7499SMichael Grosse     * @param string $schemaJSON The structure of the schema as exportet by structs export functionality
97*748e747fSAnna Dabrowska     * @param string $user optional, the user that should be set in the schemas history.
98*748e747fSAnna Dabrowska     *                      If blank, the current user is used.
99c4ce7499SMichael Grosse     * @return bool|int the id of the new schema version or false on error.
10057920350SMichael Grosse     *
10157920350SMichael Grosse     * @throws dokuwiki\plugin\struct\meta\StructException
102c4ce7499SMichael Grosse     */
103d6d97f60SAnna Dabrowska    public function importSchema($schemaName, $schemaJSON, $user = null)
104d6d97f60SAnna Dabrowska    {
105c4ce7499SMichael Grosse        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON);
10678bff02fSMichael Grosse        if (!blank($user)) {
10778bff02fSMichael Grosse            $importer->setUser($user);
10878bff02fSMichael Grosse        }
10978bff02fSMichael Grosse        $ok = $importer->build();
110fa7b96aaSMichael Grosse        return $ok;
111fa7b96aaSMichael Grosse    }
1127c5fd8d6SMichael Grosse}
113