Ticket #308: changeset_2164.diff

File changeset_2164.diff, 5.4 KB (added by rlrj60, 23 months ago)

patch for Trac 0.12.2

  • trac/TracRedirect/redirect/redirect.py

     
    4545 
    4646import re 
    4747from trac.core import Component, implements 
    4848from trac.wiki.api import WikiSystem, IWikiMacroProvider 
    4949from trac.web.api import IRequestFilter 
    5050from trac.web.chrome import ITemplateProvider, INavigationContributor, add_stylesheet 
    5151from trac.wiki.formatter import Formatter 
    5252from trac.util import Markup, arity 
    5353from StringIO import StringIO 
    5454 
    55 HREF_RE = re.compile(r'^.*href="([^"]*)".*$') 
     55HREF_RE = re.compile(r'^.*href="([^"]*)".*$') #"rlrj60 
    5656 
    5757 
    5858class RedirectFormatter(Formatter): 
    5959 
    6060    def __init__(self, *args, **kwargs): 
    6161        Formatter.__init__(self, *args, **kwargs) 
    6262        self.formatted_link = None 
    6363        self.missing = False 
    6464 
    6565    def format(self, text): 
     
    7171 
    7272    def _shref_formatter(self, match, fullmatch): 
    7373        if not self.formatted_link: 
    7474            namespace = fullmatch.group('sns') 
    7575            target = self._unquote(fullmatch.group('stgt')) 
    7676            if namespace == 'wiki': 
    7777                pagename = self.split_link(target)[0].split('@')[0] 
    7878                if not WikiSystem(self.env).has_page(pagename): 
    7979                    self.missing = True 
    8080            # compat: the signature changed between 0.11 and 0.11.1 
    81             if 6 == arity(self._make_link): 
     81            # FIXME:rlrj60:6/29/2011: port to 0.12.2 
     82            # if 6 == arity(self._make_link): 
     83            if True: 
    8284                self.formatted_link = self._make_link(namespace, target, match, match, fullmatch) 
    8385            else: 
    8486                self.formatted_link = self._make_link(namespace, target, match, match) 
    8587        return self.formatted_link 
    8688 
    8789 
    8890class TracRedirect(Component): 
    8991 
    9092    implements(IWikiMacroProvider, ITemplateProvider, INavigationContributor, IRequestFilter) 
    9193 
    9294    # IWikiMacroProvider methods 
    9395    def expand_macro(self, formatter, name, args): 
    9496        """ 
    9597        Main routine of the wiki macro. 
    9698        """ 
    9799        context = formatter.context 
    98100        req = context.req 
    99101 
    100102        preview = req.args.get('preview', '') 
    101103        should_redirect = req.args.get('redirect', 'yes') == 'yes' and (not preview) 
    102         curpage = context.resource.id or 'WikiStart' 
     104        #FIXME:rlrj60:6/29/2011: covert unicode id to string 
     105        curpage = str(context.resource.id) or 'WikiStart' 
    103106 
    104107        out = StringIO() 
    105108        link, missing = RedirectFormatter(self.env, context).format(args) 
    106109     
    107110        if link and not missing: 
    108111            out.write('<div class="system-message">') 
    109112            out.write('<strong>Redirect: </strong>') 
    110113            out.write('This page redirects to %s ' % link) 
    111114            out.write('</div>') 
    112115 
    113             self.log.debug('Redirect target=>>%s<<' %link) 
    114  
     116            self.log.debug('Redirect target=>>%s<<' % link) 
     117             
    115118            if should_redirect: 
    116                 link_target = HREF_RE.match(str(link)).group(1) 
    117  
    118                 out.write('<form id="redirect" method="GET" action="%s">' % link_target) 
    119                 out.write('<input type="hidden" name="redirectedfrom" value="%s">' % curpage) 
    120                 out.write('</form>') 
    121      
    122                 out.write('\n') 
    123                 out.write('<script language="JavaScript">\n') 
    124                 out.write('document.forms["redirect"].submit();\n') 
    125                 out.write('</script>\n') 
    126      
     119                try: 
     120                    link_target = HREF_RE.match(str(link)).group(1) 
     121                 
     122                    out.write('<form id="redirect" method="GET" action="%s">' % link_target) 
     123                    out.write('<input type="hidden" name="redirectedfrom" value="%s">' % curpage) 
     124                    out.write('</form>') 
     125         
     126                    out.write('\n') 
     127                    out.write('<script language="JavaScript">\n') 
     128                    out.write('document.forms["redirect"].submit();\n') 
     129                    out.write('</script>\n') 
     130                 
     131                # FIXME:rlrj60:6/29/2011: handle missing ticket/page 
     132                except AttributeError: 
     133                    # Internal link to a non-existing page 
     134                    out.write('<div class="system-message">') 
     135                    out.write('<strong>Redirection Error: </strong>') 
     136                    out.write('Redirection target not found: %s' % link) 
     137                    out.write('</div>') 
     138                     
    127139        elif link and missing: 
    128140            # Internal link to a non-existing page 
    129141            out.write('<div class="system-message">') 
    130142            out.write('<strong>Redirection Error: </strong>') 
    131143            out.write('Redirection target not found: %s' % link) 
    132144            out.write('</div>') 
    133145     
    134146        else: 
    135147            # No arguments given to the redirect macro 
    136148            out.write('<div class="system-message">') 
    137149            out.write('<strong>Redirection Error: </strong>') 
    138150            out.write('No redirection target given') 
    139151            out.write('</div>') 
    140      
     152         
    141153        return out.getvalue() 
    142154     
    143155    def get_macros(self): 
    144156        yield 'redirect' 
    145157 
    146158    def get_macro_description(self, name): 
    147159        from inspect import getdoc, getmodule 
    148160        return getdoc(getmodule(self)) 
    149161 
    150162