xref: /plugin/struct/helper/imexport.php (revision 579203501c09336449f2a76c95f5662540649a7d)
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    /**
69c4ce7499SMichael Grosse     * Get the json of the current version of the given schema.
70c4ce7499SMichael Grosse     * If the schema doesn't exist, then the returned JSON will have id 0.
71c4ce7499SMichael Grosse     *
72c4ce7499SMichael Grosse     * @param string $schemaName
73c4ce7499SMichael Grosse     * @return string The json string
74c4ce7499SMichael Grosse     */
75c4ce7499SMichael Grosse    public function getCurrentSchemaJSON($schemaName) {
76c4ce7499SMichael Grosse        $schemaName = new Schema($schemaName);
77c4ce7499SMichael Grosse        return $schemaName->toJSON();
78fa7b96aaSMichael Grosse    }
79fa7b96aaSMichael Grosse
80c4ce7499SMichael Grosse    /**
81c4ce7499SMichael Grosse     * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new
82c4ce7499SMichael Grosse     * schema will be created.
83c4ce7499SMichael Grosse     *
84c4ce7499SMichael Grosse     * @param string $schemaName The name of the schema
85c4ce7499SMichael Grosse     * @param string $schemaJSON The structure of the schema as exportet by structs export functionality
86c4ce7499SMichael Grosse     * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used.
87c4ce7499SMichael Grosse     * @return bool|int the id of the new schema version or false on error.
88*57920350SMichael Grosse     *
89*57920350SMichael Grosse     * @throws dokuwiki\plugin\struct\meta\StructException
90c4ce7499SMichael Grosse     */
91c4ce7499SMichael Grosse    public function importSchema($schemaName, $schemaJSON, $user = null) {
92c4ce7499SMichael Grosse        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON);
9378bff02fSMichael Grosse        if (!blank($user)) {
9478bff02fSMichael Grosse            $importer->setUser($user);
9578bff02fSMichael Grosse        }
9678bff02fSMichael Grosse        $ok = $importer->build();
97fa7b96aaSMichael Grosse        return $ok;
98fa7b96aaSMichael Grosse    }
99fa7b96aaSMichael Grosse
1007c5fd8d6SMichael Grosse}
101