Re: [Patch] Generating relative links

Carl Worth <[email protected]> Tue, 07 Dec 2004 19:56:56 -0500
Newsgroups gmane.comp.web.wiki.moin.devel
Message-ID <[email protected]>
On Mon, 22 Nov 2004 14:31:58 -0500, Carl Worth wrote:
> The patch is against version 1.2.4 (as found in recent
> Debian/unstable). Let me know if I should regenerate it against a newer
> version.

I was told in IRC that a patch against the 1.3 stuff would be much
better. I've now spent some time with that and attached the results.

My previous patch replaced code like

> 	"%s/%s" % (request.getScriptname(), pagename)

with

> 	"%s%s" % (request.getRelativeScriptname(), pagename)

and then made getRelativeScriptname return '' if the relative_links
configuration parameter was true.

I reworked this patch against 1.3 and tested it quite thoroughly. It
worked almost everywhere, except for hierarchical pages where it breaks
completely, (as should be obvious).

The current patch is a step toward fixing that, but it will still need
some further work from someone who knows moin internals better than I
do.

It now has the literal '/' back into the format strings, and no
getRelativeScriptname returns '.' rather than ''.

So there are at least two things that need to be fixed:

1) The addition of "./" at the beginning of the script name seems to
   confuse moin. This will be quite apparent to anyone who applies the
   patch and tries it out. I imagine it should be a very simple matter
   to strip off this prefix early in the processing.

2) Hierarchical pages still aren't working with relative links, but the
   fix should now be very simple. It should simply be a matter of making
   getRelativeScriptname return something like ("../" * depth) for any
   hierarchical pages.

If someone wants to pick p the patch from here I would be glad to test
it. I probably won't do any more work on it myself since I'm still
dealing with a 1.2 installation and I've found an alternate solution to
the problem I was trying to solve.

I still think it would be good in the long run to switch to using
relative links. (And if it works well it might make more sense to not
make it conditional).

-Carl
moin-1.3_relative_links_cworth_20041207.patch (text/html, 11.9 KB)
diff -ur moin-1.3/MoinMoin/action/AttachFile.py moin-1.3-relative_links/MoinMoin/action/AttachFile.py
--- moin-1.3/MoinMoin/action/AttachFile.py	2004-11-22 15:05:08.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/action/AttachFile.py	2004-11-24 08:41:19.000000000 -0500
@@ -85,7 +85,7 @@
     else:
         # send file via CGI
         url = "%s/%s?action=%s&do=get&target=%s" % (
-            request.getScriptname(), wikiutil.quoteWikinameURL(pagename),
+            request.getRelativeScriptname(), wikiutil.quoteWikinameURL(pagename),
             action_name, urllib.quote_plus(filename.encode(config.charset)))
     if escaped:
         url = wikiutil.escape(url)
@@ -204,7 +204,7 @@
         for file in files:
             fsize = float(os.stat(os.path.join(attach_dir,file).encode(config.charset))[6]) # in byte
             fsize = "%.1f" % (fsize / 1024)
-            baseurl = request.getScriptname()
+            baseurl = request.getRelativeScriptname()
             action = action_name
             urlpagename = wikiutil.quoteWikinameURL(pagename)
             urlfile = urllib.quote_plus(file.encode(config.charset))
diff -ur moin-1.3/MoinMoin/multiconfig.py moin-1.3-relative_links/MoinMoin/multiconfig.py
--- moin-1.3/MoinMoin/multiconfig.py	2004-11-22 15:05:09.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/multiconfig.py	2004-11-22 15:56:28.000000000 -0500
@@ -198,6 +198,7 @@
         'up':          ("%(q_page_parent_page)s", _("Up"), "up"),
         }
     refresh = None # (minimum_delay, type), e.g.: (2, 'internal')
+    relative_links = False
     shared_intermap = None # can be string or list of strings (filenames)
     show_hosts = 1
     show_section_numbers = 1
diff -ur moin-1.3/MoinMoin/PageEditor.py moin-1.3-relative_links/MoinMoin/PageEditor.py
--- moin-1.3/MoinMoin/PageEditor.py	2004-11-22 15:06:12.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/PageEditor.py	2004-11-24 07:49:54.000000000 -0500
@@ -331,7 +331,7 @@
         
         # send form
         self.request.write('<form method="post" action="%s/%s#preview">' % (
-            self.request.getScriptname(),
+            self.request.getRelativeScriptname(),
             wikiutil.quoteWikinameURL(self.page_name),
             ))
 
