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 20*34ea6e10SAnna 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 3046ca39b7SAndreas Gohr /** 31*34ea6e10SAnna Dabrowska * Class names of renderers which should NOT render struct data. 32*34ea6e10SAnna Dabrowska * All descendants are also blacklisted. 33*34ea6e10SAnna Dabrowska */ 34*34ea6e10SAnna Dabrowska const BLACKLIST_RENDERER = [ 35*34ea6e10SAnna Dabrowska 'Doku_Renderer_metadata', 36*34ea6e10SAnna Dabrowska '\renderer_plugin_qc' 37*34ea6e10SAnna Dabrowska ]; 38*34ea6e10SAnna Dabrowska 39*34ea6e10SAnna Dabrowska /** 4046ca39b7SAndreas Gohr * Get the structured data of a given page 4146ca39b7SAndreas Gohr * 4246ca39b7SAndreas Gohr * @param string $page The page to get data for 4346ca39b7SAndreas Gohr * @param string|null $schema The schema to use null for all 44387ee210SAnna Dabrowska * @param int $time A timestamp if you want historic data 4546ca39b7SAndreas Gohr * @return array ('schema' => ( 'fieldlabel' => 'value', ...)) 4646ca39b7SAndreas Gohr * @throws StructException 4746ca39b7SAndreas Gohr */ 48d6d97f60SAnna Dabrowska public function getData($page, $schema = null, $time = 0) 49d6d97f60SAnna Dabrowska { 507dac04ffSAndreas Gohr $page = cleanID($page); 51387ee210SAnna Dabrowska if (!$time) { 52387ee210SAnna Dabrowska $time = time(); 53387ee210SAnna Dabrowska } 5446ca39b7SAndreas Gohr 557dac04ffSAndreas Gohr if (is_null($schema)) { 56025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 577dac04ffSAndreas Gohr $schemas = $assignments->getPageAssignments($page, false); 587dac04ffSAndreas Gohr } else { 597dac04ffSAndreas Gohr $schemas = array($schema); 607dac04ffSAndreas Gohr } 617dac04ffSAndreas Gohr 627dac04ffSAndreas Gohr $result = array(); 637dac04ffSAndreas Gohr foreach ($schemas as $schema) { 644cd5cc28SAnna Dabrowska $schemaData = AccessTable::getPageAccess($schema, $page, $time); 650dd23cefSAndreas Gohr $result[$schema] = $schemaData->getDataArray(); 667dac04ffSAndreas Gohr } 677dac04ffSAndreas Gohr 687dac04ffSAndreas Gohr return $result; 6946ca39b7SAndreas Gohr } 7046ca39b7SAndreas Gohr 7146ca39b7SAndreas Gohr /** 7246ca39b7SAndreas Gohr * Saves data for a given page (creates a new revision) 7346ca39b7SAndreas Gohr * 747dac04ffSAndreas Gohr * If this call succeeds you can assume your data has either been saved or it was 757dac04ffSAndreas Gohr * not necessary to save it because the data already existed in the wanted form or 767dac04ffSAndreas Gohr * the given schemas are no longer assigned to that page. 777dac04ffSAndreas Gohr * 787dac04ffSAndreas Gohr * Important: You have to check write permissions for the given page before calling 797dac04ffSAndreas Gohr * this function yourself! 807dac04ffSAndreas Gohr * 817dac04ffSAndreas Gohr * this duplicates a bit of code from entry.php - we could also fake post data and let 827dac04ffSAndreas Gohr * entry handle it, but that would be rather unclean and might be problematic when multiple 837dac04ffSAndreas Gohr * calls are done within the same request. 847dac04ffSAndreas Gohr * 857dac04ffSAndreas Gohr * @todo should this try to lock the page? 867dac04ffSAndreas Gohr * 877dac04ffSAndreas Gohr * 8846ca39b7SAndreas Gohr * @param string $page 897dac04ffSAndreas Gohr * @param array $data ('schema' => ( 'fieldlabel' => 'value', ...)) 9046ca39b7SAndreas Gohr * @param string $summary 91178d3992SElan Ruusamäe * @param string $summary 9246ca39b7SAndreas Gohr * @throws StructException 9346ca39b7SAndreas Gohr */ 94178d3992SElan Ruusamäe public function saveData($page, $data, $summary = '', $minor = false) 95178d3992SElan Ruusamäe { 967dac04ffSAndreas Gohr $page = cleanID($page); 977dac04ffSAndreas Gohr $summary = trim($summary); 987dac04ffSAndreas Gohr if (!$summary) $summary = $this->getLang('summary'); 9946ca39b7SAndreas Gohr 1007dac04ffSAndreas Gohr if (!page_exists($page)) throw new StructException("Page does not exist. You can not attach struct data"); 1017dac04ffSAndreas Gohr 1027dac04ffSAndreas Gohr // validate and see if anything changes 10393ca6f4fSAndreas Gohr $valid = AccessDataValidator::validateDataForPage($data, $page, $errors); 10493ca6f4fSAndreas Gohr if ($valid === false) { 10593ca6f4fSAndreas Gohr throw new StructException("Validation failed:\n%s", join("\n", $errors)); 1067dac04ffSAndreas Gohr } 10793ca6f4fSAndreas Gohr if (!$valid) return; // empty array when no changes were detected 1087dac04ffSAndreas Gohr 109178d3992SElan Ruusamäe $newrevision = self::createPageRevision($page, $summary, $minor); 1107dac04ffSAndreas Gohr 1117dac04ffSAndreas Gohr // save the provided data 112025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 11393ca6f4fSAndreas Gohr foreach ($valid as $v) { 11493ca6f4fSAndreas Gohr $v->saveData($newrevision); 1157dac04ffSAndreas Gohr // make sure this schema is assigned 11693ca6f4fSAndreas Gohr $assignments->assignPageSchema($page, $v->getAccessTable()->getSchema()->getTable()); 1177dac04ffSAndreas Gohr } 11846ca39b7SAndreas Gohr } 11946ca39b7SAndreas Gohr 12046ca39b7SAndreas Gohr /** 12110575566SAnna Dabrowska * Save lookup data row 122ff2afc7cSMichael Große * 1230ceefd5cSAnna Dabrowska * @param AccessTable $access the table into which to save the data 124ff2afc7cSMichael Große * @param array $data data to be saved in the form of [columnName => 'data'] 125ff2afc7cSMichael Große */ 1260ceefd5cSAnna Dabrowska public function saveLookupData(AccessTable $access, $data) 127ff2afc7cSMichael Große { 128ff2afc7cSMichael Große if (!$access->getSchema()->isEditable()) { 129ff2afc7cSMichael Große throw new StructException('lookup save error: no permission for schema'); 130ff2afc7cSMichael Große } 131ff2afc7cSMichael Große $validator = $access->getValidator($data); 132ff2afc7cSMichael Große if (!$validator->validate()) { 133ff2afc7cSMichael Große throw new StructException("Validation failed:\n%s", implode("\n", $validator->getErrors())); 134ff2afc7cSMichael Große } 135ff2afc7cSMichael Große if (!$validator->saveData()) { 136ff2afc7cSMichael Große throw new StructException('No data saved'); 137ff2afc7cSMichael Große } 138ff2afc7cSMichael Große } 139ff2afc7cSMichael Große 140ff2afc7cSMichael Große /** 14113eddb0fSAndreas Gohr * Creates a new page revision with the same page content as before 14213eddb0fSAndreas Gohr * 14313eddb0fSAndreas Gohr * @param string $page 14413eddb0fSAndreas Gohr * @param string $summary 14513eddb0fSAndreas Gohr * @param bool $minor 14613eddb0fSAndreas Gohr * @return int the new revision 14713eddb0fSAndreas Gohr */ 148d6d97f60SAnna Dabrowska public static function createPageRevision($page, $summary = '', $minor = false) 149d6d97f60SAnna Dabrowska { 15013eddb0fSAndreas Gohr $summary = trim($summary); 15113eddb0fSAndreas Gohr // force a new page revision @see action_plugin_struct_entry::handle_pagesave_before() 15213eddb0fSAndreas Gohr $GLOBALS['struct_plugin_force_page_save'] = true; 15313eddb0fSAndreas Gohr saveWikiText($page, rawWiki($page), $summary, $minor); 15413eddb0fSAndreas Gohr unset($GLOBALS['struct_plugin_force_page_save']); 15513eddb0fSAndreas Gohr $file = wikiFN($page); 15613eddb0fSAndreas Gohr clearstatcache(false, $file); 15713eddb0fSAndreas Gohr return filemtime($file); 15813eddb0fSAndreas Gohr } 15913eddb0fSAndreas Gohr 16013eddb0fSAndreas Gohr /** 16146ca39b7SAndreas Gohr * Get info about existing schemas 16246ca39b7SAndreas Gohr * 16346ca39b7SAndreas Gohr * @param string|null $schema the schema to query, null for all 1647dac04ffSAndreas Gohr * @return Schema[] 16546ca39b7SAndreas Gohr * @throws StructException 16646ca39b7SAndreas Gohr */ 167d6d97f60SAnna Dabrowska public function getSchema($schema = null) 168d6d97f60SAnna Dabrowska { 1697dac04ffSAndreas Gohr if (is_null($schema)) { 1707dac04ffSAndreas Gohr $schemas = Schema::getAll(); 1717dac04ffSAndreas Gohr } else { 1727dac04ffSAndreas Gohr $schemas = array($schema); 1737dac04ffSAndreas Gohr } 17446ca39b7SAndreas Gohr 1757dac04ffSAndreas Gohr $result = array(); 1767dac04ffSAndreas Gohr foreach ($schemas as $table) { 1777dac04ffSAndreas Gohr $result[$table] = new Schema($table); 1787dac04ffSAndreas Gohr } 1797dac04ffSAndreas Gohr return $result; 18046ca39b7SAndreas Gohr } 18146ca39b7SAndreas Gohr 18246ca39b7SAndreas Gohr /** 18346ca39b7SAndreas Gohr * Returns all pages known to the struct plugin 18446ca39b7SAndreas Gohr * 18546ca39b7SAndreas Gohr * That means all pages that have or had once struct data saved 18646ca39b7SAndreas Gohr * 18746ca39b7SAndreas Gohr * @param string|null $schema limit the result to a given schema 1887dac04ffSAndreas Gohr * @return array (page => (schema => true), ...) 18946ca39b7SAndreas Gohr * @throws StructException 19046ca39b7SAndreas Gohr */ 191d6d97f60SAnna Dabrowska public function getPages($schema = null) 192d6d97f60SAnna Dabrowska { 193025cb9daSAndreas Gohr $assignments = Assignments::getInstance(); 1947dac04ffSAndreas Gohr return $assignments->getPages($schema); 19546ca39b7SAndreas Gohr } 196565b4cc1SAnna Dabrowska 197*34ea6e10SAnna Dabrowska /** 198*34ea6e10SAnna Dabrowska * Returns decoded JSON value or throws exception 199*34ea6e10SAnna Dabrowska * when passed parameter is not JSON 200*34ea6e10SAnna Dabrowska * 201*34ea6e10SAnna Dabrowska * @param string $value 202*34ea6e10SAnna Dabrowska * @return mixed 203*34ea6e10SAnna Dabrowska * @throws StructException 204*34ea6e10SAnna Dabrowska */ 205565b4cc1SAnna Dabrowska public static function decodeJson($value) 206565b4cc1SAnna Dabrowska { 207565b4cc1SAnna Dabrowska if (!empty($value) && $value[0] !== '[') throw new StructException('Lookup expects JSON'); 208565b4cc1SAnna Dabrowska return json_decode($value); 209565b4cc1SAnna Dabrowska } 21046ca39b7SAndreas Gohr} 211