mikuli.cz

:)
git clone https://git.sr.ht/~ashymad/mikuli.cz
Log | Files | Refs

blog.php (12255B)


      1 <?php
      2 // Blog extension, https://github.com/annaesvensson/yellow-blog
      3 
      4 class YellowBlog {
      5     const VERSION = "0.9.4";
      6     public $yellow;         // access to API
      7     
      8     // Handle initialisation
      9     public function onLoad($yellow) {
     10         $this->yellow = $yellow;
     11         $this->yellow->system->setDefault("blogStartLocation", "auto");
     12         $this->yellow->system->setDefault("blogNewLocation", "@title");
     13         $this->yellow->system->setDefault("blogFilePrefix", "1");
     14         $this->yellow->system->setDefault("blogShortcutEntries", "0");
     15         $this->yellow->system->setDefault("blogPaginationLimit", "5");
     16     }
     17     
     18     // Handle page meta data
     19     public function onParseMetaData($page) {
     20         if ($page->get("layout")=="blog") {
     21             $page->set("editNewLocation", $this->yellow->system->get("blogNewLocation"));
     22             if ($this->yellow->system->get("blogFilePrefix")) $page->set("editNewPrefix", $page->get("published"));
     23         }
     24     }
     25     
     26     // Handle page content element
     27     public function onParseContentElement($page, $name, $text, $attributes, $type) {
     28         $output = null;
     29         if (substru($name, 0, 4)=="blog" && ($type=="block" || $type=="inline")) {
     30             switch($name) {
     31                 case "blogauthors": $output = $this->getShortcutBlogauthors($page, $name, $text); break;
     32                 case "blogtags":    $output = $this->getShortcutBlogtags($page, $name, $text); break;
     33                 case "blogyears":   $output = $this->getShortcutBlogyears($page, $name, $text); break;
     34                 case "blogmonths":  $output = $this->getShortcutBlogmonths($page, $name, $text); break;
     35                 case "blogpages":   $output = $this->getShortcutBlogpages($page, $name, $text); break;
     36             }
     37         }
     38         return $output;
     39     }
     40         
     41     // Return blogauthors shortcut
     42     public function getShortcutBlogauthors($page, $name, $text) {
     43         $output = null;
     44         list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text);
     45         if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation");
     46         if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries");
     47         $blogStart = $this->getBlogStart($page, $startLocation);
     48         if (!is_null($blogStart)) {
     49             $pages = $this->getBlogPages($blogStart);
     50             $page->setLastModified($pages->getModified());
     51             $authors = $pages->group("author", false, "count");
     52             if ($shortcutEntries!=0) $authors = array_slice($authors, 0, $shortcutEntries, true);
     53             uksort($authors, "strnatcasecmp");
     54             $output = "<div class=\"".htmlspecialchars($name)."\">\n";
     55             $output .= "<ul>\n";
     56             foreach ($authors as $author=>$collection) {
     57                 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("author:$author")."\">";
     58                 $output .= htmlspecialchars($author)."</a></li>\n";
     59             }
     60             $output .= "</ul>\n";
     61             $output .= "</div>\n";
     62         } else {
     63             $page->error(500, "Blogauthors '$startLocation' does not exist!");
     64         }
     65         return $output;
     66     }
     67     
     68     // Return blogtags shortcut
     69     public function getShortcutBlogtags($page, $name, $text) {
     70         $output = null;
     71         list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text);
     72         if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation");
     73         if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries");
     74         $blogStart = $this->getBlogStart($page, $startLocation);
     75         if (!is_null($blogStart)) {
     76             $pages = $this->getBlogPages($blogStart);
     77             $page->setLastModified($pages->getModified());
     78             $tags = $pages->group("tag", false, "count");
     79             if ($shortcutEntries!=0) $tags = array_slice($tags, 0, $shortcutEntries, true);
     80             uksort($tags, "strnatcasecmp");
     81             $output = "<div class=\"".htmlspecialchars($name)."\">\n";
     82             $output .= "<ul>\n";
     83             foreach ($tags as $tag=>$collection) {
     84                 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("tag:$tag")."\">";
     85                 $output .= htmlspecialchars($tag)."</a></li>\n";
     86             }
     87             $output .= "</ul>\n";
     88             $output .= "</div>\n";
     89         } else {
     90             $page->error(500, "Blogtags '$startLocation' does not exist!");
     91         }
     92         return $output;
     93     }
     94 
     95     // Return blogyears shortcut
     96     public function getShortcutBlogyears($page, $name, $text) {
     97         $output = null;
     98         list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text);
     99         if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation");
    100         if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries");
    101         $blogStart = $this->getBlogStart($page, $startLocation);
    102         if (!is_null($blogStart)) {
    103             $pages = $this->getBlogPages($blogStart);
    104             $page->setLastModified($pages->getModified());
    105             $years = $pages->group("published", false, "Y");
    106             if ($shortcutEntries!=0) $years = array_slice($years, 0, $shortcutEntries, true);
    107             $output = "<div class=\"".htmlspecialchars($name)."\">\n";
    108             $output .= "<ul>\n";
    109             foreach ($years as $year=>$collection) {
    110                 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("published:$year")."\">";
    111                 $output .= htmlspecialchars($this->yellow->language->getDateStandard($year))."</a></li>\n";
    112             }
    113             $output .= "</ul>\n";
    114             $output .= "</div>\n";
    115         } else {
    116             $page->error(500, "Blogyears '$startLocation' does not exist!");
    117         }
    118         return $output;
    119     }
    120     
    121     // Return blogmonths shortcut
    122     public function getShortcutBlogmonths($page, $name, $text) {
    123         $output = null;
    124         list($startLocation, $shortcutEntries) = $this->yellow->toolbox->getTextArguments($text);
    125         if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation");
    126         if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries");
    127         $blogStart = $this->getBlogStart($page, $startLocation);
    128         if (!is_null($blogStart)) {
    129             $pages = $this->getBlogPages($blogStart);
    130             $page->setLastModified($pages->getModified());
    131             $months = $pages->group("published", false, "Y-m");
    132             if ($shortcutEntries!=0) $months = array_slice($months, 0, $shortcutEntries, true);
    133             $output = "<div class=\"".htmlspecialchars($name)."\">\n";
    134             $output .= "<ul>\n";
    135             foreach ($months as $month=>$collection) {
    136                 $output .= "<li><a href=\"".$blogStart->getLocation(true).$this->yellow->lookup->normaliseArguments("published:$month")."\">";
    137                 $output .= htmlspecialchars($this->yellow->language->getDateStandard($month))."</a></li>\n";
    138             }
    139             $output .= "</ul>\n";
    140             $output .= "</div>\n";
    141         } else {
    142             $page->error(500, "Blogmonths '$startLocation' does not exist!");
    143         }
    144         return $output;
    145     }
    146     
    147     // Return blogpages shortcut
    148     public function getShortcutBlogpages($page, $name, $text) {
    149         $output = null;
    150         list($startLocation, $shortcutEntries, $filterTag) = $this->yellow->toolbox->getTextArguments($text);
    151         if (is_string_empty($startLocation)) $startLocation = $this->yellow->system->get("blogStartLocation");
    152         if (is_string_empty($shortcutEntries)) $shortcutEntries = $this->yellow->system->get("blogShortcutEntries");
    153         $blogStart = $this->getBlogStart($page, $startLocation);
    154         if (!is_null($blogStart)) {
    155             $pages = $this->getBlogPages($blogStart)->remove($page);
    156             $page->setLastModified($pages->getModified());
    157             if (!is_string_empty($filterTag)) $pages->filter("tag", $filterTag);
    158             $pages->sort("published", false);
    159             if ($shortcutEntries!=0) $pages->limit($shortcutEntries);
    160             $output = "<div class=\"".htmlspecialchars($name)."\">\n";
    161             $output .= "<ul>\n";
    162             foreach ($pages as $pageBlog) {
    163                 $output .= "<li><a".($pageBlog->isExisting("tag") ? " class=\"".$this->getClass($pageBlog)."\"" : "");
    164                 $output .=" href=\"".$pageBlog->getLocation(true)."\">".$pageBlog->getHtml("title")."</a></li>\n";
    165             }
    166             $output .= "</ul>\n";
    167             $output .= "</div>\n";
    168         } else {
    169             $page->error(500, "Blogpages '$startLocation' does not exist!");
    170         }
    171         return $output;
    172     }
    173     
    174     // Handle page layout
    175     public function onParsePageLayout($page, $name) {
    176         if ($name=="blog-start") {
    177             $pages = $this->getBlogPages($page);
    178             $pagesFilter = array();
    179             if ($page->isRequest("tag")) {
    180                 $pages->filter("tag", $page->getRequest("tag"));
    181                 array_push($pagesFilter, $pages->getFilter());
    182             }
    183             if ($page->isRequest("author")) {
    184                 $pages->filter("author", $page->getRequest("author"));
    185                 array_push($pagesFilter, $pages->getFilter());
    186             }
    187             if ($page->isRequest("published")) {
    188                 $pages->filter("published", $page->getRequest("published"), false);
    189                 array_push($pagesFilter, $this->yellow->language->getDateStandard($pages->getFilter()));
    190             }
    191             $pages->sort("published", false);
    192             if (!is_array_empty($pagesFilter)) {
    193                 $text = implode(" ", $pagesFilter);
    194                 $page->set("titleHeader", $text." - ".$page->get("sitename"));
    195                 $page->set("titleContent", $page->get("title").": ".$text);
    196                 $page->set("title", $page->get("title").": ".$text);
    197                 $page->set("blogWithFilter", true);
    198             }
    199             $page->setPages("blog", $pages);
    200             $page->setLastModified($pages->getModified());
    201             $page->setHeader("Cache-Control", "max-age=60");
    202         }
    203         if ($name=="blog") {
    204             $blogStartLocation = $this->yellow->system->get("blogStartLocation");
    205             if ($blogStartLocation=="auto") {
    206                 $blogStart = $page->getParent();
    207             } else {
    208                 $blogStart = $this->yellow->content->find($blogStartLocation);
    209             }
    210             $page->setPage("blogStart", $blogStart);
    211         }
    212     }
    213     
    214     // Return blog start page, null if not found
    215     public function getBlogStart($page, $blogStartLocation) {
    216         if ($blogStartLocation=="auto") {
    217             $blogStart = null;
    218             foreach ($this->yellow->content->top(true, false) as $pageTop) {
    219                 if ($pageTop->get("layout")=="blog-start") {
    220                     $blogStart = $pageTop;
    221                     break;
    222                 }
    223             }
    224             if ($page->get("layout")=="blog-start") $blogStart = $page;
    225         } else {
    226             $blogStart = $this->yellow->content->find($blogStartLocation);
    227         }
    228         return $blogStart;
    229     }
    230 
    231     // Return blog pages for page
    232     public function getBlogPages($page) {
    233         if ($this->yellow->system->get("blogStartLocation")=="auto") {
    234             $pages = $page->getChildren();
    235         } else {
    236             $pages = $this->yellow->content->index();
    237         }
    238         $pages->filter("layout", "blog");
    239         return $pages;
    240     }
    241     
    242     // Return class for page
    243     public function getClass($page) {
    244         $class = "";
    245         if ($page->isExisting("tag")) {
    246             foreach (preg_split("/\s*,\s*/", $page->get("tag")) as $tag) {
    247                 $class .= " tag-".$this->yellow->lookup->normaliseClass($tag);
    248             }
    249         }
    250         return trim($class);
    251     }
    252 }