Commits

Bruno Martin committed 6b5b745

finished util/interwiki namespaces related refactory. Litle fix in meddleware/indexing

Comments (0)

Files changed (3)

MoinMoin/storage/middleware/indexing.py

     @property
     def name(self):
         name = self._current.get(NAME, self._name)
-        if type(name) is types.ListType:
+        if not name:
+            name = 'DoesNotExist'
+        elif type(name) is types.ListType:
             if self._name and self._name in name:
                 name = self._name
             else:
                 name = name[0]
-        elif not name:
-            name = 'DoesNotExist'
         return name
 
     @classmethod

MoinMoin/util/_tests/test_interwiki.py

 import tempfile
 import os.path
 import shutil
-import unittest
 
 from MoinMoin.util.interwiki import split_interwiki, join_wiki, InterWikiMap, url_for_item, _split_namespace
 from MoinMoin._tests import wikiconfig
 
 from flask import current_app as app
 
-class TestInterWiki():
+class TestInterWiki(object):
     class Config(wikiconfig.Config):
-        interwiki_map = {'Self': 'http://localhost/',
+        interwiki_map = {'Self': 'http://localhost',
                          'MoinMoin': 'http://moinmo.in/',
                          'OtherWiki': 'http://otherwiki.com/',
                          'OtherWiki:ns1': 'http://otherwiki.com/ns1/',
         revid = 'cdc431e0fc624d6fb8372152dcb66457'
 
         tests = [(('SomePage', '', '', CURRENT, 'frontend.show_item', False), '/SomePage'),
-                 (('SomePage', '', '', CURRENT, 'frontend.show_item', True), 'http://localhost/SomePage'),
+                 # Method signature to understand the tuple parameters
+                 # (item_name, wiki_name='', namespace='', rev=CURRENT, endpoint='frontend.show_item', _external=False):
+                 (('SomePage', '', '', CURRENT, 'frontend.show_item', True), 'http://localhost:8080/SomePage'),
                  (('SomePage', '', '', CURRENT, 'frontend.modify_item', False), '/+modify/SomePage'),
-                 # FIXME if you set interwiki_map = dict(Self='http://localhost/', MoinMoin='http://moinmo.in/', ),
+                 # FIXME if you set interwiki_map = dict(Self='http://localhost:8080', MoinMoin='http://moinmo.in/', ),
                  # the above line make it fails, it returns http://localhost/+modify/SomePage
-#                 (('SomePage', '', '', CURRENT, 'frontend.modify_item', True), 'http://localhost:8080/+modify/SomePage'),
+                 # (('SomePage', '', '', CURRENT, 'frontend.modify_item', True), 'http://localhost:8080/+modify/SomePage'),
                  (('SomePage', '', '', revid, 'frontend.show_item', False), '/+show/+{0}/SomePage'.format(revid)),
                  (('SomePage', '', '', revid, 'frontend.show_item_meta', False), '/+meta/+{0}/SomePage'.format(revid)),
                  # Valid namespaces
                  (('SomePage', '', 'ns1', CURRENT, 'frontend.show_item', False), '/:ns1:SomePage'),
-                 (('SomePage', '', 'ns1:ns2', CURRENT, 'frontend.show_item', True), 'http://localhost/:ns1:ns2:SomePage'),
+                 (('SomePage', '', 'ns1:ns2', CURRENT, 'frontend.show_item', True), 'http://localhost:8080/:ns1:ns2:SomePage'),
                  (('SomePage', '', 'ns1', CURRENT, 'frontend.modify_item', False), '/+modify/:ns1:SomePage'),
-                 (('SomePage', '', 'ns1:ns2', CURRENT, 'frontend.show_item_meta', True), 'http://localhost/+meta/:ns1:ns2:SomePage'),
-
+                 (('SomePage', '', 'ns1:ns2', CURRENT, 'frontend.show_item_meta', True), 'http://localhost:8080/+meta/:ns1:ns2:SomePage'),
                  (('SomePage', '', 'ns1', revid, 'frontend.show_item', False), '/+show/+{0}/:ns1:SomePage'.format(revid)),
                  (('SomePage', '', 'ns1:ns2', revid, 'frontend.show_item_meta', False), '/+meta/+{0}/:ns1:ns2:SomePage'.format(revid)),
 
-                 # FIXME will exist a map for this case?
                  (('SomePage', 'MoinMoin', 'ns1', CURRENT, 'frontend.show_item', False), 'http://moinmo.in/:ns1:SomePage'),
                  (('SomePage', 'MoinMoin', '', CURRENT, 'frontend.show_item', False), 'http://moinmo.in/SomePage'),
-#                 what to expect in these cases???
-#                 (('SomePage', 'MoinMoin', '', rev, 'frontend.show_item', False), 'http://moinmo.in/SomePage'),
-#                 (('SomePage', 'non-existent', '', CURRENT, 'frontend.show_item', False), '/non-existent:SomePage'),
-#                 (('SomePage', 'non-existent', 'ns1', CURRENT, 'frontend.show_item', False), '/non-existent:ns1:SomePage'),
+                 # FIXME will exist a map for this case? maybe there should be a placeholder for it.
+                 # we need that for wiki farms with common search index and search in non-current revisions.
+                 (('SomePage', 'MoinMoin', '', revid, 'frontend.show_item', False), 'http://moinmo.in/+show/+{0}/SomePage'.format(revid)),
+                 (('SomePage', 'non-existent', '', CURRENT, 'frontend.show_item', False), '/non-existent:SomePage'),
+                 (('SomePage', 'non-existent', 'ns1', CURRENT, 'frontend.show_item', False), '/non-existent:ns1:SomePage'),
                 ]
 
         for (item_name, wiki_name, namespace, rev, endpoint, _external), url in tests:
-            print (item_name, wiki_name, namespace, rev, endpoint, _external)
             assert url_for_item(item_name, wiki_name, namespace, rev, endpoint, _external) == url
 
     def test__split_namespace(self):
                 ]
         for markup, (namespace, pagename) in tests:
             assert _split_namespace(map, markup) == (namespace, pagename)
+            (namespace, pagename) = _split_namespace(map, markup)
+            assert isinstance(namespace, unicode)
+            assert isinstance(pagename, unicode)
 
     def test_split_interwiki(self):
         app.cfg.namespace_mapping = [(u'', 'default_backend'), (u'ns1:', 'default_backend'), (u'ns1:ns2:', 'other_backend')]
                  ('NonExistentWiki:OtherPage', ('Self', '', 'NonExistentWiki:OtherPage')),
                  (':ns1:OtherPage', ('Self', 'ns1', 'OtherPage')),
                  (':ns1:ns2:OtherPage', ('Self', 'ns1:ns2', 'OtherPage')),
-                 # XXX
-#                 ('ns1:OtherPage', ('Self', '', 'ns1:OtherPage')),
-#                 ('ns1:ns2:OtherPage', ('Self', '', 'ns1:ns2:OtherPage')),
                  ('ns1:OtherPage', ('Self', 'ns1', 'OtherPage')),
                  ('ns1:ns2:OtherPage', ('Self', 'ns1:ns2', 'OtherPage')),
-                 # XXX
                  ('OtherWiki:ns1:OtherPage', ('OtherWiki', 'ns1', 'OtherPage')),
                  ('OtherWiki:ns1:ns2:OtherPage', ('OtherWiki', 'ns1:ns2', 'OtherPage')),
                  ('OtherWiki:ns3:ns2:OtherPage/foo', ('OtherWiki', '', 'ns3:ns2:OtherPage/foo')),
                 ]
         for markup, (wikiname, namespace, pagename) in tests:
             assert split_interwiki(markup) == (wikiname, namespace, pagename)
