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