xref: /plugin/struct/helper.php (revision 1057556627155ce214796eceb5b80361493f882a)
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
1093ca6f4fSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessDataValidator;
11f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
12f0ee60b9SMichael Großeuse dokuwiki\plugin\struct\meta\AccessTableLookup;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
15ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1646ca39b7SAndreas Gohr
1746ca39b7SAndreas Gohrif(!defined('DOKU_INC')) die();
1846ca39b7SAndreas Gohr
1946ca39b7SAndreas Gohr/**
2046ca39b7SAndreas Gohr * The public interface for the struct plugin
2146ca39b7SAndreas Gohr *
227dac04ffSAndreas Gohr * 3rd party developers should always interact with struct data through this
237dac04ffSAndreas Gohr * helper plugin only. If additionional interface functionality is needed,
247dac04ffSAndreas Gohr * it should be added here.
2546ca39b7SAndreas Gohr *
2646ca39b7SAndreas Gohr * All functions will throw StructExceptions when something goes wrong.
277dac04ffSAndreas Gohr *
287dac04ffSAndreas Gohr * Remember to check permissions yourself!
2946ca39b7SAndreas Gohr */
3046ca39b7SAndreas Gohrclass helper_plugin_struct extends DokuWiki_Plugin {
3146ca39b7SAndreas Gohr
3246ca39b7SAndreas Gohr    /**
3346ca39b7SAndreas Gohr     * Get the structured data of a given page
3446ca39b7SAndreas Gohr     *
3546ca39b7SAndreas Gohr     * @param string $page The page to get data for
3646ca39b7SAndreas Gohr     * @param string|null $schema The schema to use null for all
3746ca39b7SAndreas Gohr     * @param int $time A timestamp if you want historic data (0 for now)
3846ca39b7SAndreas Gohr     * @return array ('schema' => ( 'fieldlabel' => 'value', ...))
3946ca39b7SAndreas Gohr     * @throws StructException
4046ca39b7SAndreas Gohr     */
4146ca39b7SAndreas Gohr    public function getData($page, $schema = null, $time = 0) {
427dac04ffSAndreas Gohr        $page = cleanID($page);
4346ca39b7SAndreas Gohr
447dac04ffSAndreas Gohr        if(is_null($schema)) {
45025cb9daSAndreas Gohr            $assignments = Assignments::getInstance();
467dac04ffSAndreas Gohr            $schemas = $assignments->getPageAssignments($page, false);
477dac04ffSAndreas Gohr        } else {
487dac04ffSAndreas Gohr            $schemas = array($schema);
497dac04ffSAndreas Gohr        }
507dac04ffSAndreas Gohr
517dac04ffSAndreas Gohr        $result = array();
527dac04ffSAndreas Gohr        foreach($schemas as $schema) {
53f411d872SAndreas Gohr            $schemaData = AccessTable::byTableName($schema, $page, $time);
540dd23cefSAndreas Gohr            $result[$schema] = $schemaData->getDataArray();
557dac04ffSAndreas Gohr        }
567dac04ffSAndreas Gohr
577dac04ffSAndreas Gohr        return $result;
5846ca39b7SAndreas Gohr    }
5946ca39b7SAndreas Gohr
6046ca39b7SAndreas Gohr    /**
6146ca39b7SAndreas Gohr     * Saves data for a given page (creates a new revision)
6246ca39b7SAndreas Gohr     *
637dac04ffSAndreas Gohr     * If this call succeeds you can assume your data has either been saved or it was
647dac04ffSAndreas Gohr     * not necessary to save it because the data already existed in the wanted form or
657dac04ffSAndreas Gohr     * the given schemas are no longer assigned to that page.
667dac04ffSAndreas Gohr     *
677dac04ffSAndreas Gohr     * Important: You have to check write permissions for the given page before calling
687dac04ffSAndreas Gohr     * this function yourself!
697dac04ffSAndreas Gohr     *
707dac04ffSAndreas Gohr     * this duplicates a bit of code from entry.php - we could also fake post data and let
717dac04ffSAndreas Gohr     * entry handle it, but that would be rather unclean and might be problematic when multiple
727dac04ffSAndreas Gohr     * calls are done within the same request.
737dac04ffSAndreas Gohr     *
747dac04ffSAndreas Gohr     * @todo should this try to lock the page?
757dac04ffSAndreas Gohr     *
767dac04ffSAndreas Gohr     *
7746ca39b7SAndreas Gohr     * @param string $page
787dac04ffSAndreas Gohr     * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...))
7946ca39b7SAndreas Gohr     * @param string $summary
80178d3992SElan Ruusamäe     * @param string $summary
8146ca39b7SAndreas Gohr     * @throws StructException
8246ca39b7SAndreas Gohr     */
83178d3992SElan Ruusamäe    public function saveData($page, $data, $summary = '', $minor = false)
84178d3992SElan Ruusamäe    {
857dac04ffSAndreas Gohr        $page = cleanID($page);
867dac04ffSAndreas Gohr        $summary = trim($summary);
877dac04ffSAndreas Gohr        if(!$summary) $summary = $this->getLang('summary');
8846ca39b7SAndreas Gohr
897dac04ffSAndreas Gohr        if(!page_exists($page)) throw new StructException("Page does not exist. You can not attach struct data");
907dac04ffSAndreas Gohr
917dac04ffSAndreas Gohr        // validate and see if anything changes
9293ca6f4fSAndreas Gohr        $valid = AccessDataValidator::validateDataForPage($data, $page, $errors);
9393ca6f4fSAndreas Gohr        if($valid === false) {
9493ca6f4fSAndreas Gohr            throw new StructException("Validation failed:\n%s", join("\n", $errors));
957dac04ffSAndreas Gohr        }
9693ca6f4fSAndreas Gohr        if(!$valid) return; // empty array when no changes were detected
977dac04ffSAndreas Gohr
98178d3992SElan Ruusamäe        $newrevision = self::createPageRevision($page, $summary, $minor);
997dac04ffSAndreas Gohr
1007dac04ffSAndreas Gohr        // save the provided data
101025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
10293ca6f4fSAndreas Gohr        foreach($valid as $v) {
10393ca6f4fSAndreas Gohr            $v->saveData($newrevision);
1047dac04ffSAndreas Gohr            // make sure this schema is assigned
10593ca6f4fSAndreas Gohr            $assignments->assignPageSchema($page, $v->getAccessTable()->getSchema()->getTable());
1067dac04ffSAndreas Gohr        }
10746ca39b7SAndreas Gohr    }
10846ca39b7SAndreas Gohr
10946ca39b7SAndreas Gohr    /**
110*10575566SAnna Dabrowska     * Save lookup data row
111ff2afc7cSMichael Große     *
1120ceefd5cSAnna Dabrowska     * @param AccessTable        $access the table into which to save the data
113ff2afc7cSMichael Große     * @param array             $data   data to be saved in the form of [columnName => 'data']
114ff2afc7cSMichael Große     */
1150ceefd5cSAnna Dabrowska    public function saveLookupData(AccessTable $access, $data)
116ff2afc7cSMichael Große    {
117ff2afc7cSMichael Große        if(!$access->getSchema()->isEditable()) {
118ff2afc7cSMichael Große            throw new StructException('lookup save error: no permission for schema');
119ff2afc7cSMichael Große        }
120ff2afc7cSMichael Große        $validator = $access->getValidator($data);
121ff2afc7cSMichael Große        if(!$validator->validate()) {
122ff2afc7cSMichael Große            throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors()));
123ff2afc7cSMichael Große        }
124ff2afc7cSMichael Große        if(!$validator->saveData()) {
125ff2afc7cSMichael Große            throw new StructException('No data saved');
126ff2afc7cSMichael Große        }
127ff2afc7cSMichael Große    }
128ff2afc7cSMichael Große
129ff2afc7cSMichael Große    /**
130*10575566SAnna Dabrowska     * Save serial data row
131b9d35ff2SAnna Dabrowska     *
132b9d35ff2SAnna Dabrowska     * @param AccessTable        $access the table into which to save the data
133b9d35ff2SAnna Dabrowska     * @param array             $data   data to be saved in the form of [columnName => 'data']
134b9d35ff2SAnna Dabrowska     */
135b9d35ff2SAnna Dabrowska    public function saveSerialData(AccessTable $access, $data)
136b9d35ff2SAnna Dabrowska    {
137b9d35ff2SAnna Dabrowska        if(!$access->getSchema()->isEditable()) {
138b9d35ff2SAnna Dabrowska            throw new StructException('serial save error: no permission for schema');
139b9d35ff2SAnna Dabrowska        }
140b9d35ff2SAnna Dabrowska        $validator = $access->getValidator($data);
141b9d35ff2SAnna Dabrowska        if(!$validator->validate()) {
142b9d35ff2SAnna Dabrowska            throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors()));
143b9d35ff2SAnna Dabrowska        }
144b9d35ff2SAnna Dabrowska        if(!$validator->saveData()) {
145b9d35ff2SAnna Dabrowska            throw new StructException('No data saved');
146b9d35ff2SAnna Dabrowska        }
147b9d35ff2SAnna Dabrowska    }
148b9d35ff2SAnna Dabrowska
149b9d35ff2SAnna Dabrowska    /**
15013eddb0fSAndreas Gohr     * Creates a new page revision with the same page content as before
15113eddb0fSAndreas Gohr     *
15213eddb0fSAndreas Gohr     * @param string $page
15313eddb0fSAndreas Gohr     * @param string $summary
15413eddb0fSAndreas Gohr     * @param bool $minor
15513eddb0fSAndreas Gohr     * @return int the new revision
15613eddb0fSAndreas Gohr     */
15713eddb0fSAndreas Gohr    static public function createPageRevision($page, $summary = '', $minor = false) {
15813eddb0fSAndreas Gohr        $summary = trim($summary);
15913eddb0fSAndreas Gohr        // force a new page revision @see action_plugin_struct_entry::handle_pagesave_before()
16013eddb0fSAndreas Gohr        $GLOBALS['struct_plugin_force_page_save'] = true;
16113eddb0fSAndreas Gohr        saveWikiText($page, rawWiki($page), $summary, $minor);
16213eddb0fSAndreas Gohr        unset($GLOBALS['struct_plugin_force_page_save']);
16313eddb0fSAndreas Gohr        $file = wikiFN($page);
16413eddb0fSAndreas Gohr        clearstatcache(false, $file);
16513eddb0fSAndreas Gohr        return filemtime($file);
16613eddb0fSAndreas Gohr    }
16713eddb0fSAndreas Gohr
16813eddb0fSAndreas Gohr    /**
16946ca39b7SAndreas Gohr     * Get info about existing schemas
17046ca39b7SAndreas Gohr     *
17146ca39b7SAndreas Gohr     * @param string|null $schema the schema to query, null for all
1727dac04ffSAndreas Gohr     * @return Schema[]
17346ca39b7SAndreas Gohr     * @throws StructException
17446ca39b7SAndreas Gohr     */
17546ca39b7SAndreas Gohr    public function getSchema($schema = null) {
1767dac04ffSAndreas Gohr        if(is_null($schema)) {
1777dac04ffSAndreas Gohr            $schemas = Schema::getAll();
1787dac04ffSAndreas Gohr        } else {
1797dac04ffSAndreas Gohr            $schemas = array($schema);
1807dac04ffSAndreas Gohr        }
18146ca39b7SAndreas Gohr
1827dac04ffSAndreas Gohr        $result = array();
1837dac04ffSAndreas Gohr        foreach($schemas as $table) {
1847dac04ffSAndreas Gohr            $result[$table] = new Schema($table);
1857dac04ffSAndreas Gohr        }
1867dac04ffSAndreas Gohr        return $result;
18746ca39b7SAndreas Gohr    }
18846ca39b7SAndreas Gohr
18946ca39b7SAndreas Gohr    /**
19046ca39b7SAndreas Gohr     * Returns all pages known to the struct plugin
19146ca39b7SAndreas Gohr     *
19246ca39b7SAndreas Gohr     * That means all pages that have or had once struct data saved
19346ca39b7SAndreas Gohr     *
19446ca39b7SAndreas Gohr     * @param string|null $schema limit the result to a given schema
1957dac04ffSAndreas Gohr     * @return array (page => (schema => true), ...)
19646ca39b7SAndreas Gohr     * @throws StructException
19746ca39b7SAndreas Gohr     */
19846ca39b7SAndreas Gohr    public function getPages($schema = null) {
199025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
2007dac04ffSAndreas Gohr        return $assignments->getPages($schema);
20146ca39b7SAndreas Gohr    }
20246ca39b7SAndreas Gohr
20346ca39b7SAndreas Gohr}
204