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