page: search results as article objects
[minimedit.git] / article.inc.php
index 2104ef9d65025e0b3a0687d0d49dc0a0e9ff34ce..7f8e622cde5297f5d71523a42b106a14983f09eb 100644 (file)
@@ -65,6 +65,18 @@ class ArchiveArticle
                return;
        }
 
+       function index()
+       {
+               $this->handler;
+               if (empty($this->handler)) {
+                       return;
+               }
+               $User = NULL;
+               $Page = $this;
+               $res = include "./{$this->handler}/index.php";
+               return $res;
+       }
+
        function restricted()
        {
                $this->handler;
@@ -149,6 +161,9 @@ class ArchiveArticle
        function thumb($size = '300x')
        {
                if (!$this->image or $this->image[0] !== '/') return;
+               if (preg_match('{^/thumb/\D}', $this->image)) {
+                       return ltrim($this->image, '/');
+               }
                return preg_replace(
                        ['{^(?:/thumb/[^/]*)?}', '/\.groot(?=\.\w+$)/'], ["thumb/$size", ''],
                        $this->image
@@ -164,17 +179,20 @@ class ArchiveArticle
 
                ob_start();
                $Page = clone $this;
-               $Page->handler = $Page->handler . $Page->path; // .= with explicit getter
-               $Page->path = '';
-               foreach ($params as $param) {
-                       if ($set = strpos($param, '=')) {
-                               $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1);
-                       }
-                       elseif (!empty($param)) {
-                               $Page->path .= '/'.$param;
+               if (is_array($params)) {
+                       $Page->place += $params;
+               }
+               else {
+                       foreach (explode(' ', $params) as $param) {
+                               if ($set = strpos($param, '=')) {
+                                       $Page->place[ substr($param, 0, $set) ] = substr($param, $set + 1);
+                               }
+                               elseif (!empty($param)) {
+                                       $Page->place[] = $param;
+                               }
                        }
                }
-               $Page->link .= $Page->path;
+
                try {
                        include "widget/$name.php";
                        return ob_get_clean();
@@ -210,7 +228,7 @@ class ArchiveArticle
                        function ($sub) {
                                list ($placeholder, $name, $params) = $sub;
                                $html = $this->place[$name] ??
-                                       $this->widget($name, explode(' ', $params));
+                                       $this->widget($name, $params);
                                if (empty($html) or $html[0] != '<') {
                                        $html = "<span>$html</span>";
                                }
@@ -225,6 +243,8 @@ class ArchiveArticle
 
 class PageSearch
 {
+       public $handlers = [];
+
        function __construct($path = '.')
        {
                $this->iterator = new RecursiveCallbackFilterIterator(
@@ -234,6 +254,11 @@ class PageSearch
                                        # skip hidden files and directories
                                        return FALSE;
                                }
+                               if (file_exists($current->getFilename() . '/index.php')) {
+                                       # contents better provided by handler code
+                                       $this->handlers[ $current->getPathname() ] = $current;
+                                       return FALSE;
+                               }
                                if ($current->isLink()) {
                                        # ignore symlinks, original contents only
                                        return FALSE;
@@ -251,12 +276,12 @@ class PageSearch
        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);
+               $dir = [];
+               foreach (new RecursiveIteratorIterator($this->iterator) as $name) {
+                       $article = new ArchiveArticle($name);
+                       $dir[$article->link] = $article;
+               }
+               ksort($dir);
                return $dir;
        }
 }