Source

cmsplugin-markup-tracwiki / cmsplugin_markup_tracwiki / tracwiki.py

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
import inspect
import os
import re
import string
import sys

from StringIO import StringIO

from genshi.builder import tag

from trac.core import *

from trac import cache
from trac import log
from trac import mimeview
from trac import test
from trac import resource
from trac import web
from trac import wiki
from trac.util import datefmt, translation as trac_translation
from trac.web import chrome as trac_chrome
from trac.web import main
from trac.wiki import interwiki

from django import http
from django import template
from django.conf import settings
from django.contrib.sites import models as sites_models
from django.core import urlresolvers
from django.core.servers import basehttp
from django.db.models import Q
from django.utils import functional
from django.utils import translation as django_translation
from django.utils import safestring

from cms import models as cms_models
from cms import utils as cms_utils
from cms.utils import moderator

if 'filer' in settings.INSTALLED_APPS:
    from filer.models import filemodels as filer_models
    USING_FILER = True
else:
    USING_FILER = False

from cmsplugin_blog import models as blog_models

from cmsplugin_markup import plugins as markup_plugins

from cmsplugin_markup_tracwiki import lazy

OBJ_ADMIN_RE_PATTERN = ur'\[\[CMSPlugin\(\s*(\d+)\s*\)\]\]'
OBJ_ADMIN_RE = re.compile(OBJ_ADMIN_RE_PATTERN)

PLUGIN_EDIT_RE_PATTERN = ur'edit-plugin/(\d+)'
PLUGIN_EDIT_RE = re.compile(PLUGIN_EDIT_RE_PATTERN)

COMPONENTS = [
    'cmsplugin_markup_tracwiki.tracwiki.DjangoComponent',
    'cmsplugin_markup_tracwiki.tracwiki.DjangoInterWikiMap',
    'trac.mimeview.pygments',
    'trac.mimeview.rst',
    'trac.mimeview.txtl',
    'trac.mimeview.patch',
    'trac.mimeview.*',
    'trac.wiki.macros.MacroListMacro',
    'trac.wiki.macros.KnownMimeTypesMacro',
    'trac.wiki.macros.PageOutlineMacro',
    'trac.wiki.intertrac',
    'trac.web.chrome.Chrome',
    'cmsplugin_markup_tracwiki.components.BaseHandler',
    'cmsplugin_markup_tracwiki.macros.CMSPluginMacro',
    'cmsplugin_markup_tracwiki.macros.URLMacro',
    'cmsplugin_markup_tracwiki.macros.NowMacro',
]

TRACWIKI_HEADER_OFFSET = 1

if not hasattr(urlresolvers, 'reverse_lazy'):
    urlresolvers.reverse_lazy = functional.lazy(urlresolvers.reverse, str)

class LazyHref(lazy.LazyObject):
    def _setup(self):
        self._wrapped = web.href.Href(*self._wrapped_args, **self._wrapped_kwargs)

def tracwiki_base_path():
    return urlresolvers.reverse_lazy('cmsplugin_markup_tracwiki', kwargs={'path': ''})

class DjangoEnvironment(test.EnvironmentStub):
    """A Django environment for Trac."""
    
    def __init__(self):
        components = list(COMPONENTS)
        components.extend(getattr(settings, 'CMS_MARKUP_TRAC_COMPONENTS', []))

        super(DjangoEnvironment, self).__init__(enable=components)
        
        for c in components:
            module_and_class = c.rsplit('.', 1)
            if len(module_and_class) == 1:
                __import__(name=module_and_class[0])
            else:
                __import__(name=module_and_class[0], fromlist=[module_and_class[1]])

        self.href = LazyHref(tracwiki_base_path())

        self.config.set('trac', 'default_charset', 'utf-8')
        self.config.set('trac', 'never_obfuscate_mailto', True)

        self.config.set('trac', 'default_handler', 'BaseHandler')

        # TODO: Use Django logging facilities?
        self.config.set('logging', 'log_level', 'WARN')
        self.config.set('logging', 'log_type', 'stderr')
        self.setup_log()

        for (ns, conf) in getattr(settings, 'CMS_MARKUP_TRAC_INTERTRAC', {}).iteritems():
            if 'URL' in conf:
                if 'TITLE' in conf:
                    self.config.set('intertrac', '%s.title' % (ns,), conf['TITLE'])
                self.config.set('intertrac', '%s.url' % (ns,), conf['URL'])
                self.config.set('intertrac', '%s.compat' % (ns,), conf.get('COMPAT', False))

        # TODO: Sync activated locales with Django?

    def set_abs_href(self, request):
        site = sites_models.Site.objects.get_current() if sites_models.Site._meta.installed else sites_models.RequestSite(request)

        server_port = str(request.META.get('SERVER_PORT', '80'))
        if request.is_secure():
            self.abs_href = LazyHref('https://' + site.domain + (':' + server_port if server_port != '443' else '') + self.href())
        else:
            self.abs_href = LazyHref('http://' + site.domain + (':' + server_port if server_port != '80' else '') + self.href())

