Attachment 'trackback.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - "trackback" action
   4 .
   5 
   6     @copyright: 2004 by Jos Yule <jos@theorganization.net>
   7     @license: GNU GPL, see COPYING for details.
   8 """
   9 
  10 import sys
  11 import os
  12 from MoinMoin import config, util, wikiutil
  13 from MoinMoin.Page import Page
  14 from xml.dom.minidom import parse, Document
  15 from time import strftime
  16 
  17 
  18 def execute(pagename, request):
  19 
  20     if config.allow_trackback:
  21         try:
  22             resource = None  # so what is it?
  23             if os.environ["REQUEST_URI"].startswith(os.environ["SCRIPT_NAME"]):
  24                 if os.environ.has_key("PATH_INFO"):
  25                     resource = os.environ["PATH_INFO"]
  26                 else:
  27                     tbError("You didn't say what resource you wanted to ping.")
  28             else:
  29                 resource = os.environ["REQUEST_URI"]
  30             if resource.endswith("index.html"): resource = resource[:-10]
  31 
  32 
  33             # Load existing TrackBack content.
  34             resourceUri = "http://%s%s" % (os.environ["SERVER_NAME"], resource)
  35             tbUri = "http://%s%s%s" % (os.environ["SERVER_NAME"], os.environ["SCRIPT_NAME"], resource)
  36             dataFilename = "%s.xml" % resource[1:].replace(os.sep, "_").replace(".", "_")
  37             #dataFile = join(dirname(os.environ["SCRIPT_FILENAME"]), "data", dataFilename)
  38             dataFile = config.data_dir + '/trackback/' + dataFilename
  39 
  40             tb_data = None  # so what is it?
  41             try:
  42                 tb_data = parse(dataFile)
  43             except IOError:
  44                 # Make a new document.
  45                 tb_data = Document()
  46 
  47                 rss = tb_data.createElement("rss")
  48                 tb_data.appendChild(rss)
  49                 rss.setAttribute("version", "0.91")
  50 
  51                 channel = tb_data.createElement("channel")
  52                 rss.appendChild(channel)
  53 
  54                 chTitle = tb_data.createElement("title")
  55                 chTitle.appendChild(tb_data.createTextNode("TrackBack for %s" % (resourceUri)))
  56                 channel.appendChild(chTitle)
  57 
  58                 chLink = tb_data.createElement("link")
  59                 chLink.appendChild(tb_data.createTextNode(resourceUri))
  60                 channel.appendChild(chLink)
  61 
  62                 chDesc = tb_data.createElement("description")
  63                 chDesc.appendChild(tb_data.createTextNode("What some people have pung me with."))
  64                 channel.appendChild(chDesc)
  65 
  66                 chLang = tb_data.createElement("language")
  67                 chLang.appendChild(tb_data.createTextNode("en-us"))
  68                 channel.appendChild(chLang)
  69 
  70             tb_channel = tb_data.documentElement.firstChild
  71 
  72 
  73             # Pinging or fetching?
  74             #if "POST" == os.environ["REQUEST_METHOD"]:
  75             # Pinging.	
  76 
  77             ping = request.form #cgi.FieldStorage()
  78 
  79             # Add this ping.
  80             pingItem = tb_data.createElement("item")
  81 
  82             if not ping.has_key("url"):
  83                 raise KeyError, "You need to include the URL for this trackback"
  84 
  85             if ping.has_key("title"):
  86                 itemTitle = pingItem.appendChild(tb_data.createElement("title"))
  87                 itemTitle.appendChild(tb_data.createTextNode(ping["title"][0]))
  88             if ping.has_key("url"):
  89                 itemLink = pingItem.appendChild(tb_data.createElement("link"))
  90                 itemLink.appendChild(tb_data.createTextNode(ping["url"][0]))
  91             if ping.has_key("excerpt"):
  92                 excerpt = ping["excerpt"][0]
  93                 if 255 < len(excerpt):
  94                     excerpt = "%s..." % excerpt[:252]
  95                 itemDesc = pingItem.appendChild(tb_data.createElement("description"))
  96                 itemDesc.appendChild(tb_data.createTextNode(excerpt))
  97             if ping.has_key("blog_name"):
  98                 itemBN = pingItem.appendChild(tb_data.createElement("blogName"))
  99                 itemBN.appendChild(tb_data.createTextNode(ping["blog_name"][0]))
 100 
 101             itemDate = tb_data.createElementNS("http://purl.org/dc/elements/1.1/", "date")
 102             itemDate.appendChild(tb_data.createTextNode(strftime("%Y-%m-%dT%H:%MZ")))
 103             pingItem.appendChild(itemDate)
 104 
 105             tb_channel.appendChild(pingItem)
 106 
 107             # Write it back out.
 108             xmlout = open(dataFile, "w")
 109             xmlout.write(tb_data.toxml())
 110             xmlout.close()
 111 
 112             request.write( """Content-type: text/xml
 113 
 114 <?xml version="1.0"?>
 115 <response>
 116     <error>0</error>
 117 </response>""")
 118         except Exception, x:
 119            
 120             request.write("""Content-type: text/xml
 121 
 122 <?xml version="1.0"?>
 123 <response>
 124     <error>1</error>
 125     <message>Error: %s</message>
 126 </response>""" % (x,))
 127     else:
 128         request.write("""Content-type: text/xml
 129 
 130 <?xml version="1.0"?>
 131 <response>
 132     <error>1</error>
 133     <message>Error: Trackback disabled</message>
 134 </response>""" )
 135 
 136     request.no_closing_html_code = 1
 137     return

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2004-06-16 03:18:22, 2.1 KB) [[attachment:ViewTrackbacks.py]]
  • [get | view] (2004-06-19 23:54:48, 1.4 KB) [[attachment:blogpost.py]]
  • [get | view] (2004-06-28 00:47:23, 0.5 KB) [[attachment:footnotediff.txt]]
  • [get | view] (2004-06-19 23:45:08, 1.3 KB) [[attachment:form.py.diff]]
  • [get | view] (2004-06-28 00:47:09, 7.9 KB) [[attachment:formatInMemory.py]]
  • [get | view] (2004-06-16 03:18:15, 4.9 KB) [[attachment:trackback.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.