1<?php
2
3/**
4 * DokuWiki Plugin struct (Helper Component)
5 *
6 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
7 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
8 */
9
10use dokuwiki\plugin\struct\meta\Assignments;
11use dokuwiki\plugin\struct\meta\Schema;
12
13class helper_plugin_struct_imexport extends DokuWiki_Plugin
14{
15    private $sqlite;
16
17    /**
18     * this possibly duplicates @see helper_plugin_struct::getSchema()
19     */
20    public function getAllSchemasList()
21    {
22        return Schema::getAll();
23    }
24
25    /**
26     * Delete all existing assignment patterns of a schema and replace them with the provided ones.
27     *
28     * @param string $schemaName
29     * @param string[] $patterns
30     */
31    public function replaceSchemaAssignmentPatterns($schemaName, $patterns)
32    {
33        /** @var \helper_plugin_struct_db $helper */
34        $helper = plugin_load('helper', 'struct_db');
35        $this->sqlite = $helper->getDB(false);
36        if (!$this->sqlite) return;
37
38        $schemaName = $this->sqlite->escape_string($schemaName);
39        $sql = array();
40        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'";
41        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'";
42        foreach ($patterns as $pattern) {
43            $pattern = $this->sqlite->escape_string($pattern);
44            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')";
45        }
46
47        $this->sqlite->doTransaction($sql);
48        $assignments = Assignments::getInstance();
49        $assignments->propagatePageAssignments($schemaName);
50    }
51
52    /**
53     * Returns array of patterns for the given Schema
54     *
55     * @param string $schemaName
56     * @return string[]
57     */
58    public function getSchemaAssignmentPatterns($schemaName)
59    {
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) {
70            return $elem['pattern'];
71        }, $patterns);
72    }
73
74    /**
75     * Get the json of the current version of the given schema or false if the schema doesn't exist.
76     *
77     * @param string $schemaName
78     * @return string|bool The json string or false if the schema doesn't exist
79     */
80    public function getCurrentSchemaJSON($schemaName)
81    {
82        $schema = new Schema($schemaName);
83        if ($schema->getId() == 0) {
84            return false;
85        }
86        return $schema->toJSON();
87    }
88
89    /**
90     * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new
91     * schema will be created.
92     *
93     * @param string $schemaName The name of the schema
94     * @param string $schemaJSON The structure of the schema as exportet by structs export functionality
95     * @param string $user optional, the user that should be set in the schemas history.
96     *                      If blank, the current user is used.
97     * @return bool|int the id of the new schema version or false on error.
98     *
99     * @throws dokuwiki\plugin\struct\meta\StructException
100     */
101    public function importSchema($schemaName, $schemaJSON, $user = null)
102    {
103        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON);
104        if (!blank($user)) {
105            $importer->setUser($user);
106        }
107        $ok = $importer->build();
108        return $ok;
109    }
110}
111