class DjangoChrome(trac_chrome.Chrome):
    pass

class DjangoRequestDispatcher(main.RequestDispatcher):
    pass

class DjangoRequest(web.Request):
    def __init__(self, env, request, context, placeholder):
        super(DjangoRequest, self).__init__(request.META, self._start_response)

        # We override request's hrefs from environment (which are based on Django's URLs)
        self.href = env.href
        self.abs_href = env.abs_href

        self.django_request = request
        self.django_context = context
        self.django_placeholder = placeholder
        self.django_response = None
        
        self.perm = main.FakePerm()
        self.session = main.FakeSession()

        chrome = DjangoChrome(env)

        if request.user.is_authenticated():
            self.session['name'] = request.user.get_full_name()
            self.session['email'] = request.user.email

        self.callbacks.update({
            'authname': self._get_authname,
            'chrome': chrome.prepare_request,
            'tz': self._get_timezone,
            'locale': self._get_locale,
        })
   
    def _get_authname(self, req):
        if self.django_request.user.is_authenticated():
            return self.django_request.user.username
        else:
            return 'anonymous'

    def _get_locale(self, req):
        if trac_translation.has_babel:
            # TODO: Use django-cms function (get_language_from_request) instead? Why req is not used?
            return trac_translation.get_negotiated_locale([django_translation.get_language()])

    def _get_timezone(self, req):
        # Django sets TZ environment variable
        return datefmt.localtz

    def _start_response(self, status, headers, exc_info=None):
        if exc_info:
            try:
                raise exc_info[0], exc_info[1], exc_info[2]
            finally:
                exc_info = None # Avoids dangling circular ref

        headers = dict(headers)

        if 'Content-Type' in headers:
            content_type = headers.pop('Content-Type')
        else:
            content_type = None

        self.django_response = http.HttpResponse(status=status, content_type=content_type)

        for (k, v) in headers.iteritems():
            self.django_response[k] = v

        return self.django_response.write

class DjangoInterWikiMap(interwiki.InterWikiMap):
    """
    InterWiki map manager.
    """

    @cache.cached
    def interwiki_map(self, db):
        """
        Map from upper-cased namespaces to (namespace, prefix, title) values.
        """
        map = {}
        for (ns, conf) in getattr(settings, 'CMS_MARKUP_TRAC_INTERWIKI', {}).iteritems():
            if 'URL' in conf:
                url = conf['URL'].strip()
                title = conf.get('TITLE')
                title = title and title.strip() or ns
                map[ns.upper()] = (ns, url, title)
        return map

class DjangoFormatter(wiki.formatter.Formatter):
    def _parse_heading(self, match, fullmatch, shorten):
        (depth, heading, anchor) = super(DjangoFormatter, self)._parse_heading(match, fullmatch, shorten)
        depth = min(depth + getattr(settings, 'CMS_MARKUP_TRAC_HEADING_OFFSET', 1), 6)
        return (depth, heading, anchor)
    
    def _make_lhref_link(self, match, fullmatch, rel, ns, target, label):
        """We override _make_lhref_link to make 'cms' namespace default."""
        return super(DjangoFormatter, self)._make_lhref_link(match, fullmatch, rel, ns or 'cms', target, label)

    def _make_ext_link(self, url, text, title=''):
        # TODO: Make configurable which links are external, currently we do not render external links any differently than internal
        return tag.a(text, href=url, title=title or None)

    def _make_interwiki_link(self, ns, target, label):
        interwiki = DjangoInterWikiMap(self.env)
        if ns in interwiki:
            url, title = interwiki.url(ns, target)
            return self._make_ext_link(url, label, title)

