1 # -*- coding: iso-8859-1 -*- 2 """ 3 MoinMoin - SharedUpdates Action 4 5 @copyright: 2012, 2013 by Paul Boddie <paul@boddie.org.uk> 6 @license: GNU GPL (v2 or later), see COPYING.txt for details. 7 """ 8 9 from MoinMoin.action import ActionBase 10 from MoinMoin.Page import Page 11 from MoinMoin import wikiutil 12 from MoinShare import getUpdatedTime, getUpdatesFromPage 13 from MoinSupport import escattr, get_form, getMetadata, getPathInfo, \ 14 writeHeaders, ActionSupport 15 16 escape = wikiutil.escape 17 18 Dependencies = ['pages'] 19 20 # Action class and supporting functions. 21 22 class SharedUpdates(ActionBase, ActionSupport): 23 24 "An update manifest retrieval dialogue requesting various parameters." 25 26 def get_form_html(self, buttons_html): 27 _ = self._ 28 request = self.request 29 form = self.get_form() 30 page = self.page 31 32 pagename = form.get("pagename", [page.page_name])[0] 33 34 d = { 35 "buttons_html" : buttons_html, 36 "pagename_label" : escape(_("Page name for updates")), 37 "pagename" : escattr(pagename), 38 } 39 40 return ''' 41 <table> 42 <tr> 43 <td class="label"><label>%(pagename_label)s</label></td> 44 <td class="content"> 45 <input name="pagename" type="text" size="40" value="%(pagename)s" /> 46 </td> 47 </tr> 48 <tr> 49 <td></td> 50 <td class="buttons"> 51 %(buttons_html)s 52 </td> 53 </tr> 54 </table> 55 ''' % d 56 57 def do_action(self): 58 59 "Write the syndication resource." 60 61 write_resource(self.request) 62 return 1, None 63 64 def render_success(self, msg, msgtype=None): 65 66 """ 67 Render neither 'msg' nor 'msgtype' since a resource has already been 68 produced. 69 NOTE: msgtype is optional because MoinMoin 1.5.x does not support it. 70 """ 71 72 pass 73 74 def write_resource(request): 75 76 """ 77 For the given 'request', write an Atom summary of updates found on the 78 specified page. 79 See: http://tools.ietf.org/html/rfc4287 80 """ 81 82 form = get_form(request) 83 84 pagename = form.get("pagename", [request.page.page_name])[0] 85 86 page = Page(request, pagename) 87 metadata = getMetadata(page) 88 89 # Output summary data... 90 91 writeHeaders(request, "application/atom+xml", metadata) 92 updated = getUpdatedTime(metadata) 93 94 # Atom output... 95 96 # Using the page name and the page URL in the title, link and 97 # subtitle. 98 99 path_info = getPathInfo(request) 100 link = "%s%s" % (request.getBaseURL(), path_info) 101 102 d = { 103 "title" : escape(path_info[1:]), 104 "link" : escape(link), 105 "href" : escattr(link), 106 "updated" : escape(updated.as_ISO8601_datetime_string()), 107 } 108 109 # Start the output. 110 111 request.write('''\ 112 <?xml version="1.0" encoding="utf-8"?>\r 113 <feed xmlns="http://www.w3.org/2005/Atom">\r 114 <title type="text">%(title)s</title>\r 115 <subtitle type="text">Shared updates published on %(link)s</subtitle>\r 116 <link rel="self" href="%(href)s"/>\r 117 <updated>%(updated)s</updated>\r 118 ''' % d) 119 120 for update in getUpdatesFromPage(page, request): 121 122 # Get the URL that yields only the fragment. 123 124 i = update.link.rfind("#") 125 if i != -1: 126 link_without_fragment = update.link[:i] 127 else: 128 link_without_fragment = update.link 129 130 fragment_link = "%s?action=SharedUpdate&fragment=%s" % (link_without_fragment, update.fragment) 131 132 download_links = [] 133 134 for mimetype in update.preferred: 135 specific_link = "%s&type=%s&doit=1" % (fragment_link, mimetype) 136 137 download_links.append('<link rel="alternate" type="%s" href="%s"/>' % ( 138 escattr(mimetype), escattr(specific_link))) 139 140 d = { 141 "title" : escape(update.title), 142 "fragment_link" : escape(fragment_link), 143 "download_links" : "\r\n".join(download_links), 144 "content" : update.content and ('<content type="html">%s</content>' % escape(update.content)) or "", 145 "updated" : escape(update.updated.as_ISO8601_datetime_string()), 146 } 147 148 # Write the entry output. 149 150 request.write('''\ 151 <entry>\r 152 <title>%(title)s</title>\r 153 <id>%(fragment_link)s</id>\r 154 <updated>%(updated)s</updated>\r 155 %(download_links)s\r 156 %(content)s\r 157 </entry>\r 158 ''' % d) 159 160 # End the feed output. 161 162 request.write('</feed>\r\n') 163 164 # Action function. 165 166 def execute(pagename, request): 167 SharedUpdates(pagename, request).render() 168 169 # vim: tabstop=4 expandtab shiftwidth=4