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