diff -ur moin-1.3/MoinMoin/Page.py moin-1.3-relative_links/MoinMoin/Page.py
--- moin-1.3/MoinMoin/Page.py	2004-11-22 15:05:09.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/Page.py	2004-11-24 08:42:22.000000000 -0500
@@ -517,7 +517,7 @@
         @rtype: string
         @return: complete url of this page (including query string if specified)
         """
-        url = "%s/%s" % (request.getScriptname(), wikiutil.quoteWikinameURL(self.page_name))
+        url = "%s/%s" % (request.getRelativeScriptname(), wikiutil.quoteWikinameURL(self.page_name))
         if querystr:
             querystr = util.web.makeQueryString(querystr)
             url = "%s?%s" % (url, querystr)
@@ -730,7 +730,7 @@
                 if request.form.has_key('action') or request.form.has_key('redirect') or content_only: continue
 
                 request.http_redirect('%s/%s?action=show&redirect=%s' % (
-                    request.getScriptname(),
+                    request.getRelativeScriptname(),
                     wikiutil.quoteWikinameURL(pi_redirect),
                     urllib.quote_plus(self.page_name.encode(config.charset), ''),))
                 return
@@ -804,7 +804,7 @@
                     page_needle = '/' + page_needle.split('/')[-1]
                     
                 link = '%s/%s?action=fullsearch&amp;value=%s&amp;literal=1&amp;case=1&amp;context=40' % (
-                    request.getScriptname(),
+                    request.getRelativeScriptname(),
                     wikiutil.quoteWikinameURL(self.page_name),
                     urllib.quote_plus(page_needle.encode(config.charset), ''))
                 title = self.split_title(request)
diff -ur moin-1.3/MoinMoin/request.py moin-1.3-relative_links/MoinMoin/request.py
--- moin-1.3/MoinMoin/request.py	2004-11-22 15:06:23.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/request.py	2004-11-24 09:59:16.000000000 -0500
@@ -318,6 +318,12 @@
             return ''
         return self.script_name
 
+    def getRelativeScriptname(self):
+        if self.cfg.relative_links:
+            return '.'
+        else:
+            return self.getScriptname()
+
     def getKnownActions(self):
         """ Create a dict of avaiable actions
 
