xref: /plugin/struct/helper/imexport.php (revision fa7b96aac3c296355e7757cc16519768d467d548)
1<?php
2/**
3 * DokuWiki Plugin struct (Helper Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class helper_plugin_struct_imexport extends DokuWiki_Plugin {
13
14    private $sqlite;
15
16
17    /**
18     * this possibly duplicates @see helper_plugin_struct::getSchema()
19     */
20    public function getAllSchemasList() {
21        /** @var \helper_plugin_struct_db $helper */
22        $helper = plugin_load('helper', 'struct_db');
23        $this->sqlite = $helper->getDB();
24
25        $sql = 'SELECT DISTINCT(tbl) FROM schemas';
26        $res = $this->sqlite->query($sql);
27        $schemas = $this->sqlite->res2arr($res);
28        $this->sqlite->res_close($res);
29        return $schemas;
30    }
31
32    /**
33     * @param string   $schema
34     * @param string[] $assignments
35     */
36    public function replaceSchemaAssignmentPatterns($schema, $patterns) {
37        /** @var \helper_plugin_struct_db $helper */
38        $helper = plugin_load('helper', 'struct_db', true);
39        $this->sqlite = $helper->getDB();
40        $schema = $this->sqlite->escape_string($schema);
41        $sql = array();
42        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schema'";
43        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schema'";
44        foreach ($patterns as $pattern) {
45            $pattern = $this->sqlite->escape_string($pattern);
46            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schema')";
47        }
48
49        $this->sqlite->doTransaction($sql);
50        $assignments = new \dokuwiki\plugin\struct\meta\Assignments();
51        $assignments->propagatePageAssignments($schema);
52    }
53
54    public function getSchemaAssignmentPatterns($schema) {
55        /** @var \helper_plugin_struct_db $helper */
56        $helper = plugin_load('helper', 'struct_db', true);
57        $this->sqlite = $helper->getDB();
58
59        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
60        $res = $this->sqlite->query($sql, $schema);
61        $patterns = $this->sqlite->res2arr($res);
62        $this->sqlite->res_close($res);
63        return array_map(function($elem){return $elem['pattern'];},$patterns);
64    }
65
66    public function getCurrentSchemaJSON($schema) {
67        $schema = new \dokuwiki\plugin\struct\meta\Schema($schema);
68        return $schema->toJSON();
69    }
70
71    public function importSchema($schemaName, $json) {
72        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $json); // todo could throw a struct exception?!
73        $ok = $importer->build(); // ToDo: Ensure that user = FARMSYNC is set
74        return $ok;
75    }
76
77}
78