xref: /plugin/struct/helper/imexport.php (revision f2d943af512b04cb1282ca403df08ac1045ccda7)
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
16    private $sqlite;
17
18
19    /**
20     * this possibly duplicates @see helper_plugin_struct::getSchema()
21     */
22    public function getAllSchemasList()
23    {
24        return Schema::getAll();
25    }
26
27    /**
28     * Delete all existing assignment patterns of a schema and replace them with the provided ones.
29     *
30     * @param string $schemaName
31     * @param string[] $patterns
32     */
33    public function replaceSchemaAssignmentPatterns($schemaName, $patterns)
34    {
35        /** @var \helper_plugin_struct_db $helper */
36        $helper = plugin_load('helper', 'struct_db');
37        $this->sqlite = $helper->getDB(false);
38        if (!$this->sqlite) return;
39
40        $schemaName = $this->sqlite->escape_string($schemaName);
41        $sql = array();
42        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'";
43        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'";
44        foreach ($patterns as $pattern) {
45            $pattern = $this->sqlite->escape_string($pattern);
46            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')";
47        }
48
49        $this->sqlite->doTransaction($sql);
50        $assignments = Assignments::getInstance();
51        $assignments->propagatePageAssignments($schemaName);
52    }
53
54    /**
55     * Returns array of patterns for the given Schema
56     *
57     * @param string $schemaName
58     * @return string[]
59     */
60    public function getSchemaAssignmentPatterns($schemaName)
61    {
62        /** @var \helper_plugin_struct_db $helper */
63        $helper = plugin_load('helper', 'struct_db');
64        $this->sqlite = $helper->getDB(false);
65        if (!$this->sqlite) return array();
66
67        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
68        $res = $this->sqlite->query($sql, $schemaName);
69        $patterns = $this->sqlite->res2arr($res);
70        $this->sqlite->res_close($res);
71        return array_map(function ($elem) {
72            return $elem['pattern'];
73        }, $patterns);
74    }
75
76    /**
77     * Get the json of the current version of the given schema or false if the schema doesn't exist.
78     *
79     * @param string $schemaName
80     * @return string|bool The json string or false if the schema doesn't exist
81     */
82    public function getCurrentSchemaJSON($schemaName)
83    {
84        $schema = new Schema($schemaName);
85        if ($schema->getId() == 0) {
86            return false;
87        }
88        return $schema->toJSON();
89    }
90
91    /**
92     * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new
93     * schema will be created.
94     *
95     * @param string $schemaName The name of the schema
96     * @param string $schemaJSON The structure of the schema as exportet by structs export functionality
97     * @param string $user optional, the user that should be set in the schemas history.
98     *                      If blank, the current user is used.
99     * @return bool|int the id of the new schema version or false on error.
100     *
101     * @throws dokuwiki\plugin\struct\meta\StructException
102     */
103    public function importSchema($schemaName, $schemaJSON, $user = null)
104    {
105        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON);
106        if (!blank($user)) {
107            $importer->setUser($user);
108        }
109        $ok = $importer->build();
110        return $ok;
111    }
112}
113