/** * Determines if the class that this node represents has any parents that are managed by SilverSmith * * @return bool */ public function getHasSilverSmithedParents() { if ($node = SilverSmithProject::get_node($this->getParent())) { return $node->isSilverSmithed(); } return false; }
/** * Adds default content to a page or DataObject * * @todo Migrate this to a decorator. DataObjects should know how to seed themeselves. * @param DataObject The object to modify * @param integer The level to which to seed content (see "silversmith help") * @param array Limit the seeding to certain fields */ public static function add_default_content(&$object, $level, $onlyFields = array()) { if ($level < 2) { return; } $fields = array(); if (!empty($onlyFields)) { foreach ($onlyFields as $f) { $fields[] = trim($f); } } $site_tree = singleton('SiteTree'); $data_object = singleton('DataObject'); $is_sitetree = $object->class == "SiteTree" || is_subclass_of($object, "SiteTree"); foreach ($object->db() as $field => $type) { if ($data_object->db($field) || $is_sitetree && $site_tree->db($field) || !empty($fields) && !in_array($field, $fields)) { continue; } if (!$object->{$field}) { if (strstr($field, "Email")) { $object->{$field} = preg_replace('/[^a-z@\\.]/', '', strtolower(self::get_lipsum_words(1) . "@" . self::get_lipsum_words(1) . ".com")); } elseif (strstr($field, "Phone")) { $object->{$field} = rand(100, 999) . "-" . rand(100, 999) . "-" . rand(100, 999); } else { $object->{$field} = self::get_default_content($object->obj($field)); } } } foreach ($object->has_one() as $relation => $class) { if ($data_object->has_one($relation) || $site_tree->has_one($relation) || !empty($fields) && !in_array($relation, $fields)) { continue; } $filter = $class == "File" ? "ClassName = 'File'" : null; $o = DataList::create($class)->where($filter)->sort("RAND()")->first(); if ($o) { $key = $relation . "ID"; $object->{$key} = $o->ID; } } if ($level > 2) { foreach ($object->has_many() as $relation => $class) { if ($data_object->has_many($relation) || $site_tree->has_many($relation) || !SilverSmithProject::get_node($class) || !empty($fields) && !in_array($relation, $fields)) { continue; } if ($name = array_search($object->class, singleton($class)->has_one())) { $key = $name . "ID"; $count = rand(1, 5); for ($i = 0; $i <= $count; $i++) { if ($candidate = DataList::create($class)->where("{$key} = 0 OR {$key} IS NULL")->first()) { $candidate->{$key} = $object->ID; $candidate->write(); } elseif (!is_subclass_of($class, "SiteTree") && !is_subclass_of($class, "File")) { $related = new $class(); $related->{$key} = $object->ID; $related->write(); self::add_default_content($related, $level); $related->write(); } else { // We don't create a site tree object for the has many, because it will mess up the hierarchy. } } } } foreach ((array) $object->stat('many_many') as $relation => $class) { if ($class == $object->class || is_subclass_of($class, $object->class)) { continue; } if ($data_object->many_many($relation) || $site_tree->many_many($relation) || !SilverSmithProject::get_node($class) || !empty($fields) && !in_array($relation, $fields)) { continue; } $table = $object->class . "_" . $relation; $parentKey = $object->class . "ID"; $childKey = $class . "ID"; $set = DataList::create($class)->sort("RAND()")->limit(5); if (!$set) { $set = new DataList(); } // never create sitetree or file objects. if (!is_subclass_of($class, "SiteTree") && !is_subclass_of($class, "File") && $class != $object->class) { $count = $set->Count(); while ($count < 5) { $related = new $class(); $related->write(); self::add_default_content($related, $level); $related->write(); $count++; } } $set = DataList::create($class)->sort("RAND()")->limit(rand(1, 5)); if ($set) { $object->{$relation}()->setByIDList($set->column('ID')); } } } }
/** * 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"); }
/** * Gets the entire node out of the SilverSmith project definition file * * @return BedrockNode */ public function getDefinition() { return SilverSmithProject::get_node($this->key); }