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