diff -ur moin-1.3/MoinMoin/theme/__init__.py moin-1.3-relative_links/MoinMoin/theme/__init__.py
--- moin-1.3/MoinMoin/theme/__init__.py	2004-11-22 15:06:12.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/theme/__init__.py	2004-11-24 08:49:23.000000000 -0500
@@ -642,7 +642,7 @@
         @return: html head
         """
         href = u'%s/RecentChanges?action=rss_rc&amp;ddiffs=1&amp;unique=1' % \
-               self.request.getScriptname()
+               self.request.getRelativeScriptname()
         return href
                            
     def rsslink(self):
diff -ur moin-1.3/MoinMoin/wikiutil.py moin-1.3-relative_links/MoinMoin/wikiutil.py
--- moin-1.3/MoinMoin/wikiutil.py	2004-11-22 15:05:38.000000000 -0500
+++ moin-1.3-relative_links/MoinMoin/wikiutil.py	2004-11-24 08:42:36.000000000 -0500
@@ -335,7 +335,7 @@
         for line in lines:
             if not line or line[0] == '#': continue
             try:
-                line = "%s %s/InterWiki" % (line, request.getScriptname()) 
+                line = "%s %s/InterWiki" % (line, request.getRelativeScriptname()) 
                 wikitag, urlprefix, trash = line.split(None, 2)
             except ValueError:
                 pass
@@ -345,9 +345,9 @@
         del lines
 
         # add own wiki as "Self" and by its configured name
-        _interwiki_list['Self'] = request.getScriptname() + '/'
+        _interwiki_list['Self'] = request.getRelativeScriptname() + '/'
         if request.cfg.interwikiname:
-            _interwiki_list[request.cfg.interwikiname] = request.getScriptname() + '/'
+            _interwiki_list[request.cfg.interwikiname] = request.getRelativeScriptname() + '/'
 
         # save for later
         request.cfg._interwiki_list = _interwiki_list
@@ -359,7 +359,7 @@
     if wikitag and _interwiki_list.has_key(wikitag):
         return (wikitag, _interwiki_list[wikitag], tail, False)
     else:
-        return (wikitag, request.getScriptname(), "/InterWiki", True)
+        return (wikitag, request.getRelativeScriptname(), "/InterWiki", True)
 
 
 #############################################################################
@@ -780,10 +780,10 @@
         text = params # default
     if formatter:
         if on != None:
-            return formatter.url(on, "%s/%s" % (request.getScriptname(),
+            return formatter.url(on, "%s/%s" % (request.getRelativeScriptname(),
                                                 params),
                                  css_class, **kw)
-        return (formatter.url(1, "%s/%s" % (request.getScriptname(), params),
+        return (formatter.url(1, "%s/%s" % (request.getRelativeScriptname(), params),
                               css_class, **kw) +
                 formatter.rawHTML(text) +
                 formatter.url(0))
@@ -795,7 +795,7 @@
         attrs += ' ' + kw['attrs']
     if css_class:
         attrs += ' class="%s"' % css_class
-    result = '<a%s href="%s/%s">' % (attrs, request.getScriptname(), params)
+    result = '<a%s href="%s/%s">' % (attrs, request.getRelativeScriptname(), params)
     if on:
         return result
     else:
@@ -982,12 +982,12 @@
 # later: <html xmlns=\"http://www.w3.org/1999/xhtml\">
 
     # Links
-    request.write('<link rel="Start" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(page_front_page)))
+    request.write('<link rel="Start" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(page_front_page)))
     if pagename:
         request.write('<link rel="Alternate" title="%s" href="%s/%s?action=raw">\n' % (
-            _('Wiki Markup'), request.getScriptname(), quoteWikinameURL(pagename),))
+            _('Wiki Markup'), request.getRelativeScriptname(), quoteWikinameURL(pagename),))
         request.write('<link rel="Alternate" media="print" title="%s" href="%s/%s?action=print">\n' % (
-            _('Print View'), request.getScriptname(), quoteWikinameURL(pagename),))
+            _('Print View'), request.getRelativeScriptname(), quoteWikinameURL(pagename),))
 
         # !!! currently disabled due to Mozilla link prefetching, see
         # http://www.mozilla.org/projects/netlib/Link_Prefetching_FAQ.html
@@ -999,24 +999,24 @@
         #~         # this shopuld never happend in theory, but let's be sure
         #~         pass
         #~     else:
-        #~         request.write('<link rel="First" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(all_pages[0]))
+        #~         request.write('<link rel="First" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(all_pages[0]))
         #~         if pos > 0:
-        #~             request.write('<link rel="Previous" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(all_pages[pos-1])))
+        #~             request.write('<link rel="Previous" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(all_pages[pos-1])))
         #~         if pos+1 < len(all_pages):
-        #~             request.write('<link rel="Next" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(all_pages[pos+1])))
-        #~         request.write('<link rel="Last" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(all_pages[-1])))
+        #~             request.write('<link rel="Next" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(all_pages[pos+1])))
+        #~         request.write('<link rel="Last" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(all_pages[-1])))
 
         if page_parent_page:
-            request.write('<link rel="Up" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(page_parent_page)))
+            request.write('<link rel="Up" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(page_parent_page)))
 
         from MoinMoin.action import AttachFile
         AttachFile.send_link_rel(request, pagename)
 
     request.write(
-        '<link rel="Search" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(page_find_page)) +
-        '<link rel="Index" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(page_title_index)) +
-        '<link rel="Glossary" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(page_word_index)) +
-        '<link rel="Help" href="%s/%s">\n' % (request.getScriptname(), quoteWikinameURL(page_help_formatting))
+        '<link rel="Search" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(page_find_page)) +
+        '<link rel="Index" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(page_title_index)) +
+        '<link rel="Glossary" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(page_word_index)) +
+        '<link rel="Help" href="%s/%s">\n' % (request.getRelativeScriptname(), quoteWikinameURL(page_help_formatting))
     )
 
     request.write("</head>\n")
diff -ur moin-1.3/wiki/config/farmconfig.py moin-1.3-relative_links/wiki/config/farmconfig.py
--- moin-1.3/wiki/config/farmconfig.py	2004-11-22 15:05:12.000000000 -0500
+++ moin-1.3-relative_links/wiki/config/farmconfig.py	2004-11-22 19:48:01.000000000 -0500
@@ -87,3 +87,4 @@
     # Needs a reliable internet connection.
     ## from MoinMoin.util.antispam import SecurityPolicy
 
+    relative_links = False
diff -ur moin-1.3/wiki/config/wikiconfig.py moin-1.3-relative_links/wiki/config/wikiconfig.py
--- moin-1.3/wiki/config/wikiconfig.py	2004-11-22 15:05:12.000000000 -0500
+++ moin-1.3-relative_links/wiki/config/wikiconfig.py	2004-11-22 19:52:04.000000000 -0500
@@ -141,4 +141,5 @@
     # Charts size, require gdchart (Set to None to disable).
     chart_options = {'width': 600, 'height': 300}   
 
+    relative_links = False