xref: /plugin/struct/helper/imexport.php (revision 78bff02fbfdef52a5e7219a35faefba032b70781)
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
10use dokuwiki\plugin\struct\meta\Schema;
11
12if(!defined('DOKU_INC')) die();
13
14class helper_plugin_struct_imexport extends DokuWiki_Plugin {
15
16    private $sqlite;
17
18
19    /**
20     * this possibly duplicates @see helper_plugin_struct::getSchema()
21     */
22    public function getAllSchemasList() {
23        return Schema::getAll();
24    }
25
26    /**
27     * @param string   $schema
28     * @param string[] $patterns
29     */
30    public function replaceSchemaAssignmentPatterns($schema, $patterns) {
31        /** @var \helper_plugin_struct_db $helper */
32        $helper = plugin_load('helper', 'struct_db');
33        $this->sqlite = $helper->getDB();
34        $schema = $this->sqlite->escape_string($schema);
35        $sql = array();
36        $sql[] = "DELETE FROM schema_assignments_patterns WHERE tbl = '$schema'";
37        $sql[] = "DELETE FROM schema_assignments WHERE tbl = '$schema'";
38        foreach ($patterns as $pattern) {
39            $pattern = $this->sqlite->escape_string($pattern);
40            $sql[] = "INSERT INTO schema_assignments_patterns (pattern, tbl) VALUES ('$pattern','$schema')";
41        }
42
43        $this->sqlite->doTransaction($sql);
44        $assignments = new \dokuwiki\plugin\struct\meta\Assignments();
45        $assignments->propagatePageAssignments($schema);
46    }
47
48    public function getSchemaAssignmentPatterns($schema) {
49        /** @var \helper_plugin_struct_db $helper */
50        $helper = plugin_load('helper', 'struct_db');
51        $this->sqlite = $helper->getDB();
52
53        $sql = 'SELECT pattern FROM schema_assignments_patterns WHERE tbl = ?';
54        $res = $this->sqlite->query($sql, $schema);
55        $patterns = $this->sqlite->res2arr($res);
56        $this->sqlite->res_close($res);
57        return array_map(function($elem){return $elem['pattern'];},$patterns);
58    }
59
60    public function getCurrentSchemaJSON($schema) {
61        $schema = new Schema($schema);
62        return $schema->toJSON();
63    }
64
65    public function importSchema($schemaName, $json, $user = null) {
66        $importer = new \dokuwiki\plugin\struct\meta\SchemaImporter($schemaName, $json); // todo could throw a struct exception?!
67        if (!blank($user)) {
68            $importer->setUser($user);
69        }
70        $ok = $importer->build();
71        return $ok;
72    }
73
74}
75