How to do backlinks in ZOPE =========================== It may not be the nicest code ever, but here is something I wrote to show backwards links from articles published using Zope. If you get it working (or not :-D) and can think of improvements, let me know... BTW, the whole thing is inspired by backlinks code at http://www.downes.ca/referrers.htm and at http://www.disenchanted.com/dis/linkback.html. I couldn't find anything similar for Zope, hence this lot! Enjoy! - Scott =========================== Step 1: Create the Referrer zClass ----------------------------------- This is a little zClass for holding the referral data. Just create a blank zClass, call it Referrer, and add a propertysheet called 'common' with three attributes (referrer, title, count). Count should be an int with default value of 1, the others are strings. Step 2: Create the referrer script to be called from your index_html method ------------------------------------------------------------------------ --- This takes a look at the Request object, and decides whether or not to create a Referrer. Note that it does this inside the context it is being called from, so your calling object must be an Object Manager! Note that you need to change the Product name here too. Call this Python script from the standard "view" method of the object (usually index-_html): ################## ## getReferrals ## ################## from Products.PythonScripts.standard import html_quote import string request = container.REQUEST RESPONSE = request.RESPONSE # # Get referrer # sReferrer = str(request.get_header('HTTP_REFERER')) # # check against the kill list. Definite room for improvement here # if sReferrer <> '' and string.count(sReferrer,'YOUR-DOMAIN-HERE')==0 and string.count(sReferrer,'frontier.userland')==0 and string.count(sReferrer,'google')==0 and sReferrer <> 'None': # # make the referral, or update count for existing referrals # bExists=0 for oReferrer in context.objectValues('Referrer'): if oReferrer.referrer == sReferrer: oReferrer.propertysheets.common.manage_changeProperties(count = oReferrer.count+1) bExists=1 if bExists==0: id = str(context.ZopeTime().strftime('%Y%m%d%H%M%S')) oNewReferrer = context.manage_addProduct['YOUR-PRODUCT-NAME-HERE'].Referrer.createInObj ectManager(id,request) oNewReferrer.propertysheets.common.manage_changeProperties(referrer = sReferrer) ######### ## end ## ######### Step 3: Create a Robot that substitutes real page titles for the URLs --------------------------------------------------------------------- Add this external method to your Zope installation, and call it periodically on all your new Referrer instances (using cron, Scheduler or whatever). NB, you may want to set Python's Proxy values before doing this. This script fetches the URL from each referrer, and extracts any Title tags. Put it into the Extensions folder. ##################### ## getPageTitle.py ## ##################### ## Fetches the URL provided, and returns the title of the page ## If the page is unavailable, or untitled, ## then returns the URL as the title ## import urllib import re import string def getPageTitle(sUrl): try: oWeb = urllib.urlopen(sUrl).read() try: oRe = re.compile("<title>(.*?)</title>",re.I|re.S) oMatch = oRe.search(oWeb) if oMatch <> None: sTitle = string.strip(oMatch.group(1)) else: sTitle = sUrl except: sTitle = sUrl if sTitle == '' or sTitle=='None': sTitle = sUrl except: sTitle = sUrl return sTitle ######### ## end ## ######### On my site the updater script I use to call the external method looks like: for oItem in context.content.objectValues('ContentItem'): for oReferrer in oItem.objectValues('Referrer'): sTitle = context.getPageTitle(oReferrer.referrer) oReferrer.propertysheets.common.manage_changeProperties(title = sTitle) (Where "getPageTitle" is the name of the external method.) ============= Scott Wilson Assistant Director Centre For Educational Technology Interoperability Standards (CETIS) +44 (0)1248 383645 web: http://www.cetis.ac.uk newsfeed: http://www.cetis.ac.uk/news.xml