page: move placeholder_include() to widget method
[minimedit.git] / article.inc.php
index bc8dbff21be1fc99969daebb3bc0bbfa2ea5d2c4..b676dd0c5e34537bb17732e13dd5a6b5f35628ff 100644 (file)
@@ -16,34 +16,76 @@ function showdate($parts)
 
 class ArchiveArticle
 {
+       public $raw, $title, $body; # file contents
+       public $meta = [];  # head metadata properties
+
        function __construct($path)
        {
-               $this->page = $path;
-               $this->link = preg_replace('{(?:/index)?\.html$}', '', $path);
+               $this->page = preg_replace('{^\.(?:/|$)}', '', $path);
+               $this->link = preg_replace('{(?:(?:/|^)index)?\.html$}', '', $this->page);
+               $this->raw($this->page);
        }
 
-       function __get($col)
+       function raw($page)
        {
-               return $this->$col = $this->$col();  # run method and cache
+               if (!file_exists($page)) {
+                       return;
+               }
+               $this->raw = file_get_contents($page);
+
+               if (preg_match_all('{
+                       \G <meta \s+ property="( [^"]+ )" \s+ content="( [^"]* )" > \s*
+               }x', $this->raw, $meta)) {
+                       $matchlen = array_sum(array_map('strlen', $meta[0]));
+                       $this->raw = substr($this->raw, $matchlen); # delete matched contents
+                       $this->meta = array_combine($meta[1], $meta[2]); # [property => content]
+               }
+
+               // find significant contents
+               $this->body = preg_replace('{<aside\b.*?</aside>}s', '', $this->raw);
+               if (preg_match('{<h2>(.*?)</h2>\s*(.*)}s', $this->body, $titlematch)) {
+                       list (, $this->title, $this->body) = $titlematch;
+               }
        }
 
-       function file()
+       function __get($col)
        {
-               if (!file_exists($this->page)) return;
-               return fopen($this->page, 'r');
+               return $this->$col = $this->$col();  # run method and cache
        }
 
-       function rawtitle()
-       {
-               return fgets($this->file);
+       function handler()
+       {
+               $path = $this->link;
+               $this->path = '';
+               $this->restricted = FALSE;
+               while (TRUE) {
+                       if (file_exists("$path/.private")) {
+                               $this->restricted = $path;
+                       }
+
+                       if (file_exists("$path/index.php")) {
+                               return $path;
+                       }
+
+                       $up = strrpos($path, '/');
+                       $this->path = substr($path, $up) . $this->path;
+                       $path = substr($path, 0, $up);
+                       if ($up === FALSE) {
+                               break;
+                       }
+               }
+               return;
        }
-       function title()
+
+       function restricted()
        {
-               return preg_replace('{<h2>(.*)</h2>\s*}', '\1', $this->rawtitle);
+               $this->handler;
+               return $this->restricted;
        }
+
        function safetitle()
        {
-               return trim(strip_tags($this->title));
+               return trim($this->meta['og:title'] ?? strip_tags($this->title));
        }
        function name()
        {
@@ -74,16 +116,10 @@ class ArchiveArticle
                return showdate($this->dateparts);
        }
 
-       function body()
-       {
-               if (!$this->file) return;
-               $this->rawtitle;
-               return fread($this->file, filesize($this->page) ?: 1);
-       }
        function story()
        {
                if ( preg_match('{
-                       \n (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
+                       (?: < (?: p | figure [^>]* ) >\s* )+ (<img\ [^>]*>) | \n <hr\ />
                }x', $this->body, $img, PREG_OFFSET_CAPTURE) ) {
                        # strip part after matching divider (image)
                        if (isset($img[1])) {
@@ -94,13 +130,17 @@ class ArchiveArticle
                return $this->body;
        }
 
-       function raw()
-       {
-               return $this->rawtitle . $this->body;
-       }
        function teaser()
        {
-               if (preg_match('{<p>(.*?)</p>}s', $this->story, $bodyp)) {
+               if ($override = @$this->meta['og:description']) {
+                       # prefer specific page description if found in metadata
+                       return $override;
+               }
+
+               # paragraph contents following the page header if any
+               if (preg_match('{
+                       \G (?> \s+ | <div [^>]*> | \[\[[^]]*\]\] )* <p> \s* (.*?) </p>
+               }sx', $this->body, $bodyp, 0)) {
                        return $bodyp[1];
                }
        }
@@ -113,6 +153,11 @@ class ArchiveArticle
        }
        function image()
        {
+               if ($override = @$this->meta['og:image']) {
+                       # prefer specific page image if found in metadata
+                       return $override;
+               }
+
                if ( preg_match('/\bsrc="([^"]*)"/', $this->img, $src) ) {
                        return $src[1];
                }
@@ -125,4 +170,74 @@ class ArchiveArticle
                        $this->image
                );
        }
+
+       function widget($name, $params = [])
+       {
+               $path = stream_resolve_include_path("widget/$name.php");
+               if (!file_exists($path)) {
+                       return '<strong class="warn"><em>'.$name.'</em> ontbreekt</strong>';
+               }
+
+               ob_start();
+               $Page = clone $this;
+               $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
+               $Page->path = '';
+               $Place = $GLOBALS['Place'];
+               foreach ($params as $param) {
+                       if ($set = strpos($param, '=')) {
+                               $Place[ substr($param, 0, $set) ] = substr($param, $set + 1);
+                       }
+                       elseif (!empty($param)) {
+                               $Page->path .= '/'.$param;
+                       }
+               }
+               $Page->link .= $Page->path;
+               try {
+                       include "widget/$name.php";
+                       return ob_get_clean();
+               }
+               catch (Exception $e) {
+                       return sprintf('<strong class="warn">%s</strong>',
+                               "fout in <em>$name</em>: {$e->getMessage()}"
+                       );
+               }
+       }
+}
+
+class PageSearch
+{
+       function __construct($path = '.')
+       {
+               $this->iterator = new RecursiveCallbackFilterIterator(
+                       new RecursiveDirectoryIterator($path),
+                       function ($current) {
+                               if ($current->getFilename()[0] === '.') {
+                                       # skip hidden files and directories
+                                       return FALSE;
+                               }
+                               if ($current->isLink()) {
+                                       # ignore symlinks, original contents only
+                                       return FALSE;
+                               }
+                               if ($current->isDir()) {
+                                       # traverse subdirectories unless untracked in any amount
+                                       return !file_exists("$current/.gitignore");
+                               }
+                               # match **/*.html
+                               return preg_match('/(?<!\.inc)\.html$/', $current->getFilename());
+                       }
+               );
+       }
+
+       function files()
+       {
+               # order alphabetically by link
+               $dir = iterator_to_array(new RecursiveIteratorIterator($this->iterator));
+               array_walk($dir, function (&$row, $name) {
+                       # prepare values for sorting (directory index first)
+                       $row = preg_replace('{/index\.html$}', '', $name);
+               });
+               asort($dir);
+               return $dir;
+       }
 }