remove unnecessary loops

This commit is contained in:
jasmine 2023-10-18 13:44:40 +07:00 committed by GitHub
parent e35a40bd47
commit 9069618c86
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 30 additions and 30 deletions

View File

@ -40,7 +40,7 @@
echo '<?xml version="1.0" encoding="utf-8"?>' echo '<?xml version="1.0" encoding="utf-8"?>'
.'<feed xmlns="http://www.w3.org/2005/Atom">' .'<feed xmlns="http://www.w3.org/2005/Atom">'
// optionally specify feed generator for debugging purposes. // optionally specify feed generator for debugging purposes.
.'<generator uri="https://github.com/jasm1nii/xml-feed-generator" version="1.1">PHP feed generator by jasm1nii.xyz | last modified by the system at ' . strtoupper(date("h:i:sa")) . ' (GMT' . date('P') . ')</generator>' .'<generator uri="https://github.com/jasm1nii/xml-feed-generator" version="1.1">PHP feed generator by jasm1nii.xyz | Last modified by system at ' . strtoupper(date("h:i:sa")) . ' (GMT' . date('P') . ')</generator>'
.'<title>' . $feed_title . '</title>' .'<title>' . $feed_title . '</title>'
.'<subtitle>' . $feed_subtitle . '</subtitle>' .'<subtitle>' . $feed_subtitle . '</subtitle>'
.'<id>' . $blog_url . '</id>' .'<id>' . $blog_url . '</id>'
@ -57,19 +57,16 @@
$first_article_dom = new DOMDocument; $first_article_dom = new DOMDocument;
$first_article_dom->loadHTML($first_article_content); $first_article_dom->loadHTML($first_article_content);
$feed_updated = $first_article_dom->getElementsByTagName('time'); $feed_updated = $first_article_dom->getElementsByTagName('time');
$f = 0; if (!empty($feed_updated)) {
foreach ($feed_updated as $feed_updated_text) { $feed_datetime = $feed_updated[0]->getAttribute('datetime');
$feed_datetime = $feed_updated_text->getAttribute('datetime'); if (strlen($feed_datetime) == 10) {
if (strlen($feed_datetime) == 10) { echo '<updated>' . $feed_datetime . 'T00:00:00' . date('P') .'</updated>';
echo '<updated>' . $feed_datetime . 'T00:00:00' . date('P') .'</updated>'; }
} elseif (strlen($feed_datetime) == 25 || strlen($feed_datetime) == 20) {
elseif (strlen($feed_datetime) == 25 || strlen($feed_datetime) == 20) { echo '<updated>' . $feed_datetime .'</updated>';
echo '<updated>' . $feed_datetime .'</updated>'; }
}
if(++$f > 0) break;
}
// if no RFC 3339 timestamp is found, use the file creation date. // if no RFC 3339 timestamp is found, use the file creation date.
if (empty($feed_updated)) { } else {
$first_article_created = filectime($first_article); $first_article_created = filectime($first_article);
echo '<updated>' . date(DATE_ATOM, $first_article_created) . '</updated>'; echo '<updated>' . date(DATE_ATOM, $first_article_created) . '</updated>';
} }
@ -92,10 +89,18 @@
echo '<entry>'; echo '<entry>';
$x = new DOMXPath($article_dom);
// title // title
$title = $article_dom->getElementsByTagName('h2'); $title_class = 'p-name';
foreach ($title as $title_text) { $title = $x->query("//*[@class='" . $title_class . "']");
echo '<title>'.$title_text->nodeValue.'</title>'; if ($title->length > 0) {
echo '<title>'. $title[0]->nodeValue . '</title>';
} elseif (!empty($title)) {
$title = $article_dom->getElementsByTagName('h2');
echo '<title>'.$title[0]->nodeValue.'</title>';
} else {
echo $feed_title;
} }
// id // id
@ -105,31 +110,28 @@
echo '<link rel="alternate" type="text/html" href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '"/>'; echo '<link rel="alternate" type="text/html" href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '"/>';
$updated = $article_dom->getElementsByTagName('time'); $updated = $article_dom->getElementsByTagName('time');
$a = 0; if (!empty($updated)) {
foreach ($updated as $updated_text) { $timestamp = $updated[0]->getAttribute('datetime');
$timestamp = $updated_text->getAttribute('datetime');
if (strlen($timestamp) == 10) { if (strlen($timestamp) == 10) {
echo '<updated>' . $timestamp . 'T00:00:00' . date('P'). '</updated>'; echo '<updated>' . $timestamp . 'T00:00:00' . date('P'). '</updated>';
} }
elseif (strlen($timestamp) == 25 || strlen($timestamp) == 20) { elseif (strlen($timestamp) == 25 || strlen($timestamp) == 20) {
echo '<updated>' . $timestamp .'</updated>'; echo '<updated>' . $timestamp .'</updated>';
} }
if(++$a > 0) break; } else {
}
// if no RFC 3339 timestamp is found, use the file creation date.
if (empty($updated)) {
$article_created = filectime($article); $article_created = filectime($article);
echo '<updated>' . date(DATE_ATOM, $article_created) . '</updated>'; echo '<updated>' . date(DATE_ATOM, $article_created) . '</updated>';
} }
// summary // summary
$x = new DOMXPath($article_dom);
$summary_class = 'p-summary'; $summary_class = 'p-summary';
$summary = $x->query("//*[@class='" . $summary_class . "']"); $summary = $x->query("//*[@class='" . $summary_class . "']");
if ($summary->length > 0) { if ($summary->length > 0) {
echo '<summary type="html">'; echo '<summary type="html">';
echo $summary->item(0)->nodeValue; echo $summary->item(0)->nodeValue;
echo '</summary>'; echo '</summary>';
} else {
echo '<summary type="html">' . 'A summary of this content is not available.' . '</summary>';
} }
// content // content
@ -138,13 +140,11 @@
if ($content->length > 0) { if ($content->length > 0) {
// strip line breaks and output a maximum of 500 characters. // strip line breaks and output a maximum of 500 characters.
echo '<content type="html">' . preg_replace('/\s\s+/', ' ',(substr($content->item(0)->nodeValue,0,500))) . '... (&lt;a href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '"&gt;read more&lt;/a&gt;)' . '</content>'; echo '<content type="html">' . preg_replace('/\s\s+/', ' ',(substr($content->item(0)->nodeValue,0,500))) . '... (&lt;a href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '"&gt;read more&lt;/a&gt;)' . '</content>';
} elseif (!empty($content)) {
$content = $article_dom->getElementsByTagName('article');
echo '<content type="html">' . preg_replace('/\s\s+/', ' ',(substr($content->item(0)->nodeValue,0,500))) . '... (&lt;a href="https://jasm1nii.xyz/blog/articles/' . ltrim($article, $blog_root) . '"&gt;read more&lt;/a&gt;)' . '</content>';
} else { } else {
// fallback for older markup echo '<content type="html">' . 'Content could not be parsed for previewing - view the original article on the website.' . '</content>';
$content_class = 'entry';
$content = $x->query("//*[@class='" . $content_class . "']");
if ($content->length >= 0) {
echo '<content type="html">' . 'whoops - this page contains markup that can&apos;t be parsed for feed-reader friendliness. read more on the website!' . '</content>';
}
} }
echo '</entry>'; echo '</entry>';