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(static fn($elem) => $elem['pattern'], $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 StructException
100     */
101    public function importSchema($schemaName, $schemaJSON, $user = null)
102    {
103        $importer = new SchemaImporter($schemaName, $schemaJSON);
104        if (!blank($user)) {
105            $importer->setUser($user);
106        }
107        $ok = $importer->build();
108        return $ok;
109    }
110}
111