paul@0 | 1 | # -*- coding: iso-8859-1 -*- |
paul@0 | 2 | """ |
paul@0 | 3 | MoinMoin - MoinSupport library (derived from EventAggregatorSupport) |
paul@0 | 4 | |
paul@36 | 5 | @copyright: 2008, 2009, 2010, 2011, 2012, 2013 by Paul Boddie <paul@boddie.org.uk> |
paul@0 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de>, |
paul@0 | 7 | 2005-2008 MoinMoin:ThomasWaldmann. |
paul@0 | 8 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 9 | """ |
paul@0 | 10 | |
paul@0 | 11 | from DateSupport import * |
paul@1 | 12 | from MoinMoin.Page import Page |
paul@37 | 13 | from MoinMoin.util import lock |
paul@25 | 14 | from MoinMoin import config, search, wikiutil |
paul@10 | 15 | from StringIO import StringIO |
paul@10 | 16 | from shlex import shlex |
paul@0 | 17 | import re |
paul@0 | 18 | import time |
paul@37 | 19 | import os |
paul@0 | 20 | |
paul@35 | 21 | # Moin 1.9 request parameters. |
paul@35 | 22 | |
paul@35 | 23 | try: |
paul@35 | 24 | from MoinMoin.support.werkzeug.datastructures import MultiDict |
paul@35 | 25 | except ImportError: |
paul@35 | 26 | pass |
paul@35 | 27 | |
paul@10 | 28 | __version__ = "0.2" |
paul@0 | 29 | |
paul@25 | 30 | # Extraction of shared fragments. |
paul@25 | 31 | |
paul@25 | 32 | marker_regexp_str = r"([{]{3,}|[}]{3,})" |
paul@25 | 33 | marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... |
paul@25 | 34 | |
paul@29 | 35 | # Extraction of headings. |
paul@29 | 36 | |
paul@29 | 37 | heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE) |
paul@29 | 38 | |
paul@25 | 39 | # Category extraction from pages. |
paul@25 | 40 | |
paul@25 | 41 | category_regexp = None |
paul@25 | 42 | |
paul@25 | 43 | # Simple content parsing. |
paul@25 | 44 | |
paul@25 | 45 | verbatim_regexp = re.compile(ur'(?:' |
paul@25 | 46 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@25 | 47 | ur'|' |
paul@25 | 48 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@25 | 49 | ur'|' |
paul@25 | 50 | ur'!(?P<verbatim3>.*?)(\s|$)?' |
paul@25 | 51 | ur'|' |
paul@25 | 52 | ur'`(?P<monospace>.*?)`' |
paul@25 | 53 | ur'|' |
paul@25 | 54 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@25 | 55 | ur')', re.UNICODE) |
paul@25 | 56 | |
paul@25 | 57 | # Category discovery. |
paul@0 | 58 | |
paul@25 | 59 | def getCategoryPattern(request): |
paul@25 | 60 | global category_regexp |
paul@25 | 61 | |
paul@25 | 62 | try: |
paul@25 | 63 | return request.cfg.cache.page_category_regexact |
paul@25 | 64 | except AttributeError: |
paul@25 | 65 | |
paul@25 | 66 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@25 | 67 | |
paul@25 | 68 | if category_regexp is None: |
paul@25 | 69 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@25 | 70 | return category_regexp |
paul@25 | 71 | |
paul@25 | 72 | def getCategories(request): |
paul@25 | 73 | |
paul@25 | 74 | """ |
paul@25 | 75 | From the AdvancedSearch macro, return a list of category page names using |
paul@25 | 76 | the given 'request'. |
paul@25 | 77 | """ |
paul@25 | 78 | |
paul@25 | 79 | # This will return all pages with "Category" in the title. |
paul@25 | 80 | |
paul@25 | 81 | cat_filter = getCategoryPattern(request).search |
paul@25 | 82 | return request.rootpage.getPageList(filter=cat_filter) |
paul@25 | 83 | |
paul@25 | 84 | def getCategoryMapping(category_pagenames, request): |
paul@2 | 85 | |
paul@2 | 86 | """ |
paul@25 | 87 | For the given 'category_pagenames' return a list of tuples of the form |
paul@25 | 88 | (category name, category page name) using the given 'request'. |
paul@25 | 89 | """ |
paul@25 | 90 | |
paul@25 | 91 | cat_pattern = getCategoryPattern(request) |
paul@25 | 92 | mapping = [] |
paul@25 | 93 | for pagename in category_pagenames: |
paul@25 | 94 | name = cat_pattern.match(pagename).group("key") |
paul@25 | 95 | if name != "Category": |
paul@25 | 96 | mapping.append((name, pagename)) |
paul@25 | 97 | mapping.sort() |
paul@25 | 98 | return mapping |
paul@25 | 99 | |
paul@25 | 100 | def getCategoryPages(pagename, request): |
paul@25 | 101 | |
paul@25 | 102 | """ |
paul@25 | 103 | Return the pages associated with the given category 'pagename' using the |
paul@25 | 104 | 'request'. |
paul@25 | 105 | """ |
paul@25 | 106 | |
paul@25 | 107 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@25 | 108 | results = search.searchPages(request, query, "page_name") |
paul@27 | 109 | return filterCategoryPages(results, request) |
paul@27 | 110 | |
paul@27 | 111 | def filterCategoryPages(results, request): |
paul@27 | 112 | |
paul@27 | 113 | "Filter category pages from the given 'results' using the 'request'." |
paul@25 | 114 | |
paul@25 | 115 | cat_pattern = getCategoryPattern(request) |
paul@25 | 116 | pages = [] |
paul@25 | 117 | for page in results.hits: |
paul@25 | 118 | if not cat_pattern.match(page.page_name): |
paul@25 | 119 | pages.append(page) |
paul@25 | 120 | return pages |
paul@25 | 121 | |
paul@25 | 122 | def getAllCategoryPages(category_names, request): |
paul@25 | 123 | |
paul@25 | 124 | """ |
paul@25 | 125 | Return all pages belonging to the categories having the given |
paul@25 | 126 | 'category_names', using the given 'request'. |
paul@25 | 127 | """ |
paul@25 | 128 | |
paul@25 | 129 | pages = [] |
paul@25 | 130 | pagenames = set() |
paul@25 | 131 | |
paul@25 | 132 | for category_name in category_names: |
paul@25 | 133 | |
paul@25 | 134 | # Get the pages and page names in the category. |
paul@25 | 135 | |
paul@25 | 136 | pages_in_category = getCategoryPages(category_name, request) |
paul@25 | 137 | |
paul@25 | 138 | # Visit each page in the category. |
paul@25 | 139 | |
paul@25 | 140 | for page_in_category in pages_in_category: |
paul@25 | 141 | pagename = page_in_category.page_name |
paul@25 | 142 | |
paul@25 | 143 | # Only process each page once. |
paul@25 | 144 | |
paul@25 | 145 | if pagename in pagenames: |
paul@25 | 146 | continue |
paul@25 | 147 | else: |
paul@25 | 148 | pagenames.add(pagename) |
paul@25 | 149 | |
paul@25 | 150 | pages.append(page_in_category) |
paul@25 | 151 | |
paul@25 | 152 | return pages |
paul@25 | 153 | |
paul@28 | 154 | def getPagesForSearch(search_pattern, request): |
paul@27 | 155 | |
paul@27 | 156 | """ |
paul@27 | 157 | Return result pages for a search employing the given 'search_pattern' and |
paul@28 | 158 | using the given 'request'. |
paul@27 | 159 | """ |
paul@27 | 160 | |
paul@27 | 161 | query = search.QueryParser().parse_query(search_pattern) |
paul@27 | 162 | results = search.searchPages(request, query, "page_name") |
paul@27 | 163 | return filterCategoryPages(results, request) |
paul@27 | 164 | |
paul@25 | 165 | # WikiDict functions. |
paul@25 | 166 | |
paul@25 | 167 | def getWikiDict(pagename, request): |
paul@25 | 168 | |
paul@25 | 169 | """ |
paul@25 | 170 | Return the WikiDict provided by the given 'pagename' using the given |
paul@25 | 171 | 'request'. |
paul@2 | 172 | """ |
paul@2 | 173 | |
paul@25 | 174 | if pagename and Page(request, pagename).exists() and request.user.may.read(pagename): |
paul@25 | 175 | if hasattr(request.dicts, "dict"): |
paul@25 | 176 | return request.dicts.dict(pagename) |
paul@25 | 177 | else: |
paul@25 | 178 | return request.dicts[pagename] |
paul@0 | 179 | else: |
paul@25 | 180 | return None |
paul@25 | 181 | |
paul@25 | 182 | # Searching-related functions. |
paul@25 | 183 | |
paul@25 | 184 | def getPagesFromResults(result_pages, request): |
paul@25 | 185 | |
paul@25 | 186 | "Return genuine pages for the given 'result_pages' using the 'request'." |
paul@25 | 187 | |
paul@25 | 188 | return [Page(request, page.page_name) for page in result_pages] |
paul@25 | 189 | |
paul@25 | 190 | # Region/section parsing. |
paul@25 | 191 | |
paul@25 | 192 | def getRegions(s, include_non_regions=False): |
paul@25 | 193 | |
paul@25 | 194 | """ |
paul@25 | 195 | Parse the string 's', returning a list of explicitly declared regions. |
paul@25 | 196 | |
paul@25 | 197 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 198 | included for text between explicitly declared regions. |
paul@25 | 199 | """ |
paul@25 | 200 | |
paul@25 | 201 | regions = [] |
paul@25 | 202 | marker = None |
paul@25 | 203 | is_block = True |
paul@25 | 204 | |
paul@25 | 205 | # Start a region for exposed text, if appropriate. |
paul@25 | 206 | |
paul@25 | 207 | if include_non_regions: |
paul@25 | 208 | regions.append("") |
paul@25 | 209 | |
paul@25 | 210 | for match_text in marker_regexp.split(s): |
paul@25 | 211 | |
paul@25 | 212 | # Capture section text. |
paul@25 | 213 | |
paul@25 | 214 | if is_block: |
paul@25 | 215 | if marker or include_non_regions: |
paul@25 | 216 | regions[-1] += match_text |
paul@25 | 217 | |
paul@25 | 218 | # Handle section markers. |
paul@25 | 219 | |
paul@31 | 220 | else: |
paul@25 | 221 | |
paul@25 | 222 | # Close any open sections, returning to exposed text regions. |
paul@25 | 223 | |
paul@25 | 224 | if marker: |
paul@31 | 225 | |
paul@31 | 226 | # Add any marker to the current region, regardless of whether it |
paul@31 | 227 | # successfully closes a section. |
paul@31 | 228 | |
paul@31 | 229 | regions[-1] += match_text |
paul@31 | 230 | |
paul@25 | 231 | if match_text.startswith("}") and len(marker) == len(match_text): |
paul@25 | 232 | marker = None |
paul@25 | 233 | |
paul@25 | 234 | # Start a region for exposed text, if appropriate. |
paul@25 | 235 | |
paul@25 | 236 | if include_non_regions: |
paul@25 | 237 | regions.append("") |
paul@25 | 238 | |
paul@31 | 239 | # Without a current marker, start a new section. |
paul@25 | 240 | |
paul@31 | 241 | else: |
paul@25 | 242 | marker = match_text |
paul@25 | 243 | regions.append("") |
paul@25 | 244 | |
paul@31 | 245 | # Add the marker to the new region. |
paul@25 | 246 | |
paul@31 | 247 | regions[-1] += match_text |
paul@0 | 248 | |
paul@25 | 249 | # The match text alternates between text between markers and the markers |
paul@25 | 250 | # themselves. |
paul@25 | 251 | |
paul@25 | 252 | is_block = not is_block |
paul@25 | 253 | |
paul@25 | 254 | return regions |
paul@25 | 255 | |
paul@25 | 256 | def getFragmentsFromRegions(regions): |
paul@25 | 257 | |
paul@25 | 258 | """ |
paul@25 | 259 | Return fragments from the given 'regions', each having the form |
paul@32 | 260 | (format, attributes, body text). |
paul@25 | 261 | """ |
paul@25 | 262 | |
paul@25 | 263 | fragments = [] |
paul@25 | 264 | |
paul@25 | 265 | for region in regions: |
paul@32 | 266 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@32 | 267 | fragments.append((format, attributes, body)) |
paul@25 | 268 | |
paul@32 | 269 | return fragments |
paul@25 | 270 | |
paul@32 | 271 | def getFragmentFromRegion(region): |
paul@25 | 272 | |
paul@32 | 273 | """ |
paul@32 | 274 | Return a fragment for the given 'region' having the form (format, |
paul@32 | 275 | attributes, body text, header, close), where the 'header' is the original |
paul@32 | 276 | declaration of the 'region' or None if no explicit region is defined, and |
paul@32 | 277 | 'close' is the closing marker of the 'region' or None if no explicit region |
paul@32 | 278 | is defined. |
paul@32 | 279 | """ |
paul@25 | 280 | |
paul@32 | 281 | if region.startswith("{{{"): |
paul@25 | 282 | |
paul@32 | 283 | body = region.lstrip("{") |
paul@32 | 284 | level = len(region) - len(body) |
paul@32 | 285 | body = body.rstrip("}").lstrip() |
paul@32 | 286 | |
paul@32 | 287 | # Remove any prelude and process metadata. |
paul@32 | 288 | |
paul@32 | 289 | if body.startswith("#!"): |
paul@25 | 290 | |
paul@32 | 291 | try: |
paul@32 | 292 | declaration, body = body.split("\n", 1) |
paul@32 | 293 | except ValueError: |
paul@32 | 294 | declaration = body |
paul@32 | 295 | body = "" |
paul@25 | 296 | |
paul@32 | 297 | arguments = declaration[2:] |
paul@32 | 298 | |
paul@32 | 299 | # Get any parser/format declaration. |
paul@25 | 300 | |
paul@32 | 301 | if arguments and not arguments[0].isspace(): |
paul@32 | 302 | details = arguments.split(None, 1) |
paul@32 | 303 | if len(details) == 2: |
paul@32 | 304 | format, arguments = details |
paul@32 | 305 | else: |
paul@32 | 306 | format = details[0] |
paul@32 | 307 | arguments = "" |
paul@32 | 308 | else: |
paul@32 | 309 | format = None |
paul@25 | 310 | |
paul@32 | 311 | # Get the attributes/arguments for the region. |
paul@32 | 312 | |
paul@32 | 313 | attributes = parseAttributes(arguments, False) |
paul@25 | 314 | |
paul@32 | 315 | # Add an entry for the format in the attribute dictionary. |
paul@25 | 316 | |
paul@32 | 317 | if format and not attributes.has_key(format): |
paul@32 | 318 | attributes[format] = True |
paul@32 | 319 | |
paul@32 | 320 | return format, attributes, body, level * "{" + declaration + "\n", level * "}" |
paul@25 | 321 | |
paul@25 | 322 | else: |
paul@32 | 323 | return None, {}, body, level * "{" + "\n", level * "}" |
paul@25 | 324 | |
paul@32 | 325 | else: |
paul@32 | 326 | return None, {}, region, None, None |
paul@25 | 327 | |
paul@25 | 328 | def getFragments(s, include_non_regions=False): |
paul@25 | 329 | |
paul@25 | 330 | """ |
paul@25 | 331 | Return fragments for the given string 's', each having the form |
paul@25 | 332 | (format, arguments, body text). |
paul@25 | 333 | |
paul@25 | 334 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 335 | included for text between explicitly declared regions. |
paul@25 | 336 | """ |
paul@25 | 337 | |
paul@25 | 338 | return getFragmentsFromRegions(getRegions(s, include_non_regions)) |
paul@25 | 339 | |
paul@29 | 340 | # Heading extraction. |
paul@29 | 341 | |
paul@29 | 342 | def getHeadings(s): |
paul@29 | 343 | |
paul@29 | 344 | """ |
paul@29 | 345 | Return tuples of the form (level, title, span) for headings found within the |
paul@29 | 346 | given string 's'. The span is itself a (start, end) tuple indicating the |
paul@29 | 347 | matching region of 's' for a heading declaration. |
paul@29 | 348 | """ |
paul@29 | 349 | |
paul@29 | 350 | headings = [] |
paul@29 | 351 | |
paul@29 | 352 | for match in heading_regexp.finditer(s): |
paul@29 | 353 | headings.append( |
paul@29 | 354 | (len(match.group("level")), match.group("heading"), match.span()) |
paul@29 | 355 | ) |
paul@29 | 356 | |
paul@29 | 357 | return headings |
paul@29 | 358 | |
paul@25 | 359 | # Region/section attribute parsing. |
paul@0 | 360 | |
paul@10 | 361 | def parseAttributes(s, escape=True): |
paul@10 | 362 | |
paul@10 | 363 | """ |
paul@10 | 364 | Parse the section attributes string 's', returning a mapping of names to |
paul@10 | 365 | values. If 'escape' is set to a true value, the attributes will be suitable |
paul@10 | 366 | for use with the formatter API. If 'escape' is set to a false value, the |
paul@10 | 367 | attributes will have any quoting removed. |
paul@10 | 368 | """ |
paul@10 | 369 | |
paul@10 | 370 | attrs = {} |
paul@10 | 371 | f = StringIO(s) |
paul@10 | 372 | name = None |
paul@10 | 373 | need_value = False |
paul@34 | 374 | lex = shlex(f) |
paul@34 | 375 | lex.wordchars += "-" |
paul@10 | 376 | |
paul@34 | 377 | for token in lex: |
paul@10 | 378 | |
paul@10 | 379 | # Capture the name if needed. |
paul@10 | 380 | |
paul@10 | 381 | if name is None: |
paul@10 | 382 | name = escape and wikiutil.escape(token) or strip_token(token) |
paul@10 | 383 | |
paul@10 | 384 | # Detect either an equals sign or another name. |
paul@10 | 385 | |
paul@10 | 386 | elif not need_value: |
paul@10 | 387 | if token == "=": |
paul@10 | 388 | need_value = True |
paul@10 | 389 | else: |
paul@10 | 390 | attrs[name.lower()] = escape and "true" or True |
paul@10 | 391 | name = wikiutil.escape(token) |
paul@10 | 392 | |
paul@10 | 393 | # Otherwise, capture a value. |
paul@10 | 394 | |
paul@10 | 395 | else: |
paul@10 | 396 | # Quoting of attributes done similarly to wikiutil.parseAttributes. |
paul@10 | 397 | |
paul@10 | 398 | if token: |
paul@10 | 399 | if escape: |
paul@10 | 400 | if token[0] in ("'", '"'): |
paul@10 | 401 | token = wikiutil.escape(token) |
paul@10 | 402 | else: |
paul@10 | 403 | token = '"%s"' % wikiutil.escape(token, 1) |
paul@10 | 404 | else: |
paul@10 | 405 | token = strip_token(token) |
paul@10 | 406 | |
paul@10 | 407 | attrs[name.lower()] = token |
paul@10 | 408 | name = None |
paul@10 | 409 | need_value = False |
paul@10 | 410 | |
paul@13 | 411 | # Handle any name-only attributes at the end of the collection. |
paul@13 | 412 | |
paul@13 | 413 | if name and not need_value: |
paul@13 | 414 | attrs[name.lower()] = escape and "true" or True |
paul@13 | 415 | |
paul@10 | 416 | return attrs |
paul@10 | 417 | |
paul@10 | 418 | def strip_token(token): |
paul@10 | 419 | |
paul@10 | 420 | "Return the given 'token' stripped of quoting." |
paul@10 | 421 | |
paul@10 | 422 | if token[0] in ("'", '"') and token[-1] == token[0]: |
paul@10 | 423 | return token[1:-1] |
paul@10 | 424 | else: |
paul@10 | 425 | return token |
paul@10 | 426 | |
paul@25 | 427 | # Request-related classes and associated functions. |
paul@0 | 428 | |
paul@0 | 429 | class Form: |
paul@0 | 430 | |
paul@0 | 431 | """ |
paul@0 | 432 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@0 | 433 | environment. |
paul@0 | 434 | """ |
paul@0 | 435 | |
paul@35 | 436 | def __init__(self, request): |
paul@35 | 437 | self.request = request |
paul@35 | 438 | self.form = request.values |
paul@0 | 439 | |
paul@0 | 440 | def has_key(self, name): |
paul@0 | 441 | return not not self.form.getlist(name) |
paul@0 | 442 | |
paul@0 | 443 | def get(self, name, default=None): |
paul@0 | 444 | values = self.form.getlist(name) |
paul@0 | 445 | if not values: |
paul@0 | 446 | return default |
paul@0 | 447 | else: |
paul@0 | 448 | return values |
paul@0 | 449 | |
paul@0 | 450 | def __getitem__(self, name): |
paul@0 | 451 | return self.form.getlist(name) |
paul@0 | 452 | |
paul@35 | 453 | def __setitem__(self, name, value): |
paul@35 | 454 | try: |
paul@35 | 455 | self.form.setlist(name, value) |
paul@35 | 456 | except TypeError: |
paul@35 | 457 | self._write_enable() |
paul@35 | 458 | self.form.setlist(name, value) |
paul@35 | 459 | |
paul@33 | 460 | def __delitem__(self, name): |
paul@35 | 461 | try: |
paul@35 | 462 | del self.form[name] |
paul@35 | 463 | except TypeError: |
paul@35 | 464 | self._write_enable() |
paul@35 | 465 | del self.form[name] |
paul@35 | 466 | |
paul@35 | 467 | def _write_enable(self): |
paul@35 | 468 | self.form = self.request.values = MultiDict(self.form) |
paul@33 | 469 | |
paul@33 | 470 | def keys(self): |
paul@33 | 471 | return self.form.keys() |
paul@33 | 472 | |
paul@32 | 473 | def items(self): |
paul@35 | 474 | return self.form.lists() |
paul@32 | 475 | |
paul@0 | 476 | class ActionSupport: |
paul@0 | 477 | |
paul@0 | 478 | """ |
paul@0 | 479 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@0 | 480 | convenience methods. |
paul@0 | 481 | """ |
paul@0 | 482 | |
paul@0 | 483 | def get_form(self): |
paul@0 | 484 | return get_form(self.request) |
paul@0 | 485 | |
paul@0 | 486 | def _get_selected(self, value, input_value): |
paul@0 | 487 | |
paul@0 | 488 | """ |
paul@0 | 489 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 490 | otherwise) if 'value' matches 'input_value'. |
paul@0 | 491 | """ |
paul@0 | 492 | |
paul@0 | 493 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@0 | 494 | |
paul@0 | 495 | def _get_selected_for_list(self, value, input_values): |
paul@0 | 496 | |
paul@0 | 497 | """ |
paul@0 | 498 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 499 | otherwise) if 'value' matches one of the 'input_values'. |
paul@0 | 500 | """ |
paul@0 | 501 | |
paul@0 | 502 | return value in input_values and 'selected="selected"' or '' |
paul@0 | 503 | |
paul@26 | 504 | def get_option_list(self, value, values): |
paul@26 | 505 | |
paul@26 | 506 | """ |
paul@26 | 507 | Return a list of HTML element definitions for options describing the |
paul@26 | 508 | given 'values', selecting the option with the specified 'value' if |
paul@26 | 509 | present. |
paul@26 | 510 | """ |
paul@26 | 511 | |
paul@26 | 512 | options = [] |
paul@26 | 513 | for available_value in values: |
paul@26 | 514 | selected = self._get_selected(available_value, value) |
paul@26 | 515 | options.append('<option value="%s" %s>%s</option>' % ( |
paul@26 | 516 | escattr(available_value), selected, wikiutil.escape(available_value))) |
paul@26 | 517 | return options |
paul@26 | 518 | |
paul@0 | 519 | def _get_input(self, form, name, default=None): |
paul@0 | 520 | |
paul@0 | 521 | """ |
paul@0 | 522 | Return the input from 'form' having the given 'name', returning either |
paul@0 | 523 | the input converted to an integer or the given 'default' (optional, None |
paul@0 | 524 | if not specified). |
paul@0 | 525 | """ |
paul@0 | 526 | |
paul@0 | 527 | value = form.get(name, [None])[0] |
paul@0 | 528 | if not value: # true if 0 obtained |
paul@0 | 529 | return default |
paul@0 | 530 | else: |
paul@0 | 531 | return int(value) |
paul@0 | 532 | |
paul@0 | 533 | def get_form(request): |
paul@0 | 534 | |
paul@0 | 535 | "Work around disruptive MoinMoin changes in 1.9." |
paul@0 | 536 | |
paul@0 | 537 | if hasattr(request, "values"): |
paul@35 | 538 | return Form(request) |
paul@0 | 539 | else: |
paul@0 | 540 | return request.form |
paul@0 | 541 | |
paul@0 | 542 | class send_headers_cls: |
paul@0 | 543 | |
paul@0 | 544 | """ |
paul@0 | 545 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@0 | 546 | 1.9.x environment. |
paul@0 | 547 | """ |
paul@0 | 548 | |
paul@0 | 549 | def __init__(self, request): |
paul@0 | 550 | self.request = request |
paul@0 | 551 | |
paul@0 | 552 | def __call__(self, headers): |
paul@0 | 553 | for header in headers: |
paul@0 | 554 | parts = header.split(":") |
paul@0 | 555 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@0 | 556 | |
paul@0 | 557 | def get_send_headers(request): |
paul@0 | 558 | |
paul@0 | 559 | "Return a function that can send response headers." |
paul@0 | 560 | |
paul@0 | 561 | if hasattr(request, "http_headers"): |
paul@0 | 562 | return request.http_headers |
paul@0 | 563 | elif hasattr(request, "emit_http_headers"): |
paul@0 | 564 | return request.emit_http_headers |
paul@0 | 565 | else: |
paul@0 | 566 | return send_headers_cls(request) |
paul@0 | 567 | |
paul@0 | 568 | def escattr(s): |
paul@0 | 569 | return wikiutil.escape(s, 1) |
paul@0 | 570 | |
paul@0 | 571 | def getPathInfo(request): |
paul@0 | 572 | if hasattr(request, "getPathinfo"): |
paul@0 | 573 | return request.getPathinfo() |
paul@0 | 574 | else: |
paul@0 | 575 | return request.path |
paul@0 | 576 | |
paul@14 | 577 | def getHeader(request, header_name, prefix=None): |
paul@17 | 578 | |
paul@17 | 579 | """ |
paul@17 | 580 | Using the 'request', return the value of the header with the given |
paul@17 | 581 | 'header_name', using the optional 'prefix' to obtain protocol-specific |
paul@17 | 582 | headers if necessary. |
paul@17 | 583 | |
paul@17 | 584 | If no value is found for the given 'header_name', None is returned. |
paul@17 | 585 | """ |
paul@17 | 586 | |
paul@14 | 587 | if hasattr(request, "getHeader"): |
paul@14 | 588 | return request.getHeader(header_name) |
paul@14 | 589 | elif hasattr(request, "headers"): |
paul@17 | 590 | return request.headers.get(header_name) |
paul@14 | 591 | else: |
paul@17 | 592 | return request.env.get((prefix and prefix + "_" or "") + header_name.upper()) |
paul@14 | 593 | |
paul@23 | 594 | def writeHeaders(request, mimetype, metadata, status=None): |
paul@23 | 595 | |
paul@23 | 596 | """ |
paul@23 | 597 | Using the 'request', write resource headers using the given 'mimetype', |
paul@23 | 598 | based on the given 'metadata'. If the optional 'status' is specified, set |
paul@23 | 599 | the status header to the given value. |
paul@23 | 600 | """ |
paul@23 | 601 | |
paul@23 | 602 | send_headers = get_send_headers(request) |
paul@23 | 603 | |
paul@23 | 604 | # Define headers. |
paul@23 | 605 | |
paul@23 | 606 | headers = ["Content-Type: %s; charset=%s" % (mimetype, config.charset)] |
paul@23 | 607 | |
paul@23 | 608 | # Define the last modified time. |
paul@23 | 609 | # NOTE: Consider using request.httpDate. |
paul@23 | 610 | |
paul@23 | 611 | latest_timestamp = metadata.get("last-modified") |
paul@23 | 612 | if latest_timestamp: |
paul@23 | 613 | headers.append("Last-Modified: %s" % latest_timestamp.as_HTTP_datetime_string()) |
paul@23 | 614 | |
paul@23 | 615 | if status: |
paul@23 | 616 | headers.append("Status: %s" % status) |
paul@23 | 617 | |
paul@23 | 618 | send_headers(headers) |
paul@23 | 619 | |
paul@1 | 620 | # Page access functions. |
paul@1 | 621 | |
paul@1 | 622 | def getPageURL(page): |
paul@1 | 623 | |
paul@1 | 624 | "Return the URL of the given 'page'." |
paul@1 | 625 | |
paul@1 | 626 | request = page.request |
paul@1 | 627 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@1 | 628 | |
paul@1 | 629 | def getFormat(page): |
paul@1 | 630 | |
paul@1 | 631 | "Get the format used on the given 'page'." |
paul@1 | 632 | |
paul@1 | 633 | return page.pi["format"] |
paul@1 | 634 | |
paul@1 | 635 | def getMetadata(page): |
paul@1 | 636 | |
paul@1 | 637 | """ |
paul@1 | 638 | Return a dictionary containing items describing for the given 'page' the |
paul@1 | 639 | page's "created" time, "last-modified" time, "sequence" (or revision number) |
paul@1 | 640 | and the "last-comment" made about the last edit. |
paul@1 | 641 | """ |
paul@1 | 642 | |
paul@1 | 643 | request = page.request |
paul@1 | 644 | |
paul@1 | 645 | # Get the initial revision of the page. |
paul@1 | 646 | |
paul@1 | 647 | revisions = page.getRevList() |
paul@26 | 648 | |
paul@26 | 649 | if not revisions: |
paul@26 | 650 | return {} |
paul@26 | 651 | |
paul@1 | 652 | event_page_initial = Page(request, page.page_name, rev=revisions[-1]) |
paul@1 | 653 | |
paul@1 | 654 | # Get the created and last modified times. |
paul@1 | 655 | |
paul@1 | 656 | initial_revision = getPageRevision(event_page_initial) |
paul@1 | 657 | |
paul@1 | 658 | metadata = {} |
paul@1 | 659 | metadata["created"] = initial_revision["timestamp"] |
paul@1 | 660 | latest_revision = getPageRevision(page) |
paul@1 | 661 | metadata["last-modified"] = latest_revision["timestamp"] |
paul@1 | 662 | metadata["sequence"] = len(revisions) - 1 |
paul@1 | 663 | metadata["last-comment"] = latest_revision["comment"] |
paul@1 | 664 | |
paul@1 | 665 | return metadata |
paul@0 | 666 | |
paul@0 | 667 | def getPageRevision(page): |
paul@0 | 668 | |
paul@0 | 669 | "Return the revision details dictionary for the given 'page'." |
paul@0 | 670 | |
paul@0 | 671 | # From Page.edit_info... |
paul@0 | 672 | |
paul@0 | 673 | if hasattr(page, "editlog_entry"): |
paul@0 | 674 | line = page.editlog_entry() |
paul@0 | 675 | else: |
paul@0 | 676 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@0 | 677 | |
paul@0 | 678 | # Similar to Page.mtime_usecs behaviour... |
paul@0 | 679 | |
paul@0 | 680 | if line: |
paul@0 | 681 | timestamp = line.ed_time_usecs |
paul@0 | 682 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@0 | 683 | comment = line.comment |
paul@0 | 684 | else: |
paul@0 | 685 | mtime = 0 |
paul@0 | 686 | comment = "" |
paul@0 | 687 | |
paul@0 | 688 | # Leave the time zone empty. |
paul@0 | 689 | |
paul@0 | 690 | return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + (None,)), "comment" : comment} |
paul@0 | 691 | |
paul@11 | 692 | # Page parsing and formatting of embedded content. |
paul@11 | 693 | |
paul@15 | 694 | def getPageParserClass(request): |
paul@15 | 695 | |
paul@15 | 696 | "Using 'request', return a parser class for the current page's format." |
paul@15 | 697 | |
paul@16 | 698 | return getParserClass(request, getFormat(request.page)) |
paul@15 | 699 | |
paul@11 | 700 | def getParserClass(request, format): |
paul@11 | 701 | |
paul@11 | 702 | """ |
paul@11 | 703 | Return a parser class using the 'request' for the given 'format', returning |
paul@11 | 704 | a plain text parser if no parser can be found for the specified 'format'. |
paul@11 | 705 | """ |
paul@11 | 706 | |
paul@11 | 707 | try: |
paul@11 | 708 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@11 | 709 | except wikiutil.PluginMissingError: |
paul@11 | 710 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@11 | 711 | |
paul@15 | 712 | def getFormatterClass(request, format): |
paul@15 | 713 | |
paul@15 | 714 | """ |
paul@15 | 715 | Return a formatter class using the 'request' for the given output 'format', |
paul@15 | 716 | returning a plain text formatter if no formatter can be found for the |
paul@15 | 717 | specified 'format'. |
paul@15 | 718 | """ |
paul@15 | 719 | |
paul@15 | 720 | try: |
paul@15 | 721 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", format or "plain") |
paul@15 | 722 | except wikiutil.PluginMissingError: |
paul@15 | 723 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", "plain") |
paul@15 | 724 | |
paul@33 | 725 | def formatText(text, request, fmt, inhibit_p=True, parser_cls=None): |
paul@15 | 726 | |
paul@15 | 727 | """ |
paul@15 | 728 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@15 | 729 | Suppress line anchors in the output, and fix lists by indicating that a |
paul@15 | 730 | paragraph has already been started. |
paul@15 | 731 | """ |
paul@15 | 732 | |
paul@15 | 733 | if not parser_cls: |
paul@15 | 734 | parser_cls = getPageParserClass(request) |
paul@15 | 735 | parser = parser_cls(text, request, line_anchors=False) |
paul@15 | 736 | |
paul@15 | 737 | old_fmt = request.formatter |
paul@15 | 738 | request.formatter = fmt |
paul@15 | 739 | try: |
paul@33 | 740 | return redirectedOutput(request, parser, fmt, inhibit_p=inhibit_p) |
paul@15 | 741 | finally: |
paul@15 | 742 | request.formatter = old_fmt |
paul@15 | 743 | |
paul@11 | 744 | def redirectedOutput(request, parser, fmt, **kw): |
paul@11 | 745 | |
paul@11 | 746 | "A fixed version of the request method of the same name." |
paul@11 | 747 | |
paul@11 | 748 | buf = StringIO() |
paul@11 | 749 | request.redirect(buf) |
paul@11 | 750 | try: |
paul@11 | 751 | parser.format(fmt, **kw) |
paul@11 | 752 | if hasattr(fmt, "flush"): |
paul@11 | 753 | buf.write(fmt.flush(True)) |
paul@11 | 754 | finally: |
paul@11 | 755 | request.redirect() |
paul@11 | 756 | text = buf.getvalue() |
paul@11 | 757 | buf.close() |
paul@11 | 758 | return text |
paul@11 | 759 | |
paul@25 | 760 | # Textual representations. |
paul@25 | 761 | |
paul@25 | 762 | def getSimpleWikiText(text): |
paul@25 | 763 | |
paul@25 | 764 | """ |
paul@25 | 765 | Return the plain text representation of the given 'text' which may employ |
paul@25 | 766 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@25 | 767 | text. |
paul@25 | 768 | """ |
paul@25 | 769 | |
paul@25 | 770 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@25 | 771 | |
paul@25 | 772 | return "".join([s for s in verbatim_regexp.split(text) if s is not None]) |
paul@25 | 773 | |
paul@25 | 774 | def getEncodedWikiText(text): |
paul@25 | 775 | |
paul@25 | 776 | "Encode the given 'text' in a verbatim representation." |
paul@25 | 777 | |
paul@25 | 778 | return "<<Verbatim(%s)>>" % text |
paul@25 | 779 | |
paul@25 | 780 | def getPrettyTitle(title): |
paul@25 | 781 | |
paul@25 | 782 | "Return a nicely formatted version of the given 'title'." |
paul@25 | 783 | |
paul@25 | 784 | return title.replace("_", " ").replace("/", u" ? ") |
paul@25 | 785 | |
paul@0 | 786 | # User interface functions. |
paul@0 | 787 | |
paul@0 | 788 | def getParameter(request, name, default=None): |
paul@0 | 789 | |
paul@0 | 790 | """ |
paul@0 | 791 | Using the given 'request', return the value of the parameter with the given |
paul@0 | 792 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@0 | 793 | in the 'request'. |
paul@0 | 794 | """ |
paul@0 | 795 | |
paul@0 | 796 | return get_form(request).get(name, [default])[0] |
paul@0 | 797 | |
paul@0 | 798 | def getQualifiedParameter(request, prefix, argname, default=None): |
paul@0 | 799 | |
paul@0 | 800 | """ |
paul@0 | 801 | Using the given 'request', 'prefix' and 'argname', retrieve the value of the |
paul@0 | 802 | qualified parameter, returning the optional 'default' (or None) if no value |
paul@0 | 803 | was supplied in the 'request'. |
paul@0 | 804 | """ |
paul@0 | 805 | |
paul@0 | 806 | argname = getQualifiedParameterName(prefix, argname) |
paul@0 | 807 | return getParameter(request, argname, default) |
paul@0 | 808 | |
paul@0 | 809 | def getQualifiedParameterName(prefix, argname): |
paul@0 | 810 | |
paul@0 | 811 | """ |
paul@0 | 812 | Return the qualified parameter name using the given 'prefix' and 'argname'. |
paul@0 | 813 | """ |
paul@0 | 814 | |
paul@27 | 815 | if not prefix: |
paul@0 | 816 | return argname |
paul@0 | 817 | else: |
paul@0 | 818 | return "%s-%s" % (prefix, argname) |
paul@0 | 819 | |
paul@0 | 820 | # Page-related functions. |
paul@0 | 821 | |
paul@0 | 822 | def getPrettyPageName(page): |
paul@0 | 823 | |
paul@0 | 824 | "Return a nicely formatted title/name for the given 'page'." |
paul@0 | 825 | |
paul@0 | 826 | title = page.split_title(force=1) |
paul@0 | 827 | return getPrettyTitle(title) |
paul@0 | 828 | |
paul@25 | 829 | def linkToPage(request, page, text, query_string=None, anchor=None, **kw): |
paul@0 | 830 | |
paul@0 | 831 | """ |
paul@0 | 832 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@25 | 833 | optional 'query_string' and 'anchor'. |
paul@0 | 834 | """ |
paul@0 | 835 | |
paul@0 | 836 | text = wikiutil.escape(text) |
paul@25 | 837 | return page.link_to_raw(request, text, query_string, anchor, **kw) |
paul@0 | 838 | |
paul@25 | 839 | def linkToResource(url, request, text, query_string=None, anchor=None): |
paul@0 | 840 | |
paul@0 | 841 | """ |
paul@0 | 842 | Using 'request', return a link to 'url' with the given link 'text' and |
paul@25 | 843 | optional 'query_string' and 'anchor'. |
paul@0 | 844 | """ |
paul@0 | 845 | |
paul@25 | 846 | if anchor: |
paul@25 | 847 | url += "#%s" % anchor |
paul@25 | 848 | |
paul@0 | 849 | if query_string: |
paul@0 | 850 | query_string = wikiutil.makeQueryString(query_string) |
paul@25 | 851 | url += "?%s" % query_string |
paul@0 | 852 | |
paul@0 | 853 | formatter = request.page and getattr(request.page, "formatter", None) or request.html_formatter |
paul@0 | 854 | |
paul@0 | 855 | output = [] |
paul@0 | 856 | output.append(formatter.url(1, url)) |
paul@0 | 857 | output.append(formatter.text(text)) |
paul@0 | 858 | output.append(formatter.url(0)) |
paul@0 | 859 | return "".join(output) |
paul@0 | 860 | |
paul@0 | 861 | def getFullPageName(parent, title): |
paul@0 | 862 | |
paul@0 | 863 | """ |
paul@0 | 864 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@0 | 865 | and 'title' (a simple page name). |
paul@0 | 866 | """ |
paul@0 | 867 | |
paul@0 | 868 | if parent: |
paul@0 | 869 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@0 | 870 | else: |
paul@0 | 871 | return title |
paul@0 | 872 | |
paul@37 | 873 | # Content storage support. |
paul@37 | 874 | |
paul@37 | 875 | class ItemStore: |
paul@37 | 876 | |
paul@37 | 877 | "A page-specific item store." |
paul@37 | 878 | |
paul@37 | 879 | def __init__(self, page, item_dir_name="items", lock_dir_name="item-locks"): |
paul@37 | 880 | |
paul@37 | 881 | "Initialise an item store for the given 'page'." |
paul@37 | 882 | |
paul@37 | 883 | self.path = page.getPagePath(item_dir_name) |
paul@37 | 884 | self.next_path = os.path.join(self.path, "next") |
paul@37 | 885 | lock_dir = page.getPagePath(lock_dir_name) |
paul@37 | 886 | self.lock = lock.WriteLock(lock_dir) |
paul@37 | 887 | |
paul@37 | 888 | def get_next(self): |
paul@37 | 889 | |
paul@37 | 890 | "Return the next item number." |
paul@37 | 891 | |
paul@37 | 892 | next = self.read_next() |
paul@37 | 893 | if not next: |
paul@37 | 894 | next = self.deduce_next() |
paul@37 | 895 | self.write_next(next) |
paul@37 | 896 | return next |
paul@37 | 897 | |
paul@37 | 898 | def deduce_next(self): |
paul@37 | 899 | |
paul@37 | 900 | "Deduce the next item number from the existing item files." |
paul@37 | 901 | |
paul@37 | 902 | return max([int(filename) for filename in os.listdir(self.path) if filename.isdigit()] or [0]) + 1 |
paul@37 | 903 | |
paul@37 | 904 | def read_next(self): |
paul@37 | 905 | |
paul@37 | 906 | "Read the next item number from a special file." |
paul@37 | 907 | |
paul@37 | 908 | if not os.path.exists(self.next_path): |
paul@37 | 909 | return 0 |
paul@37 | 910 | |
paul@37 | 911 | f = open(self.next_path) |
paul@37 | 912 | try: |
paul@37 | 913 | try: |
paul@37 | 914 | return int(f.read()) |
paul@37 | 915 | except ValueError: |
paul@37 | 916 | return 0 |
paul@37 | 917 | finally: |
paul@37 | 918 | f.close() |
paul@37 | 919 | |
paul@37 | 920 | def write_next(self, next): |
paul@37 | 921 | |
paul@37 | 922 | "Write the 'next' item number to a special file." |
paul@37 | 923 | |
paul@37 | 924 | f = open(self.next_path, "w") |
paul@37 | 925 | try: |
paul@37 | 926 | f.write(str(next)) |
paul@37 | 927 | finally: |
paul@37 | 928 | f.close() |
paul@37 | 929 | |
paul@37 | 930 | def write_item(self, item, next): |
paul@37 | 931 | |
paul@37 | 932 | "Write the given 'item' to a file with the given 'next' item number." |
paul@37 | 933 | |
paul@37 | 934 | f = open(os.path.join(self.path, str(next)), "w") |
paul@37 | 935 | try: |
paul@39 | 936 | f.write(item) |
paul@37 | 937 | finally: |
paul@37 | 938 | f.close() |
paul@37 | 939 | |
paul@37 | 940 | def append(self, item): |
paul@37 | 941 | |
paul@37 | 942 | "Append the given 'item' to the store." |
paul@37 | 943 | |
paul@37 | 944 | self.lock.acquire() |
paul@37 | 945 | try: |
paul@37 | 946 | next = self.get_next() |
paul@37 | 947 | self.write_item(item, next) |
paul@37 | 948 | self.write_next(next + 1) |
paul@37 | 949 | finally: |
paul@37 | 950 | self.lock.release() |
paul@37 | 951 | |
paul@0 | 952 | # vim: tabstop=4 expandtab shiftwidth=4 |