From 9069618c86edd4faaf4c9dee80e11e6590ebe47d Mon Sep 17 00:00:00 2001 From: Jasmine Amalia Date: Wed, 18 Oct 2023 13:44:40 +0700 Subject: [PATCH] remove unnecessary loops --- feed_generator.php | 60 +++++++++++++++++++++++----------------------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/feed_generator.php b/feed_generator.php index 0d2b4cf..bfdfa8f 100644 --- a/feed_generator.php +++ b/feed_generator.php @@ -40,7 +40,7 @@ echo '' .'' // optionally specify feed generator for debugging purposes. - .'PHP feed generator by jasm1nii.xyz | last modified by the system at ' . strtoupper(date("h:i:sa")) . ' (GMT' . date('P') . ')' + .'PHP feed generator by jasm1nii.xyz | Last modified by system at ' . strtoupper(date("h:i:sa")) . ' (GMT' . date('P') . ')' .'' . $feed_title . '' .'' . $feed_subtitle . '' .'' . $blog_url . '' @@ -57,19 +57,16 @@ $first_article_dom = new DOMDocument; $first_article_dom->loadHTML($first_article_content); $feed_updated = $first_article_dom->getElementsByTagName('time'); - $f = 0; - foreach ($feed_updated as $feed_updated_text) { - $feed_datetime = $feed_updated_text->getAttribute('datetime'); - if (strlen($feed_datetime) == 10) { - echo '' . $feed_datetime . 'T00:00:00' . date('P') .''; - } - elseif (strlen($feed_datetime) == 25 || strlen($feed_datetime) == 20) { - echo '' . $feed_datetime .''; - } - if(++$f > 0) break; - } + if (!empty($feed_updated)) { + $feed_datetime = $feed_updated[0]->getAttribute('datetime'); + if (strlen($feed_datetime) == 10) { + echo '' . $feed_datetime . 'T00:00:00' . date('P') .''; + } + elseif (strlen($feed_datetime) == 25 || strlen($feed_datetime) == 20) { + echo '' . $feed_datetime .''; + } // if no RFC 3339 timestamp is found, use the file creation date. - if (empty($feed_updated)) { + } else { $first_article_created = filectime($first_article); echo '' . date(DATE_ATOM, $first_article_created) . ''; } @@ -92,10 +89,18 @@ echo ''; + $x = new DOMXPath($article_dom); + // title - $title = $article_dom->getElementsByTagName('h2'); - foreach ($title as $title_text) { - echo ''.$title_text->nodeValue.''; + $title_class = 'p-name'; + $title = $x->query("//*[@class='" . $title_class . "']"); + if ($title->length > 0) { + echo ''. $title[0]->nodeValue . ''; + } elseif (!empty($title)) { + $title = $article_dom->getElementsByTagName('h2'); + echo ''.$title[0]->nodeValue.''; + } else { + echo $feed_title; } // id @@ -105,31 +110,28 @@ echo ''; $updated = $article_dom->getElementsByTagName('time'); - $a = 0; - foreach ($updated as $updated_text) { - $timestamp = $updated_text->getAttribute('datetime'); + if (!empty($updated)) { + $timestamp = $updated[0]->getAttribute('datetime'); if (strlen($timestamp) == 10) { echo '' . $timestamp . 'T00:00:00' . date('P'). ''; } elseif (strlen($timestamp) == 25 || strlen($timestamp) == 20) { echo '' . $timestamp .''; } - if(++$a > 0) break; - } - // if no RFC 3339 timestamp is found, use the file creation date. - if (empty($updated)) { + } else { $article_created = filectime($article); echo '' . date(DATE_ATOM, $article_created) . ''; } // summary - $x = new DOMXPath($article_dom); $summary_class = 'p-summary'; $summary = $x->query("//*[@class='" . $summary_class . "']"); if ($summary->length > 0) { echo ''; echo $summary->item(0)->nodeValue; echo ''; + } else { + echo '' . 'A summary of this content is not available.' . ''; } // content @@ -138,13 +140,11 @@ if ($content->length > 0) { // strip line breaks and output a maximum of 500 characters. echo '' . preg_replace('/\s\s+/', ' ',(substr($content->item(0)->nodeValue,0,500))) . '... (<a href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '">read more</a>)' . ''; + } elseif (!empty($content)) { + $content = $article_dom->getElementsByTagName('article'); + echo '' . preg_replace('/\s\s+/', ' ',(substr($content->item(0)->nodeValue,0,500))) . '... (<a href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '">read more</a>)' . ''; } else { - // fallback for older markup - $content_class = 'entry'; - $content = $x->query("//*[@class='" . $content_class . "']"); - if ($content->length >= 0) { - echo '' . 'whoops - this page contains markup that can't be parsed for feed-reader friendliness. read more on the website!' . ''; - } + echo '' . 'Content could not be parsed for previewing - view the original article on the website.' . ''; } echo '';