From 41811556ca2ca3ca29191bee012c35e5f40a3995 Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 27 Jun 2019 23:14:45 +0200 Subject: [PATCH] page: read file during article object initialisation 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 | 32 +++++++------------------------- login/index.php | 6 ++---- nieuws/index.php | 2 +- page.php | 2 +- 4 files changed, 11 insertions(+), 31 deletions(-) diff --git a/article.inc.php b/article.inc.php index bc8dbff..336511e 100644 --- a/article.inc.php +++ b/article.inc.php @@ -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('{

(.*?)

\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('{

(.*)

\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('{

(.*?)

}s', $this->story, $bodyp)) { diff --git a/login/index.php b/login/index.php index fc721b1..5cfbded 100644 --- a/login/index.php +++ b/login/index.php @@ -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; diff --git a/nieuws/index.php b/nieuws/index.php index 18416b3..0f81de2 100644 --- a/nieuws/index.php +++ b/nieuws/index.php @@ -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] = ' '.$Article->date.''; diff --git a/page.php b/page.php index a05e158..cdec9a8 100644 --- a/page.php +++ b/page.php @@ -173,7 +173,7 @@ ob_start(); # inner html print '
'."\n\n"; $found = FALSE; -if ($Article->file) { +if (isset($Article->raw)) { print $Article->raw; $found = 1; } -- 2.30.0