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