xref: /plugin/struct/helper/imexport.php (revision 26d59c4a115d6684051131b90d00cee087d5474a)
17c5fd8d6SMichael Grosse<?php
27c5fd8d6SMichael Grosse/**
37c5fd8d6SMichael Grosse * DokuWiki Plugin struct (Helper Component)
47c5fd8d6SMichael Grosse *
57c5fd8d6SMichael Grosse * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
67c5fd8d6SMichael Grosse * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
77c5fd8d6SMichael Grosse */
87c5fd8d6SMichael Grosse
97c5fd8d6SMichael Grosse// must be run within Dokuwiki
10aeca15adSMichael Grosseuse dokuwiki\plugin\struct\meta\Schema;
11aeca15adSMichael Grosse
127c5fd8d6SMichael Grosseif(!defined('DOKU_INC')) die();
137c5fd8d6SMichael Grosse
147c5fd8d6SMichael Grosseclass helper_plugin_struct_imexport extends DokuWiki_Plugin {
157c5fd8d6SMichael Grosse
167c5fd8d6SMichael Grosse    private $sqlite;
177c5fd8d6SMichael Grosse
18fa7b96aaSMichael Grosse
19fa7b96aaSMichael Grosse    /**
20fa7b96aaSMichael Grosse     * this possibly duplicates @see helper_plugin_struct::getSchema()
21fa7b96aaSMichael Grosse     */
227c5fd8d6SMichael Grosse    public function getAllSchemasList() {
23aeca15adSMichael Grosse        return Schema::getAll();
247c5fd8d6SMichael Grosse    }
257c5fd8d6SMichael Grosse
267c5fd8d6SMichael Grosse    /**
27c4ce7499SMichael Grosse     * Delete all existing assignment patterns of a schema and replace them with the provided ones.
28c4ce7499SMichael Grosse     *
29c4ce7499SMichael Grosse     * @param string   $schemaName
30aeca15adSMichael Grosse     * @param string[] $patterns
317c5fd8d6SMichael Grosse     */
32c4ce7499SMichael Grosse    public function replaceSchemaAssignmentPatterns($schemaName, $patterns) {
337c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
34aeca15adSMichael Grosse        $helper = plugin_load('helper', 'struct_db');
357c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
36c4ce7499SMichael Grosse        $schemaName = $this->sqlite->escape_string($schemaName);
377c5fd8d6SMichael Grosse        $sql = array();
38c4ce7499SMichael Grosse        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'";
39c4ce7499SMichael Grosse        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'";
407c5fd8d6SMichael Grosse        foreach ($patterns as $pattern) {
411bd268d2SMichael Grosse            $pattern = $this->sqlite->escape_string($pattern);
42c4ce7499SMichael Grosse            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schemaName')";
437c5fd8d6SMichael Grosse        }
447c5fd8d6SMichael Grosse
457c5fd8d6SMichael Grosse        $this->sqlite->doTransaction($sql);
46b25bb9feSMichael Grosse        $assignments = new \dokuwiki\plugin\struct\meta\Assignments();
47c4ce7499SMichael Grosse        $assignments->propagatePageAssignments($schemaName);
487c5fd8d6SMichael Grosse    }
497c5fd8d6SMichael Grosse
50c4ce7499SMichael Grosse    /**
51c4ce7499SMichael Grosse     * Returns array of patterns for the given Schema
52c4ce7499SMichael Grosse     *
53c4ce7499SMichael Grosse     * @param string $schemaName
54c4ce7499SMichael Grosse     * @return string[]
55c4ce7499SMichael Grosse     */
56c4ce7499SMichael Grosse    public function getSchemaAssignmentPatterns($schemaName) {
577c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
58aeca15adSMichael Grosse        $helper = plugin_load('helper', 'struct_db');
597c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
607c5fd8d6SMichael Grosse
617c5fd8d6SMichael Grosse        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
62c4ce7499SMichael Grosse        $res = $this->sqlite->query($sql, $schemaName);
637c5fd8d6SMichael Grosse        $patterns = $this->sqlite->res2arr($res);
647c5fd8d6SMichael Grosse        $this->sqlite->res_close($res);
657c5fd8d6SMichael Grosse        return array_map(function($elem){return $elem['pattern'];},$patterns);
667c5fd8d6SMichael Grosse    }
677c5fd8d6SMichael Grosse
68c4ce7499SMichael Grosse    /**
69*26d59c4aSMichael Grosse     * Get the json of the current version of the given schema or false if the schema doesn't exist.
70c4ce7499SMichael Grosse     *
71c4ce7499SMichael Grosse     * @param string $schemaName
72*26d59c4aSMichael Grosse     * @return string|bool The json string or false if the schema doesn't exist
73c4ce7499SMichael Grosse     */
74c4ce7499SMichael Grosse    public function getCurrentSchemaJSON($schemaName) {
75*26d59c4aSMichael Grosse        $schema = new Schema($schemaName);
76*26d59c4aSMichael Grosse        if ($schema->getId() == 0) {
77*26d59c4aSMichael Grosse            return false;
78*26d59c4aSMichael Grosse        }
79*26d59c4aSMichael Grosse        return $schema->toJSON();
80fa7b96aaSMichael Grosse    }
81fa7b96aaSMichael Grosse
82c4ce7499SMichael Grosse    /**
83c4ce7499SMichael Grosse     * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new
84c4ce7499SMichael Grosse     * schema will be created.
85c4ce7499SMichael Grosse     *
86c4ce7499SMichael Grosse     * @param string $schemaName The name of the schema
87c4ce7499SMichael Grosse     * @param string $schemaJSON The structure of the schema as exportet by structs export functionality
88c4ce7499SMichael Grosse     * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used.
89c4ce7499SMichael Grosse     * @return bool|int the id of the new schema version or false on error.
9057920350SMichael Grosse     *
9157920350SMichael Grosse     * @throws dokuwiki\plugin\struct\meta\StructException
92c4ce7499SMichael Grosse     */
93c4ce7499SMichael Grosse    public function importSchema($schemaName, $schemaJSON, $user = null) {
94c4ce7499SMichael Grosse        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON);
9578bff02fSMichael Grosse        if (!blank($user)) {
9678bff02fSMichael Grosse            $importer->setUser($user);
9778bff02fSMichael Grosse        }
9878bff02fSMichael Grosse        $ok = $importer->build();
99fa7b96aaSMichael Grosse        return $ok;
100fa7b96aaSMichael Grosse    }
101fa7b96aaSMichael Grosse
1027c5fd8d6SMichael Grosse}
103