class DjangoResource(resource.Resource):
    __slots__ = ('django_request', 'django_context')

class DjangoComponent(Component):
    implements(resource.IResourceManager, wiki.IWikiSyntaxProvider)
    
    def _format_link(self, formatter, ns, target, label, fullmatch=None):
        link, params, fragment = formatter.split_link(target)
        res = DjangoResource(ns, link)
        res.django_request = _get_django_request(req=formatter.req)
        res.django_context = _get_django_context(req=formatter.req)
        try:
            href = resource.get_resource_url(self.env, res, formatter.href)
            title = resource.get_resource_name(self.env, res)
            return tag.a(label, title=title, href=href + params + fragment)
        except resource.ResourceNotFound:
            return tag.a(label + '?', class_='missing', href=target, rel='nofollow')
    
    # IResourceManager methods
    
    def get_resource_realms(self):
        yield 'cms'
        if USING_FILER:
            yield 'filer'
        yield 'blog'
    
    def get_resource_url(self, res, href, **kwargs):
        if res.id is None or not res.realm:
            raise resource.ResourceNotFound()
        elif res.realm == 'filer':
            try:
                link = self._get_file(res).url
            except filer_models.File.DoesNotExist as e:
                raise resource.ResourceNotFound(e)
        elif res.realm == 'blog':
            try:
                link = self._get_blog(res).get_absolute_url()
            except blog_models.EntryTitle.DoesNotExist as e:
                raise resource.ResourceNotFound(e)
        elif res.realm == 'cms':
            try:
                request = _get_django_request(res=res)
                lang = cms_utils.get_language_from_request(request)
                link = self._get_page(res).get_absolute_url(language=lang)
            except cms_models.Page.DoesNotExist as e:
                # Test again as maybe we got [cms: this page] link but we could not get current page
                if not res.id:
                    raise resource.ResourceNotFound(e)

                try:
                    link = urlresolvers.reverse(res.id)
                except urlresolvers.NoReverseMatch as e:
                    raise resource.ResourceNotFound(e)
        else:
            raise RuntimeError("This should be impossible")
        
        args = [link]
        if link.endswith('/') and not link.strip('/') == '':
            # We add an empty link component at the end to force trailing slash
            args.append('')
        return href(*args, **kwargs)
    
    def get_resource_description(self, res, format='default', ctx=None, **kwargs):
        if res.id is None or not res.realm:
            raise resource.ResourceNotFound()
        elif res.realm == 'filer':
            try:
                return self._get_file(res, ctx).label
            except filer_models.File.DoesNotExist as e:
                raise resource.ResourceNotFound(e)
        elif res.realm == 'blog':
            try:
                return self._get_blog(res, ctx).title
            except blog_models.EntryTitle.DoesNotExist as e:
                raise resource.ResourceNotFound(e)
        elif res.realm == 'cms':
            try:
                request = _get_django_request(res=res, ctx=ctx)
                lang = cms_utils.get_language_from_request(request)
                return self._get_page(res, ctx).get_title(language=lang)
            except cms_models.Page.DoesNotExist as e:
                raise resource.ResourceNotFound(e)
        else:
            raise RuntimeError("This should be impossible")
    
    def resource_exists(self, res):
        if res.id is None or not res.realm:
            return False
        elif res.realm == 'filer':
            try:
                self._get_file(res)
                return True
            except filer_models.File.DoesNotExist:
                return False
        elif res.realm == 'blog':
            try:
                self._get_blog(res)
                return True
            except blog_models.EntryTitle.DoesNotExist:
                return False
        elif res.realm == 'cms':
            try:
                self._get_page(res)
                return True
            except cms_models.Page.DoesNotExist:
                pass

            # Test again as maybe we got [cms: this page] link but we could not get current page
            if not res.id:
                return False
            
            try:
                urlresolvers.reverse(res.id)
                return True
            except urlresolvers.NoReverseMatch:
                return False
        else:
            raise RuntimeError("This should be impossible")

    def _get_page(self, res, ctx=None):
        page_id = res.id
        request = _get_django_request(res=res, ctx=ctx)
        if not page_id: # links like [cms: current page]
            # cms.middleware.page.CurrentPageMiddleware is required for this
            if request.current_page:
                return request.current_page
            # It is not really necessary that the current page is known as plugins can be rendered also outside of pages (like in preview view in admin), we can try to use a hint
            elif request.POST.get('page_id'):
                return moderator.get_page_queryset(request).get(pk=request.POST['page_id'])
            else:
                context = _get_django_context(res=res, ctx=ctx)
                plugin = self._get_plugin(request, context)

                if not plugin:
                    raise cms_models.Page.DoesNotExist()

                try:
                    # TODO: If plugin is used in an app this does not find an anchor page for the app, but this happens only in a preview as otherwise request.current_page works
                    return plugin.placeholder.page_set.get()
                except cms_models.Page.MultipleObjectsReturned as e:
                    # Should not happen
                    raise cms_models.Page.DoesNotExist(e)
        else:
            return moderator.get_page_queryset(request).get(reverse_id=page_id)

    def _get_file(self, res, ctx=None):
        file_id = res.id
        if not file_id:
            raise filer_models.File.DoesNotExist()
        request = _get_django_request(res=res, ctx=ctx)
        try:
            f = filer_models.File.objects.get(Q(original_filename=file_id) | Q(name=file_id) | Q(sha1=file_id) | Q(file=file_id))
        except filer_models.File.MultipleObjectsReturned as e:
            raise filer_models.File.DoesNotExist(e)
        if f.is_public or f.has_read_permission(request):
            return f
        else:
            raise filer_models.File.DoesNotExist()

    def _get_blog(self, res, ctx=None):
        blog_id = res.id

        if not blog_id: # links like [blog: current blog entry]
            request = _get_django_request(res=res, ctx=ctx)
            context = _get_django_context(res=res, ctx=ctx)
            plugin = self._get_plugin(request, context)

            if not plugin:
                raise blog_models.EntryTitle.DoesNotExist()

            try:
                return plugin.placeholder.entry_set.get().entrytitle_set.get()
            except (blog_models.Entry.DoesNotExist, blog_models.Entry.MultipleObjectsReturned) as e:
                # MultipleObjectsReturned should not happen
                raise blog_models.EntryTitle.DoesNotExist(e)
            except blog_models.EntryTitle.MultipleObjectsReturned as e:
                # Should not happen
                raise blog_models.EntryTitle.DoesNotExist(e)

        else:
            ids = blog_id.split(":", 1)

            if len(ids) == 1:
                blog_id = ids[0]

                entries = blog_models.EntryTitle.objects.filter(slug=blog_id)
                if len(entries) == 0:
                    raise blog_models.EntryTitle.DoesNotExist
                elif len(entries) == 1:
                    return entries[0]
                else:
                    lang = django_translation.get_language()
                    return entries.get(language=lang)

            else:
                (lang, blog_id) = ids
                return blog_models.EntryTitle.objects.get(slug=blog_id, language=lang)

    def _get_plugin(self, request, context):
        if context and context.get('object'):
            return context['object']
        else:
            # We come to here probably only in admin
            match = PLUGIN_EDIT_RE.search(request.path)

            if request.POST.get('plugin_id') or match:
                try:
                    return cms_models.CMSPlugin.objects.get(pk=request.POST.get('plugin_id') or match.group(1))
                except cms_models.CMSPlugin.DoesNotExist:
                    return None
            else:
                return None
    
    # IWikiSyntaxProvider methods
    
    def get_wiki_syntax(self):
        return
    
    def get_link_resolvers(self):
        yield ('cms', self._format_link)
        if USING_FILER:
            yield ('filer', self._format_link)
        yield ('blog', self._format_link)

