GBPost::parsePathspec PHP Method

parsePathspec() static public method

Example: content/posts/2008-08/29-reading-a-book.html date: GBDateTime(2008-08-29T00:00:00Z) (resolution restricted by gb::$posts_cn_pattern) slug: "reading-a-book" fnext: "html"
static public parsePathspec ( $pathspec, &$date, &$slug, &$fnext )
    static function parsePathspec($pathspec, &$date, &$slug, &$fnext)
    {
        # cut away prefix "content/posts/"
        $name = substr($pathspec, 14);
        # split filename from filename extension
        $lastdot = strrpos($name, '.', strrpos($name, '/'));
        if ($lastdot !== false) {
            $fnext = substr($name, $lastdot + 1);
            $name = substr($name, 0, $lastdot);
        } else {
            $fnext = null;
        }
        # parse date and slug
        static $subchars = '._/';
        static $repchars = '---';
        static $ptimes = array('%Y-%m-%d' => 10, '%y-%m-%d' => 8, '%G-%m-%d' => 10, '%g-%m-%d' => 8, '%Y-%m' => 7, '%y-%m' => 5, '%G-%m' => 7, '%g-%m' => 5, '%Y' => 4, '%y' => 2, '%G' => 4, '%g' => 2);
        $nametest = strtr($name, $subchars, $repchars);
        # first, test gb::$posts_cn_pattern
        if (($st = strptime($nametest, strtr(gb::$posts_cn_pattern, $subchars, $repchars))) !== false) {
            $slug = ltrim($st['unparsed'], $subchars . '-');
        } else {
            # next, test common patterns with as many items as gb::$posts_cn_pattern
            $ptimes1 = array();
            $n = 0;
            $slug = false;
            if (preg_match_all('/%\\w/', gb::$posts_cn_pattern, $m)) {
                $n = count($m[0]);
            }
            if ($n) {
                if ($n == 1) {
                    $ptimes1 = array('%Y' => 4, '%y' => 2, '%G' => 4, '%g' => 2);
                } else {
                    if ($n == 2) {
                        $ptimes1 = array('%Y-%m' => 7, '%y-%m' => 5, '%G-%m' => 7, '%g-%m' => 5);
                    } else {
                        if ($n == 3) {
                            $ptimes1 = array('%Y-%m-%d' => 10, '%y-%m-%d' => 8, '%G-%m-%d' => 10, '%g-%m-%d' => 8);
                        }
                    }
                }
                foreach ($ptimes1 as $pattern => $pattern_len) {
                    if (($st = strptime($nametest, $pattern)) !== false) {
                        $slug = ltrim(substr($name, $pattern_len), $subchars . '-');
                        break;
                    }
                }
            }
            if ($slug === false) {
                # finally, try a series of common patterns
                foreach ($ptimes as $pattern => $pattern_len) {
                    if (($st = strptime($nametest, $pattern)) !== false) {
                        $slug = ltrim(substr($name, $pattern_len), $subchars . '-');
                        break;
                    }
                }
            }
        }
        # failed to parse
        if ($st === false) {
            throw new UnexpectedValueException('unable to parse date from ' . var_export($pathspec, 1));
        }
        $date = gmstrftime('%FT%T+00:00', gb_mkutctime($st));
        #$slug = ltrim($st['unparsed'], '-');
        $date = new GBDateTime($date . 'T00:00:00Z');
    }

Usage Example

Example #1
0
 /** Handle object */
 function onObject($name, $id)
 {
     if (substr($name, 0, 14) !== 'content/posts/') {
         return false;
     }
     GBPost::parsePathspec($name, $date, $slug, $fnext);
     # date missing means malformed pathname
     if ($date === false) {
         throw new UnexpectedValueException('malformed post "' . $name . '" missing date prefix -- skipping');
         return false;
     }
     # handle missing slug. content/posts/2009-01-22 => post
     if (!$slug) {
         $slug = 'post';
     }
     # comment or post?
     if ($fnext === 'comments') {
         $obj = $this->_onComment($name, $id, GBPost::mkCachename($date, $slug));
     } else {
         $obj = $this->_onObject(GBPost::findByDateAndSlug($date, $slug), 'GBPost', $name, $id, $slug);
         if (!$obj) {
             return false;
         }
         self::$posts[] = $obj;
     }
     if ($obj->published === null) {
         $obj->published = $date;
     }
     return true;
 }