Skip to content

Commit

Permalink
https://github.com/elmsln/issues/issues/946
Browse files Browse the repository at this point in the history
  • Loading branch information
btopro committed Aug 28, 2023
1 parent 0525d26 commit 88b1530
Show file tree
Hide file tree
Showing 3 changed files with 130 additions and 114 deletions.
8 changes: 8 additions & 0 deletions system/backend/php/lib/HAXCMS.php
Original file line number Diff line number Diff line change
Expand Up @@ -1013,6 +1013,14 @@ public function getURI() {
return $_SERVER['SCRIPT_URI'];
}
}
/**
* Return the active domain if it exists
*/
public function getDomain() {
if (isset($_SERVER['SERVER_NAME'])) {
return 'https://' . $_SERVER['SERVER_NAME'];
}
}
/**
* Load wc-registry.json relative to the site in question
*/
Expand Down
153 changes: 106 additions & 47 deletions system/backend/php/lib/HAXCMSSite.php
Original file line number Diff line number Diff line change
Expand Up @@ -692,63 +692,74 @@ public function updateAlternateFormats($format = NULL)
$rss = new FeedMe();
$siteDirectory =
$this->directory . '/' . $this->manifest->metadata->site->name . '/';
@file_put_contents($siteDirectory . 'rss.xml', $rss->getRSSFeed($this));
$domain = NULL;
if (isset($this->manifest->metadata->site->domain)) {
$domain = $this->manifest->metadata->site->domain;
}
if (is_null($domain) || $domain == "") {
$domain = str_replace('iam.','oer.', $GLOBALS['HAXCMS']->getDomain()) . "/sites/" . $this->manifest->metadata->site->name . "/";
}
@file_put_contents($siteDirectory . 'rss.xml', $rss->getRSSFeed($this, $domain));
@file_put_contents(
$siteDirectory . 'atom.xml',
$rss->getAtomFeed($this)
$rss->getAtomFeed($this, $domain)
);
} catch (Exception $e) {
// some of these XML parsers are a bit unstable
}
}
if (is_null($format) || $format == 'jsonfeed') {
// now generate the search index
@file_put_contents(
$siteDirectory . 'jsonfeed.json',
json_encode($this->jsonFeedFormat())
);
}
// build a sitemap if we have a domain, kinda required...
if (is_null($format) || $format == 'sitemap') {
try {
if (isset($this->manifest->metadata->site->domain)) {
$domain = $this->manifest->metadata->site->domain;
$generator = new \Icamys\SitemapGenerator\SitemapGenerator(
$domain,
$siteDirectory
);
// will create also compressed (gzipped) sitemap
$generator->createGZipFile = true;
// determine how many urls should be put into one file
// according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html)
$generator->maxURLsPerSitemap = 50000;
// sitemap file name
$generator->sitemapFileName = "sitemap.xml";
// sitemap index file name
$generator->sitemapIndexFileName = "sitemap-index.xml";
// adding url `loc`, `lastmodified`, `changefreq`, `priority`
foreach ($this->manifest->items as $key => $item) {
if ($item->parent == null) {
$priority = '1.0';
} elseif ($item->indent == 2) {
$priority = '0.7';
} else {
$priority = '0.5';
}
$updatedTime = new DateTime();
$updatedTime->setTimestamp($item->metadata->updated);
$updatedTime->format(DateTime::ATOM);
$generator->addUrl(
$domain .
'/' .
str_replace(
'pages/',
'',
str_replace('/index.html', '', $item->location)
),
$updatedTime,
'daily',
$priority
);
}
// generating internally a sitemap
$generator->createSitemap();
// writing early generated sitemap to file
$generator->writeSitemap();
}
if (isset($this->manifest->metadata->site->domain)) {
$domain = $this->manifest->metadata->site->domain;
}
if (is_null($domain) || $domain == "") {
$domain = str_replace('iam.','oer.', $GLOBALS['HAXCMS']->getDomain()) . "/sites/" . $this->manifest->metadata->site->name . "/";
}
$generator = new \Icamys\SitemapGenerator\SitemapGenerator(
$domain,
$siteDirectory
);
// will create also compressed (gzipped) sitemap
$generator->createGZipFile = true;
// determine how many urls should be put into one file
// according to standard protocol 50000 is maximum value (see http://www.sitemaps.org/protocol.html)
$generator->maxURLsPerSitemap = 50000;
// sitemap file name
$generator->sitemapFileName = "sitemap.xml";
// sitemap index file name
$generator->sitemapIndexFileName = "sitemap-index.xml";
// adding url `loc`, `lastmodified`, `changefreq`, `priority`
foreach ($this->manifest->items as $key => $item) {
if ($item->parent == null) {
$priority = '1.0';
} elseif ($item->indent == 2) {
$priority = '0.7';
} else {
$priority = '0.5';
}
$updatedTime = new DateTime();
$updatedTime->setTimestamp($item->metadata->updated);
$updatedTime->format(DateTime::ATOM);
$generator->addUrl(
$item->slug,
$updatedTime,
'daily',
$priority
);
}
// generating internally a sitemap
$generator->createSitemap();
// writing early generated sitemap to file
$generator->writeSitemap();
} catch (Exception $e) {
// some of these XML parsers are a bit unstable
}
Expand Down Expand Up @@ -784,6 +795,54 @@ public function updateAlternateFormats($format = NULL)
$this->rebuildManagedFiles(array('sw' => 'service-worker.js'));
}
}
/**
* Create Lunr.js style search index
*/
public function jsonFeedFormat($limit = 25) {
$domain = NULL;
if (isset($this->manifest->metadata->site->domain)) {
$domain = $this->manifest->metadata->site->domain;
}
if (is_null($domain) || $domain == "") {
// simple domain redirect, this is a bit of a hack but it works for now w/ haxiam
$domain = str_replace('iam.','oer.', $GLOBALS['HAXCMS']->getDomain()) . "/sites/" . $this->manifest->metadata->site->name . "/";
}
$data = array(
"version" => "https://jsonfeed.org/version/1.1",
"title" => $this->manifest->title,
"home_page_url" => $domain,
"feed_url" => $domain . 'jsonfeed.json',
"description" => $this->manifest->description,
"items" => array(),
);
$count = 0;
foreach ($this->manifest->items as $item) {
if ($count < $limit) {
$created = time();
if (isset($item->metadata) && isset($item->metadata->created)) {
$created = $item->metadata->created;
}
if (isset($item->slug)) {
$slug = $item->slug;
}
else {
// slug is now the URL canonical
$slug = str_replace('pages/', '', str_replace('/index.html', '', $item->location));
}
// may seem silly but IDs in lunr have a size limit for some reason in our context..
$data["items"][] = array(
"guid" => substr(str_replace('-', '', str_replace('item-', '', $item->id)), 0, 29),
"url" => $domain . $slug,
"title" => $item->title,
"summary" => $item->description,
"content_html" => @file_get_contents($this->directory . '/' . $this->manifest->metadata->site->name . '/' . $item->location),
"date_published" => date('c', $created),
);
}
$count++;
}
return $data;
}
/**
* Create Lunr.js style search index
*/
Expand Down
83 changes: 16 additions & 67 deletions system/backend/php/lib/RSS.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,24 +5,14 @@ class FeedMe
/**
* Generate the RSS 2.0 header
*/
public function getRSSFeed($site)
public function getRSSFeed($site, $domain = "")
{
$domain = "";
if (isset($site->manifest->metadata->site->domain)) {
$domain = $site->manifest->metadata->site->domain;
}
return '<?xml version="1.0" encoding="utf-8"?>
<rss xmlns:atom="http://www.w3.org/2005/Atom" version="2.0">
<channel>
<title>' .
$site->manifest->title .
'</title>
<link>' .
$domain .
'/rss.xml</link>
<description>' .
$site->manifest->description .
'</description>
<title>' . $site->manifest->title . '</title>
<link>' . $domain . '</link>
<description>' . $site->manifest->description . '</description>
<copyright>Copyright (C) ' .
date('Y') .
' ' .
Expand All @@ -35,24 +25,19 @@ public function getRSSFeed($site)
date(\DateTime::RSS, $site->manifest->metadata->site->updated) .
'</lastBuildDate>
<atom:link href="' .
$domain .
'/rss.xml" rel="self" type="application/rss+xml"/>' .
$this->rssItems($site) .
$domain .'" rel="self" type="application/rss+xml"/>' .
$this->rssItems($site, $domain) .
'
</channel>
</rss>';
}
/**
* Generate RSS items.
*/
public function rssItems($site, $limit = 25)
public function rssItems($site, $domain, $limit = 25)
{
$output = '';
$domain = "";
$count = 0;
if (isset($site->manifest->metadata->site->domain)) {
$domain = $site->manifest->metadata->site->domain;
}
$items = $site->sortItems('created');
$siteDirectory = $site->directory . '/' . $site->manifest->metadata->site->name;
foreach ($items as $key => $item) {
Expand All @@ -76,13 +61,7 @@ public function rssItems($site, $limit = 25)
$item->title .
'</title>
<link>' .
$domain .
'/' .
str_replace(
'pages/',
'',
str_replace('/index.html', '', $item->location)
) .
$domain . $item->slug .
'</link>
<description>
<![CDATA[ ' .
Expand All @@ -92,15 +71,7 @@ public function rssItems($site, $limit = 25)
<category>' .
$tags .
'</category>
<guid>' .
$domain .
'/' .
str_replace(
'pages/',
'',
str_replace('/index.html', '', $item->location)
) .
'</guid>
<guid>' . $item->id . '</guid>
<pubDate>' .
date(\DateTime::RSS, $item->metadata->created) .
'</pubDate>
Expand All @@ -113,20 +84,15 @@ public function rssItems($site, $limit = 25)
/**
* Generate the atom feed
*/
public function getAtomFeed($site)
public function getAtomFeed($site, $domain = "")
{
$domain = "";
if (isset($site->manifest->metadata->site->domain)) {
$domain = $site->manifest->metadata->site->domain;
}
return '<?xml version="1.0" encoding="utf-8"?>
<feed xmlns="http://www.w3.org/2005/Atom">
<title>' .
$site->manifest->title .
'</title>
<link href="' .
$domain .
'/atom.xml" rel="self" />
$domain . '" rel="self" />
<subtitle>' .
$site->manifest->description .
'</subtitle>
Expand All @@ -139,23 +105,18 @@ public function getAtomFeed($site)
'</name>
</author>
<id>' .
$domain .
'/feed</id>' .
$this->atomItems($site) .
$domain . '</id>' .
$this->atomItems($site, $domain) .
'
</feed>';
}
/**
* Generate Atom items.
*/
public function atomItems($site, $limit = 25)
public function atomItems($site, $domain, $limit = 25)
{
$output = '';
$domain = "";
$count = 0;
if (isset($site->manifest->metadata->site->domain)) {
$domain = $site->manifest->metadata->site->domain;
}
$items = $site->sortItems('created');
$siteDirectory = $site->directory . '/' . $site->manifest->metadata->site->name;
foreach ($items as $key => $item) {
Expand All @@ -182,13 +143,7 @@ public function atomItems($site, $limit = 25)
$item->title .
'</title>
<id>' .
$domain .
'/' .
str_replace(
'pages/',
'',
str_replace('/index.html', '', $item->location)
) .
$item->id .
'</id>
<updated>' .
date(\DateTime::ATOM, $item->metadata->updated) .
Expand All @@ -200,13 +155,7 @@ public function atomItems($site, $limit = 25)
$item->description .
'</summary>
<link href="' .
$domain .
'/' .
str_replace(
'pages/',
'',
str_replace('/index.html', '', $item->location)
) .
$domain . $item->slug .
'"/>
' .
$tags .
Expand Down

0 comments on commit 88b1530

Please sign in to comment.