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@65 | 6 | @copyright: 2000-2004 Juergen Hermann <jh@web.de> |
paul@65 | 7 | 2004 by Florian Festi |
paul@65 | 8 | 2006 by Mikko Virkkil |
paul@65 | 9 | 2005-2008 MoinMoin:ThomasWaldmann |
paul@65 | 10 | 2007 MoinMoin:ReimarBauer |
paul@65 | 11 | 2008 MoinMoin:FlorianKrupicka (redirectedOutput code) |
paul@0 | 12 | @license: GNU GPL (v2 or later), see COPYING.txt for details. |
paul@0 | 13 | """ |
paul@0 | 14 | |
paul@0 | 15 | from DateSupport import * |
paul@92 | 16 | from ItemSupport import DirectoryItemStore, GeneralItemStore |
paul@58 | 17 | from MoinMoin.parser import text_moin_wiki |
paul@92 | 18 | from MoinMoin.Page import Page, RootPage |
paul@91 | 19 | from MoinMoin.PageEditor import PageEditor |
paul@37 | 20 | from MoinMoin.util import lock |
paul@25 | 21 | from MoinMoin import config, search, wikiutil |
paul@10 | 22 | from shlex import shlex |
paul@0 | 23 | import re |
paul@0 | 24 | import time |
paul@37 | 25 | import os |
paul@66 | 26 | import codecs |
paul@66 | 27 | |
paul@66 | 28 | try: |
paul@66 | 29 | from cStringIO import StringIO |
paul@66 | 30 | except ImportError: |
paul@66 | 31 | from StringIO import StringIO |
paul@0 | 32 | |
paul@35 | 33 | # Moin 1.9 request parameters. |
paul@35 | 34 | |
paul@35 | 35 | try: |
paul@35 | 36 | from MoinMoin.support.werkzeug.datastructures import MultiDict |
paul@35 | 37 | except ImportError: |
paul@35 | 38 | pass |
paul@35 | 39 | |
paul@89 | 40 | __version__ = "0.4.1" |
paul@0 | 41 | |
paul@25 | 42 | # Extraction of shared fragments. |
paul@25 | 43 | |
paul@25 | 44 | marker_regexp_str = r"([{]{3,}|[}]{3,})" |
paul@25 | 45 | marker_regexp = re.compile(marker_regexp_str, re.MULTILINE | re.DOTALL) # {{{... or }}}... |
paul@25 | 46 | |
paul@29 | 47 | # Extraction of headings. |
paul@29 | 48 | |
paul@29 | 49 | heading_regexp = re.compile(r"^(?P<level>=+)(?P<heading>.*?)(?P=level)$", re.UNICODE | re.MULTILINE) |
paul@29 | 50 | |
paul@25 | 51 | # Category extraction from pages. |
paul@25 | 52 | |
paul@25 | 53 | category_regexp = None |
paul@25 | 54 | |
paul@25 | 55 | # Simple content parsing. |
paul@25 | 56 | |
paul@25 | 57 | verbatim_regexp = re.compile(ur'(?:' |
paul@25 | 58 | ur'<<Verbatim\((?P<verbatim>.*?)\)>>' |
paul@25 | 59 | ur'|' |
paul@25 | 60 | ur'\[\[Verbatim\((?P<verbatim2>.*?)\)\]\]' |
paul@25 | 61 | ur'|' |
paul@25 | 62 | ur'!(?P<verbatim3>.*?)(\s|$)?' |
paul@25 | 63 | ur'|' |
paul@25 | 64 | ur'`(?P<monospace>.*?)`' |
paul@25 | 65 | ur'|' |
paul@25 | 66 | ur'{{{(?P<preformatted>.*?)}}}' |
paul@25 | 67 | ur')', re.UNICODE) |
paul@25 | 68 | |
paul@25 | 69 | # Category discovery. |
paul@0 | 70 | |
paul@25 | 71 | def getCategoryPattern(request): |
paul@25 | 72 | global category_regexp |
paul@25 | 73 | |
paul@25 | 74 | try: |
paul@25 | 75 | return request.cfg.cache.page_category_regexact |
paul@25 | 76 | except AttributeError: |
paul@25 | 77 | |
paul@25 | 78 | # Use regular expression from MoinMoin 1.7.1 otherwise. |
paul@25 | 79 | |
paul@25 | 80 | if category_regexp is None: |
paul@25 | 81 | category_regexp = re.compile(u'^%s$' % ur'(?P<all>Category(?P<key>(?!Template)\S+))', re.UNICODE) |
paul@25 | 82 | return category_regexp |
paul@25 | 83 | |
paul@25 | 84 | def getCategories(request): |
paul@25 | 85 | |
paul@25 | 86 | """ |
paul@25 | 87 | From the AdvancedSearch macro, return a list of category page names using |
paul@25 | 88 | the given 'request'. |
paul@25 | 89 | """ |
paul@25 | 90 | |
paul@25 | 91 | # This will return all pages with "Category" in the title. |
paul@25 | 92 | |
paul@25 | 93 | cat_filter = getCategoryPattern(request).search |
paul@25 | 94 | return request.rootpage.getPageList(filter=cat_filter) |
paul@25 | 95 | |
paul@25 | 96 | def getCategoryMapping(category_pagenames, request): |
paul@2 | 97 | |
paul@2 | 98 | """ |
paul@25 | 99 | For the given 'category_pagenames' return a list of tuples of the form |
paul@25 | 100 | (category name, category page name) using the given 'request'. |
paul@25 | 101 | """ |
paul@25 | 102 | |
paul@25 | 103 | cat_pattern = getCategoryPattern(request) |
paul@25 | 104 | mapping = [] |
paul@25 | 105 | for pagename in category_pagenames: |
paul@25 | 106 | name = cat_pattern.match(pagename).group("key") |
paul@25 | 107 | if name != "Category": |
paul@25 | 108 | mapping.append((name, pagename)) |
paul@25 | 109 | mapping.sort() |
paul@25 | 110 | return mapping |
paul@25 | 111 | |
paul@25 | 112 | def getCategoryPages(pagename, request): |
paul@25 | 113 | |
paul@25 | 114 | """ |
paul@25 | 115 | Return the pages associated with the given category 'pagename' using the |
paul@25 | 116 | 'request'. |
paul@25 | 117 | """ |
paul@25 | 118 | |
paul@25 | 119 | query = search.QueryParser().parse_query('category:%s' % pagename) |
paul@25 | 120 | results = search.searchPages(request, query, "page_name") |
paul@27 | 121 | return filterCategoryPages(results, request) |
paul@27 | 122 | |
paul@27 | 123 | def filterCategoryPages(results, request): |
paul@27 | 124 | |
paul@27 | 125 | "Filter category pages from the given 'results' using the 'request'." |
paul@25 | 126 | |
paul@25 | 127 | cat_pattern = getCategoryPattern(request) |
paul@25 | 128 | pages = [] |
paul@25 | 129 | for page in results.hits: |
paul@25 | 130 | if not cat_pattern.match(page.page_name): |
paul@25 | 131 | pages.append(page) |
paul@25 | 132 | return pages |
paul@25 | 133 | |
paul@25 | 134 | def getAllCategoryPages(category_names, request): |
paul@25 | 135 | |
paul@25 | 136 | """ |
paul@25 | 137 | Return all pages belonging to the categories having the given |
paul@25 | 138 | 'category_names', using the given 'request'. |
paul@25 | 139 | """ |
paul@25 | 140 | |
paul@25 | 141 | pages = [] |
paul@25 | 142 | pagenames = set() |
paul@25 | 143 | |
paul@25 | 144 | for category_name in category_names: |
paul@25 | 145 | |
paul@25 | 146 | # Get the pages and page names in the category. |
paul@25 | 147 | |
paul@25 | 148 | pages_in_category = getCategoryPages(category_name, request) |
paul@25 | 149 | |
paul@25 | 150 | # Visit each page in the category. |
paul@25 | 151 | |
paul@25 | 152 | for page_in_category in pages_in_category: |
paul@25 | 153 | pagename = page_in_category.page_name |
paul@25 | 154 | |
paul@25 | 155 | # Only process each page once. |
paul@25 | 156 | |
paul@25 | 157 | if pagename in pagenames: |
paul@25 | 158 | continue |
paul@25 | 159 | else: |
paul@25 | 160 | pagenames.add(pagename) |
paul@25 | 161 | |
paul@25 | 162 | pages.append(page_in_category) |
paul@25 | 163 | |
paul@25 | 164 | return pages |
paul@25 | 165 | |
paul@28 | 166 | def getPagesForSearch(search_pattern, request): |
paul@27 | 167 | |
paul@27 | 168 | """ |
paul@27 | 169 | Return result pages for a search employing the given 'search_pattern' and |
paul@28 | 170 | using the given 'request'. |
paul@27 | 171 | """ |
paul@27 | 172 | |
paul@27 | 173 | query = search.QueryParser().parse_query(search_pattern) |
paul@27 | 174 | results = search.searchPages(request, query, "page_name") |
paul@27 | 175 | return filterCategoryPages(results, request) |
paul@27 | 176 | |
paul@25 | 177 | # WikiDict functions. |
paul@25 | 178 | |
paul@64 | 179 | def getWikiDict(pagename, request, superuser=False): |
paul@25 | 180 | |
paul@25 | 181 | """ |
paul@25 | 182 | Return the WikiDict provided by the given 'pagename' using the given |
paul@64 | 183 | 'request'. If the optional 'superuser' is specified as a true value, no read |
paul@64 | 184 | access check will be made. |
paul@2 | 185 | """ |
paul@2 | 186 | |
paul@64 | 187 | if pagename and Page(request, pagename).exists() and (superuser or request.user.may.read(pagename)): |
paul@25 | 188 | if hasattr(request.dicts, "dict"): |
paul@25 | 189 | return request.dicts.dict(pagename) |
paul@25 | 190 | else: |
paul@25 | 191 | return request.dicts[pagename] |
paul@0 | 192 | else: |
paul@25 | 193 | return None |
paul@25 | 194 | |
paul@25 | 195 | # Searching-related functions. |
paul@25 | 196 | |
paul@25 | 197 | def getPagesFromResults(result_pages, request): |
paul@25 | 198 | |
paul@25 | 199 | "Return genuine pages for the given 'result_pages' using the 'request'." |
paul@25 | 200 | |
paul@25 | 201 | return [Page(request, page.page_name) for page in result_pages] |
paul@25 | 202 | |
paul@25 | 203 | # Region/section parsing. |
paul@25 | 204 | |
paul@25 | 205 | def getRegions(s, include_non_regions=False): |
paul@25 | 206 | |
paul@25 | 207 | """ |
paul@25 | 208 | Parse the string 's', returning a list of explicitly declared regions. |
paul@25 | 209 | |
paul@25 | 210 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 211 | included for text between explicitly declared regions. |
paul@25 | 212 | """ |
paul@25 | 213 | |
paul@25 | 214 | regions = [] |
paul@25 | 215 | marker = None |
paul@25 | 216 | is_block = True |
paul@25 | 217 | |
paul@25 | 218 | # Start a region for exposed text, if appropriate. |
paul@25 | 219 | |
paul@25 | 220 | if include_non_regions: |
paul@25 | 221 | regions.append("") |
paul@25 | 222 | |
paul@25 | 223 | for match_text in marker_regexp.split(s): |
paul@25 | 224 | |
paul@25 | 225 | # Capture section text. |
paul@25 | 226 | |
paul@25 | 227 | if is_block: |
paul@25 | 228 | if marker or include_non_regions: |
paul@25 | 229 | regions[-1] += match_text |
paul@25 | 230 | |
paul@25 | 231 | # Handle section markers. |
paul@25 | 232 | |
paul@31 | 233 | else: |
paul@25 | 234 | |
paul@25 | 235 | # Close any open sections, returning to exposed text regions. |
paul@25 | 236 | |
paul@25 | 237 | if marker: |
paul@31 | 238 | |
paul@31 | 239 | # Add any marker to the current region, regardless of whether it |
paul@31 | 240 | # successfully closes a section. |
paul@31 | 241 | |
paul@31 | 242 | regions[-1] += match_text |
paul@31 | 243 | |
paul@25 | 244 | if match_text.startswith("}") and len(marker) == len(match_text): |
paul@25 | 245 | marker = None |
paul@25 | 246 | |
paul@25 | 247 | # Start a region for exposed text, if appropriate. |
paul@25 | 248 | |
paul@25 | 249 | if include_non_regions: |
paul@25 | 250 | regions.append("") |
paul@25 | 251 | |
paul@31 | 252 | # Without a current marker, start a new section. |
paul@25 | 253 | |
paul@31 | 254 | else: |
paul@25 | 255 | marker = match_text |
paul@25 | 256 | regions.append("") |
paul@25 | 257 | |
paul@31 | 258 | # Add the marker to the new region. |
paul@25 | 259 | |
paul@31 | 260 | regions[-1] += match_text |
paul@0 | 261 | |
paul@25 | 262 | # The match text alternates between text between markers and the markers |
paul@25 | 263 | # themselves. |
paul@25 | 264 | |
paul@25 | 265 | is_block = not is_block |
paul@25 | 266 | |
paul@25 | 267 | return regions |
paul@25 | 268 | |
paul@25 | 269 | def getFragmentsFromRegions(regions): |
paul@25 | 270 | |
paul@25 | 271 | """ |
paul@25 | 272 | Return fragments from the given 'regions', each having the form |
paul@32 | 273 | (format, attributes, body text). |
paul@25 | 274 | """ |
paul@25 | 275 | |
paul@25 | 276 | fragments = [] |
paul@25 | 277 | |
paul@25 | 278 | for region in regions: |
paul@32 | 279 | format, attributes, body, header, close = getFragmentFromRegion(region) |
paul@32 | 280 | fragments.append((format, attributes, body)) |
paul@25 | 281 | |
paul@32 | 282 | return fragments |
paul@25 | 283 | |
paul@32 | 284 | def getFragmentFromRegion(region): |
paul@25 | 285 | |
paul@32 | 286 | """ |
paul@32 | 287 | Return a fragment for the given 'region' having the form (format, |
paul@32 | 288 | attributes, body text, header, close), where the 'header' is the original |
paul@32 | 289 | declaration of the 'region' or None if no explicit region is defined, and |
paul@32 | 290 | 'close' is the closing marker of the 'region' or None if no explicit region |
paul@32 | 291 | is defined. |
paul@32 | 292 | """ |
paul@25 | 293 | |
paul@32 | 294 | if region.startswith("{{{"): |
paul@25 | 295 | |
paul@32 | 296 | body = region.lstrip("{") |
paul@32 | 297 | level = len(region) - len(body) |
paul@32 | 298 | body = body.rstrip("}").lstrip() |
paul@32 | 299 | |
paul@32 | 300 | # Remove any prelude and process metadata. |
paul@32 | 301 | |
paul@32 | 302 | if body.startswith("#!"): |
paul@25 | 303 | |
paul@32 | 304 | try: |
paul@32 | 305 | declaration, body = body.split("\n", 1) |
paul@32 | 306 | except ValueError: |
paul@32 | 307 | declaration = body |
paul@32 | 308 | body = "" |
paul@25 | 309 | |
paul@32 | 310 | arguments = declaration[2:] |
paul@32 | 311 | |
paul@32 | 312 | # Get any parser/format declaration. |
paul@25 | 313 | |
paul@32 | 314 | if arguments and not arguments[0].isspace(): |
paul@32 | 315 | details = arguments.split(None, 1) |
paul@32 | 316 | if len(details) == 2: |
paul@32 | 317 | format, arguments = details |
paul@32 | 318 | else: |
paul@32 | 319 | format = details[0] |
paul@32 | 320 | arguments = "" |
paul@32 | 321 | else: |
paul@32 | 322 | format = None |
paul@25 | 323 | |
paul@32 | 324 | # Get the attributes/arguments for the region. |
paul@32 | 325 | |
paul@32 | 326 | attributes = parseAttributes(arguments, False) |
paul@25 | 327 | |
paul@32 | 328 | # Add an entry for the format in the attribute dictionary. |
paul@25 | 329 | |
paul@32 | 330 | if format and not attributes.has_key(format): |
paul@32 | 331 | attributes[format] = True |
paul@32 | 332 | |
paul@32 | 333 | return format, attributes, body, level * "{" + declaration + "\n", level * "}" |
paul@25 | 334 | |
paul@25 | 335 | else: |
paul@32 | 336 | return None, {}, body, level * "{" + "\n", level * "}" |
paul@25 | 337 | |
paul@32 | 338 | else: |
paul@32 | 339 | return None, {}, region, None, None |
paul@25 | 340 | |
paul@25 | 341 | def getFragments(s, include_non_regions=False): |
paul@25 | 342 | |
paul@25 | 343 | """ |
paul@25 | 344 | Return fragments for the given string 's', each having the form |
paul@25 | 345 | (format, arguments, body text). |
paul@25 | 346 | |
paul@25 | 347 | If 'include_non_regions' is specified as a true value, fragments will be |
paul@25 | 348 | included for text between explicitly declared regions. |
paul@25 | 349 | """ |
paul@25 | 350 | |
paul@25 | 351 | return getFragmentsFromRegions(getRegions(s, include_non_regions)) |
paul@25 | 352 | |
paul@29 | 353 | # Heading extraction. |
paul@29 | 354 | |
paul@29 | 355 | def getHeadings(s): |
paul@29 | 356 | |
paul@29 | 357 | """ |
paul@29 | 358 | Return tuples of the form (level, title, span) for headings found within the |
paul@29 | 359 | given string 's'. The span is itself a (start, end) tuple indicating the |
paul@29 | 360 | matching region of 's' for a heading declaration. |
paul@29 | 361 | """ |
paul@29 | 362 | |
paul@29 | 363 | headings = [] |
paul@29 | 364 | |
paul@29 | 365 | for match in heading_regexp.finditer(s): |
paul@29 | 366 | headings.append( |
paul@29 | 367 | (len(match.group("level")), match.group("heading"), match.span()) |
paul@29 | 368 | ) |
paul@29 | 369 | |
paul@29 | 370 | return headings |
paul@29 | 371 | |
paul@25 | 372 | # Region/section attribute parsing. |
paul@0 | 373 | |
paul@10 | 374 | def parseAttributes(s, escape=True): |
paul@10 | 375 | |
paul@10 | 376 | """ |
paul@10 | 377 | Parse the section attributes string 's', returning a mapping of names to |
paul@10 | 378 | values. If 'escape' is set to a true value, the attributes will be suitable |
paul@10 | 379 | for use with the formatter API. If 'escape' is set to a false value, the |
paul@10 | 380 | attributes will have any quoting removed. |
paul@66 | 381 | |
paul@66 | 382 | Because Unicode was probably not around when shlex, used here to tokenise |
paul@66 | 383 | the attributes, was introduced, and since StringIO is not Unicode-capable, |
paul@66 | 384 | any non-ASCII characters should be quoted in attributes. |
paul@10 | 385 | """ |
paul@10 | 386 | |
paul@10 | 387 | attrs = {} |
paul@66 | 388 | f = StringIO(s.encode("utf-8")) |
paul@10 | 389 | name = None |
paul@10 | 390 | need_value = False |
paul@34 | 391 | lex = shlex(f) |
paul@34 | 392 | lex.wordchars += "-" |
paul@10 | 393 | |
paul@34 | 394 | for token in lex: |
paul@66 | 395 | token = unicode(token, "utf-8") |
paul@10 | 396 | |
paul@10 | 397 | # Capture the name if needed. |
paul@10 | 398 | |
paul@10 | 399 | if name is None: |
paul@10 | 400 | name = escape and wikiutil.escape(token) or strip_token(token) |
paul@10 | 401 | |
paul@10 | 402 | # Detect either an equals sign or another name. |
paul@10 | 403 | |
paul@10 | 404 | elif not need_value: |
paul@10 | 405 | if token == "=": |
paul@10 | 406 | need_value = True |
paul@10 | 407 | else: |
paul@10 | 408 | attrs[name.lower()] = escape and "true" or True |
paul@10 | 409 | name = wikiutil.escape(token) |
paul@10 | 410 | |
paul@10 | 411 | # Otherwise, capture a value. |
paul@10 | 412 | |
paul@10 | 413 | else: |
paul@10 | 414 | # Quoting of attributes done similarly to wikiutil.parseAttributes. |
paul@10 | 415 | |
paul@10 | 416 | if token: |
paul@10 | 417 | if escape: |
paul@10 | 418 | if token[0] in ("'", '"'): |
paul@10 | 419 | token = wikiutil.escape(token) |
paul@10 | 420 | else: |
paul@10 | 421 | token = '"%s"' % wikiutil.escape(token, 1) |
paul@10 | 422 | else: |
paul@10 | 423 | token = strip_token(token) |
paul@10 | 424 | |
paul@10 | 425 | attrs[name.lower()] = token |
paul@10 | 426 | name = None |
paul@10 | 427 | need_value = False |
paul@10 | 428 | |
paul@13 | 429 | # Handle any name-only attributes at the end of the collection. |
paul@13 | 430 | |
paul@13 | 431 | if name and not need_value: |
paul@13 | 432 | attrs[name.lower()] = escape and "true" or True |
paul@13 | 433 | |
paul@10 | 434 | return attrs |
paul@10 | 435 | |
paul@10 | 436 | def strip_token(token): |
paul@10 | 437 | |
paul@10 | 438 | "Return the given 'token' stripped of quoting." |
paul@10 | 439 | |
paul@10 | 440 | if token[0] in ("'", '"') and token[-1] == token[0]: |
paul@10 | 441 | return token[1:-1] |
paul@10 | 442 | else: |
paul@10 | 443 | return token |
paul@10 | 444 | |
paul@46 | 445 | # Macro argument parsing. |
paul@46 | 446 | |
paul@46 | 447 | def parseMacroArguments(args): |
paul@46 | 448 | |
paul@46 | 449 | """ |
paul@46 | 450 | Interpret the arguments. To support commas in labels, the label argument |
paul@46 | 451 | should be quoted. For example: |
paul@46 | 452 | |
paul@46 | 453 | "label=No, thanks!" |
paul@46 | 454 | """ |
paul@46 | 455 | |
paul@46 | 456 | try: |
paul@46 | 457 | parsed_args = args and wikiutil.parse_quoted_separated(args, name_value=False) or [] |
paul@46 | 458 | except AttributeError: |
paul@46 | 459 | parsed_args = args.split(",") |
paul@46 | 460 | |
paul@46 | 461 | pairs = [] |
paul@46 | 462 | for arg in parsed_args: |
paul@46 | 463 | if arg: |
paul@46 | 464 | pair = arg.split("=", 1) |
paul@46 | 465 | if len(pair) < 2: |
paul@46 | 466 | pairs.append((None, arg)) |
paul@46 | 467 | else: |
paul@46 | 468 | pairs.append(tuple(pair)) |
paul@46 | 469 | |
paul@46 | 470 | return pairs |
paul@46 | 471 | |
paul@62 | 472 | def parseDictEntry(entry, unqualified=None): |
paul@62 | 473 | |
paul@62 | 474 | """ |
paul@62 | 475 | Return the parameters specified by the given dict 'entry' string. The |
paul@62 | 476 | optional 'unqualified' parameter can be used to indicate parameters that |
paul@62 | 477 | need not be specified together with a keyword and can therefore be populated |
paul@62 | 478 | in the given order as such unqualified parameters are encountered. |
paul@62 | 479 | |
paul@62 | 480 | NOTE: This is similar to parseMacroArguments but employs space as a |
paul@62 | 481 | NOTE: separator and attempts to assign unqualified parameters. |
paul@62 | 482 | """ |
paul@62 | 483 | |
paul@62 | 484 | parameters = {} |
paul@62 | 485 | unqualified = unqualified or () |
paul@62 | 486 | |
paul@62 | 487 | try: |
paul@62 | 488 | parsed_args = entry and wikiutil.parse_quoted_separated(entry, separator=None, name_value=False) or [] |
paul@62 | 489 | except AttributeError: |
paul@62 | 490 | parsed_args = entry.split() |
paul@62 | 491 | |
paul@62 | 492 | for arg in parsed_args: |
paul@62 | 493 | try: |
paul@62 | 494 | argname, argvalue = arg.split("=", 1) |
paul@62 | 495 | |
paul@62 | 496 | # Detect unlikely parameter names. |
paul@62 | 497 | |
paul@62 | 498 | if not argname.isalpha(): |
paul@62 | 499 | raise ValueError |
paul@62 | 500 | |
paul@62 | 501 | parameters[argname] = argvalue |
paul@62 | 502 | |
paul@62 | 503 | # Unqualified parameters are assumed to be one of a recognised set. |
paul@62 | 504 | |
paul@62 | 505 | except ValueError: |
paul@62 | 506 | for argname in unqualified: |
paul@62 | 507 | if not parameters.has_key(argname): |
paul@62 | 508 | parameters[argname] = arg |
paul@62 | 509 | break |
paul@62 | 510 | |
paul@62 | 511 | return parameters |
paul@62 | 512 | |
paul@84 | 513 | # Macro argument quoting. |
paul@84 | 514 | |
paul@84 | 515 | def quoteMacroArguments(args): |
paul@84 | 516 | |
paul@84 | 517 | """ |
paul@84 | 518 | Quote the given 'args' - a collection of (name, value) tuples - returning a |
paul@84 | 519 | string containing the comma-separated, quoted arguments. |
paul@84 | 520 | """ |
paul@84 | 521 | |
paul@84 | 522 | quoted = [] |
paul@84 | 523 | |
paul@84 | 524 | for name, value in args: |
paul@84 | 525 | quoted.append(quoteMacroArgument(name, value)) |
paul@84 | 526 | |
paul@84 | 527 | return ",".join(quoted) |
paul@84 | 528 | |
paul@84 | 529 | def quoteMacroArgument(name, value): |
paul@84 | 530 | |
paul@84 | 531 | """ |
paul@84 | 532 | Quote the argument with the given 'name' (or None indicating an unnamed |
paul@84 | 533 | argument) and 'value' so that it can be used with a macro. |
paul@84 | 534 | """ |
paul@84 | 535 | |
paul@84 | 536 | value = unicode(value).replace('"', '""') |
paul@84 | 537 | if name is None: |
paul@84 | 538 | return '"%s"' % value |
paul@84 | 539 | else: |
paul@84 | 540 | return '"%s=%s"' % (name, value) |
paul@84 | 541 | |
paul@25 | 542 | # Request-related classes and associated functions. |
paul@0 | 543 | |
paul@0 | 544 | class Form: |
paul@0 | 545 | |
paul@0 | 546 | """ |
paul@0 | 547 | A wrapper preserving MoinMoin 1.8.x (and earlier) behaviour in a 1.9.x |
paul@0 | 548 | environment. |
paul@0 | 549 | """ |
paul@0 | 550 | |
paul@35 | 551 | def __init__(self, request): |
paul@35 | 552 | self.request = request |
paul@35 | 553 | self.form = request.values |
paul@0 | 554 | |
paul@0 | 555 | def has_key(self, name): |
paul@0 | 556 | return not not self.form.getlist(name) |
paul@0 | 557 | |
paul@0 | 558 | def get(self, name, default=None): |
paul@0 | 559 | values = self.form.getlist(name) |
paul@0 | 560 | if not values: |
paul@0 | 561 | return default |
paul@0 | 562 | else: |
paul@0 | 563 | return values |
paul@0 | 564 | |
paul@0 | 565 | def __getitem__(self, name): |
paul@0 | 566 | return self.form.getlist(name) |
paul@0 | 567 | |
paul@35 | 568 | def __setitem__(self, name, value): |
paul@35 | 569 | try: |
paul@35 | 570 | self.form.setlist(name, value) |
paul@35 | 571 | except TypeError: |
paul@35 | 572 | self._write_enable() |
paul@35 | 573 | self.form.setlist(name, value) |
paul@35 | 574 | |
paul@33 | 575 | def __delitem__(self, name): |
paul@35 | 576 | try: |
paul@35 | 577 | del self.form[name] |
paul@35 | 578 | except TypeError: |
paul@35 | 579 | self._write_enable() |
paul@35 | 580 | del self.form[name] |
paul@35 | 581 | |
paul@35 | 582 | def _write_enable(self): |
paul@35 | 583 | self.form = self.request.values = MultiDict(self.form) |
paul@33 | 584 | |
paul@33 | 585 | def keys(self): |
paul@33 | 586 | return self.form.keys() |
paul@33 | 587 | |
paul@32 | 588 | def items(self): |
paul@35 | 589 | return self.form.lists() |
paul@32 | 590 | |
paul@0 | 591 | class ActionSupport: |
paul@0 | 592 | |
paul@0 | 593 | """ |
paul@0 | 594 | Work around disruptive MoinMoin changes in 1.9, and also provide useful |
paul@0 | 595 | convenience methods. |
paul@0 | 596 | """ |
paul@0 | 597 | |
paul@0 | 598 | def get_form(self): |
paul@0 | 599 | return get_form(self.request) |
paul@0 | 600 | |
paul@0 | 601 | def _get_selected(self, value, input_value): |
paul@0 | 602 | |
paul@0 | 603 | """ |
paul@0 | 604 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 605 | otherwise) if 'value' matches 'input_value'. |
paul@0 | 606 | """ |
paul@0 | 607 | |
paul@0 | 608 | return input_value is not None and value == input_value and 'selected="selected"' or '' |
paul@0 | 609 | |
paul@0 | 610 | def _get_selected_for_list(self, value, input_values): |
paul@0 | 611 | |
paul@0 | 612 | """ |
paul@0 | 613 | Return the HTML attribute text indicating selection of an option (or |
paul@0 | 614 | otherwise) if 'value' matches one of the 'input_values'. |
paul@0 | 615 | """ |
paul@0 | 616 | |
paul@0 | 617 | return value in input_values and 'selected="selected"' or '' |
paul@0 | 618 | |
paul@26 | 619 | def get_option_list(self, value, values): |
paul@26 | 620 | |
paul@26 | 621 | """ |
paul@26 | 622 | Return a list of HTML element definitions for options describing the |
paul@26 | 623 | given 'values', selecting the option with the specified 'value' if |
paul@26 | 624 | present. |
paul@26 | 625 | """ |
paul@26 | 626 | |
paul@26 | 627 | options = [] |
paul@26 | 628 | for available_value in values: |
paul@26 | 629 | selected = self._get_selected(available_value, value) |
paul@26 | 630 | options.append('<option value="%s" %s>%s</option>' % ( |
paul@26 | 631 | escattr(available_value), selected, wikiutil.escape(available_value))) |
paul@26 | 632 | return options |
paul@26 | 633 | |
paul@0 | 634 | def _get_input(self, form, name, default=None): |
paul@0 | 635 | |
paul@0 | 636 | """ |
paul@0 | 637 | Return the input from 'form' having the given 'name', returning either |
paul@0 | 638 | the input converted to an integer or the given 'default' (optional, None |
paul@0 | 639 | if not specified). |
paul@0 | 640 | """ |
paul@0 | 641 | |
paul@0 | 642 | value = form.get(name, [None])[0] |
paul@0 | 643 | if not value: # true if 0 obtained |
paul@0 | 644 | return default |
paul@0 | 645 | else: |
paul@0 | 646 | return int(value) |
paul@0 | 647 | |
paul@0 | 648 | def get_form(request): |
paul@0 | 649 | |
paul@0 | 650 | "Work around disruptive MoinMoin changes in 1.9." |
paul@0 | 651 | |
paul@0 | 652 | if hasattr(request, "values"): |
paul@35 | 653 | return Form(request) |
paul@0 | 654 | else: |
paul@0 | 655 | return request.form |
paul@0 | 656 | |
paul@0 | 657 | class send_headers_cls: |
paul@0 | 658 | |
paul@0 | 659 | """ |
paul@0 | 660 | A wrapper to preserve MoinMoin 1.8.x (and earlier) request behaviour in a |
paul@0 | 661 | 1.9.x environment. |
paul@0 | 662 | """ |
paul@0 | 663 | |
paul@0 | 664 | def __init__(self, request): |
paul@0 | 665 | self.request = request |
paul@0 | 666 | |
paul@0 | 667 | def __call__(self, headers): |
paul@0 | 668 | for header in headers: |
paul@0 | 669 | parts = header.split(":") |
paul@0 | 670 | self.request.headers.add(parts[0], ":".join(parts[1:])) |
paul@0 | 671 | |
paul@0 | 672 | def get_send_headers(request): |
paul@0 | 673 | |
paul@0 | 674 | "Return a function that can send response headers." |
paul@0 | 675 | |
paul@0 | 676 | if hasattr(request, "http_headers"): |
paul@0 | 677 | return request.http_headers |
paul@0 | 678 | elif hasattr(request, "emit_http_headers"): |
paul@0 | 679 | return request.emit_http_headers |
paul@0 | 680 | else: |
paul@0 | 681 | return send_headers_cls(request) |
paul@0 | 682 | |
paul@0 | 683 | def escattr(s): |
paul@0 | 684 | return wikiutil.escape(s, 1) |
paul@0 | 685 | |
paul@0 | 686 | def getPathInfo(request): |
paul@0 | 687 | if hasattr(request, "getPathinfo"): |
paul@0 | 688 | return request.getPathinfo() |
paul@0 | 689 | else: |
paul@0 | 690 | return request.path |
paul@0 | 691 | |
paul@14 | 692 | def getHeader(request, header_name, prefix=None): |
paul@17 | 693 | |
paul@17 | 694 | """ |
paul@17 | 695 | Using the 'request', return the value of the header with the given |
paul@17 | 696 | 'header_name', using the optional 'prefix' to obtain protocol-specific |
paul@17 | 697 | headers if necessary. |
paul@17 | 698 | |
paul@17 | 699 | If no value is found for the given 'header_name', None is returned. |
paul@17 | 700 | """ |
paul@17 | 701 | |
paul@14 | 702 | if hasattr(request, "getHeader"): |
paul@14 | 703 | return request.getHeader(header_name) |
paul@14 | 704 | elif hasattr(request, "headers"): |
paul@17 | 705 | return request.headers.get(header_name) |
paul@64 | 706 | elif hasattr(request, "env"): |
paul@64 | 707 | return request.env.get((prefix and prefix + "_" or "") + header_name.upper()) |
paul@14 | 708 | else: |
paul@64 | 709 | return None |
paul@14 | 710 | |
paul@23 | 711 | def writeHeaders(request, mimetype, metadata, status=None): |
paul@23 | 712 | |
paul@23 | 713 | """ |
paul@23 | 714 | Using the 'request', write resource headers using the given 'mimetype', |
paul@23 | 715 | based on the given 'metadata'. If the optional 'status' is specified, set |
paul@23 | 716 | the status header to the given value. |
paul@23 | 717 | """ |
paul@23 | 718 | |
paul@23 | 719 | send_headers = get_send_headers(request) |
paul@23 | 720 | |
paul@23 | 721 | # Define headers. |
paul@23 | 722 | |
paul@23 | 723 | headers = ["Content-Type: %s; charset=%s" % (mimetype, config.charset)] |
paul@23 | 724 | |
paul@23 | 725 | # Define the last modified time. |
paul@23 | 726 | # NOTE: Consider using request.httpDate. |
paul@23 | 727 | |
paul@23 | 728 | latest_timestamp = metadata.get("last-modified") |
paul@23 | 729 | if latest_timestamp: |
paul@23 | 730 | headers.append("Last-Modified: %s" % latest_timestamp.as_HTTP_datetime_string()) |
paul@23 | 731 | |
paul@23 | 732 | if status: |
paul@23 | 733 | headers.append("Status: %s" % status) |
paul@23 | 734 | |
paul@23 | 735 | send_headers(headers) |
paul@23 | 736 | |
paul@1 | 737 | # Page access functions. |
paul@1 | 738 | |
paul@1 | 739 | def getPageURL(page): |
paul@1 | 740 | |
paul@1 | 741 | "Return the URL of the given 'page'." |
paul@1 | 742 | |
paul@1 | 743 | request = page.request |
paul@1 | 744 | return request.getQualifiedURL(page.url(request, relative=0)) |
paul@1 | 745 | |
paul@1 | 746 | def getFormat(page): |
paul@1 | 747 | |
paul@1 | 748 | "Get the format used on the given 'page'." |
paul@1 | 749 | |
paul@1 | 750 | return page.pi["format"] |
paul@1 | 751 | |
paul@1 | 752 | def getMetadata(page): |
paul@1 | 753 | |
paul@1 | 754 | """ |
paul@1 | 755 | Return a dictionary containing items describing for the given 'page' the |
paul@1 | 756 | page's "created" time, "last-modified" time, "sequence" (or revision number) |
paul@1 | 757 | and the "last-comment" made about the last edit. |
paul@1 | 758 | """ |
paul@1 | 759 | |
paul@1 | 760 | request = page.request |
paul@1 | 761 | |
paul@1 | 762 | # Get the initial revision of the page. |
paul@1 | 763 | |
paul@1 | 764 | revisions = page.getRevList() |
paul@26 | 765 | |
paul@26 | 766 | if not revisions: |
paul@26 | 767 | return {} |
paul@26 | 768 | |
paul@1 | 769 | event_page_initial = Page(request, page.page_name, rev=revisions[-1]) |
paul@1 | 770 | |
paul@1 | 771 | # Get the created and last modified times. |
paul@1 | 772 | |
paul@1 | 773 | initial_revision = getPageRevision(event_page_initial) |
paul@1 | 774 | |
paul@1 | 775 | metadata = {} |
paul@1 | 776 | metadata["created"] = initial_revision["timestamp"] |
paul@1 | 777 | latest_revision = getPageRevision(page) |
paul@1 | 778 | metadata["last-modified"] = latest_revision["timestamp"] |
paul@1 | 779 | metadata["sequence"] = len(revisions) - 1 |
paul@1 | 780 | metadata["last-comment"] = latest_revision["comment"] |
paul@1 | 781 | |
paul@1 | 782 | return metadata |
paul@0 | 783 | |
paul@0 | 784 | def getPageRevision(page): |
paul@0 | 785 | |
paul@0 | 786 | "Return the revision details dictionary for the given 'page'." |
paul@0 | 787 | |
paul@0 | 788 | # From Page.edit_info... |
paul@0 | 789 | |
paul@0 | 790 | if hasattr(page, "editlog_entry"): |
paul@0 | 791 | line = page.editlog_entry() |
paul@0 | 792 | else: |
paul@0 | 793 | line = page._last_edited(page.request) # MoinMoin 1.5.x and 1.6.x |
paul@0 | 794 | |
paul@0 | 795 | # Similar to Page.mtime_usecs behaviour... |
paul@0 | 796 | |
paul@0 | 797 | if line: |
paul@0 | 798 | timestamp = line.ed_time_usecs |
paul@0 | 799 | mtime = wikiutil.version2timestamp(long(timestamp)) # must be long for py 2.2.x |
paul@0 | 800 | comment = line.comment |
paul@0 | 801 | else: |
paul@0 | 802 | mtime = 0 |
paul@0 | 803 | comment = "" |
paul@0 | 804 | |
paul@56 | 805 | # Give the time zone as UTC. |
paul@0 | 806 | |
paul@56 | 807 | return {"timestamp" : DateTime(time.gmtime(mtime)[:6] + ("UTC",)), "comment" : comment} |
paul@0 | 808 | |
paul@11 | 809 | # Page parsing and formatting of embedded content. |
paul@11 | 810 | |
paul@65 | 811 | def getOutputTypes(request, format): |
paul@65 | 812 | |
paul@65 | 813 | """ |
paul@65 | 814 | Using the 'request' and the 'format' of a fragment, return the media types |
paul@65 | 815 | available for the fragment. |
paul@65 | 816 | """ |
paul@65 | 817 | |
paul@65 | 818 | return getParserOutputTypes(getParserClass(request, format)) |
paul@65 | 819 | |
paul@65 | 820 | def getParserOutputTypes(parser): |
paul@65 | 821 | |
paul@65 | 822 | "Return the media types supported by the given 'parser'." |
paul@65 | 823 | |
paul@65 | 824 | # This uses an extended parser API method if available. |
paul@65 | 825 | |
paul@65 | 826 | if parser and hasattr(parser, "getOutputTypes"): |
paul@65 | 827 | return parser.getOutputTypes() |
paul@65 | 828 | else: |
paul@65 | 829 | return ["text/html"] |
paul@65 | 830 | |
paul@15 | 831 | def getPageParserClass(request): |
paul@15 | 832 | |
paul@15 | 833 | "Using 'request', return a parser class for the current page's format." |
paul@15 | 834 | |
paul@16 | 835 | return getParserClass(request, getFormat(request.page)) |
paul@15 | 836 | |
paul@11 | 837 | def getParserClass(request, format): |
paul@11 | 838 | |
paul@11 | 839 | """ |
paul@11 | 840 | Return a parser class using the 'request' for the given 'format', returning |
paul@11 | 841 | a plain text parser if no parser can be found for the specified 'format'. |
paul@11 | 842 | """ |
paul@11 | 843 | |
paul@11 | 844 | try: |
paul@11 | 845 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", format or "plain") |
paul@11 | 846 | except wikiutil.PluginMissingError: |
paul@11 | 847 | return wikiutil.searchAndImportPlugin(request.cfg, "parser", "plain") |
paul@11 | 848 | |
paul@15 | 849 | def getFormatterClass(request, format): |
paul@15 | 850 | |
paul@15 | 851 | """ |
paul@15 | 852 | Return a formatter class using the 'request' for the given output 'format', |
paul@15 | 853 | returning a plain text formatter if no formatter can be found for the |
paul@15 | 854 | specified 'format'. |
paul@15 | 855 | """ |
paul@15 | 856 | |
paul@15 | 857 | try: |
paul@15 | 858 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", format or "plain") |
paul@15 | 859 | except wikiutil.PluginMissingError: |
paul@15 | 860 | return wikiutil.searchAndImportPlugin(request.cfg, "formatter", "plain") |
paul@15 | 861 | |
paul@33 | 862 | def formatText(text, request, fmt, inhibit_p=True, parser_cls=None): |
paul@15 | 863 | |
paul@15 | 864 | """ |
paul@15 | 865 | Format the given 'text' using the specified 'request' and formatter 'fmt'. |
paul@15 | 866 | Suppress line anchors in the output, and fix lists by indicating that a |
paul@15 | 867 | paragraph has already been started. |
paul@15 | 868 | """ |
paul@15 | 869 | |
paul@15 | 870 | if not parser_cls: |
paul@15 | 871 | parser_cls = getPageParserClass(request) |
paul@15 | 872 | parser = parser_cls(text, request, line_anchors=False) |
paul@15 | 873 | |
paul@15 | 874 | old_fmt = request.formatter |
paul@15 | 875 | request.formatter = fmt |
paul@15 | 876 | try: |
paul@61 | 877 | if isinstance(parser, text_moin_wiki.Parser): |
paul@58 | 878 | return redirectedOutput(request, parser, fmt, inhibit_p=inhibit_p) |
paul@58 | 879 | else: |
paul@58 | 880 | return redirectedOutput(request, parser, fmt) |
paul@15 | 881 | finally: |
paul@15 | 882 | request.formatter = old_fmt |
paul@15 | 883 | |
paul@65 | 884 | def formatTextForOutputType(text, request, parser_cls, output_type): |
paul@65 | 885 | |
paul@65 | 886 | """ |
paul@65 | 887 | Format the given 'text' using the specified 'request' and parser class |
paul@65 | 888 | 'parser_cls', producing output of the given 'output_type'. |
paul@65 | 889 | """ |
paul@65 | 890 | |
paul@65 | 891 | parser = parser_cls(text, request) |
paul@66 | 892 | buf = codecs.getwriter("utf-8")(StringIO()) |
paul@65 | 893 | try: |
paul@65 | 894 | parser.formatForOutputType(output_type, buf.write) |
paul@66 | 895 | return unicode(buf.getvalue(), "utf-8") |
paul@65 | 896 | finally: |
paul@65 | 897 | buf.close() |
paul@65 | 898 | |
paul@11 | 899 | def redirectedOutput(request, parser, fmt, **kw): |
paul@11 | 900 | |
paul@11 | 901 | "A fixed version of the request method of the same name." |
paul@11 | 902 | |
paul@66 | 903 | buf = codecs.getwriter("utf-8")(StringIO()) |
paul@11 | 904 | request.redirect(buf) |
paul@11 | 905 | try: |
paul@11 | 906 | parser.format(fmt, **kw) |
paul@11 | 907 | if hasattr(fmt, "flush"): |
paul@11 | 908 | buf.write(fmt.flush(True)) |
paul@11 | 909 | finally: |
paul@11 | 910 | request.redirect() |
paul@11 | 911 | text = buf.getvalue() |
paul@11 | 912 | buf.close() |
paul@66 | 913 | return unicode(text, "utf-8") |
paul@11 | 914 | |
paul@46 | 915 | # Finding components for content types. |
paul@46 | 916 | |
paul@46 | 917 | def getParsersForContentType(cfg, mimetype): |
paul@46 | 918 | |
paul@46 | 919 | """ |
paul@46 | 920 | Find parsers that support the given 'mimetype', constructing a dictionary |
paul@46 | 921 | mapping content types to lists of parsers that is then cached in the 'cfg' |
paul@46 | 922 | object. A list of suitable parsers is returned for 'mimetype'. |
paul@46 | 923 | """ |
paul@46 | 924 | |
paul@46 | 925 | if not hasattr(cfg.cache, "MIMETYPE_TO_PARSER"): |
paul@46 | 926 | available = {} |
paul@46 | 927 | |
paul@46 | 928 | for name in wikiutil.getPlugins("parser", cfg): |
paul@46 | 929 | |
paul@46 | 930 | # Import each parser in order to inspect supported content types. |
paul@46 | 931 | |
paul@46 | 932 | try: |
paul@46 | 933 | parser_cls = wikiutil.importPlugin(cfg, "parser", name, "Parser") |
paul@46 | 934 | except wikiutil.PluginMissingError: |
paul@46 | 935 | continue |
paul@46 | 936 | |
paul@46 | 937 | # Attempt to determine supported content types. |
paul@46 | 938 | # NOTE: Extensions and /etc/mime.types (or equivalent) could also be |
paul@46 | 939 | # NOTE: used. |
paul@46 | 940 | |
paul@46 | 941 | if hasattr(parser_cls, "input_mimetypes"): |
paul@46 | 942 | for input_mimetype in parser_cls.input_mimetypes: |
paul@46 | 943 | if not available.has_key(input_mimetype): |
paul@46 | 944 | available[input_mimetype] = [] |
paul@46 | 945 | available[input_mimetype].append(parser_cls) |
paul@46 | 946 | |
paul@46 | 947 | # Support some basic parsers. |
paul@46 | 948 | |
paul@46 | 949 | elif name == "text_moin_wiki": |
paul@46 | 950 | available["text/moin-wiki"] = [parser_cls] |
paul@46 | 951 | available["text/moin"] = [parser_cls] |
paul@58 | 952 | elif name == "text_html": |
paul@58 | 953 | available["text/html"] = [parser_cls] |
paul@59 | 954 | available["application/xhtml+xml"] = [parser_cls] |
paul@46 | 955 | |
paul@46 | 956 | cfg.cache.MIMETYPE_TO_PARSER = available |
paul@46 | 957 | |
paul@46 | 958 | return cfg.cache.MIMETYPE_TO_PARSER.get(mimetype, []) |
paul@46 | 959 | |
paul@25 | 960 | # Textual representations. |
paul@25 | 961 | |
paul@25 | 962 | def getSimpleWikiText(text): |
paul@25 | 963 | |
paul@25 | 964 | """ |
paul@25 | 965 | Return the plain text representation of the given 'text' which may employ |
paul@25 | 966 | certain Wiki syntax features, such as those providing verbatim or monospaced |
paul@25 | 967 | text. |
paul@25 | 968 | """ |
paul@25 | 969 | |
paul@25 | 970 | # NOTE: Re-implementing support for verbatim text and linking avoidance. |
paul@25 | 971 | |
paul@87 | 972 | l = [] |
paul@87 | 973 | last = 0 |
paul@87 | 974 | |
paul@87 | 975 | for m in verbatim_regexp.finditer(text): |
paul@87 | 976 | start, end = m.span() |
paul@87 | 977 | l.append(text[last:start]) |
paul@87 | 978 | |
paul@87 | 979 | # Process the verbatim macro arguments. |
paul@87 | 980 | |
paul@87 | 981 | args = m.group("verbatim") or m.group("verbatim2") |
paul@87 | 982 | if args: |
paul@87 | 983 | l += [v for (n, v) in parseMacroArguments(args)] |
paul@87 | 984 | |
paul@87 | 985 | # Or just add the match groups. |
paul@87 | 986 | |
paul@87 | 987 | else: |
paul@87 | 988 | l += [s for s in m.groups() if s] |
paul@87 | 989 | |
paul@87 | 990 | last = end |
paul@87 | 991 | |
paul@87 | 992 | l.append(text[last:]) |
paul@87 | 993 | return "".join(l) |
paul@25 | 994 | |
paul@25 | 995 | def getEncodedWikiText(text): |
paul@25 | 996 | |
paul@25 | 997 | "Encode the given 'text' in a verbatim representation." |
paul@25 | 998 | |
paul@85 | 999 | return "<<Verbatim(%s)>>" % quoteMacroArgument(None, text) |
paul@25 | 1000 | |
paul@25 | 1001 | def getPrettyTitle(title): |
paul@25 | 1002 | |
paul@25 | 1003 | "Return a nicely formatted version of the given 'title'." |
paul@25 | 1004 | |
paul@25 | 1005 | return title.replace("_", " ").replace("/", u" ? ") |
paul@25 | 1006 | |
paul@0 | 1007 | # User interface functions. |
paul@0 | 1008 | |
paul@0 | 1009 | def getParameter(request, name, default=None): |
paul@0 | 1010 | |
paul@0 | 1011 | """ |
paul@0 | 1012 | Using the given 'request', return the value of the parameter with the given |
paul@0 | 1013 | 'name', returning the optional 'default' (or None) if no value was supplied |
paul@0 | 1014 | in the 'request'. |
paul@0 | 1015 | """ |
paul@0 | 1016 | |
paul@0 | 1017 | return get_form(request).get(name, [default])[0] |
paul@0 | 1018 | |
paul@0 | 1019 | def getQualifiedParameter(request, prefix, argname, default=None): |
paul@0 | 1020 | |
paul@0 | 1021 | """ |
paul@0 | 1022 | Using the given 'request', 'prefix' and 'argname', retrieve the value of the |
paul@0 | 1023 | qualified parameter, returning the optional 'default' (or None) if no value |
paul@0 | 1024 | was supplied in the 'request'. |
paul@0 | 1025 | """ |
paul@0 | 1026 | |
paul@0 | 1027 | argname = getQualifiedParameterName(prefix, argname) |
paul@0 | 1028 | return getParameter(request, argname, default) |
paul@0 | 1029 | |
paul@0 | 1030 | def getQualifiedParameterName(prefix, argname): |
paul@0 | 1031 | |
paul@0 | 1032 | """ |
paul@0 | 1033 | Return the qualified parameter name using the given 'prefix' and 'argname'. |
paul@0 | 1034 | """ |
paul@0 | 1035 | |
paul@27 | 1036 | if not prefix: |
paul@0 | 1037 | return argname |
paul@0 | 1038 | else: |
paul@0 | 1039 | return "%s-%s" % (prefix, argname) |
paul@0 | 1040 | |
paul@0 | 1041 | # Page-related functions. |
paul@0 | 1042 | |
paul@0 | 1043 | def getPrettyPageName(page): |
paul@0 | 1044 | |
paul@0 | 1045 | "Return a nicely formatted title/name for the given 'page'." |
paul@0 | 1046 | |
paul@0 | 1047 | title = page.split_title(force=1) |
paul@0 | 1048 | return getPrettyTitle(title) |
paul@0 | 1049 | |
paul@25 | 1050 | def linkToPage(request, page, text, query_string=None, anchor=None, **kw): |
paul@0 | 1051 | |
paul@0 | 1052 | """ |
paul@0 | 1053 | Using 'request', return a link to 'page' with the given link 'text' and |
paul@25 | 1054 | optional 'query_string' and 'anchor'. |
paul@0 | 1055 | """ |
paul@0 | 1056 | |
paul@0 | 1057 | text = wikiutil.escape(text) |
paul@25 | 1058 | return page.link_to_raw(request, text, query_string, anchor, **kw) |
paul@0 | 1059 | |
paul@25 | 1060 | def linkToResource(url, request, text, query_string=None, anchor=None): |
paul@0 | 1061 | |
paul@0 | 1062 | """ |
paul@0 | 1063 | Using 'request', return a link to 'url' with the given link 'text' and |
paul@25 | 1064 | optional 'query_string' and 'anchor'. |
paul@0 | 1065 | """ |
paul@0 | 1066 | |
paul@25 | 1067 | if anchor: |
paul@25 | 1068 | url += "#%s" % anchor |
paul@25 | 1069 | |
paul@0 | 1070 | if query_string: |
paul@0 | 1071 | query_string = wikiutil.makeQueryString(query_string) |
paul@25 | 1072 | url += "?%s" % query_string |
paul@0 | 1073 | |
paul@0 | 1074 | formatter = request.page and getattr(request.page, "formatter", None) or request.html_formatter |
paul@0 | 1075 | |
paul@0 | 1076 | output = [] |
paul@0 | 1077 | output.append(formatter.url(1, url)) |
paul@0 | 1078 | output.append(formatter.text(text)) |
paul@0 | 1079 | output.append(formatter.url(0)) |
paul@0 | 1080 | return "".join(output) |
paul@0 | 1081 | |
paul@0 | 1082 | def getFullPageName(parent, title): |
paul@0 | 1083 | |
paul@0 | 1084 | """ |
paul@0 | 1085 | Return a full page name from the given 'parent' page (can be empty or None) |
paul@0 | 1086 | and 'title' (a simple page name). |
paul@0 | 1087 | """ |
paul@0 | 1088 | |
paul@0 | 1089 | if parent: |
paul@0 | 1090 | return "%s/%s" % (parent.rstrip("/"), title) |
paul@0 | 1091 | else: |
paul@0 | 1092 | return title |
paul@0 | 1093 | |
paul@37 | 1094 | # Content storage support. |
paul@37 | 1095 | |
paul@92 | 1096 | # Specific storage mechanisms. |
paul@92 | 1097 | |
paul@92 | 1098 | class SubpageItemStore(GeneralItemStore): |
paul@92 | 1099 | |
paul@92 | 1100 | "A subpage-based item store." |
paul@92 | 1101 | |
paul@92 | 1102 | def __init__(self, page, lock_dir): |
paul@92 | 1103 | |
paul@92 | 1104 | "Initialise an item store for subpages under the given 'page'." |
paul@92 | 1105 | |
paul@92 | 1106 | GeneralItemStore.__init__(self, lock_dir) |
paul@92 | 1107 | self.page = page |
paul@92 | 1108 | |
paul@92 | 1109 | def mtime(self): |
paul@92 | 1110 | |
paul@92 | 1111 | "Return the last modified time of the item store." |
paul@92 | 1112 | |
paul@92 | 1113 | keys = self.get_keys() |
paul@92 | 1114 | if not keys: |
paul@92 | 1115 | page = self.page |
paul@92 | 1116 | else: |
paul@92 | 1117 | page = Page(self.page.request, self.get_item_path(max(keys))) |
paul@37 | 1118 | |
paul@92 | 1119 | return wikiutil.version2timestamp( |
paul@92 | 1120 | getMetadata(page)["last-modified"] |
paul@92 | 1121 | ) |
paul@92 | 1122 | |
paul@92 | 1123 | def get_next(self): |
paul@92 | 1124 | |
paul@92 | 1125 | "Return the next item number." |
paul@92 | 1126 | |
paul@92 | 1127 | return self.deduce_next() |
paul@92 | 1128 | |
paul@92 | 1129 | def get_keys(self): |
paul@92 | 1130 | |
paul@92 | 1131 | "Return the item keys." |
paul@37 | 1132 | |
paul@92 | 1133 | is_subpage = re.compile(u"^%s/" % re.escape(self.page.page_name), re.UNICODE).match |
paul@92 | 1134 | |
paul@92 | 1135 | # Collect the strict subpages of the parent page. |
paul@92 | 1136 | |
paul@92 | 1137 | leafnames = [] |
paul@92 | 1138 | parentname = self.page.page_name |
paul@92 | 1139 | |
paul@92 | 1140 | for pagename in RootPage(self.page.request).getPageList(filter=is_subpage): |
paul@92 | 1141 | parts = pagename[len(parentname)+1:].split("/") |
paul@92 | 1142 | |
paul@92 | 1143 | # Only collect numbered pages immediately below the parent. |
paul@92 | 1144 | |
paul@92 | 1145 | if len(parts) == 1 and parts[0].isdigit(): |
paul@92 | 1146 | leafnames.append(int(parts[0])) |
paul@37 | 1147 | |
paul@92 | 1148 | return leafnames |
paul@92 | 1149 | |
paul@92 | 1150 | def write_item(self, item, next): |
paul@92 | 1151 | |
paul@92 | 1152 | "Write the given 'item' to a file with the given 'next' item number." |
paul@92 | 1153 | |
paul@92 | 1154 | page = PageEditor(self.page.request, self.get_item_path(next)) |
paul@92 | 1155 | page.saveText(item, 0) |
paul@92 | 1156 | |
paul@92 | 1157 | def read_item(self, number): |
paul@92 | 1158 | |
paul@92 | 1159 | "Read the item with the given item 'number'." |
paul@92 | 1160 | |
paul@92 | 1161 | page = Page(self.page.request, self.get_item_path(number)) |
paul@92 | 1162 | return page.get_raw_body() |
paul@92 | 1163 | |
paul@92 | 1164 | def remove_item(self, number): |
paul@92 | 1165 | |
paul@92 | 1166 | "Remove the item with the given item 'number'." |
paul@92 | 1167 | |
paul@92 | 1168 | page = PageEditor(self.page.request, self.get_item_path(number)) |
paul@92 | 1169 | page.deletePage() |
paul@92 | 1170 | |
paul@92 | 1171 | def get_item_path(self, number): |
paul@37 | 1172 | |
paul@92 | 1173 | "Get the path for the given item 'number'." |
paul@92 | 1174 | |
paul@92 | 1175 | return "%s/%s" % (self.page.page_name, number) |
paul@92 | 1176 | |
paul@92 | 1177 | # High-level methods. |
paul@92 | 1178 | |
paul@92 | 1179 | def append(self, item): |
paul@92 | 1180 | |
paul@92 | 1181 | "Append the given 'item' to the store." |
paul@92 | 1182 | |
paul@92 | 1183 | self.writelock.acquire() |
paul@92 | 1184 | try: |
paul@92 | 1185 | next = self.get_next() |
paul@92 | 1186 | self.write_item(item, next) |
paul@92 | 1187 | finally: |
paul@92 | 1188 | self.writelock.release() |
paul@92 | 1189 | |
paul@92 | 1190 | # General item store classes. |
paul@92 | 1191 | |
paul@92 | 1192 | class ItemStoreBase: |
paul@92 | 1193 | |
paul@92 | 1194 | "Support for page-specific item stores." |
paul@92 | 1195 | |
paul@92 | 1196 | def __init__(self, page, store): |
paul@40 | 1197 | self.page = page |
paul@92 | 1198 | self.store = store |
paul@37 | 1199 | |
paul@40 | 1200 | def can_write(self): |
paul@40 | 1201 | |
paul@40 | 1202 | """ |
paul@40 | 1203 | Return whether the user associated with the request can write to the |
paul@40 | 1204 | page owning this store. |
paul@40 | 1205 | """ |
paul@40 | 1206 | |
paul@40 | 1207 | user = self.page.request.user |
paul@40 | 1208 | return user and user.may.write(self.page.page_name) |
paul@40 | 1209 | |
paul@42 | 1210 | def can_read(self): |
paul@42 | 1211 | |
paul@42 | 1212 | """ |
paul@42 | 1213 | Return whether the user associated with the request can read from the |
paul@42 | 1214 | page owning this store. |
paul@42 | 1215 | """ |
paul@42 | 1216 | |
paul@42 | 1217 | user = self.page.request.user |
paul@42 | 1218 | return user and user.may.read(self.page.page_name) |
paul@42 | 1219 | |
paul@63 | 1220 | def can_delete(self): |
paul@63 | 1221 | |
paul@63 | 1222 | """ |
paul@63 | 1223 | Return whether the user associated with the request can delete the |
paul@63 | 1224 | page owning this store. |
paul@63 | 1225 | """ |
paul@63 | 1226 | |
paul@63 | 1227 | user = self.page.request.user |
paul@63 | 1228 | return user and user.may.delete(self.page.page_name) |
paul@63 | 1229 | |
paul@91 | 1230 | # Store-specific methods. |
paul@91 | 1231 | |
paul@91 | 1232 | def mtime(self): |
paul@91 | 1233 | return self.store.mtime() |
paul@91 | 1234 | |
paul@41 | 1235 | # High-level methods. |
paul@41 | 1236 | |
paul@91 | 1237 | def keys(self): |
paul@91 | 1238 | |
paul@91 | 1239 | "Return a list of keys for items in the store." |
paul@91 | 1240 | |
paul@91 | 1241 | if not self.can_read(): |
paul@91 | 1242 | return 0 |
paul@91 | 1243 | |
paul@91 | 1244 | return self.store.keys() |
paul@91 | 1245 | |
paul@41 | 1246 | def append(self, item): |
paul@41 | 1247 | |
paul@41 | 1248 | "Append the given 'item' to the store." |
paul@41 | 1249 | |
paul@41 | 1250 | if not self.can_write(): |
paul@41 | 1251 | return |
paul@41 | 1252 | |
paul@91 | 1253 | self.store.append(item) |
paul@41 | 1254 | |
paul@42 | 1255 | def __len__(self): |
paul@42 | 1256 | |
paul@63 | 1257 | "Return the number of items in the store." |
paul@42 | 1258 | |
paul@42 | 1259 | if not self.can_read(): |
paul@42 | 1260 | return 0 |
paul@42 | 1261 | |
paul@91 | 1262 | return len(self.store) |
paul@42 | 1263 | |
paul@42 | 1264 | def __getitem__(self, number): |
paul@42 | 1265 | |
paul@42 | 1266 | "Return the item with the given 'number'." |
paul@42 | 1267 | |
paul@42 | 1268 | if not self.can_read(): |
paul@42 | 1269 | raise IndexError, number |
paul@42 | 1270 | |
paul@91 | 1271 | return self.store.__getitem__(number) |
paul@42 | 1272 | |
paul@63 | 1273 | def __delitem__(self, number): |
paul@63 | 1274 | |
paul@63 | 1275 | "Remove the item with the given 'number'." |
paul@63 | 1276 | |
paul@63 | 1277 | if not self.can_delete(): |
paul@63 | 1278 | return |
paul@63 | 1279 | |
paul@91 | 1280 | return self.store.__delitem__(number) |
paul@91 | 1281 | |
paul@91 | 1282 | def __iter__(self): |
paul@91 | 1283 | return self.store.__iter__() |
paul@91 | 1284 | |
paul@91 | 1285 | def next(self): |
paul@91 | 1286 | return self.store.next() |
paul@91 | 1287 | |
paul@92 | 1288 | def getDirectoryItemStoreForPage(page, item_dir, lock_dir): |
paul@92 | 1289 | item_dir_path = tuple(item_dir.split("/")) |
paul@92 | 1290 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@92 | 1291 | return DirectoryItemStore(page.getPagePath(*item_dir_path), page.getPagePath(*lock_dir_path)) |
paul@91 | 1292 | |
paul@92 | 1293 | def getSubpageItemStoreForPage(page, lock_dir): |
paul@92 | 1294 | lock_dir_path = tuple(lock_dir.split("/")) |
paul@92 | 1295 | return SubpageItemStore(page, page.getPagePath(*lock_dir_path)) |
paul@91 | 1296 | |
paul@92 | 1297 | # Convenience store constructors. |
paul@91 | 1298 | |
paul@92 | 1299 | def ItemStore(page, item_dir="items", lock_dir="item_locks"): |
paul@91 | 1300 | |
paul@92 | 1301 | "Store items in a directory via a page." |
paul@91 | 1302 | |
paul@92 | 1303 | return ItemStoreBase(page, getDirectoryItemStoreForPage(page, item_dir, lock_dir)) |
paul@91 | 1304 | |
paul@92 | 1305 | def ItemSubpageStore(page, lock_dir="item_locks"): |
paul@91 | 1306 | |
paul@92 | 1307 | "Store items in subpages of a page." |
paul@91 | 1308 | |
paul@92 | 1309 | return ItemStoreBase(page, getSubpageItemStoreForPage(page, lock_dir)) |
paul@63 | 1310 | |
paul@0 | 1311 | # vim: tabstop=4 expandtab shiftwidth=4 |