/** * Loads the member cache into the local cache variable if not done yet * * @return void */ public static function loadMembers() { if (self::$members_loaded) { return; } // set that we've loaded things self::$members_loaded = true; self::$path = Config::getConfigPath() . '/users/%s.yaml'; self::$members = unserialize(File::get(Path::tidy(BASE_PATH . "/_cache/_app/members/members.php"))); // no members found, that's ok, load a blank array if (!is_array(self::$members)) { self::$members = array(); } }
/** * Returns a given user's profile * * @param string $username Username's profile to return * @return array */ public static function getUserProfile($username) { if (!UserAuth::isUser($username)) { return null; } $content = substr(File::get(Config::getConfigPath() . "/users/" . $username . ".yaml"), 3); $divide = strpos($content, "\n---"); $front_matter = trim(substr($content, 0, $divide)); $content_raw = trim(substr($content, $divide + 4)); $profile = YAML::parse($front_matter); $profile['biography_raw'] = $content_raw; $profile['biography'] = Content::transform($content_raw); $profile['username'] = $username; return $profile; }
/** * Loads a Member's profile * * @param string $username username to load data for * @return array * @throws Exception */ private static function loadMemberData($username) { // pull profile data from filesystem $file = Config::getConfigPath() . '/users/' . $username . '.yaml'; $raw_profile = substr(File::get($file), 3); // no profile found, throw an exception if (!$raw_profile) { throw new Exception('Member `' . $username . '` not found.'); } // split out the profile into parts $divide = strpos($raw_profile, "\n---"); $front_matter = trim(substr($raw_profile, 0, $divide)); $content_raw = trim(substr($raw_profile, $divide + 4)); // create data array for populating into the Member object $data = YAML::parse($front_matter); $data['biography_raw'] = $content_raw; $data['biography'] = Content::transform($content_raw); $data['username'] = $username; // return the Member data return $data; }
/** * Loads the data file and merges all configs together * * @param string $dataset Dataset to attempt to load * @param array $parameters Parameters that were called on the plugin * @return array */ public function loadDataset($dataset, $parameters = array()) { // remove null values from parameters foreach ($parameters as $key => $value) { if (is_null($value)) { unset($parameters[$key]); } } // set some defaults if nothing is set $default_values = array('use_stemming' => false, 'use_alternates' => false, 'stop_words' => array('the', 'a', 'an'), 'query_cache_length' => 30, 'log_successful_searches' => true, 'log_failed_searches' => true, 'folders' => URL::getCurrent(), 'taxonomy' => false, 'show_hidden' => false, 'show_drafts' => false, 'show_future' => false, 'show_past' => true, 'type' => 'all', 'limit' => 10, 'offset' => 0, 'paginate' => true, 'query_variable' => 'query', 'include_content' => true, 'include_404' => false); // a complete list of all possible config variables $config = array('match_weights' => null, 'min_characters' => null, 'min_word_characters' => null, 'score_threshold' => null, 'property_weights' => null, 'query_mode' => null, 'use_stemming' => null, 'use_alternates' => null, 'include_full_query' => null, 'enable_too_many_results' => null, 'sort_by_score' => null, 'exclude_properties' => null, 'include_properties' => null, 'stop_words' => null, 'log_successful_searches' => null, 'log_failed_searches' => null, 'query_cache_length' => null, 'folders' => null, 'taxonomy' => null, 'show_hidden' => null, 'show_drafts' => null, 'since' => null, 'until' => null, 'show_future' => null, 'show_past' => null, 'type' => null, 'conditions' => null, 'limit' => null, 'page_limit' => null, 'offset' => null, 'paginate' => null, 'query_variable' => null, 'include_content' => null, 'include_404' => null, 'exclude' => null, 'query' => null); $loaded_config = array(); if ($dataset) { $dataset_file = $file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '/datasets/' . $dataset . '.yaml'; if (File::exists($dataset_file)) { $loaded_config = YAML::parseFile($dataset_file); } else { $this->log->error("Could not use dataset `" . $dataset . "`, YAML file does not exist."); } } // load global config $global_config = Helper::pick($this->getConfig(), array()); // merge config variables in order $all_config = array_merge($config, $default_values, $global_config, $loaded_config, $parameters); // get query if (!isset($parameters['query']) && is_null($all_config['query']) && $all_config['query_variable']) { $new_query = filter_input(INPUT_GET, $all_config['query_variable']); $all_config['query'] = $new_query; } // always add content to exclude properties, don't worry, content_raw will take care of it if (is_array($all_config['exclude_properties'])) { $all_config['exclude_properties'] = array_unique(array_merge($all_config['exclude_properties'], array('content'))); } else { $all_config['exclude_properties'] = array('content'); } return $all_config; }
/** * Load the config (yaml) files in a specified order: * * 1. Loose per-site configs * 2. Routes * 3. Settings * 4. Theme overrides */ public static function loadAllConfigs($admin = false) { $hash = Debug::markStart('config', 'finding'); /* |-------------------------------------------------------------------------- | YAML Mode |-------------------------------------------------------------------------- | | We need to know the YAML mode first (loose, strict, transitional), | so we parse the settings file once to check before doing anything else. | */ $preload_config = YAML::parse(Config::getConfigPath() . '/settings.yaml'); $yaml_mode = array_get($preload_config, '_yaml_mode', 'loose'); /* |-------------------------------------------------------------------------- | Default Settings |-------------------------------------------------------------------------- | | We keep a set of default options that the user config overrides, allowing | us to always have clean defaults. | */ $settings_to_parse = File::get(Config::getAppConfigPath() . '/default.settings.yaml'); /* |-------------------------------------------------------------------------- | User Site Settings |-------------------------------------------------------------------------- | | Next we parse and override the user's settings. | */ $settings_to_parse .= "\n\n" . File::get(Config::getConfigPath() . '/settings.yaml'); /* |-------------------------------------------------------------------------- | Routes and vanity URLs |-------------------------------------------------------------------------- | | Any URL can be manipulated by routes or vanity urls. We need this info | early on, before content parsing begins. | */ $settings_to_parse .= "\n\n_routes:\n " . trim(preg_replace("/\n/", "\n ", File::get(Config::getConfigPath() . '/routes.yaml'))); $settings_to_parse .= "\n\n_vanity_urls:\n " . trim(preg_replace("/\n/", "\n ", File::get(Config::getConfigPath() . '/vanity.yaml'))); /* |-------------------------------------------------------------------------- | Global Variables |-------------------------------------------------------------------------- | | We parse all the yaml files in the root (except settings and routes) of | the config folder and make them available as global template variables. | */ if (Folder::exists($config_files_location = Config::getConfigPath())) { $files = glob($config_files_location . '/*.yaml'); if ($files) { foreach ($files as $file) { if (strpos($file, 'routes.yaml') !== false || strpos($file, 'vanity.yaml') !== false || strpos($file, 'settings.yaml')) { continue; } $settings_to_parse .= "\n\n" . File::get($file); } } } /* |-------------------------------------------------------------------------- | Parse settings up until now |-------------------------------------------------------------------------- | | Parses the concatenated settings string we've made so far. | */ $config = YAML::parse($settings_to_parse, $yaml_mode); /* |-------------------------------------------------------------------------- | Theme Variables |-------------------------------------------------------------------------- | | Theme variables need to specifically parsed later so they can override | any site/global defaults. | */ $themes_path = array_get($config, '_themes_path', '_themes'); $theme_name = array_get($config, '_theme', 'acadia'); // reset $settings_to_parse = ''; if (Folder::exists($theme_files_location = Path::assemble(BASE_PATH, $themes_path, $theme_name))) { $theme_files = glob(Path::tidy($theme_files_location . '/*.yaml')); if ($theme_files) { foreach ($theme_files as $file) { $settings_to_parse .= "\n\n" . File::get($file); } } } Debug::markEnd($hash); // parse theme settings if any if ($settings_to_parse) { $config = YAML::parse($settings_to_parse, $yaml_mode) + $config; } /* |-------------------------------------------------------------------------- | Load Environment Configs and Variables |-------------------------------------------------------------------------- | | Environments settings explicitly overwrite any existing settings, and | therefore must be loaded late. We also set a few helper variables | to make working with environments even easier. | */ _Environment::establish($config); /* |-------------------------------------------------------------------------- | MIME Types |-------------------------------------------------------------------------- */ $config = array('_mimes' => require Config::getAppConfigPath() . '/mimes.php') + $config; /* |-------------------------------------------------------------------------- | Localization |-------------------------------------------------------------------------- | | We load up English by default. We're American after all. Doesn't the | world revolve around us? Hello? Bueller? More hamburgers please. | */ $config['_translations'] = array(); $config['_translations']['en'] = YAML::parse(Config::getAppConfigPath() . '/default.en.yaml'); if ($lang = array_get($config, '_language', false)) { if (File::exists(Config::getTranslation($lang))) { $translation = YAML::parse(Config::getTranslation($lang)); $config['_translations'][$lang] = Helper::arrayCombineRecursive($config['_translations']['en'], $translation); } } $finder = new Finder(); // clear previous Finder interator results try { $translation_files = $finder->files()->in(BASE_PATH . Config::getAddonsPath() . '/*/translations')->name($lang . '.*.yaml')->depth(0)->followLinks(); foreach ($translation_files as $file) { $translation = YAML::parse($file->getRealPath()); $config['_translations'][$lang] = Helper::arrayCombineRecursive($translation, $config['_translations'][$lang]); } } catch (Exception $e) { // meh. not important. } /* |-------------------------------------------------------------------------- | Set Slim Config |-------------------------------------------------------------------------- | | Slim needs to be initialized with a set of config options, so these | need to be set earlier than the set_default_tags() method. | */ // $config['view'] = new Statamic_View(); $config['cookies.lifetime'] = $config['_cookies.lifetime']; if ($admin) { $admin_theme = array_get($config, '_admin_theme', 'ascent'); if (!Folder::exists(BASE_PATH . Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme))) { $admin_theme = 'ascent'; } $theme_path = Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme . '/'); $config['theme_path'] = $theme_path; $config['templates.path'] = '.' . $theme_path; } else { $public_path = isset($config['_public_path']) ? $config['_public_path'] : ''; $config['theme_path'] = $themes_path . '/' . $config['_theme'] . '/'; $config['templates.path'] = Path::tidy($public_path . $themes_path . '/' . $config['_theme'] . '/'); } if (!array_get($config, '_display_debug_panel', false)) { Debug::disable(); } return $config; }
doStatamicVersionCheck($admin_app); $data = array(); if (!Statamic::are_users_writable()) { $url = $admin_app->urlFor('error') . "?code=users_not_writable"; $admin_app->redirect($url); } $name = Session::getFlash('member_is_new', filter_input(INPUT_GET, 'name', FILTER_SANITIZE_STRING)); $new = Session::getFlash('member_is_new', filter_input(INPUT_GET, 'new', FILTER_SANITIZE_NUMBER_INT)); $original_name = $name; if ($new) { $data['status_message'] = Localization::fetch('creating_member'); } else { $data = Member::getProfile($name, array('password')); $data['status_message'] = Localization::fetch('editing_member'); } $data['fields'] = YAML::parse(Config::getConfigPath() . '/bundles/member/fields.yaml'); $data['original_name'] = $original_name; $data['full_name'] = array_get($data, 'first_name', $name) . ' ' . array_get($data, 'last_name', ''); // overwrite 'biography' as it needs to be 'biography_raw' here if (isset($data['fields']['fields']['biography'])) { $data['fields']['fields']['biography_raw'] = $data['fields']['fields']['biography']; unset($data['fields']['fields']['biography']); } $template_list = array("member"); // check for flash data $errors = Session::getFlash('member_errors', array()); $old_values = Session::getFlash('member_old_values', array()); // merge $data = $old_values + $data + array('_errors' => $errors, 'new' => $new); Statamic_View::set_templates(array_reverse($template_list)); $admin_app->render(null, array('route' => 'members', 'app' => $admin_app) + $data);
/** * Retrieves the config file for this Add-on * * @return array */ public function getConfig() { // config caching if (empty(self::$config_caches[$this->addon_name])) { self::$config_caches[$this->addon_name] = array(); } if (!empty(self::$config_caches[$this->addon_name]['_core'])) { return self::$config_caches[$this->addon_name]['_core']; } $config = array(); $environment = strtolower(Environment::get()); $to_parse = ''; // load defaults if they exist if (File::exists($file = $this->getAddonLocation() . 'default.yaml')) { $to_parse .= File::get($file) . "\n\n"; } // load config if (File::exists($file = Config::getConfigPath() . '/bundles/' . $this->addon_name . '/' . $this->addon_name . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '/' . $this->addon_name . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } // load environment-specific config if it exists if ($environment) { if (File::exists($file = Config::getConfigPath() . '/bundles/' . $this->addon_name . '/' . $environment . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '/' . $environment . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } } // did we find something to parse? if ($to_parse) { $config = YAML::parse($to_parse); } // cache for later self::$config_caches[$this->addon_name]['_core'] = $config; return $config; }
/** * Prepares the member data for use in loops * * @param bool $parse_biography Parse content? This is a performance hit. * @param bool $override_flag Override `prepared` flag and re-loop? * @return void */ public function prepare($parse_biography = true, $override_flag = false) { if ($this->prepared && !$override_flag) { return; } $this->prepared = true; $count = $this->count(); $i = 1; // loop through the content adding contextual data foreach ($this->members as $username => $item) { $this->members[$username]['first'] = $i === 1; $this->members[$username]['last'] = $i === $count; $this->members[$username]['count'] = $i; $this->members[$username]['total_results'] = $count; $file = sprintf(Config::getConfigPath() . '/users/%s.yaml', $username); // parse full content if that's been requested if ($parse_biography && $file) { $raw_file = substr(File::get($file), 3); $divide = strpos($raw_file, "\n---"); $this->members[$username]['biography_raw'] = trim(substr($raw_file, $divide + 4)); $this->members[$username]['biography'] = Content::parse($this->members[$username]['biography_raw'], $item); } $i++; } }
/** * Load the config (yaml) files in a specified order: * * 1. Loose per-site configs * 2. Routes * 3. Settings * 4. Theme overrides */ public static function loadAllConfigs($admin = FALSE) { /* |-------------------------------------------------------------------------- | YAML Mode |-------------------------------------------------------------------------- | | We need to know the YAML mode first (loose, strict, transitional), | so we parse the settings file once to check before doing anything else. | */ $preload_config = YAML::parse(Config::getConfigPath() . '/settings.yaml'); $yaml_mode = array_get($preload_config, '_yaml_mode', 'loose'); /* |-------------------------------------------------------------------------- | Default Settings |-------------------------------------------------------------------------- | | We keep a set of default options that the user config overrides, allowing | us to always have clean defaults. | */ $default_config = YAML::parse(Config::getAppConfigPath() . '/default.settings.yaml'); /* |-------------------------------------------------------------------------- | User Site Settings |-------------------------------------------------------------------------- | | Next we parse and override the user's settings. | */ $user_config = YAML::parse(Config::getConfigPath() . '/settings.yaml', $yaml_mode); $config = array_merge($default_config, $user_config); /* |-------------------------------------------------------------------------- | Routes and vanity URLs |-------------------------------------------------------------------------- | | Any URL can be manipulated by routes or vanity urls. We need this info | early on, before content parsing begins. | */ $routes = array(); if (File::exists(Config::getConfigPath() . '/routes.yaml')) { $routes['_routes'] = YAML::parse('_config/routes.yaml', $yaml_mode); } // check for vanity URLs first, we may need to redirect $vanity = array(); if (File::exists(Config::getConfigPath() . '/vanity.yaml')) { $vanity['_vanity_urls'] = YAML::parse(Config::getConfigPath() . '/vanity.yaml', $yaml_mode); } $config = array_merge($config, $routes, $vanity); /* |-------------------------------------------------------------------------- | Global Variables |-------------------------------------------------------------------------- | | We parse all the yaml files in the root (except settings and routes) of | the config folder and make them available as global template variables. | */ if (Folder::exists($config_files_location = Config::getConfigPath())) { $finder = new Finder(); $files = $finder->files()->in($config_files_location)->name('*.yaml')->notName('routes.yaml')->notName('vanity.yaml')->notName('settings.yaml')->depth(0); if (iterator_count($files) > 0) { foreach ($files as $file) { $config = array_merge($config, YAML::parse($file->getRealPath(), $yaml_mode)); } } } /* |-------------------------------------------------------------------------- | Theme Variables |-------------------------------------------------------------------------- | | Theme variables need to specifically parsed later so they can override | any site/global defaults. | */ $themes_path = array_get($config, '_themes_path', '_themes'); $theme_name = array_get($config, '_theme', 'denali'); if (Folder::exists($theme_files_location = URL::assemble(BASE_PATH, $themes_path, $theme_name))) { $finder = new Finder(); // clear previous Finder interator results $theme_files = $finder->files()->in($theme_files_location)->name('*.yaml')->depth(0); if (iterator_count($theme_files) > 0) { foreach ($theme_files as $file) { $config = array_merge($config, YAML::parse($file->getRealPath(), $yaml_mode)); } } } /* |-------------------------------------------------------------------------- | MIME Types |-------------------------------------------------------------------------- */ $config = array_merge($config, array('_mimes' => require Config::getAppConfigPath() . '/mimes.php')); /* |-------------------------------------------------------------------------- | Localization |-------------------------------------------------------------------------- | | We load up English by default. We're American after all. Doesn't the | world revolve around us? Hello? Bueller? More hamburgers please. | */ $config['_translations'] = array(); $config['_translations']['en'] = YAML::parse(Config::getAppConfigPath() . '/default.en.yaml'); /* |-------------------------------------------------------------------------- | Set Slim Config |-------------------------------------------------------------------------- | | Slim needs to be initialized with a set of config options, so these | need to be set earlier than the set_default_tags() method. | */ // $config['view'] = new Statamic_View(); $config['cookies.lifetime'] = $config['_cookies.lifetime']; if ($admin) { $admin_theme = array_get($config, '_admin_theme', 'ascent'); if (!Folder::exists(Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme))) { $admin_theme = 'ascent'; } $theme_path = Path::tidy('/' . $config['_admin_path'] . '/' . 'themes/' . $admin_theme . '/'); $config['_admin_path'] = $config['_admin_path']; $config['theme_path'] = $theme_path; $config['templates.path'] = '.' . $theme_path; } else { $public_path = isset($config['_public_path']) ? $config['_public_path'] : ''; $config['theme_path'] = '_themes/' . $config['_theme'] . "/"; $config['templates.path'] = Path::tidy($public_path . '_themes/' . $config['_theme'] . "/"); } return $config; }
/** * Retrieves the config file for this Add-on * * @return mixed */ public function getConfig() { if (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '/' . $this->addon_name . '.yaml')) { return YAML::parseFile($file); } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '.yaml')) { return YAML::parseFile($file); } return null; }
/** * Retrieves the config file for this Add-on * * @return array */ public function getConfig() { $config = array(); $environment = strtolower(Environment::get()); $to_parse = ''; // load defaults if they exist if (File::exists($file = $this->getAddonLocation() . 'default.yaml')) { $to_parse .= File::get($file) . "\n\n"; } // load config if (File::exists($file = Config::getConfigPath() . '/bundles/' . $this->addon_name . '/' . $this->addon_name . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '/' . $this->addon_name . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } // load environment-specific config if it exists if ($environment) { if (File::exists($file = Config::getConfigPath() . '/bundles/' . $this->addon_name . '/' . $environment . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } elseif (File::exists($file = Config::getConfigPath() . '/add-ons/' . $this->addon_name . '/' . $environment . '.yaml')) { $to_parse .= File::get($file) . "\n\n"; } } // did we find something to parse? if ($to_parse) { $config = YAML::parse($to_parse); } return $config; }
/** * Updates the internal content cache * * @return boolean */ public static function update() { // start measuring $content_hash = Debug::markStart('caching', 'content'); // track if any files have changed $files_changed = false; $settings_changed = false; $members_changed = false; // grab length of content type extension $content_type = Config::getContentType(); $full_content_root = rtrim(Path::tidy(BASE_PATH . "/" . Config::getContentRoot()), "/"); $content_type_length = strlen($content_type) + 1; // the cache files we'll use $cache_file = BASE_PATH . '/_cache/_app/content/content.php'; $settings_file = BASE_PATH . '/_cache/_app/content/settings.php'; $structure_file = BASE_PATH . '/_cache/_app/content/structure.php'; $time_file = BASE_PATH . '/_cache/_app/content/last.php'; $members_file = BASE_PATH . '/_cache/_app/members/members.php'; $now = time(); // start measuring settings hash $settings_hash = Debug::markStart('caching', 'settings'); // check for current and new settings $settings = unserialize(File::get($settings_file)); if (!is_array($settings)) { $settings = array('site_root' => '', 'site_url' => '', 'timezone' => '', 'date_format' => '', 'time_format' => '', 'content_type' => '', 'taxonomy' => '', 'taxonomy_case_sensitive' => '', 'taxonomy_force_lowercase' => '', 'entry_timestamps' => '', 'base_path' => '', 'app_version' => ''); } // look up current settings $current_settings = array('site_root' => Config::getSiteRoot(), 'site_url' => Config::getSiteURL(), 'timezone' => Config::get('timezone'), 'date_format' => Config::get('date_format'), 'time_format' => Config::get('time_format'), 'content_type' => Config::get('content_type'), 'taxonomy' => Config::getTaxonomies(), 'taxonomy_case_sensitive' => Config::getTaxonomyCaseSensitive(), 'taxonomy_force_lowercase' => Config::getTaxonomyForceLowercase(), 'entry_timestamps' => Config::getEntryTimestamps(), 'base_path' => BASE_PATH, 'app_version' => STATAMIC_VERSION); // have cache-altering settings changed? if ($settings !== $current_settings) { // settings have changed $settings_changed = true; // clear the cache and set current settings $cache = self::getCleanCacheArray(); $settings = $current_settings; $last = null; } else { // grab the existing cache $cache = unserialize(File::get($cache_file)); if (!is_array($cache)) { $cache = self::getCleanCacheArray(); } $last = File::get($time_file); } // mark end of settings hash measuring Debug::markEnd($settings_hash); // grab a list of all content files $files = File::globRecursively(Path::tidy(BASE_PATH . '/' . Config::getContentRoot() . '/*'), Config::getContentType()); // grab a separate list of files that have changed since last check $updated = array(); $current_files = array(); // loop through files, getting local paths and checking for updated files foreach ($files as $file) { $local_file = Path::trimFilesystemFromContent(Path::standardize($file)); // add to current files $current_files[] = $local_file; // is this updated? if ($last && File::getLastModified($file) >= $last) { $updated[] = $local_file; } } // get a diff of files we know about and files currently existing $known_files = array(); foreach ($cache['urls'] as $url_data) { array_push($known_files, $url_data['path']); } $new_files = array_diff($current_files, $known_files); // create a master list of files that need updating $changed_files = array_unique(array_merge($new_files, $updated)); // store a list of changed URLs $changed_urls = array(); // add to the cache if files have been updated if (count($changed_files)) { $files_changed = true; // build content cache foreach ($changed_files as $file) { $file = $full_content_root . $file; $local_path = Path::trimFilesystemFromContent($file); // before cleaning anything, check for hidden or draft content $is_hidden = Path::isHidden($local_path); $is_draft = Path::isDraft($local_path); // now clean up the path $local_filename = Path::clean($local_path); // file parsing $content = substr(File::get($file), 3); $divide = strpos($content, "\n---"); $front_matter = trim(substr($content, 0, $divide)); $content_raw = trim(substr($content, $divide + 4)); // parse data $data = YAML::parse($front_matter); if ($content_raw) { $data['content'] = 'true'; $data['content_raw'] = 'true'; } // set additional information $data['_file'] = $file; $data['_local_path'] = $local_path; $data['_order_key'] = null; $data['datetimestamp'] = null; // legacy $data['datestamp'] = null; $data['date'] = null; $data['time'] = null; $data['numeric'] = null; $data['last_modified'] = filemtime($file); $data['_is_hidden'] = $is_hidden; $data['_is_draft'] = $is_draft; // get initial slug (may be changed below) $data['slug'] = ltrim(basename($file, "." . $content_type), "_"); // folder $instance = $data['slug'] == 'page' ? 1 : 0; $data['_folder'] = Path::clean($data['_local_path']); $slash = Helper::strrpos_count($data['_folder'], '/', $instance); $data['_folder'] = !$slash ? '' : substr($data['_folder'], 1, $slash - 1); $data['_folder'] = !strlen($data['_folder']) ? "/" : $data['_folder']; $data['_basename'] = $data['slug'] . '.' . $content_type; $data['_filename'] = $data['slug']; $data['_is_entry'] = preg_match(Pattern::ENTRY_FILEPATH, $data['_basename']); $data['_is_page'] = preg_match(Pattern::PAGE_FILEPATH, $data['_basename']); // 404 is special if ($data['_local_path'] === "/404.{$content_type}") { $local_filename = $local_path; // order key: date or datetime } elseif (preg_match(Pattern::DATE_OR_DATETIME, $data['_basename'], $matches)) { // order key: date or datetime $date = $matches[1] . '-' . $matches[2] . '-' . $matches[3]; $time = null; if (Config::getEntryTimestamps() && isset($matches[4])) { $time = substr($matches[4], 0, 2) . ":" . substr($matches[4], 2); $date = $date . " " . $time; $data['slug'] = substr($data['slug'], 16); $data['datetimestamp'] = $data['_order_key']; } else { $data['slug'] = substr($data['slug'], 11); } $data['_order_key'] = strtotime($date); $data['datestamp'] = $data['_order_key']; $data['date'] = Date::format(Config::getDateFormat(), $data['_order_key']); $data['time'] = $time ? Date::format(Config::getTimeFormat(), $data['_order_key']) : null; // order key: slug is page, back up a level } elseif ($data['slug'] == 'page' && preg_match(Pattern::NUMERIC, substr($data['_local_path'], Helper::strrpos_count($data['_local_path'], '/', 1)), $matches)) { // order key: slug is page, back up a level $data['_order_key'] = $matches[1]; $data['numeric'] = $data['_order_key']; // order key: numeric } elseif (preg_match(Pattern::NUMERIC, $data['_basename'], $matches)) { // order key: numeric $data['_order_key'] = $matches[1]; $data['numeric'] = $data['_order_key']; $data['slug'] = substr($data['slug'], strlen($matches[1]) + 1); // order key: other } else { // order key: other $data['_order_key'] = $data['_basename']; } // determine url $data['url'] = preg_replace('#/__?#', '/', $local_filename); // remove any content type extensions from the end of filename if (substr($data['url'], -$content_type_length) === '.' . $content_type) { $data['url'] = substr($data['url'], 0, strlen($data['url']) - $content_type_length); } // remove any base pages from filename if (substr($data['url'], -5) == '/page') { $data['url'] = substr($data['url'], 0, strlen($data['url']) - 5); } // add the site root $data['url'] = Path::tidy(Config::getSiteRoot() . $data['url']); // add the site URL to get the permalink $data['permalink'] = Path::tidy(Config::getSiteURL() . $data['url']); // new content if (!isset($cache['content'][$data['_folder']]) || !is_array($cache['content'][$data['_folder']])) { $cache['content'][$data['_folder']] = array(); } $slug_with_extension = $data['_filename'] == 'page' ? substr($data['url'], strrpos($data['url'], '/') + 1) . '/' . $data['_filename'] . "." . $content_type : $data['_filename'] . "." . $content_type; $cache['content'][$data['_folder']][$slug_with_extension] = array('folder' => $data['_folder'], 'path' => $local_path, 'file' => $slug_with_extension, 'url' => $data['url'], 'data' => $data); $cache['urls'][$data['url']] = array('folder' => $data['_folder'], 'path' => $local_path, 'file' => $slug_with_extension); $changed_urls[$data['url']] = true; } } // loop through all cached content for deleted files // this isn't as expensive as you'd think in real-world situations foreach ($cache['content'] as $folder => $folder_contents) { foreach ($folder_contents as $path => $data) { if (File::exists($full_content_root . $data['path'])) { // still here, keep it continue; } $files_changed = true; // get URL $url = isset($cache['content'][$folder][$path]['url']) ? $cache['content'][$folder][$path]['url'] : null; // only remove from URLs list if not in changed URLs list if (!isset($changed_urls[$url]) && !is_null($url)) { // remove from url cache unset($cache['urls'][$url]); } // remove from content cache unset($cache['content'][$folder][$path]); } } // build taxonomy cache // only happens if files were added, updated, or deleted above if ($files_changed) { $taxonomies = Config::getTaxonomies(); $force_lowercase = Config::getTaxonomyForceLowercase(); $case_sensitive = Config::getTaxonomyCaseSensitive(); $cache['taxonomies'] = array(); // rebuild taxonomies if (count($taxonomies)) { // set up taxonomy array foreach ($taxonomies as $taxonomy) { $cache['taxonomies'][$taxonomy] = array(); } // loop through content to build cached array foreach ($cache['content'] as $pages) { foreach ($pages as $item) { $data = $item['data']; // loop through the types of taxonomies foreach ($taxonomies as $taxonomy) { // if this file contains this type of taxonomy if (isset($data[$taxonomy])) { $values = Helper::ensureArray($data[$taxonomy]); // add the file name to the list of found files for a given taxonomy value foreach ($values as $value) { if (!$value) { continue; } $key = !$case_sensitive ? strtolower($value) : $value; if (!isset($cache['taxonomies'][$taxonomy][$key])) { $cache['taxonomies'][$taxonomy][$key] = array('name' => $force_lowercase ? strtolower($value) : $value, 'files' => array()); } array_push($cache['taxonomies'][$taxonomy][$key]['files'], $data['url']); } } } } } } // build structure cache $structure = array(); $home = Path::tidy('/' . Config::getSiteRoot() . '/'); foreach ($cache['content'] as $pages) { foreach ($pages as $item) { // set up base variables $parent = null; // Trim off home and any /page.md ending so that all URLs are treated // equally regardless of page type. $order_key = str_replace('/page.md', '', str_replace($home, '', $item['path'])); $sub_order_key = $item['data']['_order_key']; // does this have a parent (and if so, what is it?) if ($item['url'] !== $home) { $parent = $home; $depth = substr_count(str_replace($home, '/', $item['url']), '/'); $last_slash = strrpos($item['url'], '/', 1); $last_order_slash = strrpos($order_key, '/', 0); if ($last_slash !== false) { $parent = substr($item['url'], 0, $last_slash); } if ($last_order_slash !== false) { $order_key = substr($order_key, 0, $last_order_slash); } if ($item['data']['_is_page']) { $type = $item['data']['slug'] == 'page' ? 'folder' : 'page'; } else { $type = 'entry'; } } else { $depth = 0; $type = 'folder'; $order_key = $home; } $structure[$item['url']] = array('parent' => $parent, 'is_entry' => $item['data']['_is_entry'], 'is_page' => $item['data']['_is_page'], 'is_hidden' => $item['data']['_is_hidden'], 'is_draft' => $item['data']['_is_draft'], 'depth' => $depth, 'order_key' => $order_key ? $order_key : $sub_order_key, 'sub_order_key' => $sub_order_key, 'type' => $type); } } } // mark ending of content cache measuring Debug::markEnd($content_hash); if (!Config::get('disable_member_cache')) { // build member cache // ---------------------------------------------------------------- // start measuring $member_hash = Debug::markStart('caching', 'member'); // grab a list of existing members $users = File::globRecursively(Path::tidy(Config::getConfigPath() . '/users/*'), 'yaml'); // clone for reuse, set up our list of updated users $updated = array(); $current_users = array(); foreach ($users as $user) { $local_file = Path::trimFilesystemFromContent(Path::standardize($user)); // add to current users $current_users[] = $local_file; // is this updated? if ($last && File::getLastModified($user) >= $last) { $updated[] = $local_file; } } // get users from the file $members = unserialize(File::get($members_file)); // get a diff of users we know about and files currently existing $known_users = array(); if (!empty($members)) { foreach ($members as $username => $member_data) { $known_users[$username] = $member_data['_path']; } } // create a master list of users that need updating $changed_users = array_unique(array_merge(array_diff($current_users, $known_users), $updated)); $removed_users = array_diff($known_users, $current_users); if (count($changed_users)) { $members_changed = true; foreach ($changed_users as $user_file) { // file parsing $last_slash = strrpos($user_file, '/') + 1; $last_dot = strrpos($user_file, '.'); $username = substr($user_file, $last_slash, $last_dot - $last_slash); $content = substr(File::get($user_file), 3); $divide = strpos($content, "\n---"); $data = YAML::parse(trim(substr($content, 0, $divide))); $bio_raw = trim(substr($content, $divide + 4)); $data['_path'] = $user_file; if ($bio_raw) { $data['biography'] = 'true'; $data['biography_raw'] = 'true'; } $members[$username] = $data; } } // loop through all cached content for deleted files // this isn't as expensive as you'd think in real-world situations if (!empty($removed_users)) { $members_changed = true; $members = array_diff_key($members, $removed_users); } // mark ending of member cache measuring Debug::markEnd($member_hash); } // write to caches // -------------------------------------------------------------------- // add file-writing to content-cache actions $content_hash = Debug::markStart('caching', 'content'); if ($files_changed) { // store the content cache if (File::put($cache_file, serialize($cache)) === false) { if (!File::isWritable($cache_file)) { Log::fatal('Cache folder is not writable.', 'core', 'content-cache'); } Log::fatal('Could not write to the cache.', 'core', 'content-cache'); return false; } // store the structure cache if (File::put($structure_file, serialize($structure)) === false) { if (!File::isWritable($structure_file)) { Log::fatal('Structure cache file is not writable.', 'core', 'structure-cache'); } Log::fatal('Could not write to the structure cache.', 'core', 'structure-cache'); return false; } } // mark ending of content cache file write measuring Debug::markEnd($content_hash); // add file-writing to settings-cache actions $settings_hash = Debug::markStart('caching', 'settings'); // store the settings cache if ($settings_changed) { if (File::put($settings_file, serialize($settings)) === false) { if (!File::isWritable($settings_file)) { Log::fatal('Settings cache file is not writable.', 'core', 'settings-cache'); } Log::fatal('Could not write to the settings cache file.', 'core', 'settings-cache'); return false; } } // mark ending of settings cache file write measuring Debug::markEnd($settings_hash); if (!Config::get('disable_member_cache')) { // add file-writing to settings-cache actions $member_hash = Debug::markStart('caching', 'member'); // store the members cache if ($members_changed) { if (File::put($members_file, serialize($members)) === false) { if (!File::isWritable($members_file)) { Log::fatal('Member cache file is not writable.', 'core', 'member-cache'); } Log::fatal('Could not write to the member cache file.', 'core', 'member-cache'); return false; } } // mark ending of member cache file write measuring Debug::markEnd($member_hash); } File::put($time_file, $now - 1); return true; }
/** * Get Globals & Theme Variables * * Create global variables from v1 globals and theme variables * * @return array */ private function createGlobals() { $globals = array('settings' => array(), 'global' => array(), 'theme' => array()); // Get a list of variables added to _config/settings.yaml // Anything not also in the defaults will be considered a global added manually. $defaults = array_keys(YAML::parseFile(Config::getAppConfigPath() . '/default.settings.yaml')); $settings = array_keys(YAML::parseFile(Config::getConfigPath() . '/settings.yaml')); $settings_globals = array_diff($settings, $defaults); foreach ($settings_globals as $setting) { $setting = ltrim($setting, '_'); $globals['settings'][$setting] = Config::get($setting); } // Get a list of variables in _config/global.yaml $site_globals = Config::getConfigPath() . '/global.yaml'; if (File::exists($site_globals)) { $globals['global'] = YAML::parse($site_globals); } // Get a list of variables in the theme.yaml $theme_globals = Config::getCurrentThemePath() . 'theme.yaml'; if (File::exists($theme_globals)) { $globals['theme'] = YAML::parse($theme_globals); } $this->migration['globals'] = $globals; }