@@ -17,7 +17,7 @@ | |||||
# along with this program. If not, see <http://www.gnu.org/licenses/>. | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
import cgi, cgitb, os, sys | |||||
import cgi, cgitb, sys | |||||
cgitb.enable() | cgitb.enable() | ||||
# import libs | # import libs | ||||
@@ -28,12 +28,10 @@ sys.path.append('../app') | |||||
import webnotes | import webnotes | ||||
import webnotes.auth | import webnotes.auth | ||||
import conf | |||||
if __name__=='__main__': | if __name__=='__main__': | ||||
# webnotes.http_request = webnotes.auth.HTTPRequest() | # webnotes.http_request = webnotes.auth.HTTPRequest() | ||||
webnotes.connect() | webnotes.connect() | ||||
from website.helpers import blog_feed | |||||
from website.doctype.blog_post import blog_feed | |||||
try: | try: | ||||
print 'Content-Type: text/xml' | print 'Content-Type: text/xml' | ||||
@@ -31,7 +31,7 @@ if __name__=='__main__': | |||||
webnotes.http_request = webnotes.auth.HTTPRequest() | webnotes.http_request = webnotes.auth.HTTPRequest() | ||||
domain = os.environ.get('HTTP_HOST') | domain = os.environ.get('HTTP_HOST') | ||||
protocol = os.environ.get('HTTPS') and 'https://' or 'http://' | protocol = os.environ.get('HTTPS') and 'https://' or 'http://' | ||||
from website.helpers import sitemap | |||||
import website.sitemap | |||||
print 'Content-Type: text/xml' | print 'Content-Type: text/xml' | ||||
print sitemap.generate(protocol + domain + '/') | |||||
print website.sitemap.generate(protocol + domain + '/') |
@@ -58,17 +58,21 @@ def build_page(page_name): | |||||
if not webnotes.conn: | if not webnotes.conn: | ||||
webnotes.connect() | webnotes.connect() | ||||
sitemap = webnotes.cache().get_value("website_sitemap", build_sitemap) | |||||
sitemap = get_website_sitemap() | |||||
page_options = sitemap.get(page_name) | page_options = sitemap.get(page_name) | ||||
if not page_options: | if not page_options: | ||||
if page_name=="index": | if page_name=="index": | ||||
# page not found, try home page | # page not found, try home page | ||||
page_options = sitemap.get(get_home_page()) | |||||
home_page = get_home_page() | |||||
page_options = sitemap.get(home_page) | |||||
if not page_options: | if not page_options: | ||||
raise PageNotFoundError | raise PageNotFoundError | ||||
page_options["page_name"] = home_page | |||||
else: | else: | ||||
raise PageNotFoundError | raise PageNotFoundError | ||||
else: | |||||
page_options["page_name"] = page_name | |||||
basepath = webnotes.utils.get_base_path() | basepath = webnotes.utils.get_base_path() | ||||
module = None | module = None | ||||
@@ -82,8 +86,8 @@ def build_page(page_name): | |||||
if page_options.get("is_generator"): | if page_options.get("is_generator"): | ||||
if not module: | if not module: | ||||
raise Exception("Generator controller not defined") | raise Exception("Generator controller not defined") | ||||
name = webnotes.conn.get_value(module.doctype, {"page_name": page_name}) | |||||
name = webnotes.conn.get_value(module.doctype, {"page_name": page_options["page_name"]}) | |||||
obj = webnotes.get_obj(module.doctype, name, with_children=True) | obj = webnotes.get_obj(module.doctype, name, with_children=True) | ||||
if hasattr(obj, 'get_context'): | if hasattr(obj, 'get_context'): | ||||
@@ -237,9 +241,12 @@ def clear_cache(page_name=None): | |||||
cache.delete_value("page:" + p) | cache.delete_value("page:" + p) | ||||
cache.delete_value("website_sitemap") | cache.delete_value("website_sitemap") | ||||
cache.delete_value("website_sitemap_config") | cache.delete_value("website_sitemap_config") | ||||
def get_website_sitemap(): | |||||
return webnotes.cache().get_value("website_sitemap", build_sitemap) | |||||
def get_all_pages(): | def get_all_pages(): | ||||
return webnotes.cache().get_value("website_sitemap", build_sitemap).keys() | |||||
return get_website_sitemap().keys() | |||||
def delete_page_cache(page_name): | def delete_page_cache(page_name): | ||||
if page_name: | if page_name: | ||||
@@ -2,54 +2,26 @@ | |||||
# MIT License. See license.txt | # MIT License. See license.txt | ||||
from __future__ import unicode_literals | from __future__ import unicode_literals | ||||
import urllib | |||||
import webnotes | |||||
import webnotes.webutils | |||||
from webnotes.utils import nowdate | |||||
frame_xml = """<?xml version="1.0" encoding="UTF-8"?> | |||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s | |||||
</urlset>""" | |||||
link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>""" | |||||
# generate the sitemap XML | |||||
def generate(domain): | def generate(domain): | ||||
global frame_xml, link_xml | |||||
import urllib, os | |||||
import webnotes | |||||
import webnotes.webutils | |||||
from webnotes.utils import nowdate | |||||
"""generate the sitemap XML""" | |||||
# settings | |||||
max_items = 1000 | |||||
count = 0 | |||||
frame_xml = """<?xml version="1.0" encoding="UTF-8"?> | |||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">%s | |||||
</urlset>""" | |||||
link_xml = """\n<url><loc>%s</loc><lastmod>%s</lastmod></url>""" | |||||
site_map = '' | |||||
site_map = "" | |||||
if domain: | if domain: | ||||
today = nowdate() | today = nowdate() | ||||
# generated pages | |||||
for doctype, opts in webnotes.webutils.get_generators().items(): | |||||
pages = webnotes.conn.sql("""select page_name, `modified` | |||||
from `tab%s` where ifnull(%s,0)=1 | |||||
order by modified desc""" % (doctype, opts.get("condition_field"))) | |||||
for p in pages: | |||||
if count >= max_items: break | |||||
if p[0]: | |||||
page_url = os.path.join(domain, urllib.quote(p[0])) | |||||
modified = p[1].strftime('%Y-%m-%d') | |||||
site_map += link_xml % (page_url, modified) | |||||
count += 1 | |||||
if count >= max_items: break | |||||
# standard pages | |||||
for page, opts in webnotes.get_config()["web"]["pages"].items(): | |||||
if "no_cache" in opts: | |||||
continue | |||||
if count >= max_items: break | |||||
page_url = os.path.join(domain, urllib.quote(page)) | |||||
modified = today | |||||
site_map += link_xml % (page_url, modified) | |||||
count += 1 | |||||
for page_name, page_options in webnotes.webutils.get_website_sitemap().items(): | |||||
url = urllib.basejoin(domain, urllib.quote(page_name)) | |||||
site_map += link_xml % (url, today) | |||||
return frame_xml % site_map | return frame_xml % site_map |