/** * Builds out the SiteTree hierarchy as specified in _fixtures.txt * * @see "silversmith help" * @param The parameters, e.g. from the command line */ public static function build_fixtures($params = array()) { ClassInfo::reset_db_cache(); $fixtures_file = isset($params['file']) ? $params['file'] : self::$project_dir . "/_fixtures.txt"; if (!file_exists($fixtures_file)) { fail("The file {$fixtures_file} doesn't exist."); } $code = file_get_contents($fixtures_file); $architectureData = array(); $lines = explode("\n", $code); if (empty($lines)) { fail("The files {$fixtures_file} is empty."); } $sample = Folder::find_or_make("silversmith-samples"); if (!$sample->hasChildren()) { $answer = ask("This project does not have sample assets installed, which can be useful for content seeding. Do you want to install them now? (y/n)"); if (strtolower(trim($answer)) == "y") { SilverSmith::add_sample_assets(); } } $answer = ask("This process will completely empty and repopulate your site tree. Are you sure you want to continue? (y/n)"); if (strtolower($answer) != "y") { die; } say("Parsing architecture file..."); foreach ($lines as $line) { if (empty($line)) { continue; } $level = 0; $count = 1; $class = "Page"; $title = $line; preg_match('/^[ ]+[^ ]/', $line, $matches); if ($matches) { $level = strlen(substr(reset($matches), 0, -1)); } if (stristr($line, ">")) { list($title, $class) = explode(" > ", $line); $class = SilverSmithUtil::proper_form($class); } preg_match('/\\*[0-9]+/', $title, $m); if ($m) { $match = reset($m); $count = (int) trim(str_replace("*", "", $match)); $title = str_replace($match, "", $title); } $architectureData[] = array('title' => trim($title), 'level' => $level, 'class' => trim($class), 'count' => $count, 'new' => !class_exists($class) || !in_array($class, ClassInfo::getValidSubclasses("SiteTree"))); } // Clean the slate say("Deleting current site tree"); DB::query("DELETE FROM SiteTree"); DB::query("DELETE FROM SiteTree_Live"); DB::query("DELETE FROM SiteTree_versions"); say("Done."); // Update the DB with any new page types $new = array(); say("Checking architecture file for new page types..."); foreach ($architectureData as $arr) { if ($arr['new']) { $new[] = $arr['class']; SilverSmithProject::get_configuration()->addNode($arr['class'], "PageTypes"); SilverSmithProject::get_node($arr['class'])->createFile(); say(success("Created " . $arr['class'])); } } if (!empty($new)) { state("Rebuilding database to support " . sizeof($new) . " new page types..."); $result = self::rebuild_database(); self::rebuild_manifest(); state("Done\n"); } $previousParentIDs = array('0' => '0'); $previousLevel = 0; $seeding = isset($params['seeding-level']) ? $params['seeding-level'] : 1; $total = 0; foreach ($architectureData as $arr) { $parentID = 0; $currentLevel = $arr['level']; $title = $arr['title']; $class = $arr['class']; $count = $arr['count']; $indent = ""; while (strlen($indent) < $currentLevel * 2) { $indent .= " "; } if ($currentLevel > 0) { $parentID = $previousParentIDs[$currentLevel - 2]; } for ($i = 0; $i < $count; $i++) { $p = new $class(); if (strtolower($title) == "_auto_") { $p->Title = SilverSmithUtil::get_lipsum_words(rand(2, 5)); } else { $p->Title = $title; } state($indent . $p->Title, "green", "bold"); state(" [{$class}] created..."); if ($seeding > 0) { state("Seeding..."); $p->Content = SilverSmithUtil::get_default_content($p->obj('Content'), $seeding); } $p->Status = "Published"; $p->ParentID = $parentID; $p->write(); if ($seeding > 1) { SilverSmithUtil::add_default_content($p, $seeding); } $p->write(); $p->publish("Stage", "Live"); state("Done.\n"); $total++; $previousParentIDs[$currentLevel] = $p->ID; } } $errorPages = DataList::create("ErrorPage"); if ($errorPages->count()) { state("Fixing error pages..."); $max = DB::query("SELECT MAX(Sort) FROM SiteTree")->value(); foreach ($errorPages as $e) { $max++; $e->Sort = $max; $e->write(); $e->publish("Stage", "Live"); } "Done\n"; } self::rebuild_manifest(); say(success("Success!")); state("Important", "red"); state(": You must "); state("restart your browser", null, null, "bold"); state(" to clear your session in order to view the architecture changes.\n"); }