<?php
/* GoogleSiteMap_XML Snippet for MODx
Michal Till
`->Based on Ryan Nutt's Google Sitemap snippet for Etomite
`->Based on the ListSiteMap snippet by JaredDC
USAGE
=====
Use &allowedtemplates to specify templates, that would be included in the sitemap listing. SEPARATE BY COMMA. Don't forget to switch mime type to text/xml. NOTE: &allowed templates may be case sensitive!
EXAMPLE
=======
[[GoogleSiteMap_XML? &allowedtemplates=`Site,Article,Homepage`]]
*/
// Overcome single use limitation on functions
global $MakeMapDefined,$site_url;
if(!function_exists(datediff))
{
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds
switch($interval) {
case 'yyyy': // Number of full years
$years_difference = floor($difference / 31536000);
if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
$years_difference--;
}
if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;
case "q": // Number of full quarters
$quarters_difference = floor($difference / 8035200);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference--;
$datediff = $quarters_difference;
break;
case "m": // Number of full months
$months_difference = floor($difference / 2678400);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference--;
$datediff = $months_difference;
break;
case 'y': // Difference between day numbers
$datediff = date("z", $dateto) - date("z", $datefrom);
break;
case "d": // Number of full days
$datediff = floor($difference / 86400);
break;
case "w": // Number of full weekdays
$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks
$first_day = date("w", $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
if ($odd_days > 7) { // Sunday
$days_remainder--;
}
if ($odd_days > 6) { // Saturday
$days_remainder--;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;
case "ww": // Number of full weeks
$datediff = floor($difference / 604800);
break;
case "h": // Number of full hours
$datediff = floor($difference / 3600);
break;
case "n": // Number of full minutes
$datediff = floor($difference / 60);
break;
default: // Number of full seconds (default)
$datediff = $difference;
break;
}
return $datediff;
}
}
if(!isset($MakeMapDefined))
{
function MakeMap_snippet($funcEtomite, $listParent,$selfid=-1,$settings)
{
global $site_url;
$children = $funcEtomite->getActiveChildren($listParent, "menuindex", "ASC", "id, editedon, template, privateweb ,alias");
foreach($children as $child)
{
$id = $child['id'];
if ($selfid!=$id) {
$url = $site_url."[~".$id."~]";
$date = $child['editedon'];
$date = date("Y-m-d", $date);
// Get the date difference
$datediff = datediff("d", $date, date("Y-m-d"));
if ($datediff <=1)
{
$priority = "1.0";
$update = "daily";
}
elseif (($datediff >1) && ($datediff<=7))
{
$priority = "0.75";
$update = "weekly";
}
elseif (($datediff >7) && ($datediff<=30))
{
$priority = "0.50";
$update = "weekly";
}
else
{
$priority = "0.25";
$update = "monthly";
}
if (@$settings["allowedtemplates"][$child["template"]]&&$child["privateweb"]==0) {
$output .= "<url>\n";
$output .= "<loc>$url</loc>\n";
$output .= "<lastmod>$date</lastmod>\n";
$output .= "<changefreq>$update</changefreq>\n";
$output .= "<priority>$priority</priority>\n";
$output .= "</url>\n";
}
if ($funcEtomite->getActiveChildren($child['id']))
{
$output .= MakeMap_snippet($funcEtomite, $child['id'],$selfid,$settings);
}
}
}
return $output;
}
$MakeMapDefined = true;
}
$templatesok=array();
@$allowedtemplates.="";
$allowedtemplates=",".$allowedtemplates.",";
$allowedtemplates=preg_replace("/ *, */",",",$allowedtemplates);
$allowedtemplates=str_replace("'","",$allowedtemplates);
$sql="select * from ".$this->getFullTableName('site_templates')." where POSITION(CONCAT(',',templatename,',') IN '".$allowedtemplates."')>0";
$templates=$modx->db->query($sql);
echo $query;
while ($template=$modx->db->getRow($templates)) {
$templatesok[$template["id"]]=true;
}
$settings["allowedtemplates"]=$templatesok;
$out .= "<urlset xmlns=\"http://www.google.com/schemas/sitemap/0.84\">\n";
$out .= MakeMap_snippet($etomite, 0,$selfid,$settings);
$out .= "</urlset>";
return $out;
?>
)
<?php
/* GoogleSiteMap_XML Snippet for MODx
Michal Till
`->Based on Ryan Nutt's Google Sitemap snippet for Etomite
`->Based on the ListSiteMap snippet by JaredDC
2007-18-01 [pixelchutes] - Implemented TV option for toggling documents to hide/show in the sitemap :)
2007-18-01 [pixelchutes] - Updated to allow @BINDINGS support for TV
2007-12-02 [pixelchutes] - Updated XML name space to match Google's base sitemap.xml example (https://www.google.com/webmasters/tools/docs/en/protocol.html)
USAGE
=====
Use &allowedtemplates to specify templates, that would be included in the sitemap listing. SEPARATE BY COMMA. Don't forget to switch mime type to text/xml. NOTE: &allowed templates may be case sensitive!
EXAMPLE
=======
[[GoogleSiteMap_XML? &allowedtemplates=`Site,Article,Homepage`]]
*/
// Overcome single use limitation on functions
global $MakeMapDefined,$site_url;
if(!function_exists(datediff))
{
function datediff($interval, $datefrom, $dateto, $using_timestamps = false) {
/*
$interval can be:
yyyy - Number of full years
q - Number of full quarters
m - Number of full months
y - Difference between day numbers
(eg 1st Jan 2004 is "1", the first day. 2nd Feb 2003 is "33". The datediff is "-32".)
d - Number of full days
w - Number of full weekdays
ww - Number of full weeks
h - Number of full hours
n - Number of full minutes
s - Number of full seconds (default)
*/
if (!$using_timestamps) {
$datefrom = strtotime($datefrom, 0);
$dateto = strtotime($dateto, 0);
}
$difference = $dateto - $datefrom; // Difference in seconds
switch($interval) {
case 'yyyy': // Number of full years
$years_difference = floor($difference / 31536000);
if (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom), date("j", $datefrom), date("Y", $datefrom)+$years_difference) > $dateto) {
$years_difference--;
}
if (mktime(date("H", $dateto), date("i", $dateto), date("s", $dateto), date("n", $dateto), date("j", $dateto), date("Y", $dateto)-($years_difference+1)) > $datefrom) {
$years_difference++;
}
$datediff = $years_difference;
break;
case "q": // Number of full quarters
$quarters_difference = floor($difference / 8035200);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($quarters_difference*3), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$quarters_difference--;
$datediff = $quarters_difference;
break;
case "m": // Number of full months
$months_difference = floor($difference / 2678400);
while (mktime(date("H", $datefrom), date("i", $datefrom), date("s", $datefrom), date("n", $datefrom)+($months_difference), date("j", $dateto), date("Y", $datefrom)) < $dateto) {
$months_difference++;
}
$months_difference--;
$datediff = $months_difference;
break;
case 'y': // Difference between day numbers
$datediff = date("z", $dateto) - date("z", $datefrom);
break;
case "d": // Number of full days
$datediff = floor($difference / 86400);
break;
case "w": // Number of full weekdays
$days_difference = floor($difference / 86400);
$weeks_difference = floor($days_difference / 7); // Complete weeks
$first_day = date("w", $datefrom);
$days_remainder = floor($days_difference % 7);
$odd_days = $first_day + $days_remainder; // Do we have a Saturday or Sunday in the remainder?
if ($odd_days > 7) { // Sunday
$days_remainder--;
}
if ($odd_days > 6) { // Saturday
$days_remainder--;
}
$datediff = ($weeks_difference * 5) + $days_remainder;
break;
case "ww": // Number of full weeks
$datediff = floor($difference / 604800);
break;
case "h": // Number of full hours
$datediff = floor($difference / 3600);
break;
case "n": // Number of full minutes
$datediff = floor($difference / 60);
break;
default: // Number of full seconds (default)
$datediff = $difference;
break;
}
return $datediff;
}
}
if(!isset($MakeMapDefined))
{
function MakeMap_snippet($funcEtomite, $listParent,$selfid=-1,$settings)
{
global $site_url,$modx; // pixelchutes update
$children = $funcEtomite->getActiveChildren($listParent, "menuindex", "ASC", "id, editedon, template, privateweb ,alias");
foreach($children as $child)
{
$id = $child['id'];
if ($selfid!=$id) {
$url = $site_url."[~".$id."~]";
$date = $child['editedon'];
$date = date("Y-m-d", $date);
// Get the date difference
$datediff = datediff("d", $date, date("Y-m-d"));
if ($datediff <=1)
{
$priority = "1.0";
$update = "daily";
}
elseif (($datediff >1) && ($datediff<=7))
{
$priority = "0.75";
$update = "weekly";
}
elseif (($datediff >7) && ($datediff<=30))
{
$priority = "0.50";
$update = "weekly";
}
else
{
$priority = "0.25";
$update = "monthly";
}
$XMLSiteMap_Toggle = $modx->getTemplateVarOutput('XMLSiteMap_Toggle',$id); // pixelchutes update
$showDoc = ( is_array($XMLSiteMap_Toggle) ) ? $XMLSiteMap_Toggle[XMLSiteMap_Toggle] : 0; // pixelchutes update
if (@$settings["allowedtemplates"][$child["template"]] and $child["privateweb"]==0 and (int)$showDoc==0) // pixelchutes update
{
$output .= "<url>\n";
$output .= "<loc>$url</loc>\n";
$output .= "<lastmod>$date</lastmod>\n";
$output .= "<changefreq>$update</changefreq>\n";
$output .= "<priority>$priority</priority>\n";
$output .= "</url>\n";
}
if ($funcEtomite->getActiveChildren($child['id']))
{
$output .= MakeMap_snippet($funcEtomite, $child['id'],$selfid,$settings);
}
}
}
return $output;
}
$MakeMapDefined = true;
}
$templatesok=array();
@$allowedtemplates.="";
$allowedtemplates=",".$allowedtemplates.",";
$allowedtemplates=preg_replace("/ *, */",",",$allowedtemplates);
$allowedtemplates=str_replace("'","",$allowedtemplates);
$sql="select * from ".$this->getFullTableName('site_templates')." where POSITION(CONCAT(',',templatename,',') IN '".$allowedtemplates."')>0";
$templates=$modx->db->query($sql);
echo $query;
while ($template=$modx->db->getRow($templates)) {
$templatesok[$template["id"]]=true;
}
$settings["allowedtemplates"]=$templatesok;
$out .= '<urlset xmlns="http://www.google.com/schemas/sitemap/0.84">'."\n";
$out .= MakeMap_snippet($etomite, 0,$selfid,$settings);
$out .= "</urlset>";
return $out;
?>


...get the mod on Page 1 of this thread
The Snippet appears to hide any any pages that are contained in Unpublished containers.
For example, in my site, its hiding all the blog items because these are displayed via Ditto.
Richard
Sweet, thanks. I installed and excluded my 404 and thankyou pages; seems to be working great for me.
I saw you added: xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/09/sitemap.xsd"
Probably a good idea.