+            (wikiname, namespace, pagename) = split_interwiki(markup)
+            assert isinstance(namespace, unicode)
+            assert isinstance(pagename, unicode)
+            assert isinstance(wikiname, unicode)
 
     def testJoinWiki(self):
         tests = [(('http://example.org/', u'SomePage', ''), 'http://example.org/SomePage'),
+                 (('', u'SomePage', ''), 'SomePage'),
                  (('http://example.org/?page=$PAGE&action=show', u'SomePage', ''), 'http://example.org/?page=SomePage&action=show'),
                  (('http://example.org/', u'Aktuelle\xc4nderungen', ''), 'http://example.org/Aktuelle%C3%84nderungen'),
                  (('http://example.org/$PAGE/show', u'Aktuelle\xc4nderungen', ''), 'http://example.org/Aktuelle%C3%84nderungen/show'),

MoinMoin/util/interwiki.py

     """
     check if <wiki_name> is THIS wiki
     """
-    return wiki_name in ['', 'Self', app.cfg.interwikiname, ]
+    return wiki_name in [u'', u'Self', app.cfg.interwikiname, ]
 
 
 def is_known_wiki(wiki_name):
     return wiki_name in app.cfg.interwiki_map
 
 
-def url_for_item(item_name, wiki_name='', namespace='', rev=CURRENT, endpoint='frontend.show_item', _external=False):
+def url_for_item(item_name, wiki_name=u'', namespace=u'', rev=CURRENT, endpoint=u'frontend.show_item', _external=False):
     """
     Compute URL for some local or remote/interwiki item.
 
     URLs are built in the same way as local URLs.
     Computed URLs are always fully specified.
     """
-    # TODO check if both namespace and Interwiki exists
     if is_local_wiki(wiki_name):
-        if namespace != '':
-            item_name = ':' + namespace + ':' + item_name
+        if namespace:
+            item_name = u':{0}:{1}'.format(namespace, item_name)
         if rev is None or rev == CURRENT:
             url = url_for(endpoint, item_name=item_name, _external=_external)
         else:
             wiki_base_url = app.cfg.interwiki_map[wiki_name]
         except KeyError, err:
             logging.warning("no interwiki_map entry for {0!r}".format(wiki_name))
-            url = '' # can we find something useful?
+            if namespace:
+                item_name = u'{0}:{1}'.format(namespace, item_name)
+            if wiki_name:
+                url = u'{0}:{1}'.format(wiki_name, item_name)
+            url = u'/{0}'.format(url)
         else:
             if (rev is None or rev == CURRENT) and endpoint == 'frontend.show_item':
                 # we just want to show latest revision (no special revision given) -
                 # rev and/or endpoint was given, assume same URL building as for local wiki.
                 # we need this for moin wiki farms, e.g. to link from search results to
                 # some specific item/revision in another farm wiki.
-                item_name = namespace + ':' + item_name
+                if namespace:
+                    item_name = u'{0}:{1}'.format(namespace, item_name)
                 local_url = url_for(endpoint, item_name=item_name, rev=rev, _external=False)
                 # we know that everything left of the + belongs to script url, but we
                 # just want e.g. +show/42/FooBar to append it to the other wiki's
                 url = wiki_base_url + path
     return url
 
-def _split_namespace(namespaces_set, url):
+def _split_namespace(namespaces, url):
     """
     Find the longest namespace in the set.
-    the namespaces are separeted by colons (:).
+    the namespaces are separated by colons (:).
     Example:
         namespaces_set(['ns1', 'ns1:ns2'])
         url: ns1:urlalasd return: ns1, urlalasd
     param url: string
     returns: (namespace, url)
     """
-    namespace = ''
+    namespace = u''
     tokens_list = url.split(':')
     for token in tokens_list:
         if namespace:
-            token = namespace + ':' + token
-        if token in namespaces_set:
+            token = u'{0}:{1}'.format(namespace, token)
+        if token in namespaces:
             namespace = token
         else:
             break
     if namespace:
         length = len(namespace) + 1
         url = url[length:]
-    return namespace, url
+    return namespace.decode('utf-8'), url.decode('utf-8')
 
 def split_interwiki(wikiurl):
     """ Split a interwiki name, into wikiname and pagename, e.g:
     'FrontPage' -> "Self", "", "FrontPage"
     'MoinMoin:Page with blanks' -> "MoinMoin", "", "Page with blanks"
     'MoinMoin:' -> "MoinMoin", "", ""
-    XXX
-    'MoinMoin:interwikins:AnyPage' -> "MoinMoin:interwikins:", "", "AnyPage"
-    XXX
+    'MoinMoin:interwikins:AnyPage' -> "MoinMoin", "interwikins", "AnyPage"
     ':ns:AnyPage' -> "Self", "ns", "AnyPage" if ns namespace exists or "Self", "", ":ns:AnyPage" if not.
+    'ns:AnyPage' -> "Self", "ns", "AnyPage" if ns namespace exists or "Self", "", "ns:AnyPage" if not.
     ':ns1:ns2:AnyPage' -> "Self", "ns1:ns2", "AnyPage" if ns1:ns2 namespace exists OR
                          "Self", "ns1", "ns2:AnyPage" if ns1 namespace exists OR
                          "Self", "", "ns1:ns2:AnyPage" else.
     :rtype: tuple
     :returns: (wikiname, namespace, pagename)
     """
-    namespace_mapping_set = set()
+    namespace_mapping = set()
     for namespace, _ in app.cfg.namespace_mapping:
-        namespace_mapping_set.add(namespace.rstrip(':'))
-    #Base case: no colon in wikiurl
-    if wikiurl.find(':') == -1:
-        return 'Self', '', wikiurl
+        namespace_mapping.add(namespace.rstrip(':'))
+    # Base case: no colon in wikiurl
+    if not ':' in wikiurl:
+        return u'Self', u'', wikiurl.decode('utf-8')
     if not wikiurl.startswith(':'):
-        interwikinamespace, item_name = _split_namespace(set(app.cfg.interwiki_map.keys()), wikiurl)
-        if not interwikinamespace:
-            # XXX if we want to process local name spaces whitout the colon at the beging, just uncoment the lines above:
-            localnamespace, item_name =  _split_namespace(set(namespace_mapping_set), wikiurl)
-            return 'Self', localnamespace, item_name
-#            return 'Self', '', item_name
-        if interwikinamespace.find(':') == -1:
-            return interwikinamespace, '', item_name
+        wikiname, item_name = _split_namespace(set(app.cfg.interwiki_map.keys()), wikiurl)
+        namespace = u''
+        if not wikiname:
+            namespace, item_name =  _split_namespace(set(namespace_mapping), wikiurl)
+            wikiname = u'Self'
         else:
-            return interwikinamespace.split(':', 1)[0], interwikinamespace.split(':', 1)[1], item_name
+            if ':' in wikiname:
+                namespace = wikiname.split(':', 1)[1]
+                wikiname = wikiname.split(':', 1)[0]
+        return wikiname.decode('utf-8'), namespace.decode('utf-8'), item_name.decode('utf-8')
     else:
-        localnamespace, item_name =  _split_namespace(set(namespace_mapping_set), wikiurl.split(':', 1)[1])
-        if not localnamespace:
-            item_name = ':' + item_name
-        return 'Self', localnamespace, item_name
+        namespace, item_name =  _split_namespace(set(namespace_mapping), wikiurl.split(':', 1)[1])
+        if not namespace:
+            item_name = u':{0}'.format(item_name)
+        return u'Self', namespace.decode('utf-8'), item_name.decode('utf-8')
 
 def join_wiki(wikiurl, wikitail, namespace):
     """
     wikitail = url_quote(wikitail, charset=config.charset, safe='/')
     namespace = url_quote(namespace, charset=config.charset, safe='/')
     if not('$PAGE' in wikiurl or '$NAMESPACE' in wikiurl):
-        if namespace != '':
-            namespace = ':' + namespace + ':'
+        if namespace:
+            namespace = u':{0}:'.format(namespace)
+        elif not wikiurl:
+            return wikitail
         return wikiurl + namespace + wikitail
     if '$PAGE' in wikiurl:
         wikiurl = wikiurl.replace('$PAGE', wikitail)
         wikiurl = wikiurl.replace('$NAMESPACE', namespace)
     return wikiurl
 
-
 def getInterwikiName(item_name):
     """
     Get the (fully qualified) interwiki name of a local item name.
     :rtype: unicode
     :returns: wiki_name:item_name
     """
-    return "{0}:{1}".format(app.cfg.interwikiname, item_name)
+    return u'{0}:{1}'.format(app.cfg.interwikiname, item_name)
 
 
 def getInterwikiHome(username):