xref: /plugin/struct/helper/imexport.php (revision aeca15adbcb7b14b5eb24b3b758311d9abd8ecde)
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
10*aeca15adSMichael Grosseuse dokuwiki\plugin\struct\meta\Schema;
11*aeca15adSMichael 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() {
23*aeca15adSMichael Grosse        return Schema::getAll();
247c5fd8d6SMichael Grosse    }
257c5fd8d6SMichael Grosse
267c5fd8d6SMichael Grosse    /**
277c5fd8d6SMichael Grosse     * @param string   $schema
28*aeca15adSMichael Grosse     * @param string[] $patterns
297c5fd8d6SMichael Grosse     */
307c5fd8d6SMichael Grosse    public function replaceSchemaAssignmentPatterns($schema, $patterns) {
317c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
32*aeca15adSMichael Grosse        $helper = plugin_load('helper', 'struct_db');
337c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
347c5fd8d6SMichael Grosse        $schema = $this->sqlite->escape_string($schema);
357c5fd8d6SMichael Grosse        $sql = array();
361bd268d2SMichael Grosse        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schema'";
371bd268d2SMichael Grosse        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schema'";
387c5fd8d6SMichael Grosse        foreach ($patterns as $pattern) {
391bd268d2SMichael Grosse            $pattern = $this->sqlite->escape_string($pattern);
401bd268d2SMichael Grosse            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schema')";
417c5fd8d6SMichael Grosse        }
427c5fd8d6SMichael Grosse
437c5fd8d6SMichael Grosse        $this->sqlite->doTransaction($sql);
44b25bb9feSMichael Grosse        $assignments = new \dokuwiki\plugin\struct\meta\Assignments();
45b25bb9feSMichael Grosse        $assignments->propagatePageAssignments($schema);
467c5fd8d6SMichael Grosse    }
477c5fd8d6SMichael Grosse
487c5fd8d6SMichael Grosse    public function getSchemaAssignmentPatterns($schema) {
497c5fd8d6SMichael Grosse        /** @var \helper_plugin_struct_db $helper */
50*aeca15adSMichael Grosse        $helper = plugin_load('helper', 'struct_db');
517c5fd8d6SMichael Grosse        $this->sqlite = $helper->getDB();
527c5fd8d6SMichael Grosse
537c5fd8d6SMichael Grosse        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
547c5fd8d6SMichael Grosse        $res = $this->sqlite->query($sql, $schema);
557c5fd8d6SMichael Grosse        $patterns = $this->sqlite->res2arr($res);
567c5fd8d6SMichael Grosse        $this->sqlite->res_close($res);
577c5fd8d6SMichael Grosse        return array_map(function($elem){return $elem['pattern'];},$patterns);
587c5fd8d6SMichael Grosse    }
597c5fd8d6SMichael Grosse
60fa7b96aaSMichael Grosse    public function getCurrentSchemaJSON($schema) {
61*aeca15adSMichael Grosse        $schema = new Schema($schema);
62fa7b96aaSMichael Grosse        return $schema->toJSON();
63fa7b96aaSMichael Grosse    }
64fa7b96aaSMichael Grosse
65fa7b96aaSMichael Grosse    public function importSchema($schemaName, $json) {
66fa7b96aaSMichael Grosse        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $json); // todo could throw a struct exception?!
67fa7b96aaSMichael Grosse        $ok = $importer->build(); // ToDo: Ensure that user = FARMSYNC is set
68fa7b96aaSMichael Grosse        return $ok;
69fa7b96aaSMichael Grosse    }
70fa7b96aaSMichael Grosse
717c5fd8d6SMichael Grosse}
72