page: read file during article object initialisation
authorMischa POSLAWSKY <perl@shiar.org>
Thu, 27 Jun 2019 21:14:45 +0000 (23:14 +0200)
committerMischa POSLAWSKY <perl@shiar.org>
Fri, 12 Jul 2019 02:18:16 +0000 (04:18 +0200)
Derive parts (title, body) from full contents instead of reading piecemeal.
Assume most pages are small enough so added overhead should be minimal,
adding support for titles after the first line.

article.inc.php
login/index.php
nieuws/index.php
page.php

index bc8dbff21be1fc99969daebb3bc0bbfa2ea5d2c4..336511e87069e59174a80c73d84898562dabe431 100644 (file)
@@ -16,10 +16,17 @@ function showdate($parts)
 
 class ArchiveArticle
 {
+       public $raw, $preface, $title, $body;
+
        function __construct($path)
        {
                $this->page = $path;
                $this->link = preg_replace('{(?:/index)?\.html$}', '', $path);
+               if (file_exists($this->page)) {
+                       $this->raw = file_get_contents($this->page);
+                       @list ($this->preface, $this->title, $this->body) =
+                               preg_split('{<h2>(.*?)</h2>\s*}', $this->raw, 2, PREG_SPLIT_DELIM_CAPTURE);
+               }
        }
 
        function __get($col)
@@ -27,20 +34,6 @@ class ArchiveArticle
                return $this->$col = $this->$col();  # run method and cache
        }
 
-       function file()
-       {
-               if (!file_exists($this->page)) return;
-               return fopen($this->page, 'r');
-       }
-
-       function rawtitle()
-       {
-               return fgets($this->file);
-       }
-       function title()
-       {
-               return preg_replace('{<h2>(.*)</h2>\s*}', '\1', $this->rawtitle);
-       }
        function safetitle()
        {
                return trim(strip_tags($this->title));
@@ -74,12 +67,6 @@ 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('{
@@ -93,11 +80,6 @@ class ArchiveArticle
                }
                return $this->body;
        }
-
-       function raw()
-       {
-               return $this->rawtitle . $this->body;
-       }
        function teaser()
        {
                if (preg_match('{<p>(.*?)</p>}s', $this->story, $bodyp)) {
index fc721b1a70d4dcac5a508ba21899067449756bb0..5cfbdedf8d322d00fd171a1436e48a238a6812f4 100644 (file)
@@ -43,10 +43,8 @@ if (empty($User)) {
        if (isset($_REQUEST['goto'])) {
                $target = ltrim($_REQUEST['goto'], '/');
                $target = new ArchiveArticle("$target.html");
-               if ($target->file) {
-                       if ($target->title) {
-                               $Article->title .= ' voor ' . $target->title;
-                       }
+               if ($target->title) {
+                       $Article->title .= ' voor ' . $target->title;
                }
        }
        return TRUE;
index 18416b32ad342377fe3a5283608b05940e76e99c..0f81de28edcf428bd5dacdc1fa6a945b5751fbe9 100644 (file)
@@ -11,7 +11,7 @@ if ($page and !is_numeric($page)) {
        if ($edit) {
                $Article->title = $edit;
        }
-       if ($Article->file) {
+       if (isset($Article->raw)) {
                $Place['description'] = $Article->teaser;
        }
        $Place[1] = ' <small class="date">'.$Article->date.'</small>';
index a05e158f198960358599af2001522c2f13cc1fc7..cdec9a802b1d55c8bbd46dcb78283e647a65125d 100644 (file)
--- a/page.php
+++ b/page.php
@@ -173,7 +173,7 @@ ob_start(); # inner html
 print '<div class="static">'."\n\n";
 
 $found = FALSE;
-if ($Article->file) {
+if (isset($Article->raw)) {
        print $Article->raw;
        $found = 1;
 }