public function render() { // Generate a hash unique to this field's config and data $hash = Helper::makeHash($this->field_config, $this->field_data); // If we've already saved the output, grab it from blink's cache // and avoid further processing. if ($this->blink->exists($hash)) { $html = $this->blink->get($hash); return $this->renderFieldReplacements($html); } // Let's make sure they set an upload destination if (array_get($this->field_config, 'destination', false) === false) { throw new Exception("You need to set a destination for your File field."); } // Normalize the destination $this->destination = trim(array_get($this->field_config, 'destination'), '/') . '/'; // Allow a string or an array, but we want an array $has_data = $this->field_data != ''; $this->field_data = Helper::ensureArray($this->field_data); // Clean up {{ _site_root }} and lack of leading slash existence foreach ($this->field_data as $i => $file) { $this->field_data[$i] = URL::tidy('/' . str_replace('{{ _site_root }}', '', $file)); } // Whether or not to allow the browse existing files functionality $allow_browse = array_get($this->field_config, 'browse', true); // Resizing config if ($resize = array_get($this->field_config, 'resize')) { $resize['resize'] = true; $resize = http_build_query($resize); } // If we're in a subdirectory, prepend it to all the filenames if (($site_root = Config::getSiteRoot()) != '/') { foreach ($this->field_data as $i => $file) { $this->field_data[$i] = URL::assemble($site_root, $file); } } // Send data to the view $vars = array('field_id' => $this->field_id, 'field_name' => $this->fieldname, 'tabindex' => $this->tabindex, 'has_data' => $has_data, 'field_data' => $this->field_data, 'field_config' => $this->field_config, 'destination' => $this->destination, 'allow_browse' => $allow_browse, 'browse_url' => URL::assemble(Config::getSiteRoot(), Config::get('admin_path') . '.php/files?config=' . rawurlencode(Helper::encrypt(serialize($this->field_config)))), 'file_thumb' => $this->tasks->defaultFileThumbnail(), 'resize' => $resize); // Get the view template from the file $template = File::get($this->getAddonLocation() . 'views/fieldtype.html'); // Parse it $html = Parse::template($template, $vars); // Save it to cache for other similar fields $this->blink->set($hash, $html); // Output! return $this->renderFieldReplacements($html); }
public function index() { // if disabled, do nothing if (!$this->core->isEnabled()) { return Parse::contextualTemplate($this->content, array(), $this->context); } // grab and prepare parameters $key = $this->fetchParam('key', null, null, false, false); $age = time() - Date::resolve('-' . $this->fetch(array('for', 'default_cache_length'), '12 hours', null, false, false)); $hash = $this->fetch(array('scope', 'default_scope'), 'site') === 'page' ? Helper::makeHash(URL::getCurrent(), $this->content) : Helper::makeHash($this->content); $path = 'troves/' . $hash; // deal with keys if ($key) { // split on pipes $keys = explode('|', $key); // loop through keys, storing this hash to this key foreach ($keys as $key) { $key_path = 'keys/' . trim($key); // get existing keys $hashes = $this->cache->getYAML($key_path, array()); $new_hashes = array(); // check that the hashes are all still valid foreach ($hashes as $new_hash) { if ($this->cache->exists('troves/' . $new_hash)) { $new_hashes[] = $new_hash; } } // add this one $new_hashes[] = $hash; // append new ones $this->cache->putYAML($key_path, array_unique($new_hashes)); } } // check for pre-existing cache if (!$this->cache->exists($path) || $this->cache->getAge($path) > $age) { // cache doesn't exist or has expired, so parse this contextually... $html = Parse::contextualTemplate($this->content, array(), $this->context); // ...and store the HTML $this->cache->put($path, $html); } // garbage collection $this->core->collectGarbage(); // return what we know return $this->cache->get($path); }
/** * Returns a ContentSet object with the appropriate content * * @param array $settings Settings for filtering content and such * @return ContentSet */ private function getContentSet($settings) { // create a unique hash for these settings $content_hash = Helper::makeHash($settings); if ($this->blink->exists($content_hash)) { // blink content exists, use that $content_set = new ContentSet($this->blink->get($content_hash)); } else { // no blink content exists, get data the hard way if ($settings['taxonomy']) { $taxonomy_parts = Taxonomy::getCriteria(URL::getCurrent()); $taxonomy_type = $taxonomy_parts[0]; $taxonomy_slug = Config::get('_taxonomy_slugify') ? Slug::humanize($taxonomy_parts[1]) : urldecode($taxonomy_parts[1]); $content_set = ContentService::getContentByTaxonomyValue($taxonomy_type, $taxonomy_slug, $settings['folders']); } else { $content_set = ContentService::getContentByFolders($settings['folders']); } // filter $content_set->filter($settings); // grab total entries for setting later $total_entries = $content_set->count(); // pre-sort supplement $content_set->supplement(array('total_found' => $total_entries) + $settings); // sort $content_set->multisort($settings['sort']); // post-sort supplement $content_set->supplement(array( 'group_by_date' => trim($this->fetchParam("group_by_date", null, null, false, false)) ), true); // store content as blink content for future use $this->blink->set($content_hash, $content_set->extract()); } return $content_set; }
public function count() { // grab parameters $from = $this->fetchParam('from', URL::getCurrent()); $exclude = $this->fetchParam('exclude', false); $max_depth = $this->fetchParam('max_depth', 1, 'is_numeric'); $include_entries = $this->fetchParam('include_entries', false, false, true); $folders_only = $this->fetchParam('folders_only', true, false, true); $include_content = $this->fetchParam('include_content', false, false, true); $show_hidden = $this->fetchParam('show_hidden', false, null, true); // add in left-/ if not present if (substr($from, 0, 1) !== '/') { $from = '/' . $from; } // if this doesn't start with the site root, add the site root if (!Pattern::startsWith($from, Config::getSiteRoot())) { $from = Path::tidy(Config::getSiteRoot() . '/' . $from); } // standardize excludes if ($exclude && !is_array($exclude)) { $exclude = Helper::explodeOptions($exclude, array()); foreach ($exclude as $key => $value) { $exclude[$key] = Path::tidy(Config::getSiteRoot() . '/' . $value); } } // option hash $hash = Helper::makeHash($from, $exclude, $max_depth, $include_entries, $folders_only, $include_content, $show_hidden); // load the content tree from cache if ($this->blink->exists($hash)) { $tree = $this->blink->get($hash); } else { $tree = ContentService::getContentTree($from, $max_depth, $folders_only, $include_entries, $show_hidden, $include_content, $exclude); $this->blink->set($hash, $tree); } if ($this->content) { return Parse::template($this->content, array('count' => count($tree))); } elseif (count($tree)) { return count($tree); } return ''; }
/** * Returns a MemberSet object with the appropriate content * * @param array $settings Settings for filtering members and such * @return MemberSet */ private function getMemberSet($settings) { // create a unique hash for these settings $set_hash = Helper::makeHash($settings); if ($this->blink->exists($set_hash)) { // blink content exists, use that $member_set = new MemberSet($this->blink->get($set_hash)); } else { $member_set = MemberService::getMembers(); // filter $member_set->filter($settings); // sort $member_set->sort($settings['sort_by'], $settings['sort_dir']); // store content as blink content for future use $this->blink->set($set_hash, $member_set->extract()); } return $member_set; }
/** * Fetch a single content entry or page * * @param string $url URL to fetch * @param bool $parse_content Should we parse content? * @param bool $supplement Should we supplement the content? * @return array */ public static function get($url, $parse_content = true, $supplement = true) { $hash = Debug::markStart('content', 'getting'); $url_hash = Helper::makeHash($url, $parse_content, $supplement); if (!isset(self::$fetched_content[$url_hash])) { $content_set = ContentService::getContentByURL($url); $content = $content_set->get($parse_content, $supplement); self::$fetched_content[$url_hash] = isset($content[0]) ? $content[0] : array(); } Debug::markEnd($hash); return self::$fetched_content[$url_hash]; }
/** * Gets a list of all revisions for the given $file * * @param string $file The file to retrieve revisions for * @return array|mixed */ public function getRevisions($file) { $hash = Helper::makeHash($file); $revisions = array(); $stored_revisions = array_reverse($this->storage->listAll($hash)); $current = $this->addon->api('revisions')->getCurrentRevision(); $i = 0; foreach ($stored_revisions as $revision) { $storage_filename = $hash . '/' . $revision; $data = $this->storage->getYAML($storage_filename); $this_revision = str_pad($data['revision'], 4, '0', STR_PAD_LEFT); $revisions[] = array('revision' => $this_revision, 'message' => $data['message'], 'timestamp' => $this->getRevisionTimestamp($file, $this_revision), 'author' => $this->getRevisionAuthor($file, $this_revision), 'is_current' => $current === $revision || is_null($current) && $i === 0); $i++; } return $revisions; }
/** * Returns a ContentSet object with the appropriate content * * @param array $settings Settings for filtering content and such * @return ContentSet */ private function getContentSet($settings) { // create a unique hash for these settings $content_hash = Helper::makeHash($settings); if ($this->blink->exists($content_hash)) { // blink content exists, use that $content_set = new ContentSet($this->blink->get($content_hash)); } else { // no blink content exists, get data the hard way if ($settings['taxonomy']) { $taxonomy_parts = Taxonomy::getCriteria(URL::getCurrent()); $taxonomy_type = $taxonomy_parts[0]; $taxonomy_slug = Config::get('_taxonomy_slugify') ? Slug::humanize($taxonomy_parts[1]) : urldecode($taxonomy_parts[1]); $content_set = ContentService::getContentByTaxonomyValue($taxonomy_type, $taxonomy_slug, $settings['folders']); } else { $content_set = ContentService::getContentByFolders($settings['folders']); } // filter $content_set->filter($settings); // sort $content_set->sort($settings['sort_by'], $settings['sort_dir']); // store content as blink content for future use $this->blink->set($content_hash, $content_set->extract()); } return $content_set; }
/** * Returns a ContentSet object with the appropriate content * * @param array $settings Settings for filtering content and such * @return ContentSet */ private function getContentSet($settings) { // create a unique hash for these settings $content_hash = Helper::makeHash($settings); if ($this->blink->exists($content_hash)) { // blink content exists, use that $content_set = new ContentSet($this->blink->get($content_hash)); } else { // no blink content exists, get data the hard way if ($settings['taxonomy']) { $taxonomy_parts = Taxonomy::getCriteria(URL::getCurrent()); $taxonomy_type = $taxonomy_parts[0]; $taxonomy_slug = Config::get('_taxonomy_slugify') ? Slug::humanize($taxonomy_parts[1]) : urldecode($taxonomy_parts[1]); $content_set = ContentService::getContentByTaxonomyValue($taxonomy_type, $taxonomy_slug, $settings['folders']); } else { $content_set = ContentService::getContentByFolders($settings['folders']); } // filter $content_set->filter($settings); // supplement $content_set->supplement(array( 'locate_with' => $settings['locate_with'], 'center_point' => $settings['center_point'], 'pop_up_template' => $this->content )); // re-filter, we only want entries that have been found $content_set->filter(array('located' => true)); // sort $content_set->sort($settings['sort_by'], $settings['sort_dir']); // store content as blink content for future use $this->blink->set($content_hash, $content_set->extract()); } return $content_set; }
/** * Invalidated the cache * * @param string $url An optional URL to invalidate the HTML cache for * @return void */ public function invalidateCache($url = null) { // url-specific if (!is_null($url) && $this->isPageCached($url)) { $this->cache->delete(Helper::makeHash($url)); return; } // the whole thing $this->cache->destroy(); }