xref: /plugin/struct/helper/imexport.php (revision fa7b96aac3c296355e7757cc16519768d467d548)
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
107c5fd8d6SMichael Grosseif(!defined('DOKU_INC')) die();
117c5fd8d6SMichael Grosse
127c5fd8d6SMichael Grosseclass helper_plugin_struct_imexport extends DokuWiki_Plugin {
137c5fd8d6SMichael Grosse
147c5fd8d6SMichael Grosse    private $sqlite;
157c5fd8d6SMichael Grosse
16*fa7b96aaSMichael Grosse
17*fa7b96aaSMichael Grosse    /**
18*fa7b96aaSMichael Grosse     * this possibly duplicates @see helper_plugin_struct::getSchema()
19*fa7b96aaSMichael Grosse     */
207c5fd8d6SMichael Grosse    public function getAllSchemasList() {
217c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
227c5fd8d6SMichael Grosse        $helper = plugin_load('helper', 'struct_db');
237c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
247c5fd8d6SMichael Grosse
257c5fd8d6SMichael Grosse        $sql = 'SELECT DISTINCT(tbl) FROM schemas';
267c5fd8d6SMichael Grosse        $res = $this->sqlite->query($sql);
277c5fd8d6SMichael Grosse        $schemas = $this->sqlite->res2arr($res);
287c5fd8d6SMichael Grosse        $this->sqlite->res_close($res);
297c5fd8d6SMichael Grosse        return $schemas;
307c5fd8d6SMichael Grosse    }
317c5fd8d6SMichael Grosse
327c5fd8d6SMichael Grosse    /**
337c5fd8d6SMichael Grosse     * @param string   $schema
347c5fd8d6SMichael Grosse     * @param string[] $assignments
357c5fd8d6SMichael Grosse     */
367c5fd8d6SMichael Grosse    public function replaceSchemaAssignmentPatterns($schema, $patterns) {
377c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
387c5fd8d6SMichael Grosse        $helper = plugin_load('helper', 'struct_db', true);
397c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
407c5fd8d6SMichael Grosse        $schema = $this->sqlite->escape_string($schema);
417c5fd8d6SMichael Grosse        $sql = array();
421bd268d2SMichael Grosse        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schema'";
431bd268d2SMichael Grosse        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schema'";
447c5fd8d6SMichael Grosse        foreach ($patterns as $pattern) {
451bd268d2SMichael Grosse            $pattern = $this->sqlite->escape_string($pattern);
461bd268d2SMichael Grosse            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schema')";
477c5fd8d6SMichael Grosse        }
487c5fd8d6SMichael Grosse
497c5fd8d6SMichael Grosse        $this->sqlite->doTransaction($sql);
50b25bb9feSMichael Grosse        $assignments = new \dokuwiki\plugin\struct\meta\Assignments();
51b25bb9feSMichael Grosse        $assignments->propagatePageAssignments($schema);
527c5fd8d6SMichael Grosse    }
537c5fd8d6SMichael Grosse
547c5fd8d6SMichael Grosse    public function getSchemaAssignmentPatterns($schema) {
557c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
567c5fd8d6SMichael Grosse        $helper = plugin_load('helper', 'struct_db', true);
577c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
587c5fd8d6SMichael Grosse
597c5fd8d6SMichael Grosse        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
607c5fd8d6SMichael Grosse        $res = $this->sqlite->query($sql, $schema);
617c5fd8d6SMichael Grosse        $patterns = $this->sqlite->res2arr($res);
627c5fd8d6SMichael Grosse        $this->sqlite->res_close($res);
637c5fd8d6SMichael Grosse        return array_map(function($elem){return $elem['pattern'];},$patterns);
647c5fd8d6SMichael Grosse    }
657c5fd8d6SMichael Grosse
66*fa7b96aaSMichael Grosse    public function getCurrentSchemaJSON($schema) {
67*fa7b96aaSMichael Grosse        $schema = new \dokuwiki\plugin\struct\meta\Schema($schema);
68*fa7b96aaSMichael Grosse        return $schema->toJSON();
69*fa7b96aaSMichael Grosse    }
70*fa7b96aaSMichael Grosse
71*fa7b96aaSMichael Grosse    public function importSchema($schemaName, $json) {
72*fa7b96aaSMichael Grosse        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $json); // todo could throw a struct exception?!
73*fa7b96aaSMichael Grosse        $ok = $importer->build(); // ToDo: Ensure that user = FARMSYNC is set
74*fa7b96aaSMichael Grosse        return $ok;
75*fa7b96aaSMichael Grosse    }
76*fa7b96aaSMichael Grosse
777c5fd8d6SMichael Grosse}
78