From version 28.2
To version < 28.3
edited by Vincent Massol
on 2017/08/03 11:00
Change comment: Added some warnings

Summary

Details

Page properties
Parent
... ... @@ -1,0 +1,1 @@
1 +Extension.WebHome
Author
... ... @@ -1,1 +1,1 @@
1 -XWiki.XWikiGuest
1 +xwiki:XWiki.VincentMassol
Tags
... ... @@ -1,0 +1,1 @@
1 +administration|SEO|xml
Content
EXOExtensionCode.ExtensionClass[0]
ExtensionCode.ExtensionClass[0]
Display Icon Location
... ... @@ -1,0 +1,1 @@
1 +icon:map
Description
... ... @@ -1,0 +1,121 @@
1 +These snippets allows you to generate automatically an [[XML Sitemap>>https://www.google.com/webmasters/tools/docs/en/protocol.html]] 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##.
2 +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.
3 +
4 +== Code ==
5 +
6 +=== 1st version: Create a link to download your sitemap ===
7 +
8 +
9 +{{warning}}
10 +Requires Programming Rights
11 +{{/warning}}
12 +
13 +
14 +{{code language="none"}}
15 +{{groovy}}
16 +if (xcontext.action == "get") {
17 + response.setContentType("text/xml")
18 +
19 + def sitePages = [
20 + // here you define the page you want in your sitemap
21 + // the order is: document name, priority, change frequency
22 + ["Main.WebHome","1","weekly"],
23 + ["About.Contact","0.8","monthly"],
24 + ["About.Team","0.5", "monthly"]
25 + // add more here...
26 + ]
27 +
28 + println('')
29 + 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">')
30 +
31 + for (page in sitePages) {
32 + println("<url>")
33 + println(" <loc>" + xwiki.getDocument(page[0]).getExternalURL() + "</loc>")
34 + println(" <lastmod>" + xwiki.formatDate(xwiki.getDocument(page[0]).contentUpdateDate, 'yyyy-MM-dd') + "</lastmod>")
35 + println(" <changefreq>" + page[2]+ "</changefreq>")
36 + println(" <priority>" + page[1] + "</priority>")
37 + println("</url>")
38 + }
39 +
40 + println("</urlset>")
41 +} else {
42 + println("{{html}}<a href='"+ doc.getURL('get','outputSyntax=plain') +"'>Generate Sitemap</a>{{/html}}")
43 +}
44 +{{/groovy}}
45 +{{/code}}
46 +
47 +=== 2nd version: automatically write sitemap.xml in your web application folder ===
48 +
49 +{{warning}}
50 +Content in XWiki Syntax 1.0. Wrap this code in a pre tag and groovy <% %> tag
51 +{{/warning}}
52 +
53 +{{code}}
54 +if(request.gen && request.gen == "1") {
55 + content = ""
56 + def sitePages = [
57 + // here you define the page you want in your sitemap
58 + // the order is: document name, priority, change frequency
59 +
60 + ["Main.WebHome","1","weekly"],
61 + ["About.Contact","0.8","monthly"],
62 + ["About.Team","0.5", "monthly"],
63 + // add more here...
64 + ];
65 +
66 + content += ''
67 + 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">'
68 +
69 + for(page in sitePages) {
70 + content += "<url>"
71 + content += " <loc>" + xwiki.getDocument(page[0]).getExternalURL() + "</loc>"
72 + content += " <lastmod>" + xwiki.formatDate(xwiki.getDocument(page[0]).contentUpdateDate, 'yyyy-MM-dd') + "</lastmod>"
73 + content += " <changefreq>" + page[2]+ "</changefreq>"
74 + content += " <priority>" + page[1] + "</priority>"
75 + content += "</url>"
76 + }
77 +
78 + content += "</urlset>"
79 + new File("../webapps/xwiki/sitemap.xml").withPrintWriter { w |
80 + w.println content
81 + }
82 + println("Done: <a href='/xwiki/sitemap.xml'>sitemap.xml</a>")
83 +}
84 +
85 +else {
86 +
87 + println("<a href='"+ doc.getURL('view', "gen=1") +"'>Generate Sitemap</a>")
88 +
89 +}
90 +{{/code}}
91 +
92 +=== 3rd version: let XWiki generate and serve up your sitemap dynamically (useful for farms!) ===
93 +
94 +{{code}}
95 +{{velocity}}
96 +$response.setContentType('application/xml')
97 +#set ($sitePages = [
98 + { 'page' : 'Main.WebHome', 'prio' : '1.0', 'update' : 'monthly' },
99 + { 'page' : 'About.Contact', 'prio' : '0.8', 'update' : 'monthly' },
100 + ## Add more here
101 + { 'page' : 'About.Team', 'prio' : '0.6', 'update' : 'monthly' }
102 +])
103 +#set ($sitemap = $response.getOutputStream())
104 +$sitemap.print("")
105 +$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'>")
106 +#foreach($sitePage in $sitePages)
107 + $sitemap.print("<url>")
108 + $sitemap.print(" <loc>${escapetool.xml($xwiki.getDocument($sitePage.get('page')).getExternalURL())}</loc>")
109 + $sitemap.print(" <lastmod>${escapetool.xml($datetool.format('yyyy-MM-dd', $xwiki.getDocument($sitePage.get('page')).contentUpdateDate))}</lastmod>")
110 + $sitemap.print(" <changefreq>${escapetool.xml($sitePage.get('update'))}</changefreq>")
111 + $sitemap.print(" <priority>${escapetool.xml($sitePage.get('prio'))}</priority>")
112 + $sitemap.print("</url>")
113 +#end
114 +$sitemap.print("</urlset>")
115 +$xcontext.setFinished(true)
116 +{{/velocity}}
117 +{{/code}}
118 +
119 +== Result ==
120 +
121 +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).
Type
... ... @@ -1,0 +1,1 @@
1 +snippet
Only Custom Installation instructions
... ... @@ -1,0 +1,1 @@
1 +No
Summary
... ... @@ -1,0 +1,1 @@
1 +Generate a sitemap for your wiki
License Name
... ... @@ -1,0 +1,1 @@
1 +GNU Lesser General Public License 2.1
license
... ... @@ -1,0 +1,1 @@
1 +LGPL
Name
... ... @@ -1,0 +1,1 @@
1 +Sitemap Generator
contributors
... ... @@ -1,0 +1,1 @@
1 +Jerome Velociter
Authors
... ... @@ -1,0 +1,1 @@
1 +xwiki:XWiki.jvelociter xwiki:XWiki.MarkvandenBroek
supported
... ... @@ -1,0 +1,1 @@
1 +0
XWiki.AverageRatingsClass[0]
Average Vote method
... ... @@ -1,0 +1,1 @@
1 +average
Average Vote
... ... @@ -1,0 +1,1 @@
1 +5.0
Number of Votes
... ... @@ -1,0 +1,1 @@
1 +1

