xref: /plugin/struct/helper/imexport.php (revision 7234bfb14e712ff548d9266ef32fdcc8eaf2d04e)
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\Extension\Plugin;
11use dokuwiki\plugin\sqlite\SQLiteDB;
12use dokuwiki\plugin\struct\meta\StructException;
13use dokuwiki\plugin\struct\meta\SchemaImporter;
14use dokuwiki\plugin\struct\meta\Assignments;
15use dokuwiki\plugin\struct\meta\Schema;
16
17class helper_plugin_struct_imexport extends Plugin
18{
19    private $sqlite;
20
21    /**
22     * this possibly duplicates @see helper_plugin_struct::getSchema()
23     */
24    public function getAllSchemasList()
25    {
26        return Schema::getAll();
27    }
28
29    /**
30     * Delete all existing assignment patterns of a schema and replace them with the provided ones.
31     *
32     * @param string $schemaName
33     * @param string[] $patterns
34     */
35    public function replaceSchemaAssignmentPatterns($schemaName, $patterns)
36    {
37        /** @var \helper_plugin_struct_db $helper */
38        $helper = plugin_load('helper', 'struct_db');
39        $this->sqlite = $helper->getDB(false);
40        if (!$this->sqlite instanceof SQLiteDB) return;
41
42        $schemaName = $this->sqlite->escape_string($schemaName);
43        $sql = [];
44        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'";
45        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'";
46        foreach ($patterns as $pattern) {
47            $pattern = $this->sqlite->escape_string($pattern);
48            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')";
49        }
50
51        $this->sqlite->doTransaction($sql);
52        $assignments = Assignments::getInstance();
53        $assignments->propagatePageAssignments($schemaName);
54    }
55
56    /**
57     * Returns array of patterns for the given Schema
58     *
59     * @param string $schemaName
60     * @return string[]
61     */
62    public function getSchemaAssignmentPatterns($schemaName)
63    {
64        /** @var \helper_plugin_struct_db $helper */
65        $helper = plugin_load('helper', 'struct_db');
66        $this->sqlite = $helper->getDB(false);
67        if (!$this->sqlite instanceof SQLiteDB) return [];
68
69        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
70        $patterns = $this->sqlite->queryAll($sql, $schemaName);
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 StructException
102     */
103    public function importSchema($schemaName, $schemaJSON, $user = null)
104    {
105        $importer = new SchemaImporter($schemaName, $schemaJSON);
106        if (!blank($user)) {
107            $importer->setUser($user);
108        }
109        $ok = $importer->build();
110        return $ok;
111    }
112}
113