| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | # |
|---|
| 3 | # Copyright (C) 2006 Alec Thomas <alec@swapoff.org> |
|---|
| 4 | # Copyright (C) 2011 Itamar Ostricher <itamarost@gmail.com> |
|---|
| 5 | # Copyright (C) 2011-2015 Steffen Hoffmann <hoff.st@web.de> |
|---|
| 6 | # Copyright (C) 2015 Ryan J Ollos <ryan.j.ollos@gmail.com> |
|---|
| 7 | # |
|---|
| 8 | # This software is licensed as described in the file COPYING, which |
|---|
| 9 | # you should have received as part of this distribution. |
|---|
| 10 | # |
|---|
| 11 | |
|---|
| 12 | import collections |
|---|
| 13 | import re |
|---|
| 14 | |
|---|
| 15 | from fnmatch import fnmatchcase |
|---|
| 16 | from pkg_resources import resource_filename |
|---|
| 17 | |
|---|
| 18 | from trac.config import BoolOption, ListOption, Option |
|---|
| 19 | from trac.core import Component, TracError, implements |
|---|
| 20 | from trac.resource import Resource, get_resource_url, render_resource_link |
|---|
| 21 | from trac.ticket.api import TicketSystem |
|---|
| 22 | from trac.ticket.model import Ticket |
|---|
| 23 | from trac.util import as_int, embedded_numbers |
|---|
| 24 | from trac.util.html import html as builder |
|---|
| 25 | from trac.util.presentation import Paginator |
|---|
| 26 | from trac.util.text import shorten_line, to_unicode |
|---|
| 27 | from trac.web.chrome import Chrome, ITemplateProvider, add_link, \ |
|---|
| 28 | add_stylesheet |
|---|
| 29 | from trac.wiki.api import IWikiMacroProvider, parse_args |
|---|
| 30 | from trac.wiki.formatter import format_to_oneliner, system_message |
|---|
| 31 | |
|---|
| 32 | from tractags.api import InvalidTagRealm, TagSystem, N_, _, gettext |
|---|
| 33 | from tractags.query import InvalidQuery |
|---|
| 34 | from tractags.util import query_realms |
|---|
| 35 | |
|---|
| 36 | # Check for unsupported pre-tags-0.6 macro keyword arguments. |
|---|
| 37 | _OBSOLETE_ARGS_RE = re.compile(r""" |
|---|
| 38 | (expression|operation|showheadings|tagspace|tagspaces)= |
|---|
| 39 | """, re.VERBOSE) |
|---|
| 40 | |
|---|
| 41 | |
|---|
| 42 | class TagTemplateProvider(Component): |
|---|
| 43 | """Provides templates and static resources for the tags plugin.""" |
|---|
| 44 | |
|---|
| 45 | implements(ITemplateProvider) |
|---|
| 46 | |
|---|
| 47 | abstract = True |
|---|
| 48 | |
|---|
| 49 | # ITemplateProvider methods |
|---|
| 50 | |
|---|
| 51 | def get_templates_dirs(self): |
|---|
| 52 | """Return the absolute path of the directory containing the provided |
|---|
| 53 | Genshi templates. |
|---|
| 54 | """ |
|---|
| 55 | return [resource_filename(__name__, 'templates')] |
|---|
| 56 | |
|---|
| 57 | def get_htdocs_dirs(self): |
|---|
| 58 | """Return the absolute path of a directory containing additional |
|---|
| 59 | static resources (such as images, style sheets, etc). |
|---|
| 60 | """ |
|---|
| 61 | return [('tags', resource_filename(__name__, 'htdocs'))] |
|---|
| 62 | |
|---|
| 63 | |
|---|
| 64 | class TagWikiMacros(TagTemplateProvider): |
|---|
| 65 | """[opt] Provides macros, that utilize the tag system in wiki markup.""" |
|---|
| 66 | |
|---|
| 67 | implements(IWikiMacroProvider) |
|---|
| 68 | |
|---|
| 69 | caseless_sort = BoolOption('tags', 'cloud_caseless_sort', default=False, |
|---|
| 70 | doc="Whether the tag cloud should be sorted case-sensitive.") |
|---|
| 71 | default_cols = Option('tags', 'listtagged_default_table_cols', |
|---|
| 72 | 'id|description|tags', |
|---|
| 73 | doc="Select columns and column order for table format.\n\n" |
|---|
| 74 | "See `ListTagged` description (WikiMacros) for supported values.") |
|---|
| 75 | default_format = Option('tags', 'listtagged_default_format', 'oldlist', |
|---|
| 76 | doc="Set default format for the handler of the `/tags` domain.\n\n" |
|---|
| 77 | "See `ListTagged` description (WikiMacros) for supported values.") |
|---|
| 78 | exclude_realms = ListOption('tags', 'listtagged_exclude_realms', [], |
|---|
| 79 | doc="Comma-separated list of realms to exclude from tags queries " |
|---|
| 80 | "by default, unless specifically included using 'realm:<realm>' " |
|---|
| 81 | "in a query.") |
|---|
| 82 | items_per_page = Option('tags', 'listtagged_items_per_page', 100, |
|---|
| 83 | doc="Number of tagged resources displayed per page of tag query " |
|---|
| 84 | "results requested by `ListTagged` macros and from `/tags`.") |
|---|
| 85 | items_per_page = as_int(items_per_page, 100) |
|---|
| 86 | supported_cols = frozenset(['realm', 'id', 'description', 'tags']) |
|---|
| 87 | |
|---|
| 88 | def __init__(self): |
|---|
| 89 | # TRANSLATOR: Keep macro doc style formatting here, please. |
|---|
| 90 | self.doc_cloud = N_("""Display a tag cloud. |
|---|
| 91 | |
|---|
| 92 | Show a tag cloud for all tags on resources matching query. |
|---|
| 93 | |
|---|
| 94 | Usage: |
|---|
| 95 | |
|---|
| 96 | {{{ |
|---|
| 97 | [[TagCloud(<query>[,caseless_sort=<bool>][,mincount=<n>])]] |
|---|
| 98 | }}} |
|---|
| 99 | caseless_sort:: |
|---|
| 100 | Whether the tag cloud should be sorted case-sensitive. |
|---|
| 101 | mincount:: |
|---|
| 102 | Optional integer threshold to hide tags with smaller count. |
|---|
| 103 | |
|---|
| 104 | See tags documentation for the query syntax. |
|---|
| 105 | """) |
|---|
| 106 | self.doc_listtagged = N_("""List tagged resources. |
|---|
| 107 | |
|---|
| 108 | Usage: |
|---|
| 109 | |
|---|
| 110 | {{{ |
|---|
| 111 | [[ListTagged(<query>[,exclude=<list>],[[format=<format>],cols=<columns>])]] |
|---|
| 112 | }}} |
|---|
| 113 | format:: |
|---|
| 114 | result list presentation; supported values: |
|---|
| 115 | |
|---|
| 116 | || `compact` || comma-separated inline list of "linked-description" || |
|---|
| 117 | || `oldlist` (default) || " * linked-id description (tags)" list || |
|---|
| 118 | || `table` || table... (see corresponding column option too) || |
|---|
| 119 | || `short` or other value || bulleted list of "linked-description" || |
|---|
| 120 | cols:: |
|---|
| 121 | columns for 'table' format using a "|"-separated list of column names |
|---|
| 122 | (order matters); supported columns: realm, id, description, tags |
|---|
| 123 | exclude:: |
|---|
| 124 | exclude tagged resources that match a name in the colon-separated list |
|---|
| 125 | of resource ids, accepts shell-style patterns |
|---|
| 126 | |
|---|
| 127 | See tags documentation for the query syntax. |
|---|
| 128 | """) |
|---|
| 129 | |
|---|
| 130 | # IWikiMacroProvider |
|---|
| 131 | |
|---|
| 132 | def get_macros(self): |
|---|
| 133 | yield 'ListTagged' |
|---|
| 134 | yield 'TagCloud' |
|---|
| 135 | |
|---|
| 136 | def get_macro_description(self, name): |
|---|
| 137 | if name == 'ListTagged': |
|---|
| 138 | return gettext(self.doc_listtagged) |
|---|
| 139 | if name == 'TagCloud': |
|---|
| 140 | return gettext(self.doc_cloud) |
|---|
| 141 | |
|---|
| 142 | def expand_macro(self, formatter, name, content, realms=[]): |
|---|
| 143 | """Evaluate macro call and render results. |
|---|
| 144 | |
|---|
| 145 | Calls from web-UI come with pre-processed realm selection. |
|---|
| 146 | """ |
|---|
| 147 | env = self.env |
|---|
| 148 | req = formatter.req |
|---|
| 149 | tag_system = TagSystem(env) |
|---|
| 150 | |
|---|
| 151 | all_realms = tag_system.get_taggable_realms() |
|---|
| 152 | if not all_realms: |
|---|
| 153 | # Tag providers are required, no result without at least one. |
|---|
| 154 | return '' |
|---|
| 155 | args, kw = parse_args(content) |
|---|
| 156 | |
|---|
| 157 | query = args and args[0].strip() or None |
|---|
| 158 | if not realms: |
|---|
| 159 | # Check macro arguments for realms (typical wiki macro call). |
|---|
| 160 | realms = 'realm' in kw and kw['realm'].split('|') or [] |
|---|
| 161 | if query: |
|---|
| 162 | # Add realms from query expression. |
|---|
| 163 | realms.extend(query_realms(query, all_realms)) |
|---|
| 164 | # Remove redundant realm selection for performance. |
|---|
| 165 | if set(realms) == all_realms: |
|---|
| 166 | query = re.sub('(^|\W)realm:\S+(\W|$)', ' ', query).strip() |
|---|
| 167 | if name == 'TagCloud': |
|---|
| 168 | # Set implicit 'all tagged realms' as default. |
|---|
| 169 | if not realms: |
|---|
| 170 | realms = all_realms |
|---|
| 171 | if query: |
|---|
| 172 | all_tags = collections.Counter() |
|---|
| 173 | # Require per resource query including view permission checks. |
|---|
| 174 | for resource, tags in tag_system.query(req, query): |
|---|
| 175 | all_tags.update(tags) |
|---|
| 176 | else: |
|---|
| 177 | # Allow faster per tag query, side steps permission checks. |
|---|
| 178 | all_tags = tag_system.get_all_tags(req, realms=realms) |
|---|
| 179 | mincount = 'mincount' in kw and kw['mincount'] or None |
|---|
| 180 | return self.render_cloud(req, all_tags, |
|---|
| 181 | caseless_sort=self.caseless_sort, |
|---|
| 182 | mincount=mincount, realms=realms) |
|---|
| 183 | elif name == 'ListTagged': |
|---|
| 184 | if content and _OBSOLETE_ARGS_RE.search(content): |
|---|
| 185 | data = {'warning': 'obsolete_args'} |
|---|
| 186 | else: |
|---|
| 187 | data = {'warning': None} |
|---|
| 188 | context = formatter.context |
|---|
| 189 | # Use TagsQuery arguments (most likely wiki macro calls). |
|---|
| 190 | cols = 'cols' in kw and kw['cols'] or self.default_cols |
|---|
| 191 | format = 'format' in kw and kw['format'] or self.default_format |
|---|
| 192 | if not realms: |
|---|
| 193 | # Apply ListTagged defaults to macro call w/o realm. |
|---|
| 194 | realms = list(set(all_realms)-set(self.exclude_realms)) |
|---|
| 195 | if not realms: |
|---|
| 196 | return '' |
|---|
| 197 | query = '(%s) (%s)' % (query or '', ' or '.join(['realm:%s' % (r) |
|---|
| 198 | for r in realms])) |
|---|
| 199 | query_result = tag_system.query(req, query) |
|---|
| 200 | excludes = [exc.strip() |
|---|
| 201 | for exc in kw.get('exclude', '' ).split(':') |
|---|
| 202 | if exc.strip()] |
|---|
| 203 | if excludes and query_result: |
|---|
| 204 | filtered_result = [(resource, tags) |
|---|
| 205 | for resource, tags in query_result |
|---|
| 206 | if not any(fnmatchcase(resource.id, exc) |
|---|
| 207 | for exc in excludes)] |
|---|
| 208 | query_result = filtered_result |
|---|
| 209 | if not query_result: |
|---|
| 210 | return '' |
|---|
| 211 | |
|---|
| 212 | def _link(resource): |
|---|
| 213 | if resource.realm == 'tag': |
|---|
| 214 | # Keep realm selection in tag links. |
|---|
| 215 | return builder.a(resource.id, |
|---|
| 216 | href=self.get_href(req, realms, |
|---|
| 217 | tag=resource)) |
|---|
| 218 | elif resource.realm == 'ticket': |
|---|
| 219 | # Return resource link including ticket status dependend |
|---|
| 220 | # class to allow for common Trac ticket link style. |
|---|
| 221 | ticket = Ticket(env, resource.id) |
|---|
| 222 | return builder.a('#%s' % ticket.id, |
|---|
| 223 | class_=ticket['status'], |
|---|
| 224 | href=formatter.href.ticket(ticket.id), |
|---|
| 225 | title=shorten_line(ticket['summary'])) |
|---|
| 226 | return render_resource_link(env, context, resource, 'compact') |
|---|
| 227 | |
|---|
| 228 | if format == 'table': |
|---|
| 229 | cols = [col for col in cols.split('|') |
|---|
| 230 | if col in self.supported_cols] |
|---|
| 231 | # Use available translations from Trac core. |
|---|
| 232 | try: |
|---|
| 233 | labels = TicketSystem(env).get_ticket_field_labels() |
|---|
| 234 | labels['id'] = _('Id') |
|---|
| 235 | except AttributeError: |
|---|
| 236 | # Trac 0.11 neither has the attribute nor uses i18n. |
|---|
| 237 | labels = {'id': 'Id', 'description': 'Description'} |
|---|
| 238 | labels['realm'] = _('Realm') |
|---|
| 239 | labels['tags'] = _('Tags') |
|---|
| 240 | headers = [{'label': labels.get(col)} |
|---|
| 241 | for col in cols] |
|---|
| 242 | data.update({'cols': cols, |
|---|
| 243 | 'headers': headers}) |
|---|
| 244 | |
|---|
| 245 | try: |
|---|
| 246 | results = sorted(query_result, key=lambda r: |
|---|
| 247 | embedded_numbers(to_unicode(r[0].id))) |
|---|
| 248 | except (InvalidQuery, InvalidTagRealm), e: |
|---|
| 249 | return system_message(_("ListTagged macro error"), e) |
|---|
| 250 | results = self._paginate(req, results, realms) |
|---|
| 251 | rows = [] |
|---|
| 252 | for resource, tags in results: |
|---|
| 253 | desc = tag_system.describe_tagged_resource(req, resource) |
|---|
| 254 | tags = sorted(tags) |
|---|
| 255 | wiki_desc = format_to_oneliner(env, context, desc) |
|---|
| 256 | if tags: |
|---|
| 257 | rendered_tags = [_link(Resource('tag', tag)) |
|---|
| 258 | for tag in tags] |
|---|
| 259 | if 'oldlist' == format: |
|---|
| 260 | resource_link = _link(resource) |
|---|
| 261 | else: |
|---|
| 262 | resource_link = builder.a(wiki_desc, |
|---|
| 263 | href=get_resource_url( |
|---|
| 264 | env, resource, context.href)) |
|---|
| 265 | if 'table' == format: |
|---|
| 266 | cells = [] |
|---|
| 267 | for col in cols: |
|---|
| 268 | if col == 'id': |
|---|
| 269 | cells.append(_link(resource)) |
|---|
| 270 | # Don't duplicate links to resource in both. |
|---|
| 271 | elif col == 'description' and 'id' in cols: |
|---|
| 272 | cells.append(wiki_desc) |
|---|
| 273 | elif col == 'description': |
|---|
| 274 | cells.append(resource_link) |
|---|
| 275 | elif col == 'realm': |
|---|
| 276 | cells.append(resource.realm) |
|---|
| 277 | elif col == 'tags': |
|---|
| 278 | cells.append( |
|---|
| 279 | builder([(tag, ' ') |
|---|
| 280 | for tag in rendered_tags])) |
|---|
| 281 | rows.append({'cells': cells}) |
|---|
| 282 | continue |
|---|
| 283 | rows.append({'desc': wiki_desc, |
|---|
| 284 | 'rendered_tags': None, |
|---|
| 285 | 'resource_link': _link(resource)}) |
|---|
| 286 | data.update({'format': format, |
|---|
| 287 | 'paginator': results, |
|---|
| 288 | 'results': rows, |
|---|
| 289 | 'tags_url': req.href('tags')}) |
|---|
| 290 | |
|---|
| 291 | # Work around a bug in trac/templates/layout.html, that causes a |
|---|
| 292 | # TypeError for the wiki macro call, if we use add_link() alone. |
|---|
| 293 | add_stylesheet(req, 'common/css/search.css') |
|---|
| 294 | |
|---|
| 295 | return Chrome(env).render_template( |
|---|
| 296 | req, 'listtagged_results.html', data, 'text/html', True) |
|---|
| 297 | |
|---|
| 298 | def get_href(self, req, realms, query=None, per_page=None, page=None, |
|---|
| 299 | tag=None, **kwargs): |
|---|
| 300 | """Prepare href objects for tag links and pager navigation. |
|---|
| 301 | |
|---|
| 302 | Generate form-related arguments, strip arguments with default values. |
|---|
| 303 | """ |
|---|
| 304 | # Prepare realm arguments to keep form data consistent. |
|---|
| 305 | form_realms = dict((realm, 'on') for realm in realms) |
|---|
| 306 | if not page and not per_page: |
|---|
| 307 | # We're not serving pager navigation here. |
|---|
| 308 | return get_resource_url(self.env, tag, req.href, |
|---|
| 309 | form_realms=form_realms, **kwargs) |
|---|
| 310 | if page == 1: |
|---|
| 311 | page = None |
|---|
| 312 | if per_page == self.items_per_page: |
|---|
| 313 | per_page = None |
|---|
| 314 | return req.href(req.path_info, form_realms, q=query, |
|---|
| 315 | realms=realms, listtagged_per_page=per_page, |
|---|
| 316 | listtagged_page=page, **kwargs) |
|---|
| 317 | |
|---|
| 318 | def render_cloud(self, req, cloud, renderer=None, caseless_sort=False, |
|---|
| 319 | mincount=None, realms=()): |
|---|
| 320 | """Render a tag cloud. |
|---|
| 321 | |
|---|
| 322 | :cloud: Dictionary of {object: count} representing the cloud. |
|---|
| 323 | :param renderer: A callable with signature (tag, count, percent) |
|---|
| 324 | used to render the cloud objects. |
|---|
| 325 | :param caseless_sort: Boolean, whether tag cloud should be sorted |
|---|
| 326 | case-sensitive. |
|---|
| 327 | :param mincount: Integer threshold to hide tags with smaller count. |
|---|
| 328 | """ |
|---|
| 329 | min_px = 10.0 |
|---|
| 330 | max_px = 30.0 |
|---|
| 331 | scale = 1.0 |
|---|
| 332 | |
|---|
| 333 | if renderer is None: |
|---|
| 334 | def default_renderer(tag, count, percent): |
|---|
| 335 | href = self.get_href(req, realms, tag=Resource('tag', tag)) |
|---|
| 336 | return builder.a(tag, rel='tag', title='%i' % count, |
|---|
| 337 | href=href, style='font-size: %ipx' |
|---|
| 338 | % int(min_px + percent * (max_px - min_px))) |
|---|
| 339 | renderer = default_renderer |
|---|
| 340 | |
|---|
| 341 | # A LUT from count to n/len(cloud) |
|---|
| 342 | size_lut = dict([(c, float(i)) for i, c in |
|---|
| 343 | enumerate(sorted(set([r for r in cloud.values()])))]) |
|---|
| 344 | if size_lut: |
|---|
| 345 | scale = 1.0 / len(size_lut) |
|---|
| 346 | |
|---|
| 347 | if caseless_sort: |
|---|
| 348 | # Preserve upper-case precedence within similar tags. |
|---|
| 349 | items = reversed(sorted(cloud.iteritems(), |
|---|
| 350 | key=lambda t: t[0].lower(), reverse=True)) |
|---|
| 351 | else: |
|---|
| 352 | items = sorted(cloud.iteritems()) |
|---|
| 353 | ul = li = None |
|---|
| 354 | for i, (tag, count) in enumerate(items): |
|---|
| 355 | percent = size_lut[count] * scale |
|---|
| 356 | if mincount and count < as_int(mincount, 1): |
|---|
| 357 | # Tag count is too low. |
|---|
| 358 | continue |
|---|
| 359 | if ul: |
|---|
| 360 | # Found new tag for cloud; now add previously prepared one. |
|---|
| 361 | ul('\n', li) |
|---|
| 362 | else: |
|---|
| 363 | # Found first tag for cloud; now create the list. |
|---|
| 364 | ul = builder.ul(class_='tagcloud') |
|---|
| 365 | # Prepare current tag entry. |
|---|
| 366 | li = builder.li(renderer(tag, count, percent)) |
|---|
| 367 | if li: |
|---|
| 368 | # All tags checked; mark latest tag as last one (no tailing colon). |
|---|
| 369 | li(class_='last') |
|---|
| 370 | ul('\n', li, '\n') |
|---|
| 371 | return ul and ul or _("No tags found") |
|---|
| 372 | |
|---|
| 373 | def _paginate(self, req, results, realms): |
|---|
| 374 | query = req.args.get('q', None) |
|---|
| 375 | current_page = as_int(req.args.get('listtagged_page'), 1, min=1) |
|---|
| 376 | items_per_page = as_int(req.args.get('listtagged_per_page'), |
|---|
| 377 | self.items_per_page) |
|---|
| 378 | if items_per_page < 1: |
|---|
| 379 | items_per_page = self.items_per_page |
|---|
| 380 | try: |
|---|
| 381 | result = Paginator(results, current_page - 1, items_per_page) |
|---|
| 382 | except (AssertionError, TracError), e: |
|---|
| 383 | # AssertionError raised in Trac < 1.0.10, TracError otherwise |
|---|
| 384 | self.log.warn("ListTagged macro: %s", e) |
|---|
| 385 | current_page = 1 |
|---|
| 386 | result = Paginator(results, current_page - 1, items_per_page) |
|---|
| 387 | |
|---|
| 388 | pagedata = [] |
|---|
| 389 | shown_pages = result.get_shown_pages(21) |
|---|
| 390 | for page in shown_pages: |
|---|
| 391 | page_href = self.get_href(req, realms, query, items_per_page, page) |
|---|
| 392 | pagedata.append([page_href, None, str(page), |
|---|
| 393 | _("Page %(num)d", num=page)]) |
|---|
| 394 | |
|---|
| 395 | attributes = ['href', 'class', 'string', 'title'] |
|---|
| 396 | result.shown_pages = [dict(zip(attributes, p)) for p in pagedata] |
|---|
| 397 | |
|---|
| 398 | result.current_page = {'href': None, 'class': 'current', |
|---|
| 399 | 'string': str(result.page + 1), 'title': None} |
|---|
| 400 | |
|---|
| 401 | if result.has_next_page: |
|---|
| 402 | next_href = self.get_href(req, realms, query, items_per_page, |
|---|
| 403 | current_page + 1) |
|---|
| 404 | add_link(req, 'next', next_href, _('Next Page')) |
|---|
| 405 | |
|---|
| 406 | if result.has_previous_page: |
|---|
| 407 | prev_href = self.get_href(req, realms, query, items_per_page, |
|---|
| 408 | current_page - 1) |
|---|
| 409 | add_link(req, 'prev', prev_href, _('Previous Page')) |
|---|
| 410 | return result |
|---|