Back to index

moin  1.9.0~rc2
pollsistersites.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - "pollsistersites" action
00004 
00005     This action fetches lists of page urls and page names from sister sites,
00006     so we can implement SisterWiki functionality.
00007     See: http://usemod.com/cgi-bin/mb.pl?SisterSitesImplementationGuide
00008 
00009     @copyright: 2007 MoinMoin:ThomasWaldmann
00010     @license: GNU GPL, see COPYING for details.
00011 """
00012 
00013 import time, urllib
00014 
00015 from MoinMoin import caching
00016 from MoinMoin.util import timefuncs
00017 
00018 def execute(pagename, request):
00019     status = []
00020     for sistername, sisterurl in request.cfg.sistersites:
00021         arena = 'sisters'
00022         key = sistername
00023         cache = caching.CacheEntry(request, arena, key, scope='farm', use_pickle=True)
00024         if cache.exists():
00025             data = cache.content()
00026         else:
00027             data = {'lastmod': ''}
00028         uo = urllib.URLopener()
00029         uo.version = 'MoinMoin SisterPage list fetcher 1.0'
00030         lastmod = data['lastmod']
00031         if lastmod:
00032             uo.addheader('If-Modified-Since', lastmod)
00033         try:
00034             sisterpages = {}
00035             f = uo.open(sisterurl)
00036             for line in f:
00037                 line = line.strip()
00038                 try:
00039                     page_url, page_name = line.split(' ', 1)
00040                     sisterpages[page_name.decode('utf-8')] = page_url
00041                 except:
00042                     pass # ignore invalid lines
00043             try:
00044                 lastmod = f.info()["Last-Modified"]
00045             except:
00046                 lastmod = timefuncs.formathttpdate(time.time())
00047             f.close()
00048             data['lastmod'] = lastmod
00049             data['sisterpages'] = sisterpages
00050             cache.update(data)
00051             status.append(u"Site: %s Status: Updated. Pages: %d" % (sistername, len(sisterpages)))
00052         except IOError, (title, code, msg, headers): # code e.g. 304
00053             status.append(u"Site: %s Status: Not updated." % sistername)
00054         except TypeError: # catch bug in python 2.5: "EnvironmentError expected at most 3 arguments, got 4"
00055             status.append(u"Site: %s Status: Not updated." % sistername)
00056 
00057     request.mimetype = 'text/plain'
00058     request.write("\r\n".join(status).encode("utf-8"))