blob: 3956da1a709bdb825262fc0f0a7f7ee7b9dcd965 [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
Anand Doshi488a8212013-05-22 15:51:47 +05305
Rushabh Mehtaa494b882012-12-07 12:44:45 +05306frame_xml = """<?xml version="1.0" encoding="UTF-8"?>
7<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s
8</urlset>"""
9
10link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>"""
11
12# generate the sitemap XML
13def generate(domain):
14 global frame_xml, link_xml
15 import urllib, os
16 import webnotes
Rushabh Mehta9db1a682013-04-02 10:41:37 +053017 import webnotes.webutils
Anand Doshi09480e22013-04-16 20:57:09 +053018 from webnotes.utils import nowdate
Rushabh Mehtaa494b882012-12-07 12:44:45 +053019
20 # settings
Rushabh Mehtaa494b882012-12-07 12:44:45 +053021 max_items = 1000
Anand Doshi09480e22013-04-16 20:57:09 +053022 count = 0
Rushabh Mehtaa494b882012-12-07 12:44:45 +053023
24 site_map = ''
Rushabh Mehtaa494b882012-12-07 12:44:45 +053025 if domain:
Anand Doshi09480e22013-04-16 20:57:09 +053026 today = nowdate()
27
28 # generated pages
29 for doctype, opts in webnotes.webutils.get_generators().items():
Rushabh Mehtaa494b882012-12-07 12:44:45 +053030 pages = webnotes.conn.sql("""select page_name, `modified`
31 from `tab%s` where ifnull(%s,0)=1
Anand Doshi09480e22013-04-16 20:57:09 +053032 order by modified desc""" % (doctype, opts.get("condition_field")))
Rushabh Mehtaa494b882012-12-07 12:44:45 +053033
34 for p in pages:
Anand Doshi09480e22013-04-16 20:57:09 +053035 if count >= max_items: break
Anand Doshi488a8212013-05-22 15:51:47 +053036 if p[0]:
37 page_url = os.path.join(domain, urllib.quote(p[0]))
38 modified = p[1].strftime('%Y-%m-%d')
39 site_map += link_xml % (page_url, modified)
40 count += 1
Anand Doshi09480e22013-04-16 20:57:09 +053041
42 if count >= max_items: break
43
44 # standard pages
45 for page, opts in webnotes.get_config()["web"]["pages"].items():
46 if "no_cache" in opts:
47 continue
48
49 if count >= max_items: break
50 page_url = os.path.join(domain, urllib.quote(page))
51 modified = today
52 site_map += link_xml % (page_url, modified)
53 count += 1
Rushabh Mehtaa494b882012-12-07 12:44:45 +053054
Anand Doshi09480e22013-04-16 20:57:09 +053055 return frame_xml % site_map