blob: c8b6fd0a53b4406c94c1185c3498bccf0896e0ea [file] [log] [blame]
Rushabh Mehtaa494b882012-12-07 12:44:45 +05301# Copyright (c) 2012 Web Notes Technologies Pvt Ltd.
2# License: GNU General Public License (v3). For more information see license.txt
3
4from __future__ import unicode_literals
5frame_xml = """<?xml version="1.0" encoding="UTF-8"?>
6<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
7</urlset>"""
8
9link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>"""
10
11# generate the sitemap XML
12def generate(domain):
13 global frame_xml, link_xml
14 import urllib, os
15 import webnotes
Rushabh Mehta9db1a682013-04-02 10:41:37 +053016 import webnotes.webutils
Anand Doshi09480e22013-04-16 20:57:09 +053017 from webnotes.utils import nowdate
Rushabh Mehtaa494b882012-12-07 12:44:45 +053018
19 # settings
Rushabh Mehtaa494b882012-12-07 12:44:45 +053020 max_items = 1000
Anand Doshi09480e22013-04-16 20:57:09 +053021 count = 0
Rushabh Mehtaa494b882012-12-07 12:44:45 +053022
23 site_map = ''
Rushabh Mehtaa494b882012-12-07 12:44:45 +053024 if domain:
Anand Doshi09480e22013-04-16 20:57:09 +053025 today = nowdate()
26
27 # generated pages
28 for doctype, opts in webnotes.webutils.get_generators().items():
Rushabh Mehtaa494b882012-12-07 12:44:45 +053029 pages = webnotes.conn.sql("""select page_name, `modified`
30 from `tab%s` where ifnull(%s,0)=1
Anand Doshi09480e22013-04-16 20:57:09 +053031 order by modified desc""" % (doctype, opts.get("condition_field")))
Rushabh Mehtaa494b882012-12-07 12:44:45 +053032
33 for p in pages:
Anand Doshi09480e22013-04-16 20:57:09 +053034 if count >= max_items: break
Rushabh Mehta5f183982013-01-01 10:55:58 +053035 page_url = os.path.join(domain, urllib.quote(p[0]))
Rushabh Mehtaa494b882012-12-07 12:44:45 +053036 modified = p[1].strftime('%Y-%m-%d')
37 site_map += link_xml % (page_url, modified)
Anand Doshi09480e22013-04-16 20:57:09 +053038 count += 1
39
40 if count >= max_items: break
41
42 # standard pages
43 for page, opts in webnotes.get_config()["web"]["pages"].items():
44 if "no_cache" in opts:
45 continue
46
47 if count >= max_items: break
48 page_url = os.path.join(domain, urllib.quote(page))
49 modified = today
50 site_map += link_xml % (page_url, modified)
51 count += 1
Rushabh Mehtaa494b882012-12-07 12:44:45 +053052
Anand Doshi09480e22013-04-16 20:57:09 +053053 return frame_xml % site_map