class Markup(markup_plugins.MarkupBase):
    name = 'Trac wiki'
    identifier = 'tracwiki'
    text_enabled_plugins = True
    is_dynamic = True

    def __init__(self, *args, **kwargs):
        self.env = DjangoEnvironment()
        self.scripts = []
        self.links = {}

    def parse(self, value, context=None, placeholder=None):
        request = _get_django_request(context=context)
        self.env.set_abs_href(request)
        if not context:
            context = template.RequestContext(request, {})
        req = DjangoRequest(self.env, request, context, placeholder)
        res = DjangoResource('cms', 'pages-root') # TODO: Get ID from request (and version?)
        ctx = mimeview.Context.from_request(req, res)
        out = StringIO()
        self._early_scripts_and_links(req)
        DjangoFormatter(self.env, ctx).format(value, out)
        self._all_scripts_and_links(req)
        return out.getvalue()

    def plugin_id_list(self, text):
        return OBJ_ADMIN_RE.findall(text)

    def _early_scripts_and_links(self, req):
        self._early_scripts_hrefs = [s['href'] for s in req.chrome.get('scripts', [])]
        self._early_links_ids = ['%s:%s' % (r, l['href']) for (r, ls) in req.chrome.get('links', {}).iteritems() for l in ls]

    def _all_scripts_and_links(self, req):
        for script in req.chrome.get('scripts', []):
            if script['href'] not in self._early_scripts_hrefs:
                self.scripts.append({'href': script['href'], 'type': script.get('type', "text/javascript")})

        for (rel, links) in req.chrome.get('links', {}).iteritems():
            for l in links:
                if '%s:%s' % (rel, l['href']) not in self._early_links_ids:
                    self.links.setdefault(rel, []).append(l)

    def get_scripts(self):
        return self.scripts

    def get_stylesheets(self):
        return [{'href': s['href'], 'type': s.get('type', "text/css")} for s in self.links.get('stylesheet', [])]

    def get_plugin_urls(self):
        from django.conf.urls.defaults import patterns, url
        
        urls = super(Markup, self).get_plugin_urls()

        trac_urls = patterns('',
            url(r'^tracwiki/(?P<path>.*)$', self.serve_trac_path, name='cmsplugin_markup_tracwiki'),
        )

        return trac_urls + urls

    def serve_trac_path(self, request, path):
        self.env.set_abs_href(request)
        context = template.RequestContext(request, {})
        req = DjangoRequest(self.env, request, context, None)

        base_path = req.href()
        req.environ['PATH_INFO'] = req.environ.get('PATH_INFO', '')[len(base_path):]

        # Have to encode Unicode back to UTF-8 for Trac
        if isinstance(req.environ.get('PATH_INFO', ''), unicode):
            req.environ['PATH_INFO'] = req.environ.get('PATH_INFO', '').encode('utf-8')

        try:
            dispatcher = DjangoRequestDispatcher(self.env)
            dispatcher.dispatch(req)
        except web.RequestDone:
            pass
        except web.HTTPNotFound, e:
            raise http.Http404(e)
        
        if req._response:
            req.django_response._container = req._response
            req.django_response._is_string = False
        
        return req.django_response

    def replace_plugins(self, text, id_dict):
        def _replace_tag(m):
            plugin_id = int(m.groups()[0])
            new_id = id_dict.get(plugin_id)
            try:
                obj = cms_models.CMSPlugin.objects.get(pk=new_id)
            except cms_models.CMSPlugin.DoesNotExist:
                # Object must have been deleted.  It cannot be rendered to
                # end user, or edited, so just remove it from the HTML
                # altogether
                return u''
            return u'[[CMSPlugin(%s)]]' % (new_id,)
        return OBJ_ADMIN_RE.sub(_replace_tag, text)

    def plugin_markup(self):
        return safestring.mark_safe(r"""function(plugin_id, icon_src, icon_alt) { return '[[CMSPlugin(' + plugin_id + ')]]'; }""")

    def plugin_regexp(self):
        return safestring.mark_safe(r"""function(plugin_id) { return new RegExp('\\[\\[CMSPlugin\\(\\s*' + plugin_id + '\\s*\\)\\]\\]', 'g'); }""")

def _get_django_request(req=None, context=None, res=None, ctx=None):
    if req and getattr(req, 'django_request', None):
        return req.django_request
    if context and context.get('request'):
        return context['request']
    if getattr(res, 'django_request', None):
        return res.django_request
    if ctx and ctx.req and getattr(ctx.req, 'django_request', None):
        return ctx.req.django_request

    frame = inspect.currentframe()
    try:
        while frame.f_back:
            frame = frame.f_back
            request = frame.f_locals.get('request')
            if request and isinstance(request, http.HttpRequest):
                return request
    finally:
        del frame
    return None

def _get_django_context(req=None, res=None, ctx=None):
    if req and getattr(req, 'django_context', None):
        return req.django_context
    if getattr(res, 'django_context', None):
        return res.django_context
    if ctx and ctx.req and getattr(ctx.req, 'django_context', None):
        return ctx.req.django_context

    frame = inspect.currentframe()
    try:
        while frame.f_back:
            frame = frame.f_back
            context = frame.f_locals.get('context')
            if context and isinstance(context, template.Context):
                return context
    finally:
        del frame
    return None