Пример #1
0
 protected function installData(\Tabulate\DB\Database $db)
 {
     $this->write("Confirming existance of administrative user, group, and grant");
     // Can't log changes without a user (admin, in this case). So we create a user manually.
     $pwd = password_hash('admin', PASSWORD_DEFAULT);
     $adminUserData = ['id' => Users::ADMIN, 'name' => 'Admin', 'email' => Config::siteEmail(), 'password' => $pwd];
     $adminSql = "INSERT IGNORE INTO `users` SET `id`=:id, `name`=:name, `email`=:email, `password`=:password";
     $db->query($adminSql, $adminUserData);
     // Then we want to create a second user (anon), but this time recording changes. The change-tracker needs to
     // know about permissions, so before creating the 2nd user that we need to grant permission to admin.
     // Permissions are granted to groups, not users, so we put admin in an admin group first (manually).
     $params2 = ['id' => Groups::ADMINISTRATORS, 'name' => 'Administrators'];
     $db->query("INSERT IGNORE INTO `groups` SET `id`=:id, `name`=:name", $params2);
     $params3 = ['user' => Users::ADMIN, 'group' => Groups::ADMINISTRATORS];
     $db->query("INSERT IGNORE INTO `group_members` SET `user`=:user, `group`=:group", $params3);
     // Now we can grant everything (on everything) to the admin group.
     $db->query("INSERT IGNORE INTO `grants` SET `group`=:group", ['group' => Groups::ADMINISTRATORS]);
     // And finally 'reset' the DB so it knows about the above new records.
     $db->reset();
     // Start tracking changes now that there's a user to attribute it to.
     $db->setCurrentUser(Users::ADMIN);
     $changeTracker = new \Tabulate\DB\ChangeTracker($db);
     $changeTracker->openChangeset('Installation', true);
     // Create remaining default users and groups.
     if (!$db->getTable('users')->getRecord(Users::ANON)) {
         $this->write("Inserting user 'Anonymous'");
         $db->getTable('users')->saveRecord(['id' => Users::ANON, 'name' => 'Anonymous']);
     }
     if (!$db->getTable('groups', false)->getRecord(Groups::GENERAL_PUBLIC)) {
         $this->write("Inserting group 'General Public'");
         $db->getTable('groups', false)->saveRecord(['id' => Groups::GENERAL_PUBLIC, 'name' => 'General Public']);
     }
     // Add Anon user to the General Public group.
     $groupMembers = $db->getTable('group_members', false);
     $groupMembers->addFilter('user', '=', Users::ANON);
     $groupMembers->addFilter('group', '=', Groups::GENERAL_PUBLIC);
     if ($groupMembers->getRecordCount() === 0) {
         $this->write("Adding user 'Anonymous' to group 'General Public'");
         $groupMembers->saveRecord(['group' => Groups::GENERAL_PUBLIC, 'user' => Users::ANON]);
     }
     // Add first report (to list reports).
     if (0 == $db->query("SELECT COUNT(*) FROM `" . Reports::reportsTableName() . "`")->fetchColumn()) {
         // Create the default report, to list all reports.
         $templateString = "<dl>\n" . "{% for report in reports %}\n" . "  <dt><a href='{{baseurl}}/reports/{{report.id}}'>{{report.title}}</a></dt>\n" . "  <dd>{{report.description}}</dd>\n" . "{% endfor %}\n" . "</dl>";
         $sql1 = "INSERT INTO `" . Reports::reportsTableName() . "` SET" . " id          = " . Reports::DEFAULT_REPORT_ID . ", " . " title       = 'Reports', " . " description = 'List of all Reports.'," . " template    = :template;";
         $db->query($sql1, ['template' => $templateString]);
         // And the query for the above report.
         $query = "SELECT * FROM " . Reports::reportsTableName();
         $sql2 = "INSERT INTO `" . Reports::reportSourcesTableName() . "` SET " . " report = " . Reports::DEFAULT_REPORT_ID . "," . " name   = 'reports'," . " query  = :query;";
         $db->query($sql2, ['query' => $query]);
     }
     // Finish up.
     $changeTracker->closeChangeset();
 }
Пример #2
0
 public function tearDown()
 {
     // Close any still-open changeset.
     $changeTracker = new \Tabulate\DB\ChangeTracker($this->db);
     $changeTracker->closeChangeset();
     // Remove all tables.
     $this->db->query('SET FOREIGN_KEY_CHECKS = 0');
     foreach ($this->db->getTableNames(false) as $tbl) {
         $this->db->query("DROP TABLE IF EXISTS `{$tbl}`");
     }
     $this->db->query('SET FOREIGN_KEY_CHECKS = 1');
     parent::tearDown();
 }
Пример #3
0
 /**
  * Assume all data is now valid, and only FK values remain to be translated.
  *
  * @param DB\Table $table The table into which to import data.
  * @param array $column_map array of DB names to import names.
  * @return integer The number of rows imported.
  */
 public function importData($table, $column_map)
 {
     $changeTracker = new \Tabulate\DB\ChangeTracker($table->getDatabase());
     $changeTracker->openChangeset('CSV import.', true);
     $count = 0;
     $headers = $this->remap($column_map);
     for ($rowNum = 1; $rowNum <= $this->rowCount(); $rowNum++) {
         $row = array();
         foreach ($this->data[$rowNum] as $colNum => $value) {
             if (!isset($headers[$colNum])) {
                 continue;
             }
             $dbColumnName = $headers[$colNum];
             $column = $table->getColumn($dbColumnName);
             // Get actual foreign key value
             if ($column->isForeignKey()) {
                 if (empty($value)) {
                     // Ignore empty-string FKs.
                     continue;
                 } else {
                     $fk_rows = $this->getRecordsByTitle($column->getReferencedTable(), $value);
                     $foreign_row = array_shift($fk_rows);
                     $value = $foreign_row->getPrimaryKey();
                 }
             }
             // All other values are used as they are
             $row[$dbColumnName] = $value;
         }
         $pk_name = $table->getPkColumn()->getName();
         $pk_value = isset($row[$pk_name]) ? $row[$pk_name] : null;
         $table->saveRecord($row, $pk_value);
         $count++;
     }
     $changeTracker->closeChangeset();
     return $count;
 }