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