1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227
| <?php
# $startid [ int ]
# Id of the 'root' document from which the sitemap
# starts.
# Default: 0
$startid = (isset($startid)) ? $startid : 0;
# $format [ sp | txt | ror ]
# Which format of sitemap to use:
# - sp <- Sitemap Protocol used by Google
# - txt <- text file with list of URLs
# TODO - ror <- Resource Of Resources
# Default: sp
$format = (isset($format) && ($format != 'ror')) ? $format : 'sp';
# $priority [ str ]
# Name of TV which sets the relative priority of
# the document. If there is no such TV, this
# parameter will not be used.
# Default: sitemap_priority
$priority = (isset($priority)) ? $priority : 'sitemap_priority';
# $changefreq [ str ]
# Name of TV which sets the change frequency. If
# there is no such TV this parameter will not be
# used.
# Default: sitemap_changefreq
$changefreq = (isset($changefreq)) ? $changefreq : 'sitemap_changefreq';
# $excludeTemplates [ str ]
# Documents based on which templates should not be
# included in the sitemap. Comma separated list
# with names of templates.
# Default: empty
$excludeTemplates = (isset($excludeTemplates)) ? $excludeTemplates : array();
# $excludeTV [ str ]
# Name of TV (boolean type) which sets document
# exclusion form sitemap. If there is no such TV
# this parameter will not be used.
# Default: 'sitemap_exclude'
$excludeTV = (isset($excludeTV)) ? $excludeTV : 'sitemap_exclude';
# $xsl [ str ]
# URL to the XSL style sheet
# or
# $xsl [ int ]
# doc ID of the XSL style sheet
$xsl = (isset($xsl)) ? $xsl : '';
if (is_numeric($xsl)) { $xsl = $modx->makeUrl($xsl); }
# $excludeWeblinks [ bool ]
# Should weblinks be excluded?
# You may not want to include links to external sites in your sitemap,
# and Google gives warnings about multiple redirects to pages
# within your site.
# Default: false
$excludeWeblinks = (isset($excludeWeblinks)) ? $excludeWeblinks : false;
/* End parameters
----------------------------------------------- */
# get list of documents
# ---------------------------------------------
$docs = getDocs ($modx,$startid,$priority,$changefreq,$excludeTV);
# filter out documents by template or TV
# ---------------------------------------------
// get all templates
$select = $modx->db->select("id, templatename", $modx->getFullTableName('site_templates'));
while ($query = $modx->db->getRow($select)) {
$allTemplates[$query['id']] = $query['templatename'];
}
$remainingTemplates = $allTemplates;
// get templates to exclude, and remove them from the all templates list
if (!empty ($excludeTemplates)) {
$excludeTemplates = explode(",", $excludeTemplates);
// Loop through each template we want to exclude
foreach ($excludeTemplates as $template) {
$template = trim($template);
// If it's numeric, assume it's an ID, and remove directly from the $allTemplates array
if (is_numeric($template) && isset($remainingTemplates[$template])) {
unset($remainingTemplates[$template]);
} else if (trim($template) && in_array($template, $remainingTemplates)) { // If it's text, and not empty, assume it's a template name
unset($remainingTemplates[array_search($template, $remainingTemplates)]);
}
} // end foreach
}
$output= array();
// filter out documents which shouldn't be included
foreach ($docs as $doc)
{
if (isset($remainingTemplates[$doc['template']]) && !$doc[$excludeTV] && $doc['published'] && $doc['template']!=0 && $doc['searchable']) {
if (!$excludeWeblinks || ($excludeWeblinks && $doc['type'] != 'reference')) {
$output[] = $doc;
}
}
}
$docs = $output;
unset ($output, $allTemplates, $excludeTemplates);
# build sitemap in specified format
# ---------------------------------------------
switch ($format)
{
// Next case added in version 1.0.4
case 'ulli': // UL List
$output .= "<ul class="sitemap ">\n";
// TODO: Sort the array on Menu Index
// TODO: Make a nested ul-li based on the levels in the document tree.
foreach ($docs as $doc)
{
$s = " <li class="sitemap ">";
$s .= "<a href="[(site_url )][~ " . $doc['id'] . "~ ]" class="sitemap ">" . $doc['pagetitle'] . "</a>";
$s .= "</li>\n";
$output .= $s;
} // end foreach
$output .= "</ul>\n";
break;
case 'txt': // plain text list of URLs
foreach ($docs as $doc)
{
$url = '[(site_url)][~'.$doc['id'].'~]';
$output .= $url."\n";
} // end foreach
break;
case 'ror': // TODO
default: // Sitemap Protocol
$output = '<?xml version="1.0" encoding="UTF-8"?>'."\n";
if ($xsl != '') {
$output .='<?xml-stylesheet type="text/xsl" href="'.$xsl.'"?>'."\n";
}
$output .='<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'."\n";
foreach ($docs as $doc) {
$url = '[(site_url)][~'.$doc['id'].'~]';
$date = $doc['editedon'];
$date = date("Y-m-d", $date);
$docPriority = ($doc[$priority]) ? $doc[$priority] : 0; // false if TV doesn't exist
$docChangefreq = ($doc[$changefreq]) ? $doc[$changefreq] : 0; // false if TV doesn't exist
$output .= "\t".'<url>'."\n";
$output .= "\t\t".'<loc>'.$url.'</loc>'."\n";
$output .= "\t\t".'<lastmod>'.$date.'</lastmod>'."\n";
$output .= ($docPriority) ? ("\t\t".'<priority>'.$docPriority.'</priority>'."\n") : ''; // don't output anything if TV doesn't exist
$output .= ($docChangefreq) ? ("\t\t".'<changefreq>'.$docChangefreq.'</changefreq>'."\n") : ''; // don't output anything if TV doesn't exist
$output .= "\t".'</url>'."\n";
} // end foreach
$output .= '</urlset>';
} // end switch
return $output;
# functions
# ---------------------------------------------
# gets (inherited) value of template variable
function getTV ($modx,$docid,$doctv)
{
/* apparently in 0.9.2.1 the getTemplateVarOutput function doesn't work as expected and doesn't return INHERITED value; this is probably to be fixed for next release; see http://modxcms.com/bugs/task/464
$output = $modx->getTemplateVarOutput($tv,$docid);
return $output[$tv];
*/
while ($pid = $modx->getDocument($docid,'parent'))
{
$tv = $modx->getTemplateVar($doctv,'*',$docid);
if (($tv['value'] && substr($tv['value'],0,8) != '@INHERIT') or !$tv['value']) // tv default value is overriden (including empty)
{
$output = $tv['value'];
break;
}
else // there is no parent with default value overriden
{
$output = trim(substr($tv['value'],8));
}
$docid = $pid['parent']; // move up one document in document tree
} // end while
return $output;
}
# gets list of published documents with properties
function getDocs ($modx,$startid,$priority,$changefreq,$excludeTV)
{
// get children documents
$docs = $modx->getActiveChildren($startid,'menuindex','asc','id,editedon,template,published,searchable,pagetitle,type');
// add sub-children to the list
foreach ($docs as $key => $doc)
{
$id = $doc['id'];
$docs[$key][$priority] = getTV ($modx,$id,$priority); // add priority property
$docs[$key][$changefreq] = getTV ($modx,$id,$changefreq); // add changefreq property
$docs[$key][$excludeTV] = getTV ($modx,$id,$excludeTV); // add excludeTV property
if ($modx->getActiveChildren($id))
$docs = array_merge($docs, getDocs ($modx,$id,$priority,$changefreq,$excludeTV));
} // end foreach
return $docs;
}
?> |