xref: /plugin/struct/helper.php (revision 7dac04fff93a17d88fae3e7276b0015b4427cb02)
146ca39b7SAndreas Gohr<?php
246ca39b7SAndreas Gohr/**
346ca39b7SAndreas Gohr * DokuWiki Plugin struct (Helper Component)
446ca39b7SAndreas Gohr *
546ca39b7SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
646ca39b7SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
746ca39b7SAndreas Gohr */
846ca39b7SAndreas Gohr
946ca39b7SAndreas Gohr// must be run within Dokuwiki
10*7dac04ffSAndreas Gohruse plugin\struct\meta\Assignments;
11*7dac04ffSAndreas Gohruse plugin\struct\meta\Schema;
12*7dac04ffSAndreas Gohruse plugin\struct\meta\SchemaData;
1346ca39b7SAndreas Gohruse plugin\struct\meta\StructException;
14*7dac04ffSAndreas Gohruse plugin\struct\meta\Validator;
1546ca39b7SAndreas Gohr
1646ca39b7SAndreas Gohrif(!defined('DOKU_INC')) die();
1746ca39b7SAndreas Gohr
1846ca39b7SAndreas Gohr/**
1946ca39b7SAndreas Gohr * The public interface for the struct plugin
2046ca39b7SAndreas Gohr *
21*7dac04ffSAndreas Gohr * 3rd party developers should always interact with struct data through this
22*7dac04ffSAndreas Gohr * helper plugin only. If additionional interface functionality is needed,
23*7dac04ffSAndreas Gohr * it should be added here.
2446ca39b7SAndreas Gohr *
2546ca39b7SAndreas Gohr * All functions will throw StructExceptions when something goes wrong.
26*7dac04ffSAndreas Gohr *
27*7dac04ffSAndreas Gohr * Remember to check permissions yourself!
2846ca39b7SAndreas Gohr */
2946ca39b7SAndreas Gohrclass helper_plugin_struct extends DokuWiki_Plugin {
3046ca39b7SAndreas Gohr
3146ca39b7SAndreas Gohr    /**
3246ca39b7SAndreas Gohr     * Get the structured data of a given page
3346ca39b7SAndreas Gohr     *
3446ca39b7SAndreas Gohr     * @param string $page The page to get data for
3546ca39b7SAndreas Gohr     * @param string|null $schema The schema to use null for all
3646ca39b7SAndreas Gohr     * @param int $time A timestamp if you want historic data (0 for now)
3746ca39b7SAndreas Gohr     * @return array ('schema' => ( 'fieldlabel' => 'value', ...))
3846ca39b7SAndreas Gohr     * @throws StructException
3946ca39b7SAndreas Gohr     */
4046ca39b7SAndreas Gohr    public function getData($page, $schema=null, $time=0) {
41*7dac04ffSAndreas Gohr        $page = cleanID($page);
4246ca39b7SAndreas Gohr
43*7dac04ffSAndreas Gohr        if(is_null($schema)) {
44*7dac04ffSAndreas Gohr            $assignments = new Assignments();
45*7dac04ffSAndreas Gohr            $schemas = $assignments->getPageAssignments($page, false);
46*7dac04ffSAndreas Gohr        } else {
47*7dac04ffSAndreas Gohr            $schemas = array($schema);
48*7dac04ffSAndreas Gohr        }
49*7dac04ffSAndreas Gohr
50*7dac04ffSAndreas Gohr        $result = array();
51*7dac04ffSAndreas Gohr        foreach($schemas as $schema) {
52*7dac04ffSAndreas Gohr            $schemaData = new SchemaData($schema, $page, $time);
53*7dac04ffSAndreas Gohr            $result[$schema] = $schemaData->getDataArray(false);
54*7dac04ffSAndreas Gohr        }
55*7dac04ffSAndreas Gohr
56*7dac04ffSAndreas Gohr        return $result;
5746ca39b7SAndreas Gohr    }
5846ca39b7SAndreas Gohr
5946ca39b7SAndreas Gohr    /**
6046ca39b7SAndreas Gohr     * Saves data for a given page (creates a new revision)
6146ca39b7SAndreas Gohr     *
62*7dac04ffSAndreas Gohr     * If this call succeeds you can assume your data has either been saved or it was
63*7dac04ffSAndreas Gohr     * not necessary to save it because the data already existed in the wanted form or
64*7dac04ffSAndreas Gohr     * the given schemas are no longer assigned to that page.
65*7dac04ffSAndreas Gohr     *
66*7dac04ffSAndreas Gohr     * Important: You have to check write permissions for the given page before calling
67*7dac04ffSAndreas Gohr     * this function yourself!
68*7dac04ffSAndreas Gohr     *
69*7dac04ffSAndreas Gohr     * this duplicates a bit of code from entry.php - we could also fake post data and let
70*7dac04ffSAndreas Gohr     * entry handle it, but that would be rather unclean and might be problematic when multiple
71*7dac04ffSAndreas Gohr     * calls are done within the same request.
72*7dac04ffSAndreas Gohr     *
73*7dac04ffSAndreas Gohr     * @todo should this try to lock the page?
74*7dac04ffSAndreas Gohr     *
75*7dac04ffSAndreas Gohr     *
7646ca39b7SAndreas Gohr     * @param string $page
77*7dac04ffSAndreas Gohr     * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...))
7846ca39b7SAndreas Gohr     * @param string $summary
7946ca39b7SAndreas Gohr     * @throws StructException
8046ca39b7SAndreas Gohr     */
81*7dac04ffSAndreas Gohr    public function saveData($page, $data, $summary='') {
82*7dac04ffSAndreas Gohr        $page = cleanID($page);
83*7dac04ffSAndreas Gohr        $summary = trim($summary);
84*7dac04ffSAndreas Gohr        if(!$summary) $summary = $this->getLang('summary');
8546ca39b7SAndreas Gohr
86*7dac04ffSAndreas Gohr        if(!page_exists($page)) throw new StructException("Page does not exist. You can not attach struct data");
87*7dac04ffSAndreas Gohr
88*7dac04ffSAndreas Gohr        // validate and see if anything changes
89*7dac04ffSAndreas Gohr        $validator = new Validator();
90*7dac04ffSAndreas Gohr        if(!$validator->validate($data, $page)) {
91*7dac04ffSAndreas Gohr            throw new StructException("Validation failed:\n%s", join("\n", $validator->getErrors()));
92*7dac04ffSAndreas Gohr        }
93*7dac04ffSAndreas Gohr        $data = $validator->getCleanedData();
94*7dac04ffSAndreas Gohr        $tosave = $validator->getChangedSchemas();
95*7dac04ffSAndreas Gohr        if(!$tosave) return;
96*7dac04ffSAndreas Gohr
97*7dac04ffSAndreas Gohr        // force a new page revision @see action_plugin_struct_entry::handle_pagesave_before()
98*7dac04ffSAndreas Gohr        $GLOBALS['struct_plugin_force_page_save'] = true;
99*7dac04ffSAndreas Gohr        saveWikiText($page, rawWiki($page), $summary);
100*7dac04ffSAndreas Gohr        unset($GLOBALS['struct_plugin_force_page_save']);
101*7dac04ffSAndreas Gohr        $file = wikiFN($page);
102*7dac04ffSAndreas Gohr        clearstatcache(false, $file);
103*7dac04ffSAndreas Gohr        $newrevision = filemtime($file);
104*7dac04ffSAndreas Gohr
105*7dac04ffSAndreas Gohr        // save the provided data
106*7dac04ffSAndreas Gohr        $assignments = new Assignments();
107*7dac04ffSAndreas Gohr        foreach($tosave as $table) {
108*7dac04ffSAndreas Gohr            $schemaData = new SchemaData($table, $page, $newrevision);
109*7dac04ffSAndreas Gohr            $schemaData->saveData($data[$table]);
110*7dac04ffSAndreas Gohr            // make sure this schema is assigned
111*7dac04ffSAndreas Gohr            $assignments->assignPageSchema($page, $table);
112*7dac04ffSAndreas Gohr        }
11346ca39b7SAndreas Gohr    }
11446ca39b7SAndreas Gohr
11546ca39b7SAndreas Gohr    /**
11646ca39b7SAndreas Gohr     * Get info about existing schemas
11746ca39b7SAndreas Gohr     *
11846ca39b7SAndreas Gohr     * @param string|null $schema the schema to query, null for all
119*7dac04ffSAndreas Gohr     * @return Schema[]
12046ca39b7SAndreas Gohr     * @throws StructException
12146ca39b7SAndreas Gohr     */
12246ca39b7SAndreas Gohr    public function getSchema($schema=null) {
123*7dac04ffSAndreas Gohr        if(is_null($schema)) {
124*7dac04ffSAndreas Gohr            $schemas = Schema::getAll();
125*7dac04ffSAndreas Gohr        } else {
126*7dac04ffSAndreas Gohr            $schemas = array($schema);
127*7dac04ffSAndreas Gohr        }
12846ca39b7SAndreas Gohr
129*7dac04ffSAndreas Gohr        $result = array();
130*7dac04ffSAndreas Gohr        foreach($schemas as $table) {
131*7dac04ffSAndreas Gohr            $result[$table] = new Schema($table);
132*7dac04ffSAndreas Gohr        }
133*7dac04ffSAndreas Gohr        return $result;
13446ca39b7SAndreas Gohr    }
13546ca39b7SAndreas Gohr
13646ca39b7SAndreas Gohr    /**
13746ca39b7SAndreas Gohr     * Returns all pages known to the struct plugin
13846ca39b7SAndreas Gohr     *
13946ca39b7SAndreas Gohr     * That means all pages that have or had once struct data saved
14046ca39b7SAndreas Gohr     *
14146ca39b7SAndreas Gohr     * @param string|null $schema limit the result to a given schema
142*7dac04ffSAndreas Gohr     * @return array (page => (schema => true), ...)
14346ca39b7SAndreas Gohr     * @throws StructException
14446ca39b7SAndreas Gohr     */
14546ca39b7SAndreas Gohr    public function getPages($schema=null) {
146*7dac04ffSAndreas Gohr        $assignments = new Assignments();
147*7dac04ffSAndreas Gohr        return $assignments->getPages($schema);
14846ca39b7SAndreas Gohr    }
14946ca39b7SAndreas Gohr
15046ca39b7SAndreas Gohr}
151