null

mapGenerate a sitemap for your wiki
Developed by

Jerome, Mark van den Broek

Rating
Rate!
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('')

  for (page in sitePages) {
    println("")
    println(" " + xwiki.getDocument(page[0]).getExternalURL() + "")
    println(" " + xwiki.formatDate(xwiki.getDocument(page[0]).contentUpdateDate, 'yyyy-MM-dd') + "")
    println(" " + page[2]+ "")
    println(" " + page[1] + "")
    println("
")
  }

  println("
")
} else {
  println("{{html}}Generate Sitemap{{/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 += ''

for(page in sitePages) {
 content += ""
 content += " " + xwiki.getDocument(page[0]).getExternalURL() + ""
 content += " " + xwiki.formatDate(xwiki.getDocument(page[0]).contentUpdateDate, 'yyyy-MM-dd') + ""
 content += " " + page[2]+ ""
 content += " " + page[1] + ""
 content += ""
 }

content += ""
new File("../webapps/xwiki/sitemap.xml").withPrintWriter { w |
   w.println content
 }
println("Done: sitemap.xml")
}

else {

 println("view', "gen=1") +"'>Generate Sitemap")

}

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(" 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("")
 $sitemap.print("  ${escapetool.xml($xwiki.getDocument($sitePage.get('page')).getExternalURL())}")
 $sitemap.print("  ${escapetool.xml($datetool.format('yyyy-MM-dd', $xwiki.getDocument($sitePage.get('page')).contentUpdateDate))}")
 $sitemap.print("  ${escapetool.xml($sitePage.get('update'))}")
 $sitemap.print("  ${escapetool.xml($sitePage.get('prio'))}")
 $sitemap.print("")
#end
$sitemap.print("")
$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).

Prerequisites & Installation Instructions

Copy the code snippet to a page and save it.

Get Connected