/** * Load a whole schema as fields * * @param Doku_Event $event event object by reference * @param mixed $param [the parameters passed as fifth argument to register_hook() when this * handler was registered] * @return bool */ public function handle_schema(Doku_Event $event, $param) { $args = $event->data['args']; if ($args[0] != 'struct_schema') { return false; } $event->preventDefault(); $event->stopPropagation(); /** @var helper_plugin_bureaucracy_field $helper */ $helper = plugin_load('helper', 'bureaucracy_field'); $helper->initialize($args); $schema = new Schema($helper->opt['label']); if (!$schema->getId()) { msg('This schema does not exist', -1); return false; } foreach ($schema->getColumns(false) as $column) { /** @var helper_plugin_struct_field $field */ $field = plugin_load('helper', 'struct_field'); // we don't initialize the field but set the appropriate values $field->opt = $helper->opt; // copy all the settings to each field $field->opt['label'] = $column->getFullQualifiedLabel(); $field->column = $column; $event->data['fields'][] = $field; } return true; }
/** * Get the json of the current version of the given schema or false if the schema doesn't exist. * * @param string $schemaName * @return string|bool The json string or false if the schema doesn't exist */ public function getCurrentSchemaJSON($schemaName) { $schema = new Schema($schemaName); if ($schema->getId() == 0) { return false; } return $schema->toJSON(); }
/** * @param Schema $schema * @param int|string $pid * @param int $ts * @return meta\AccessTableLookup|AccessTableData */ public static function bySchema(Schema $schema, $pid, $ts = 0) { if ($schema->isLookup()) { return new meta\AccessTableLookup($schema, $pid, $ts); // FIXME not mocked, yet } else { return new AccessTableData($schema, $pid, $ts); } }
public function test_deleteok() { $this->loadSchemaJSON('schema1'); $schema = new Schema('schema1'); $this->assertEquals(1, $schema->getId()); $schema->delete(); $this->assertEquals(0, $schema->getId()); $schema = new Schema('schema1'); $this->assertEquals(0, $schema->getId()); }
/** * Add a schema to be searched * * Call multiple times for multiple schemas. * * @param string $table * @param string $alias */ public function addSchema($table, $alias = '') { $schema = new Schema($table); if (!$schema->getId()) { throw new StructException('schema missing', $table); } if ($this->schemas && ($schema->isLookup() || reset($this->schemas)->isLookup())) { throw new StructException('nolookupmix'); } $this->schemas[$table] = $schema; if ($alias) { $this->aliases[$alias] = $table; } }
public function test_import_export() { $sb = new meta\SchemaImporter('foobar', file_get_contents(__DIR__ . '/json/schema1.struct.json')); $this->assertTrue((bool) $sb->build()); $schema = new meta\Schema('foobar'); $expect = json_decode(file_get_contents(__DIR__ . '/json/schema1.struct.json'), true); $actual = json_decode($schema->toJSON(), true); // we don't expect this to match unset($expect['structversion']); unset($actual['structversion']); $expect['schema'] = 'foobar'; // we exported the new schema $this->assertEquals($expect, $actual); }
public function test_build_update() { // arrange $initialdata = array(); $initialdata['new']['new1']['sort'] = 70; $initialdata['new']['new1']['label'] = 'testcolumn'; $initialdata['new']['new1']['ismulti'] = 0; $initialdata['new']['new1']['config'] = '{"prefix": "", "postfix": ""}'; $initialdata['new']['new1']['class'] = 'Text'; $initialdata['new']['new1']['isenabled'] = '1'; $testname = 'testTable'; $testname = Schema::cleanTableName($testname); $builder = new SchemaBuilder($testname, $initialdata); $result = $builder->build(); $this->assertSame($result, '1', 'Prerequiste setup in order to have basis which to change during act'); $updatedata = array(); $updatedata['id'] = "1"; $updatedata['cols']['1']['sort'] = 65; $updatedata['cols']['1']['label'] = 'testColumn'; $updatedata['cols']['1']['ismulti'] = 1; $updatedata['cols']['1']['config'] = '{"prefix": "pre", "postfix": "fix"}'; $updatedata['cols']['1']['class'] = 'Text'; $updatedata['cols']['1']['isenabled'] = '1'; // act $builder = new SchemaBuilder($testname, $updatedata); $result = $builder->build(); $res = $this->sqlite->query("SELECT * FROM types"); $actual_types = $this->sqlite->res2arr($res); $this->sqlite->res_close($res); $expected_types = array(array('id' => "1", 'class' => 'Text', 'ismulti' => "0", 'label' => "testcolumn", 'config' => '{"prefix": "", "postfix": ""}'), array('id' => "2", 'class' => 'Text', 'ismulti' => "1", 'label' => "testColumn", 'config' => '{"prefix": "pre", "postfix": "fix"}')); // assert $this->assertSame($result, '2'); $this->assertEquals($actual_types, $expected_types); }
/** * Renames all occurances of a page ID in the database * * @param Doku_Event $event event object by reference * @param mixed $param [the parameters passed as fifth argument to register_hook() when this * handler was registered] * @return bool */ public function handle_move(Doku_Event $event, $param) { /** @var helper_plugin_struct_db $hlp */ $hlp = plugin_load('helper', 'struct_db'); $db = $hlp->getDB(); if (!$db) { return false; } $old = $event->data['src_id']; $new = $event->data['dst_id']; // ALL data tables (we don't trust the assigments are still there) foreach (Schema::getAll() as $tbl) { $sql = "UPDATE data_{$tbl} SET pid = ? WHERE pid = ?"; $db->query($sql, array($new, $old)); $sql = "UPDATE multi_{$tbl} SET pid = ? WHERE pid = ?"; $db->query($sql, array($new, $old)); } // assignments $sql = "UPDATE schema_assignments SET pid = ? WHERE pid = ?"; $db->query($sql, array($new, $old)); // make sure assignments still match patterns; $assignments = new Assignments(); $assignments->reevaluatePageAssignments($new); return true; }
public function test_autocomplete() { global $INPUT; $schema = new Schema('tag'); // search tag field, should not find Aragon because tag is not in current revision $INPUT->set('search', 'ar'); $tag = $schema->findColumn('tag')->getType(); $return = $tag->handleAjax(); $expect = array(array('label' => 'Eldarion', 'value' => 'Eldarion'), array('label' => 'Treebeard', 'value' => 'Treebeard')); $this->assertEquals($expect, $return); // multi value $INPUT->set('search', 'ar'); $tag = $schema->findColumn('tags')->getType(); $return = $tag->handleAjax(); $expect = array(array('label' => 'Arwen', 'value' => 'Arwen'), array('label' => 'Saruman', 'value' => 'Saruman')); $this->assertEquals($expect, $return); }
/** * Check the input variables and run the AJAX call * * @throws StructException * @return mixed */ protected function executeTypeAjax() { global $INPUT; $col = $INPUT->str('column'); if (blank($col)) { throw new StructException('No column provided'); } list($schema, $colname) = explode('.', $col, 2); if (blank($schema) || blank($colname)) { throw new StructException('Column format is wrong'); } $schema = new Schema($schema); if (!$schema->getId()) { throw new StructException('Unknown Schema'); } $column = $schema->findColumn($colname); if ($column === false) { throw new StructException('Column not found'); } return $column->getType()->handleAjax(); }
/** * Sort by lookup table * * @param QueryBuilder $QB * @param string $tablealias * @param string $colname * @param string $order */ public function sort(QueryBuilder $QB, $tablealias, $colname, $order) { if (!$this->usesLookup()) { parent::sort($QB, $tablealias, $colname, $order); return; } $schema = 'data_' . $this->schema->getTable(); $field = $this->column->getColName(); $rightalias = $QB->generateTableAlias(); $QB->addLeftJoin($tablealias, $schema, $rightalias, "{$tablealias}.{$colname} = {$rightalias}.pid"); $this->column->getType()->sort($QB, $rightalias, $field, $order); }
/** * Adds all available schemas to the Table of Contents * * @return array */ public function getTOC() { global $ID; $toc = array(); $link = wl($ID, array('do' => 'admin', 'page' => 'struct_assignments')); $toc[] = html_mktocitem($link, $this->getLang('menu_assignments'), 0, ''); $slink = wl($ID, array('do' => 'admin', 'page' => 'struct_schemas')); $toc[] = html_mktocitem($slink, $this->getLang('menu'), 0, ''); $tables = Schema::getAll('page'); if ($tables) { $toc[] = html_mktocitem($slink, $this->getLang('page schema'), 1, ''); foreach ($tables as $table) { $link = wl($ID, array('do' => 'admin', 'page' => 'struct_schemas', 'table' => $table)); $toc[] = html_mktocitem($link, hsc($table), 2, ''); } } $tables = Schema::getAll('lookup'); if ($tables) { $toc[] = html_mktocitem($slink, $this->getLang('lookup schema'), 1, ''); foreach ($tables as $table) { $link = wl($ID, array('do' => 'admin', 'page' => 'struct_schemas', 'table' => $table)); $toc[] = html_mktocitem($link, hsc($table), 2, ''); } } return $toc; }
/** * Get info about existing schemas * * @param string|null $schema the schema to query, null for all * @return Schema[] * @throws StructException */ public function getSchema($schema = null) { if (is_null($schema)) { $schemas = Schema::getAll(); } else { $schemas = array($schema); } $result = array(); foreach ($schemas as $table) { $result[$table] = new Schema($table); } return $result; }
/** * Render HTML output, e.g. helpful text and a form */ public function html() { global $ID; echo $this->locale_xhtml('assignments_intro'); $ass = new Assignments(); $assignments = $ass->getAllPatterns(); echo '<form action="' . wl($ID) . '" action="post">'; echo '<input type="hidden" name="do" value="admin" />'; echo '<input type="hidden" name="page" value="struct_assignments" />'; echo '<input type="hidden" name="sectok" value="' . getSecurityToken() . '" />'; echo '<table class="inline">'; // header echo '<tr>'; echo '<th>' . $this->getLang('assign_assign') . '</th>'; echo '<th>' . $this->getLang('assign_tbl') . '</th>'; echo '<th></th>'; echo '</tr>'; // existing assignments foreach ($assignments as $assignment) { $schema = $assignment['tbl']; $assignee = $assignment['pattern']; $link = wl($ID, array('do' => 'admin', 'page' => 'struct_assignments', 'action' => 'delete', 'sectok' => getSecurityToken(), 'assignment[tbl]' => $schema, 'assignment[assign]' => $assignee)); echo '<tr>'; echo '<td>' . hsc($assignee) . '</td>'; echo '<td>' . hsc($schema) . '</td>'; echo '<td><a class="deleteSchema" href="' . $link . '">' . $this->getLang('assign_del') . '</a></td>'; echo '</tr>'; } // new assignment form echo '<tr>'; echo '<td><input type="text" name="assignment[assign]" /></td>'; echo '<td>'; echo '<select name="assignment[tbl]">'; foreach (Schema::getAll('page') as $table) { echo '<option value="' . hsc($table) . '">' . hsc($table) . '</option>'; } echo '</select>'; echo '</td>'; echo '<td><button type="submit" name="action" value="add">' . $this->getLang('assign_add') . '</button></td>'; echo '</tr>'; echo '</table>'; }
/** * Tries to find the correct column and schema * * @throws StructException * @param string $colname * @return \dokuwiki\plugin\struct\meta\Column */ protected function findColumn($colname) { list($table, $label) = explode('.', $colname, 2); if (!$table || !$label) { throw new StructException('Field \'%s\' not given in schema.field form', $colname); } $schema = new Schema($table); return $schema->findColumn($label); }
/** * Create the Editor for a new lookup row */ protected function lookup_new() { global $INPUT; global $lang; $tablename = $INPUT->str('schema'); $schema = new Schema($tablename); if (!$schema->isEditable()) { return; } // no permissions, no editor echo '<div class="struct_entry_form">'; echo '<fieldset>'; echo '<legend>' . $this->getLang('lookup new entry') . '</legend>'; /** @var action_plugin_struct_entry $entry */ $entry = plugin_load('action', 'struct_entry'); foreach ($schema->getColumns(false) as $column) { $label = $column->getLabel(); $field = new Value($column, ''); echo $entry->makeField($field, "entry[{$label}]"); } formSecurityToken(); // csrf protection echo '<input type="hidden" name="call" value="plugin_struct_lookup_save" />'; echo '<input type="hidden" name="schema" value="' . hsc($tablename) . '" />'; echo '<button type="submit">' . $lang['btn_save'] . '</button>'; echo '<div class="err"></div>'; echo '</fieldset>'; echo '</div>'; }