xref: /plugin/struct/helper.php (revision 6c9d1a10af9a568283576743abf68ccc0788c27f)
146ca39b7SAndreas Gohr<?php
2d6d97f60SAnna Dabrowska
346ca39b7SAndreas Gohr/**
446ca39b7SAndreas Gohr * DokuWiki Plugin struct (Helper Component)
546ca39b7SAndreas Gohr *
646ca39b7SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
746ca39b7SAndreas Gohr * @author  Andreas Gohr, Michael Große <dokuwiki@cosmocode.de>
846ca39b7SAndreas Gohr */
946ca39b7SAndreas Gohr
1093ca6f4fSAndreas Gohruse dokuwiki\plugin\struct\meta\AccessDataValidator;
11f411d872SAndreas Gohruse dokuwiki\plugin\struct\meta\AccessTable;
12ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Assignments;
13ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\Schema;
14ba766201SAndreas Gohruse dokuwiki\plugin\struct\meta\StructException;
1546ca39b7SAndreas Gohr
1646ca39b7SAndreas Gohr/**
1746ca39b7SAndreas Gohr * The public interface for the struct plugin
1846ca39b7SAndreas Gohr *
197dac04ffSAndreas Gohr * 3rd party developers should always interact with struct data through this
2034ea6e10SAnna Dabrowska * helper plugin only. If additional interface functionality is needed,
217dac04ffSAndreas Gohr * it should be added here.
2246ca39b7SAndreas Gohr *
2346ca39b7SAndreas Gohr * All functions will throw StructExceptions when something goes wrong.
247dac04ffSAndreas Gohr *
257dac04ffSAndreas Gohr * Remember to check permissions yourself!
2646ca39b7SAndreas Gohr */
27d6d97f60SAnna Dabrowskaclass helper_plugin_struct extends DokuWiki_Plugin
28d6d97f60SAnna Dabrowska{
2946ca39b7SAndreas Gohr    /**
3034ea6e10SAnna Dabrowska     * Class names of renderers which should NOT render struct data.
3134ea6e10SAnna Dabrowska     * All descendants are also blacklisted.
3234ea6e10SAnna Dabrowska     */
3317a3a578SAndreas Gohr    public const BLACKLIST_RENDERER = [
3434ea6e10SAnna Dabrowska        'Doku_Renderer_metadata',
3534ea6e10SAnna Dabrowska        '\renderer_plugin_qc'
3634ea6e10SAnna Dabrowska    ];
3734ea6e10SAnna Dabrowska
3834ea6e10SAnna Dabrowska    /**
3946ca39b7SAndreas Gohr     * Get the structured data of a given page
4046ca39b7SAndreas Gohr     *
4146ca39b7SAndreas Gohr     * @param string $page The page to get data for
4246ca39b7SAndreas Gohr     * @param string|null $schema The schema to use null for all
43387ee210SAnna Dabrowska     * @param int $time A timestamp if you want historic data
4446ca39b7SAndreas Gohr     * @return array ('schema' => ( 'fieldlabel' => 'value', ...))
4546ca39b7SAndreas Gohr     * @throws StructException
4646ca39b7SAndreas Gohr     */
47d6d97f60SAnna Dabrowska    public function getData($page, $schema = null, $time = 0)
48d6d97f60SAnna Dabrowska    {
497dac04ffSAndreas Gohr        $page = cleanID($page);
50387ee210SAnna Dabrowska        if (!$time) {
51387ee210SAnna Dabrowska            $time = time();
52387ee210SAnna Dabrowska        }
5346ca39b7SAndreas Gohr
547dac04ffSAndreas Gohr        if (is_null($schema)) {
55025cb9daSAndreas Gohr            $assignments = Assignments::getInstance();
567dac04ffSAndreas Gohr            $schemas = $assignments->getPageAssignments($page, false);
577dac04ffSAndreas Gohr        } else {
587dac04ffSAndreas Gohr            $schemas = array($schema);
597dac04ffSAndreas Gohr        }
607dac04ffSAndreas Gohr
617dac04ffSAndreas Gohr        $result = array();
627dac04ffSAndreas Gohr        foreach ($schemas as $schema) {
634cd5cc28SAnna Dabrowska            $schemaData = AccessTable::getPageAccess($schema, $page, $time);
640dd23cefSAndreas Gohr            $result[$schema] = $schemaData->getDataArray();
657dac04ffSAndreas Gohr        }
667dac04ffSAndreas Gohr
677dac04ffSAndreas Gohr        return $result;
6846ca39b7SAndreas Gohr    }
6946ca39b7SAndreas Gohr
7046ca39b7SAndreas Gohr    /**
7146ca39b7SAndreas Gohr     * Saves data for a given page (creates a new revision)
7246ca39b7SAndreas Gohr     *
737dac04ffSAndreas Gohr     * If this call succeeds you can assume your data has either been saved or it was
747dac04ffSAndreas Gohr     * not necessary to save it because the data already existed in the wanted form or
757dac04ffSAndreas Gohr     * the given schemas are no longer assigned to that page.
767dac04ffSAndreas Gohr     *
777dac04ffSAndreas Gohr     * Important: You have to check write permissions for the given page before calling
787dac04ffSAndreas Gohr     * this function yourself!
797dac04ffSAndreas Gohr     *
807dac04ffSAndreas Gohr     * this duplicates a bit of code from entry.php - we could also fake post data and let
817dac04ffSAndreas Gohr     * entry handle it, but that would be rather unclean and might be problematic when multiple
827dac04ffSAndreas Gohr     * calls are done within the same request.
837dac04ffSAndreas Gohr     *
8446ca39b7SAndreas Gohr     * @param string $page
857dac04ffSAndreas Gohr     * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...))
8646ca39b7SAndreas Gohr     * @param string $summary
87178d3992SElan Ruusamäe     * @param string $summary
8846ca39b7SAndreas Gohr     * @throws StructException
890549dcc5SAndreas Gohr     * @todo should this try to lock the page?
900549dcc5SAndreas Gohr     *
910549dcc5SAndreas Gohr     *
9246ca39b7SAndreas Gohr     */
93178d3992SElan Ruusamäe    public function saveData($page, $data, $summary = '', $minor = false)
94178d3992SElan Ruusamäe    {
957dac04ffSAndreas Gohr        $page = cleanID($page);
967dac04ffSAndreas Gohr        $summary = trim($summary);
977dac04ffSAndreas Gohr        if (!$summary) $summary = $this->getLang('summary');
9846ca39b7SAndreas Gohr
997dac04ffSAndreas Gohr        if (!page_exists($page)) throw new StructException("Page does not exist. You can not attach struct data");
1007dac04ffSAndreas Gohr
1017dac04ffSAndreas Gohr        // validate and see if anything changes
10293ca6f4fSAndreas Gohr        $valid = AccessDataValidator::validateDataForPage($data, $page, $errors);
10393ca6f4fSAndreas Gohr        if ($valid === false) {
10493ca6f4fSAndreas Gohr            throw new StructException("Validation failed:\n%s", join("\n", $errors));
1057dac04ffSAndreas Gohr        }
10693ca6f4fSAndreas Gohr        if (!$valid) return; // empty array when no changes were detected
1077dac04ffSAndreas Gohr
108178d3992SElan Ruusamäe        $newrevision = self::createPageRevision($page, $summary, $minor);
1097dac04ffSAndreas Gohr
1107dac04ffSAndreas Gohr        // save the provided data
111025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
11293ca6f4fSAndreas Gohr        foreach ($valid as $v) {
11393ca6f4fSAndreas Gohr            $v->saveData($newrevision);
1147dac04ffSAndreas Gohr            // make sure this schema is assigned
11593ca6f4fSAndreas Gohr            $assignments->assignPageSchema($page, $v->getAccessTable()->getSchema()->getTable());
1167dac04ffSAndreas Gohr        }
11746ca39b7SAndreas Gohr    }
11846ca39b7SAndreas Gohr
11946ca39b7SAndreas Gohr    /**
12010575566SAnna Dabrowska     * Save lookup data row
121ff2afc7cSMichael Große     *
1220ceefd5cSAnna Dabrowska     * @param AccessTable $access the table into which to save the data
123ff2afc7cSMichael Große     * @param array $data data to be saved in the form of [columnName => 'data']
124ff2afc7cSMichael Große     */
1250ceefd5cSAnna Dabrowska    public function saveLookupData(AccessTable $access, $data)
126ff2afc7cSMichael Große    {
127ff2afc7cSMichael Große        if (!$access->getSchema()->isEditable()) {
128ff2afc7cSMichael Große            throw new StructException('lookup save error: no permission for schema');
129ff2afc7cSMichael Große        }
130ff2afc7cSMichael Große        $validator = $access->getValidator($data);
131ff2afc7cSMichael Große        if (!$validator->validate()) {
132ff2afc7cSMichael Große            throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors()));
133ff2afc7cSMichael Große        }
134ff2afc7cSMichael Große        if (!$validator->saveData()) {
135ff2afc7cSMichael Große            throw new StructException('No data saved');
136ff2afc7cSMichael Große        }
137ff2afc7cSMichael Große    }
138ff2afc7cSMichael Große
139ff2afc7cSMichael Große    /**
14013eddb0fSAndreas Gohr     * Creates a new page revision with the same page content as before
14113eddb0fSAndreas Gohr     *
14213eddb0fSAndreas Gohr     * @param string $page
14313eddb0fSAndreas Gohr     * @param string $summary
14413eddb0fSAndreas Gohr     * @param bool $minor
14513eddb0fSAndreas Gohr     * @return int the new revision
14613eddb0fSAndreas Gohr     */
147d6d97f60SAnna Dabrowska    public static function createPageRevision($page, $summary = '', $minor = false)
148d6d97f60SAnna Dabrowska    {
14913eddb0fSAndreas Gohr        $summary = trim($summary);
15013eddb0fSAndreas Gohr        // force a new page revision @see action_plugin_struct_entry::handle_pagesave_before()
15113eddb0fSAndreas Gohr        $GLOBALS['struct_plugin_force_page_save'] = true;
15213eddb0fSAndreas Gohr        saveWikiText($page, rawWiki($page), $summary, $minor);
15313eddb0fSAndreas Gohr        unset($GLOBALS['struct_plugin_force_page_save']);
15413eddb0fSAndreas Gohr        $file = wikiFN($page);
15513eddb0fSAndreas Gohr        clearstatcache(false, $file);
15613eddb0fSAndreas Gohr        return filemtime($file);
15713eddb0fSAndreas Gohr    }
15813eddb0fSAndreas Gohr
15913eddb0fSAndreas Gohr    /**
16046ca39b7SAndreas Gohr     * Get info about existing schemas
16146ca39b7SAndreas Gohr     *
16246ca39b7SAndreas Gohr     * @param string|null $schema the schema to query, null for all
1637dac04ffSAndreas Gohr     * @return Schema[]
16446ca39b7SAndreas Gohr     * @throws StructException
16546ca39b7SAndreas Gohr     */
166*6c9d1a10SAnna Dabrowska    public static function getSchema($schema = null)
167d6d97f60SAnna Dabrowska    {
1687dac04ffSAndreas Gohr        if (is_null($schema)) {
1697dac04ffSAndreas Gohr            $schemas = Schema::getAll();
1707dac04ffSAndreas Gohr        } else {
1717dac04ffSAndreas Gohr            $schemas = array($schema);
1727dac04ffSAndreas Gohr        }
17346ca39b7SAndreas Gohr
1747dac04ffSAndreas Gohr        $result = array();
1757dac04ffSAndreas Gohr        foreach ($schemas as $table) {
1767dac04ffSAndreas Gohr            $result[$table] = new Schema($table);
1777dac04ffSAndreas Gohr        }
1787dac04ffSAndreas Gohr        return $result;
17946ca39b7SAndreas Gohr    }
18046ca39b7SAndreas Gohr
18146ca39b7SAndreas Gohr    /**
18246ca39b7SAndreas Gohr     * Returns all pages known to the struct plugin
18346ca39b7SAndreas Gohr     *
18446ca39b7SAndreas Gohr     * That means all pages that have or had once struct data saved
18546ca39b7SAndreas Gohr     *
18646ca39b7SAndreas Gohr     * @param string|null $schema limit the result to a given schema
1877dac04ffSAndreas Gohr     * @return array (page => (schema => true), ...)
18846ca39b7SAndreas Gohr     * @throws StructException
18946ca39b7SAndreas Gohr     */
190d6d97f60SAnna Dabrowska    public function getPages($schema = null)
191d6d97f60SAnna Dabrowska    {
192025cb9daSAndreas Gohr        $assignments = Assignments::getInstance();
1937dac04ffSAndreas Gohr        return $assignments->getPages($schema);
19446ca39b7SAndreas Gohr    }
195565b4cc1SAnna Dabrowska
19634ea6e10SAnna Dabrowska    /**
19734ea6e10SAnna Dabrowska     * Returns decoded JSON value or throws exception
19834ea6e10SAnna Dabrowska     * when passed parameter is not JSON
19934ea6e10SAnna Dabrowska     *
20034ea6e10SAnna Dabrowska     * @param string $value
20134ea6e10SAnna Dabrowska     * @return mixed
20234ea6e10SAnna Dabrowska     * @throws StructException
20334ea6e10SAnna Dabrowska     */
204565b4cc1SAnna Dabrowska    public static function decodeJson($value)
205565b4cc1SAnna Dabrowska    {
206565b4cc1SAnna Dabrowska        if (!empty($value) && $value[0] !== '[') throw new StructException('Lookup expects JSON');
207565b4cc1SAnna Dabrowska        return json_decode($value);
208565b4cc1SAnna Dabrowska    }
20946ca39b7SAndreas Gohr}
210