Sitemap Generator

Last modified by Vincent Massol on 2021/03/18 11:28

mapGenerate a sitemap for your wiki
TypeSnippet
Category
Developed by

Jerome, Mark van den Broek

Rating
1 Votes
LicenseGNU Lesser General Public License 2.1

Description

These snippets allows you to generate automatically an XML Sitemap for your XWiki. You can easily define the wiki pages you want to appear in the site map, give them a priority and a change frequency. The first version of the snippet will offer you a link to download the sitemap on your computer (or display it in your browser), while the second will write it at the root directory of your XWiki webapp: /xwiki/sitemap.xml.
The third snippet generates a sitemap dynamically which is particularly useful for XWiki farms: in most use cases you'll need a separate sitemap for separate wiki's (especially if the wiki's use different domain names or have multiple aliases). In those cases, one sitemap.xml file (in the root directory) for the entire farm will not work, since you cannot mix and match different domains in one sitemap. Generating the sitemap on the fly (each time a search engines requests the sitemap for a specific domain) is the easiest way to make sure the sitemap contains the right URLs.

Code

1st version: Create a link to download your sitemap

Requires Programming Rights

{{groovy}}
if (xcontext.action == "get") {
  response.setContentType("text/xml")

  def sitePages = [
    // here you define the page you want in your sitemap
    // the order is: document name, priority, change frequency
    ["Main.WebHome","1","weekly"],
    ["About.Contact","0.8","monthly"],
    ["About.Team","0.5", "monthly"]
    // add more here...
  ]

  println('')
  println('<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">')

  for (page in sitePages) {
    println("<url>")
    println(" <loc>" + xwiki.getDocument(page[0]).getExternalURL() + "</loc>")
    println(" <lastmod>" + xwiki.formatDate(xwiki.getDocument(page[0]).contentUpdateDate, 'yyyy-MM-dd') + "</lastmod>")
    println(" <changefreq>" + page[2]+ "</changefreq>")
    println(" <priority>" + page[1] + "</priority>")
    println("</url>")
  }

  println("</urlset>")
} else {
  println("{{html}}<a href='"+ doc.getURL('get','outputSyntax=plain') +"'>Generate Sitemap</a>{{/html}}")
}
{{/groovy}}

2nd version: automatically write sitemap.xml in your web application folder

Content in XWiki Syntax 1.0. Wrap this code in a pre tag and groovy <% %> tag

if(request.gen && request.gen == "1") {
content = ""
def sitePages = [  
   // here you define the page you want in your sitemap
   // the order is: document name, priority, change frequency
  
    ["Main.WebHome","1","weekly"],
    ["About.Contact","0.8","monthly"],
    ["About.Team","0.5", "monthly"],
   // add more here...
 ];

content += ''
content += '<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">'

for(page in sitePages) {
 content += "<url>"
 content += " <loc>" + xwiki.getDocument(page[0]).getExternalURL() + "</loc>"
 content += " <lastmod>" + xwiki.formatDate(xwiki.getDocument(page[0]).contentUpdateDate, 'yyyy-MM-dd') + "</lastmod>"
 content += " <changefreq>" + page[2]+ "</changefreq>"
 content += " <priority>" + page[1] + "</priority>"
 content += "</url>"
 }

content += "</urlset>"
new File("../webapps/xwiki/sitemap.xml").withPrintWriter { w |
   w.println content
 }
println("Done: <a href='/xwiki/sitemap.xml'>sitemap.xml</a>")
}

else {

 println("<a href='"+ doc.getURL('view', "gen=1") +"'>Generate Sitemap</a>")

}

3rd version: let XWiki generate and serve up your sitemap dynamically (useful for farms!)

{{velocity}}
$response.setContentType('application/xml')
#set ($sitePages = [
  { 'page' : 'Main.WebHome', 'prio' : '1.0', 'update' : 'monthly' },
  { 'page' : 'About.Contact', 'prio' : '0.8', 'update' : 'monthly' },
  ## Add more here
  { 'page' : 'About.Team', 'prio' : '0.6', 'update' : 'monthly' }
])
#set ($sitemap = $response.getOutputStream())
$sitemap.print("")
$sitemap.print("<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'>")
#foreach($sitePage in $sitePages)
 $sitemap.print("<url>")
 $sitemap.print("  <loc>${escapetool.xml($xwiki.getDocument($sitePage.get('page')).getExternalURL())}</loc>")
 $sitemap.print("  <lastmod>${escapetool.xml($datetool.format('yyyy-MM-dd', $xwiki.getDocument($sitePage.get('page')).contentUpdateDate))}</lastmod>")
 $sitemap.print("  <changefreq>${escapetool.xml($sitePage.get('update'))}</changefreq>")
 $sitemap.print("  <priority>${escapetool.xml($sitePage.get('prio'))}</priority>")
 $sitemap.print("</url>")
#end
$sitemap.print("</urlset>")
$xcontext.setFinished(true)
{{/velocity}}

Result

Pasted in a document, the first snippet will give you download your wiki's sitemap. The second one will provide a link to automatically write a sitemap.xml file at the root directory of your XWiki web app. The third snippet will dynamically generate and serve up the sitemap (just point the search engine to the page with the script, either by the webmaster tools or in robots.txt).

     

Get Connected