xref: /plugin/struct/helper/imexport.php (revision c4ce749927b8064c4494ffd19c5ad1ae45e1ae4c)
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    /**
27*c4ce7499SMichael Grosse     * Delete all existing assignment patterns of a schema and replace them with the provided ones.
28*c4ce7499SMichael Grosse     *
29*c4ce7499SMichael Grosse     * @param string   $schemaName
30aeca15adSMichael Grosse     * @param string[] $patterns
317c5fd8d6SMichael Grosse     */
32*c4ce7499SMichael 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();
36*c4ce7499SMichael Grosse        $schemaName = $this->sqlite->escape_string($schemaName);
377c5fd8d6SMichael Grosse        $sql = array();
38*c4ce7499SMichael Grosse        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schemaName'";
39*c4ce7499SMichael Grosse        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schemaName'";
407c5fd8d6SMichael Grosse        foreach ($patterns as $pattern) {
411bd268d2SMichael Grosse            $pattern = $this->sqlite->escape_string($pattern);
42*c4ce7499SMichael 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();
47*c4ce7499SMichael Grosse        $assignments->propagatePageAssignments($schemaName);
487c5fd8d6SMichael Grosse    }
497c5fd8d6SMichael Grosse
50*c4ce7499SMichael Grosse    /**
51*c4ce7499SMichael Grosse     * Returns array of patterns for the given Schema
52*c4ce7499SMichael Grosse     *
53*c4ce7499SMichael Grosse     * @param string $schemaName
54*c4ce7499SMichael Grosse     * @return string[]
55*c4ce7499SMichael Grosse     */
56*c4ce7499SMichael 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 = ?';
62*c4ce7499SMichael 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
68*c4ce7499SMichael Grosse    /**
69*c4ce7499SMichael Grosse     * Get the json of the current version of the given schema.
70*c4ce7499SMichael Grosse     * If the schema doesn't exist, then the returned JSON will have id 0.
71*c4ce7499SMichael Grosse     *
72*c4ce7499SMichael Grosse     * @param string $schemaName
73*c4ce7499SMichael Grosse     * @return string The json string
74*c4ce7499SMichael Grosse     */
75*c4ce7499SMichael Grosse    public function getCurrentSchemaJSON($schemaName) {
76*c4ce7499SMichael Grosse        $schemaName = new Schema($schemaName);
77*c4ce7499SMichael Grosse        return $schemaName->toJSON();
78fa7b96aaSMichael Grosse    }
79fa7b96aaSMichael Grosse
80*c4ce7499SMichael Grosse    /**
81*c4ce7499SMichael Grosse     * Import a schema. If a schema with the given name already exists, then it will be overwritten. Otherwise a new
82*c4ce7499SMichael Grosse     * schema will be created.
83*c4ce7499SMichael Grosse     *
84*c4ce7499SMichael Grosse     * @param string $schemaName The name of the schema
85*c4ce7499SMichael Grosse     * @param string $schemaJSON The structure of the schema as exportet by structs export functionality
86*c4ce7499SMichael Grosse     * @param string $user optional, the user that should be set in the schemas history. If blank, the current user is used.
87*c4ce7499SMichael Grosse     * @return bool|int the id of the new schema version or false on error.
88*c4ce7499SMichael Grosse     */
89*c4ce7499SMichael Grosse    public function importSchema($schemaName, $schemaJSON, $user = null) {
90*c4ce7499SMichael Grosse        try {
91*c4ce7499SMichael Grosse            $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $schemaJSON);
9278bff02fSMichael Grosse            if (!blank($user)) {
9378bff02fSMichael Grosse                $importer->setUser($user);
9478bff02fSMichael Grosse            }
9578bff02fSMichael Grosse            $ok = $importer->build();
96*c4ce7499SMichael Grosse        } catch(dokuwiki\plugin\struct\meta\StructException $e) {
97*c4ce7499SMichael Grosse            msg(hsc($e->getMessage()), -1);
98*c4ce7499SMichael Grosse            return false;
99*c4ce7499SMichael Grosse        }
100fa7b96aaSMichael Grosse        return $ok;
101fa7b96aaSMichael Grosse    }
102fa7b96aaSMichael Grosse
1037c5fd8d6SMichael Grosse}
104