mirror of
https://github.com/e107inc/e107.git
synced 2025-08-03 05:07:27 +02:00
Experimental: Dynamically generated news sitemap.
This commit is contained in:
@@ -42,6 +42,23 @@ class gsitemap_url // plugin-folder + '_url'
|
|||||||
|
|
||||||
);
|
);
|
||||||
|
|
||||||
|
$addons = e107::getAddonConfig('e_gsitemap', 'gsitemap');
|
||||||
|
|
||||||
|
foreach($addons as $plug => $item)
|
||||||
|
{
|
||||||
|
foreach($item as $data )
|
||||||
|
{
|
||||||
|
$key = $plug.'-'.$data['function']; // eg. news-latest
|
||||||
|
$config[$key] = array(
|
||||||
|
'alias' => $key,
|
||||||
|
'regex' => '^'.$key.'-sitemap\.xml$', // matched against url, and if true, redirected to 'redirect' below.
|
||||||
|
'sef' => $key.'-sitemap.xml', // used by e107::url(); to create a url from the db table.
|
||||||
|
'redirect' => '{e_BASE}gsitemap.php?plug='.$plug.'&func='.$data['function'], // file-path of what to load when the regex returns true.
|
||||||
|
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
return $config;
|
return $config;
|
||||||
|
@@ -20,6 +20,40 @@ e107::coreLan('news');
|
|||||||
|
|
||||||
class news_gsitemap
|
class news_gsitemap
|
||||||
{
|
{
|
||||||
|
|
||||||
|
// Dynamically Generated Sitemap;
|
||||||
|
function config()
|
||||||
|
{
|
||||||
|
$config = array();
|
||||||
|
|
||||||
|
// Viewable from my-website.com/news-latest-sitemap.xml ie. plugin-folder + function + 'sitemap.xml'
|
||||||
|
$config[] = array(
|
||||||
|
'name' => "Latest News Posts",
|
||||||
|
'function' => "latest",
|
||||||
|
);
|
||||||
|
|
||||||
|
return $config;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getNewsPosts()
|
||||||
|
{
|
||||||
|
/* public, guests */
|
||||||
|
$userclass_list = "0,252";
|
||||||
|
$_t = time(); /* public, quests */
|
||||||
|
|
||||||
|
$query = "SELECT n.*, nc.category_name, nc.category_sef FROM #news AS n
|
||||||
|
LEFT JOIN #news_category AS nc ON n.news_category = nc.category_id
|
||||||
|
WHERE n.news_class IN (". $userclass_list.") AND n.news_start < ".$_t." AND (n.news_end=0 || n.news_end>".time().") ORDER BY n.news_datestamp ASC ";
|
||||||
|
|
||||||
|
// $data = $sql->retrieve("news", "*", "news_class IN (" . $userclass_list . ") AND news_start < " . $_t . " ORDER BY news_datestamp ASC", true);
|
||||||
|
|
||||||
|
return e107::getDb()->retrieve($query,true);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
function import()
|
function import()
|
||||||
{
|
{
|
||||||
$import = array();
|
$import = array();
|
||||||
@@ -41,16 +75,9 @@ class news_gsitemap
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$data = $this->getNewsPosts();
|
||||||
|
|
||||||
|
|
||||||
$query = "SELECT n.*, nc.category_name, nc.category_sef FROM #news AS n
|
|
||||||
LEFT JOIN #news_category AS nc ON n.news_category = nc.category_id
|
|
||||||
WHERE n.news_class IN (". $userclass_list.") AND n.news_start < ".$_t." AND (n.news_end=0 || n.news_end>".time().") ORDER BY n.news_datestamp ASC ";
|
|
||||||
|
|
||||||
// $data = $sql->retrieve("news", "*", "news_class IN (" . $userclass_list . ") AND news_start < " . $_t . " ORDER BY news_datestamp ASC", true);
|
|
||||||
|
|
||||||
$data = $sql->retrieve($query,true);
|
|
||||||
|
|
||||||
foreach($data as $row)
|
foreach($data as $row)
|
||||||
{
|
{
|
||||||
$import[] = array(
|
$import[] = array(
|
||||||
@@ -66,6 +93,31 @@ class news_gsitemap
|
|||||||
return $import;
|
return $import;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
/* Custom Function for dynamic sitemap of news posts */
|
||||||
|
function latest()
|
||||||
|
{
|
||||||
|
$data = $this->getNewsPosts();
|
||||||
|
|
||||||
|
$ret = [];
|
||||||
|
|
||||||
|
foreach($data as $row)
|
||||||
|
{
|
||||||
|
$ret[] = [
|
||||||
|
'url' => $this->url('news', $row),
|
||||||
|
'lastmod' => (int) $row['news_datestamp'],
|
||||||
|
'freq' => 'always',
|
||||||
|
'priority' => 0.7
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
return $ret;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Used above and by gsitemap/e_event.php to update the URL when changed in news, pages etc.
|
* Used above and by gsitemap/e_event.php to update the URL when changed in news, pages etc.
|
||||||
*
|
*
|
||||||
|
139
gsitemap.php
139
gsitemap.php
@@ -23,6 +23,105 @@ if(!e107::isInstalled('gsitemap'))
|
|||||||
|
|
||||||
e107::lan('gsitemap');
|
e107::lan('gsitemap');
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
class gsitemap_xml
|
||||||
|
{
|
||||||
|
function __construct()
|
||||||
|
{
|
||||||
|
|
||||||
|
$items = [];
|
||||||
|
|
||||||
|
// Gsitemap Addon.
|
||||||
|
if(!empty($_GET['plug']) && !empty($_GET['func']))
|
||||||
|
{
|
||||||
|
if(!e107::isInstalled($_GET['plug']))
|
||||||
|
{
|
||||||
|
exit;
|
||||||
|
}
|
||||||
|
|
||||||
|
$obj = e107::getAddon($_GET['plug'], 'e_gsitemap');
|
||||||
|
if($items = e107::callMethod($obj, $_GET['func']))
|
||||||
|
{
|
||||||
|
$this->renderXML($items);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
else // From Gsitemap Database Table.
|
||||||
|
{
|
||||||
|
$this->renderXML([], 'gsitemap_');
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param $items
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
function renderXML($items=array())
|
||||||
|
{
|
||||||
|
header('Content-type: application/xml', TRUE);
|
||||||
|
$xml = "<?xml version='1.0' encoding='UTF-8'?>
|
||||||
|
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>";
|
||||||
|
|
||||||
|
if(empty($items))
|
||||||
|
{
|
||||||
|
$smArray = e107::getDb()->retrieve("gsitemap", "*", "gsitemap_active IN (".USERCLASS_LIST.") ORDER BY gsitemap_order ",true);
|
||||||
|
$xml .= $this->renderXMLItems($smArray);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$xml .= $this->renderXMLItems($items);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
$xml .= "
|
||||||
|
</urlset>";
|
||||||
|
|
||||||
|
echo $xml;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
function renderXMLItems($data, $prefix = '')
|
||||||
|
{
|
||||||
|
$tp = e107::getParser();
|
||||||
|
|
||||||
|
$xml = '';
|
||||||
|
|
||||||
|
foreach($data as $sm)
|
||||||
|
{
|
||||||
|
$url = $sm[$prefix.'url'];
|
||||||
|
|
||||||
|
if($url[0] === '/')
|
||||||
|
{
|
||||||
|
$url = ltrim($url, '/');
|
||||||
|
}
|
||||||
|
|
||||||
|
$loc = (strpos($url, 'http') === 0) ? $url : SITEURL.$tp->replaceConstants($url,true);
|
||||||
|
$xml .= "
|
||||||
|
<url>
|
||||||
|
<loc>".$loc."</loc>
|
||||||
|
<lastmod>".date('c', (int) $sm[$prefix.'lastmod'])."</lastmod>
|
||||||
|
<changefreq>".$sm[$prefix.'freq']."</changefreq>
|
||||||
|
<priority>".$sm[$prefix.'priority']."</priority>
|
||||||
|
</url>";
|
||||||
|
}
|
||||||
|
|
||||||
|
return $xml;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
// HTML below.
|
||||||
|
|
||||||
if(e_QUERY == "show" || !empty($_GET['show']))
|
if(e_QUERY == "show" || !empty($_GET['show']))
|
||||||
{
|
{
|
||||||
e107::canonical('gsitemap');
|
e107::canonical('gsitemap');
|
||||||
@@ -30,7 +129,9 @@ if(e_QUERY == "show" || !empty($_GET['show']))
|
|||||||
|
|
||||||
require_once(HEADERF);
|
require_once(HEADERF);
|
||||||
|
|
||||||
$nfArray = $sql ->retrieve("gsitemap", "*", "gsitemap_active IN (".USERCLASS_LIST.") ORDER BY gsitemap_order ",true);
|
$nfArray = e107::getDb()->retrieve("gsitemap", "*", "gsitemap_active IN (".USERCLASS_LIST.") ORDER BY gsitemap_order ",true);
|
||||||
|
|
||||||
|
$tp = e107::getParser();
|
||||||
|
|
||||||
if(deftrue('BOOTSTRAP'))
|
if(deftrue('BOOTSTRAP'))
|
||||||
{
|
{
|
||||||
@@ -54,45 +155,13 @@ if(e_QUERY == "show" || !empty($_GET['show']))
|
|||||||
}
|
}
|
||||||
$text .= "</ul></div>";
|
$text .= "</ul></div>";
|
||||||
|
|
||||||
$ns -> tablerender(GSLAN_Name."", $text);
|
e107::getRender() -> tablerender(GSLAN_Name, $text);
|
||||||
|
|
||||||
require_once(FOOTERF);
|
require_once(FOOTERF);
|
||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
header('Content-type: application/xml', TRUE);
|
new gsitemap_xml;
|
||||||
$xml = "<?xml version='1.0' encoding='UTF-8'?>
|
|
||||||
<urlset xmlns='http://www.sitemaps.org/schemas/sitemap/0.9'>";
|
|
||||||
|
|
||||||
$smArray = e107::getDb()->retrieve("gsitemap", "*", "gsitemap_active IN (".USERCLASS_LIST.") ORDER BY gsitemap_order ",true);
|
|
||||||
|
|
||||||
foreach($smArray as $sm)
|
|
||||||
{
|
|
||||||
if($sm['gsitemap_url'][0] == '/') $sm['gsitemap_url'] = ltrim($sm['gsitemap_url'], '/');
|
|
||||||
$loc = (substr($sm['gsitemap_url'],0,4)== "http")? $sm['gsitemap_url'] : SITEURL.$tp->replaceConstants($sm['gsitemap_url'],TRUE);
|
|
||||||
$xml .= "
|
|
||||||
<url>
|
|
||||||
<loc>".$loc."</loc>
|
|
||||||
<lastmod>".get_iso_8601_date($sm['gsitemap_lastmod'])."</lastmod>
|
|
||||||
<changefreq>".$sm['gsitemap_freq']."</changefreq>
|
|
||||||
<priority>".$sm['gsitemap_priority']."</priority>
|
|
||||||
</url>";
|
|
||||||
}
|
|
||||||
|
|
||||||
$xml .= "
|
|
||||||
</urlset>";
|
|
||||||
|
|
||||||
echo $xml;
|
|
||||||
|
|
||||||
/* ungu at terong dot com */
|
|
||||||
function get_iso_8601_date($int_date)
|
|
||||||
{
|
|
||||||
$date_mod = date('Y-m-d\TH:i:s', $int_date);
|
|
||||||
$pre_timezone = date('O', $int_date);
|
|
||||||
$time_zone = substr($pre_timezone, 0, 3).":".substr($pre_timezone, 3, 2);
|
|
||||||
$date_mod .= $time_zone;
|
|
||||||
return $date_mod;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
Reference in New Issue
Block a user