GBPost::find PHP Method

find() static public method

static public find ( $uri_path_or_slug, $version = null, $strptime = null, $applyBodyFilters = true )
    static function find($uri_path_or_slug, $version = null, $strptime = null, $applyBodyFilters = true)
    {
        $version = self::parseVersion($version);
        $path = false;
        if (strpos($uri_path_or_slug, 'content/posts/') !== false) {
            $path = $uri_path_or_slug;
            if ($path[0] !== '/') {
                $path = gb::$site_dir . '/' . $path;
            }
        }
        if ($version === 'work') {
            # find path to raw content
            if ((!$path || !is_file($path)) && ($path = self::pathToWork($uri_path_or_slug, $strptime)) === null) {
                return false;
            }
            # parse pathspec, producing date and actual slug needed to look up cached
            try {
                self::parsePathspec(self::pathspecFromAbsPath($path), $date, $slug, $fnext);
            } catch (UnexpectedValueException $e) {
                return null;
            }
            # try to find a cached version
            $post = self::findByDateAndSlug($date, $slug);
            # load work
            return self::loadWork($path, $post, 'GBPost', $version, $slug, $applyBodyFilters);
        } elseif ($version === null) {
            if ($path) {
                self::parsePathspec(self::pathspecFromAbsPath($path), $date, $slug, $fnext);
                return self::findByDateAndSlug($date, $slug);
            }
            $path = self::pathToCached($uri_path_or_slug, $strptime);
            $data = @file_get_contents($path);
            if ($data === false && gb::$posts_fuzzy_lookup === true) {
                # exact match failed -- try fuzzy matching using glob and our knowledge of patterns
                list($prefix, $suffix) = self::cachenameFromURI($uri_path_or_slug, $strptime, true);
                $path = strtr($prefix, array('/' => '{/,*,.}', '-' => '{/,*,.}', '.' => '{/,*,.}')) . '*' . $suffix;
                $path = GBExposedContent::pathToCached('posts', $path . gb::$content_cache_fnext);
                # try any file with the cachename as prefix
                if ($path = gb::glob($path)) {
                    $data = @file_get_contents($path);
                    /*
                    Send premanent redirect if we found a valid article.
                     
                    Discussion: This is where things might go really wrong -- imagine we did find an 
                                article on fuzzy matching but it's _another_ article. Fail. But that
                                case is almost negligible since we only expand the time prefix, not
                                the slug.
                    */
                    global $gb_handle_request;
                    if ($data !== false && isset($gb_handle_request) && $gb_handle_request === true && ($post = unserialize($data)) && headers_sent() === false) {
                        header('HTTP/1.1 301 Moved Permanently');
                        header('Location: ' . $post->url());
                        exit('Moved to <a href="' . h($post->url()) . '">' . h($post->url()) . '</a>');
                    }
                }
            }
            return $data === false ? false : unserialize($data);
        }
        throw new Exception('arbitrary version retrieval not yet implemented');
    }

Usage Example

Example #1
0
    $q = array_fill_keys(array_keys($fields), null);
}
$post = false;
$body = '';
# default version
if ($q['version'] === null) {
    $q['version'] = 'work';
}
# load existing post
if ($q['name']) {
    if (!($post = GBPost::findByName($q['name'], $q['version']))) {
        gb::$errors[] = 'No post could be found at path ' . r($q['name']);
    }
} elseif ($q['uri']) {
    $q['uri'] = ltrim($q['uri'], '/');
    if (!($post = GBPost::find($q['uri'], $q['version']))) {
        gb::$errors[] = 'No post could be found for URI ' . r($q['uri']);
    }
}
# no post found or new post
if (!$post) {
    $post = new GBPost();
    $post->published = new GBDateTime();
    $post->author = gb::$authorized;
    $post->mimeType = $admin_conf->get('composing/default_mime_type', 'text/html');
}
include '../_header.php';
?>
<script type="text/javascript" charset="utf-8">//<![CDATA[
	var post = {
		savedState: {},
All Usage Examples Of GBPost::find