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