paul@13 | 1 | # -*- coding: iso-8859-1 -*- |
paul@13 | 2 | """ |
paul@13 | 3 | MoinMoin - SharedContent macro, based on the FeedReader macro |
paul@13 | 4 | |
paul@18 | 5 | @copyright: 2008, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@13 | 6 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@13 | 7 | """ |
paul@13 | 8 | |
paul@24 | 9 | from DateSupport import getDateTimeFromISO8601, DateTime |
paul@13 | 10 | from MoinMoin.Page import Page |
paul@14 | 11 | from MoinRemoteSupport import * |
paul@32 | 12 | from MoinSupport import parseMacroArguments, getParsersForContentType, formatText |
paul@34 | 13 | from MoinShare import getUpdateSources, getUpdatesFromPage, \ |
paul@34 | 14 | getUpdatesFromStore, Update, get_make_parser |
paul@22 | 15 | from email.utils import parsedate |
paul@13 | 16 | import xml.dom.pulldom |
paul@13 | 17 | |
paul@14 | 18 | try: |
paul@14 | 19 | from cStringIO import StringIO |
paul@14 | 20 | except ImportError: |
paul@14 | 21 | from StringIO import StringIO |
paul@14 | 22 | |
paul@13 | 23 | Dependencies = ["time"] |
paul@13 | 24 | |
paul@13 | 25 | MAX_ENTRIES = 5 |
paul@13 | 26 | ATOM_NS = "http://www.w3.org/2005/Atom" |
paul@13 | 27 | |
paul@22 | 28 | # Utility functions. |
paul@22 | 29 | |
paul@13 | 30 | def text(element): |
paul@13 | 31 | nodes = [] |
paul@13 | 32 | for node in element.childNodes: |
paul@13 | 33 | if node.nodeType == node.TEXT_NODE: |
paul@13 | 34 | nodes.append(node.nodeValue) |
paul@13 | 35 | return "".join(nodes) |
paul@13 | 36 | |
paul@32 | 37 | def children(element): |
paul@32 | 38 | nodes = [] |
paul@32 | 39 | for node in element.childNodes: |
paul@32 | 40 | nodes.append(node.toxml()) |
paul@32 | 41 | return "".join(nodes) |
paul@32 | 42 | |
paul@20 | 43 | def unescape(text): |
paul@20 | 44 | return text.replace("<", "<").replace(">", ">").replace("&", "&") |
paul@20 | 45 | |
paul@13 | 46 | def linktext(element, feed_type): |
paul@13 | 47 | if feed_type == "rss": |
paul@13 | 48 | return text(element) |
paul@13 | 49 | else: |
paul@13 | 50 | return element.getAttribute("href") |
paul@13 | 51 | |
paul@29 | 52 | def need_content(show_content, tagname): |
paul@29 | 53 | return show_content in ("content", "description") and tagname in ("content", "description") |
paul@29 | 54 | |
paul@22 | 55 | # Error classes. |
paul@22 | 56 | |
paul@22 | 57 | class FeedError(Exception): |
paul@22 | 58 | pass |
paul@22 | 59 | |
paul@22 | 60 | class FeedMissingError(FeedError): |
paul@22 | 61 | pass |
paul@13 | 62 | |
paul@22 | 63 | class FeedContentTypeError(FeedError): |
paul@22 | 64 | pass |
paul@22 | 65 | |
paul@22 | 66 | # Feed retrieval. |
paul@20 | 67 | |
paul@28 | 68 | def getUpdates(request, feed_url, max_entries, show_content): |
paul@22 | 69 | |
paul@22 | 70 | """ |
paul@22 | 71 | Using the given 'request', retrieve from 'feed_url' up to the given number |
paul@28 | 72 | 'max_entries' of update entries. The 'show_content' parameter can indicate |
paul@28 | 73 | that a "summary" is to be obtained for each update, that the "content" of |
paul@28 | 74 | each update is to be obtained (falling back to a summary if no content is |
paul@28 | 75 | provided), or no content (indicated by a false value) is to be obtained. |
paul@22 | 76 | |
paul@22 | 77 | A tuple of the form ((feed_type, channel_title, channel_link), updates) is |
paul@22 | 78 | returned. |
paul@22 | 79 | """ |
paul@22 | 80 | |
paul@22 | 81 | feed_updates = [] |
paul@13 | 82 | |
paul@14 | 83 | # Obtain the resource, using a cached version if appropriate. |
paul@13 | 84 | |
paul@14 | 85 | max_cache_age = int(getattr(request.cfg, "moin_share_max_cache_age", "300")) |
paul@14 | 86 | data = getCachedResource(request, feed_url, "MoinShare", "wiki", max_cache_age) |
paul@14 | 87 | if not data: |
paul@22 | 88 | raise FeedMissingError |
paul@22 | 89 | |
paul@22 | 90 | # Interpret the cached feed. |
paul@14 | 91 | |
paul@14 | 92 | feed = StringIO(data) |
paul@20 | 93 | _url, content_type, _encoding, _metadata = getCachedResourceMetadata(feed) |
paul@20 | 94 | |
paul@27 | 95 | if content_type not in ("application/atom+xml", "application/rss+xml", "application/xml"): |
paul@22 | 96 | raise FeedContentTypeError |
paul@13 | 97 | |
paul@13 | 98 | try: |
paul@13 | 99 | # Parse each node from the feed. |
paul@13 | 100 | |
paul@13 | 101 | channel_title = channel_link = None |
paul@13 | 102 | |
paul@13 | 103 | feed_type = None |
paul@22 | 104 | update = None |
paul@27 | 105 | in_source = False |
paul@13 | 106 | |
paul@13 | 107 | events = xml.dom.pulldom.parse(feed) |
paul@13 | 108 | |
paul@13 | 109 | for event, value in events: |
paul@13 | 110 | |
paul@27 | 111 | if not in_source and event == xml.dom.pulldom.START_ELEMENT: |
paul@13 | 112 | tagname = value.localName |
paul@13 | 113 | |
paul@13 | 114 | # Detect the feed type and items. |
paul@13 | 115 | |
paul@13 | 116 | if tagname == "feed" and value.namespaceURI == ATOM_NS: |
paul@13 | 117 | feed_type = "atom" |
paul@13 | 118 | |
paul@13 | 119 | elif tagname == "rss": |
paul@13 | 120 | feed_type = "rss" |
paul@13 | 121 | |
paul@13 | 122 | # Detect items. |
paul@13 | 123 | |
paul@13 | 124 | elif feed_type == "rss" and tagname == "item" or \ |
paul@13 | 125 | feed_type == "atom" and tagname == "entry": |
paul@13 | 126 | |
paul@22 | 127 | update = Update() |
paul@13 | 128 | |
paul@27 | 129 | # Detect source declarations. |
paul@27 | 130 | |
paul@27 | 131 | elif feed_type == "atom" and tagname == "source": |
paul@27 | 132 | in_source = True |
paul@27 | 133 | |
paul@27 | 134 | # Handle item elements. |
paul@27 | 135 | |
paul@13 | 136 | elif tagname == "title": |
paul@13 | 137 | events.expandNode(value) |
paul@22 | 138 | if update: |
paul@22 | 139 | update.title = text(value) |
paul@13 | 140 | else: |
paul@22 | 141 | channel_title = text(value) |
paul@13 | 142 | |
paul@13 | 143 | elif tagname == "link": |
paul@13 | 144 | events.expandNode(value) |
paul@22 | 145 | if update: |
paul@22 | 146 | update.link = linktext(value, feed_type) |
paul@13 | 147 | else: |
paul@22 | 148 | channel_link = linktext(value, feed_type) |
paul@13 | 149 | |
paul@29 | 150 | elif show_content and ( |
paul@29 | 151 | feed_type == "atom" and tagname in ("content", "summary") or |
paul@29 | 152 | feed_type == "rss" and tagname == "description"): |
paul@29 | 153 | |
paul@20 | 154 | events.expandNode(value) |
paul@28 | 155 | |
paul@28 | 156 | # Obtain content where requested or, failing that, a |
paul@28 | 157 | # summary. |
paul@28 | 158 | |
paul@29 | 159 | if update and (need_content(show_content, tagname) or tagname == "summary" and not update.content): |
paul@29 | 160 | if feed_type == "atom": |
paul@29 | 161 | update.content_type = value.getAttribute("type") or "text" |
paul@32 | 162 | |
paul@32 | 163 | # Normalise the content types and extract the |
paul@32 | 164 | # content. |
paul@32 | 165 | |
paul@29 | 166 | if update.content_type in ("xhtml", "application/xhtml+xml", "application/xml"): |
paul@32 | 167 | update.content = children(value) |
paul@32 | 168 | update.content_type = "application/xhtml+xml" |
paul@32 | 169 | elif update.content_type in ("html", "text/html"): |
paul@32 | 170 | update.content = text(value) |
paul@32 | 171 | update.content_type = "text/html" |
paul@29 | 172 | else: |
paul@29 | 173 | update.content = text(value) |
paul@32 | 174 | update.content_type = "text/plain" |
paul@27 | 175 | else: |
paul@32 | 176 | update.content_type = "text/html" |
paul@27 | 177 | update.content = text(value) |
paul@22 | 178 | |
paul@22 | 179 | elif feed_type == "atom" and tagname == "updated" or \ |
paul@22 | 180 | feed_type == "rss" and tagname == "pubDate": |
paul@29 | 181 | |
paul@22 | 182 | events.expandNode(value) |
paul@22 | 183 | |
paul@22 | 184 | if update: |
paul@22 | 185 | if feed_type == "atom": |
paul@24 | 186 | value = getDateTimeFromISO8601(text(value)) |
paul@22 | 187 | else: |
paul@22 | 188 | value = DateTime(parsedate(text(value))) |
paul@22 | 189 | update.updated = value |
paul@20 | 190 | |
paul@13 | 191 | elif event == xml.dom.pulldom.END_ELEMENT: |
paul@13 | 192 | tagname = value.localName |
paul@13 | 193 | |
paul@13 | 194 | if feed_type == "rss" and tagname == "item" or \ |
paul@13 | 195 | feed_type == "atom" and tagname == "entry": |
paul@13 | 196 | |
paul@23 | 197 | feed_updates.append(update) |
paul@20 | 198 | |
paul@22 | 199 | update = None |
paul@13 | 200 | |
paul@27 | 201 | elif feed_type == "atom" and tagname == "source": |
paul@27 | 202 | in_source = False |
paul@27 | 203 | |
paul@22 | 204 | finally: |
paul@22 | 205 | feed.close() |
paul@22 | 206 | |
paul@22 | 207 | return (feed_type, channel_title, channel_link), feed_updates |
paul@22 | 208 | |
paul@22 | 209 | # The macro itself. |
paul@22 | 210 | |
paul@22 | 211 | def execute(macro, args): |
paul@22 | 212 | request = macro.request |
paul@22 | 213 | fmt = macro.formatter |
paul@22 | 214 | _ = request.getText |
paul@22 | 215 | |
paul@31 | 216 | source_pages = [] |
paul@22 | 217 | show_content = None |
paul@22 | 218 | max_entries = None |
paul@22 | 219 | |
paul@22 | 220 | for arg, value in parseMacroArguments(args): |
paul@31 | 221 | if arg == "sources": |
paul@31 | 222 | source_pages.append(value) |
paul@22 | 223 | elif arg == "show": |
paul@28 | 224 | show_content = value.lower() |
paul@22 | 225 | elif arg == "limit": |
paul@22 | 226 | try: |
paul@22 | 227 | max_entries = int(value) |
paul@22 | 228 | except ValueError: |
paul@22 | 229 | return fmt.text(_("SharedContent: limit must be set to the maximum number of entries to be shown")) |
paul@22 | 230 | |
paul@31 | 231 | if not source_pages: |
paul@31 | 232 | return fmt.text(_("SharedContent: at least one sources page must be specified")) |
paul@31 | 233 | |
paul@31 | 234 | sources = {} |
paul@31 | 235 | |
paul@31 | 236 | for source_page in source_pages: |
paul@31 | 237 | sources.update(getUpdateSources(source_page, request)) |
paul@31 | 238 | |
paul@31 | 239 | if not sources: |
paul@31 | 240 | return fmt.text(_("SharedContent: at least one update source must be specified")) |
paul@22 | 241 | |
paul@22 | 242 | show_content = show_content or False |
paul@22 | 243 | max_entries = max_entries or MAX_ENTRIES |
paul@22 | 244 | |
paul@31 | 245 | # Retrieve updates, classifying them as missing or bad and excluding them if |
paul@31 | 246 | # appropriate. |
paul@23 | 247 | |
paul@22 | 248 | updates = [] |
paul@22 | 249 | feeds = [] |
paul@31 | 250 | unspecified = [] |
paul@22 | 251 | missing = [] |
paul@22 | 252 | bad_content = [] |
paul@13 | 253 | |
paul@31 | 254 | for source_name, source_parameters in sources.items(): |
paul@31 | 255 | location = source_parameters.get("location") |
paul@31 | 256 | if not location: |
paul@31 | 257 | unspecified.append(source_name) |
paul@31 | 258 | continue |
paul@31 | 259 | |
paul@22 | 260 | try: |
paul@31 | 261 | max_entries_for_feed = int(source_parameters["limit"]) |
paul@31 | 262 | except (KeyError, ValueError): |
paul@31 | 263 | max_entries_for_feed = None |
paul@22 | 264 | |
paul@31 | 265 | # Retrieve updates from feeds. |
paul@30 | 266 | |
paul@31 | 267 | if source_parameters.get("type") == "url": |
paul@31 | 268 | try: |
paul@35 | 269 | feed_info, feed_updates = getUpdates(request, location, max_entries_for_feed, show_content) |
paul@31 | 270 | updates += feed_updates |
paul@31 | 271 | feeds.append((location, feed_info)) |
paul@31 | 272 | except FeedMissingError: |
paul@31 | 273 | missing.append(location) |
paul@31 | 274 | except FeedContentTypeError: |
paul@31 | 275 | bad_content.append(location) |
paul@30 | 276 | |
paul@31 | 277 | # Retrieve updates from pages. |
paul@30 | 278 | |
paul@31 | 279 | elif source_parameters.get("type") == "page": |
paul@31 | 280 | page = Page(request, location) |
paul@31 | 281 | updates += getUpdatesFromPage(page, request) |
paul@31 | 282 | |
paul@31 | 283 | # Build feed-equivalent information for the update source. |
paul@31 | 284 | |
paul@31 | 285 | feeds.append(( |
paul@31 | 286 | page.url(request, {"action" : "SharedUpdates", "doit" : "1"}), ( |
paul@31 | 287 | "internal", _("Updates from page %s") % location, |
paul@31 | 288 | page.url(request) |
paul@31 | 289 | ) |
paul@31 | 290 | )) |
paul@30 | 291 | |
paul@33 | 292 | # Retrieve updates from message stores. |
paul@33 | 293 | |
paul@33 | 294 | elif source_parameters.get("type") == "store": |
paul@33 | 295 | page = Page(request, location) |
paul@33 | 296 | updates += getUpdatesFromStore(page, request) |
paul@33 | 297 | |
paul@33 | 298 | # Build feed-equivalent information for the update source. |
paul@33 | 299 | |
paul@33 | 300 | feeds.append(( |
paul@33 | 301 | page.url(request, {"action" : "SharedUpdates", "store" : "1", "doit" : "1"}), ( |
paul@33 | 302 | "internal", _("Updates from message store on page %s") % location, |
paul@33 | 303 | page.url(request) |
paul@33 | 304 | ) |
paul@33 | 305 | )) |
paul@33 | 306 | |
paul@30 | 307 | # Prepare the output. |
paul@30 | 308 | |
paul@22 | 309 | output = [] |
paul@22 | 310 | append = output.append |
paul@22 | 311 | |
paul@22 | 312 | # Show the updates. |
paul@22 | 313 | |
paul@22 | 314 | if not show_content: |
paul@22 | 315 | append(fmt.bullet_list(on=1)) |
paul@22 | 316 | |
paul@22 | 317 | # NOTE: Permit configurable sorting. |
paul@22 | 318 | |
paul@22 | 319 | updates.sort() |
paul@22 | 320 | updates.reverse() |
paul@22 | 321 | |
paul@23 | 322 | # Truncate the number of updates to the maximum number. |
paul@23 | 323 | |
paul@23 | 324 | updates = updates[:max_entries] |
paul@23 | 325 | |
paul@22 | 326 | for update in updates: |
paul@22 | 327 | |
paul@22 | 328 | # Emit content where appropriate. |
paul@32 | 329 | # NOTE: Some control over the HTML and XHTML should be exercised. |
paul@22 | 330 | |
paul@22 | 331 | if show_content: |
paul@22 | 332 | append(fmt.div(on=1, css_class="moinshare-update")) |
paul@24 | 333 | append(fmt.div(on=1, css_class="moinshare-content")) |
paul@32 | 334 | |
paul@27 | 335 | if update.content: |
paul@34 | 336 | if update.content_type == "text/html" and update.message_number is not None: |
paul@34 | 337 | parsers = [get_make_parser(update.page, update.message_number)] |
paul@34 | 338 | else: |
paul@34 | 339 | parsers = getParsersForContentType(request.cfg, update.content_type) |
paul@32 | 340 | |
paul@32 | 341 | if parsers: |
paul@32 | 342 | for parser_cls in parsers: |
paul@32 | 343 | append(formatText(update.content, request, fmt, parser_cls=parser_cls)) |
paul@32 | 344 | break |
paul@32 | 345 | else: |
paul@32 | 346 | append(fmt.text(_("Update cannot be shown for content of type %s.") % update.content_type)) |
paul@32 | 347 | |
paul@22 | 348 | append(fmt.div(on=0)) |
paul@24 | 349 | append(fmt.div(on=1, css_class="moinshare-date")) |
paul@24 | 350 | append(fmt.text(str(update.updated))) |
paul@24 | 351 | append(fmt.div(on=0)) |
paul@24 | 352 | append(fmt.div(on=0)) |
paul@22 | 353 | |
paul@22 | 354 | # Or emit title and link information for items. |
paul@22 | 355 | |
paul@22 | 356 | elif update.title and update.link: |
paul@22 | 357 | append(fmt.listitem(on=1, css_class="moinshare-update")) |
paul@22 | 358 | append(fmt.url(on=1, href=update.link)) |
paul@22 | 359 | append(fmt.icon('www')) |
paul@22 | 360 | append(fmt.text(" " + update.title)) |
paul@22 | 361 | append(fmt.url(on=0)) |
paul@22 | 362 | append(fmt.listitem(on=0)) |
paul@22 | 363 | |
paul@22 | 364 | if not show_content: |
paul@22 | 365 | append(fmt.bullet_list(on=0)) |
paul@22 | 366 | |
paul@22 | 367 | # Show the feeds. |
paul@22 | 368 | |
paul@26 | 369 | for feed_url, (feed_type, channel_title, channel_link) in feeds: |
paul@13 | 370 | if channel_title and channel_link: |
paul@22 | 371 | append(fmt.paragraph(on=1, css_class="moinshare-feed")) |
paul@22 | 372 | append(fmt.url(on=1, href=channel_link)) |
paul@22 | 373 | append(fmt.text(channel_title)) |
paul@13 | 374 | append(fmt.url(on=0)) |
paul@13 | 375 | append(fmt.text(" ")) |
paul@13 | 376 | append(fmt.url(on=1, href=feed_url)) |
paul@13 | 377 | append(fmt.icon('rss')) |
paul@13 | 378 | append(fmt.url(on=0)) |
paul@13 | 379 | append(fmt.paragraph(on=0)) |
paul@13 | 380 | |
paul@22 | 381 | # Show errors. |
paul@22 | 382 | |
paul@22 | 383 | for feed_url in missing: |
paul@22 | 384 | append(fmt.paragraph(on=1, css_class="moinshare-missing-feed-error")) |
paul@22 | 385 | append(fmt.text(_("SharedContent: updates could not be retrieved for %s") % feed_url)) |
paul@22 | 386 | append(fmt.paragraph(on=0)) |
paul@22 | 387 | |
paul@22 | 388 | for feed_url in bad_content: |
paul@22 | 389 | append(fmt.paragraph(on=1, css_class="moinshare-content-type-feed-error")) |
paul@22 | 390 | return fmt.text(_("SharedContent: updates for %s were not provided in Atom or RSS format") % feed_url) |
paul@22 | 391 | append(fmt.paragraph(on=0)) |
paul@13 | 392 | |
paul@13 | 393 | return ''.join(output) |
paul@13 | 394 | |
paul@13 | 395 | # vim: tabstop=4 expandtab shiftwidth=4 |