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