annotate MoinMoin/util/interwiki.py @ 2657:080673c3e60a

Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
author Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
date Sun, 06 Jul 2014 09:25:31 +0530
parents 92a9c6ef1ec7
children
rev   line source
26
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 24
diff changeset
1 # Copyright: 2010 MoinMoin:ThomasWaldmann
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 24
diff changeset
2 # Copyright: 2010 MoinMoin:MicheleOrru
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 24
diff changeset
3 # License: GNU GPL v2 (or any later version), see LICENSE.txt for details.
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 24
diff changeset
4
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
5 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
6 MoinMoin - interwiki support code
26
4cd8edde9e94 automatically transform module docstrings, moving legal stuff to comments
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 24
diff changeset
7 """
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
8
134
901bd4e1861e import absolute_import, division from __future__ in every module
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 109
diff changeset
9 from __future__ import absolute_import, division
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
10
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
11 from werkzeug import url_quote
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
12
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
13 from flask import current_app as app
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
14 from flask import url_for
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
15
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
16 import os.path
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
17 from collections import namedtuple
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
18
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
19 from MoinMoin.constants.keys import CURRENT, FIELDS, NAME_EXACT, NAMESPACE
1973
286e315935ae refactor usage of constants
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
20 from MoinMoin.constants.contenttypes import CHARSET
894
93ae039cb363 fixed indexable and diff views, use revids not revnos for diffs and urls
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 536
diff changeset
21
318
b5c4372d5194 add missing log/logging import
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 313
diff changeset
22 from MoinMoin import log
b5c4372d5194 add missing log/logging import
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 313
diff changeset
23 logging = log.getLogger(__name__)
b5c4372d5194 add missing log/logging import
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 313
diff changeset
24
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
25
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
26 def is_local_wiki(wiki_name):
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
27 """
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
28 check if <wiki_name> is THIS wiki
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
29 """
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
30 return wiki_name in [u'', u'Self', app.cfg.interwikiname, ]
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
31
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
32
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
33 def is_known_wiki(wiki_name):
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
34 """
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
35 check if <wiki_name> is a known wiki name
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
36
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
37 Note: interwiki_map should have entries for the special wikinames
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
38 denoting THIS wiki, so we do not need to check these names separately.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
39 """
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
40 return wiki_name in app.cfg.interwiki_map
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
41
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
42
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
43 def get_fqname(item_name, field, namespace):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
44 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
45 Compute composite name from item_name, field, namespace
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
46 composite name == [NAMESPACE/][@FIELD/]NAME
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
47 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
48 if field and field != NAME_EXACT:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
49 item_name = u'@{0}/{1}'.format(field, item_name)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
50 if namespace:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
51 item_name = u'{0}/{1}'.format(namespace, item_name)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
52 return item_name
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
53
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
54
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
55 def url_for_item(item_name, wiki_name=u'', field=u'', namespace=u'', rev=CURRENT, endpoint=u'frontend.show_item', _external=False):
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
56 """
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
57 Compute URL for some local or remote/interwiki item.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
58
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
59 For local items:
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
60 give <rev> to get the url of some specific revision.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
61 give the <endpoint> to get the url of some specific view,
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
62 give _external=True to compute fully specified URLs.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
63
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
64 For remote/interwiki items:
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
65 If you just give <item_name> and <wiki_name>, a generic interwiki URL
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
66 will be built.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
67 If you also give <rev> and/or <endpoint>, it is assumed that remote wiki
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
68 URLs are built in the same way as local URLs.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
69 Computed URLs are always fully specified.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
70 """
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
71 if field == NAME_EXACT:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
72 field = u''
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
73 if is_local_wiki(wiki_name):
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
74 item_name = get_fqname(item_name, field, namespace)
894
93ae039cb363 fixed indexable and diff views, use revids not revnos for diffs and urls
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 536
diff changeset
75 if rev is None or rev == CURRENT:
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
76 url = url_for(endpoint, item_name=item_name, _external=_external)
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
77 else:
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
78 url = url_for(endpoint, item_name=item_name, rev=rev, _external=_external)
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
79 else:
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
80 try:
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
81 wiki_base_url = app.cfg.interwiki_map[wiki_name]
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
82 except KeyError, err:
969
5bf6d7a2ffcf Convert all %r, %s, %x, %d, %i, %o to new Format String Syntax
Vedran Mileti? <rivanvx@gmail.com>
parents: 894
diff changeset
83 logging.warning("no interwiki_map entry for {0!r}".format(wiki_name))
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
84 item_name = get_fqname(item_name, field, namespace)
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
85 if wiki_name:
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
86 url = u'{0}/{1}'.format(wiki_name, item_name)
1983
bb2f526d961c fix some stuff discovered by pycharm code inspection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
87 else:
bb2f526d961c fix some stuff discovered by pycharm code inspection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
88 url = item_name
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
89 url = u'/{0}'.format(url)
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
90 else:
894
93ae039cb363 fixed indexable and diff views, use revids not revnos for diffs and urls
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 536
diff changeset
91 if (rev is None or rev == CURRENT) and endpoint == 'frontend.show_item':
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
92 # we just want to show latest revision (no special revision given) -
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
93 # this is the generic interwiki url support, should work for any remote wiki
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
94 url = join_wiki(wiki_base_url, item_name, field, namespace)
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
95 else:
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
96 # rev and/or endpoint was given, assume same URL building as for local wiki.
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
97 # we need this for moin wiki farms, e.g. to link from search results to
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
98 # some specific item/revision in another farm wiki.
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
99 item_name = get_fqname(item_name, field, namespace)
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
100 local_url = url_for(endpoint, item_name=item_name, rev=rev, _external=False)
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
101 # we know that everything left of the + belongs to script url, but we
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
102 # just want e.g. +show/42/FooBar to append it to the other wiki's
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
103 # base URL.
2117
ad303592c64c Raise requirements to require latest flask/werkzeug/jinja2 release, latest emeraldtree.
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1983
diff changeset
104 i = local_url.index('/+')
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
105 path = local_url[i + 1:]
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
106 url = wiki_base_url + path
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
107 return url
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
108
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
109
2657
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
110 def get_download_file_name(fqname):
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
111 """
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
112 returns the filename that is used for downloading items
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
113 """
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
114 if fqname.field == NAME_EXACT:
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
115 return fqname.value
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
116 else:
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
117 return '{0}-{1}'.format(fqname.field, fqname.value)
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
118
080673c3e60a Now nameless items can also be downloaded and removed unnecessary blank spaces in items/content.py
Saurabh Kathpalia <saurabh.kathpalia95@gmail.com>
parents: 2590
diff changeset
119
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
120 def _split_namespace(namespaces, url):
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
121 """
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
122 Find the longest namespace in the set.
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
123 the namespaces are separated by slashes (/).
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
124 Example:
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
125 namespaces_set(['ns1', 'ns1/ns2'])
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
126 url: ns1/urlalasd return: ns1, urlalasd
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
127 url: ns3/urlalasd return: '', ns3/urlalasd
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
128 url: ns2/urlalasd return: '', ns2/urlalasd
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
129 url: ns1/ns2/urlalasd return: ns1/ns2, urlalasd
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
130 param namespaces_set: set of namespaces (strings) to search
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
131 param url: string
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
132 returns: (namespace, url)
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
133 """
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
134 namespace = u''
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
135 tokens_list = url.split('/')
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
136 for token in tokens_list:
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
137 if namespace:
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
138 token = u'{0}/{1}'.format(namespace, token)
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
139 if token in namespaces:
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
140 namespace = token
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
141 else:
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
142 break
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
143 if namespace:
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
144 length = len(namespace) + 1
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
145 url = url[length:]
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
146 return namespace, url
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
147
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
148
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
149 class CompositeName(namedtuple('CompositeName', 'namespace, field, value')):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
150 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
151 namedtuple to hold the compositename
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
152 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
153 @property
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
154 def split(self):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
155 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
156 returns a dict of field_names/field_values
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
157 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
158 return {NAMESPACE: self.namespace, u'field': self.field, u'item_name': self.value}
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
159
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
160 @property
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
161 def fullname(self):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
162 return get_fqname(self.value, self.field, self.namespace)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
163
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
164 def __unicode__(self):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
165 return self.fullname
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
166
2223
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
167 @property
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
168 def query(self):
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
169 """
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
170 returns a dict that can be used as a whoosh query
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
171 to lookup index documents matching this CompositeName
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
172 """
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
173 field = NAME_EXACT if not self.field else self.field
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
174 return {NAMESPACE: self.namespace, field: self.value}
fa049fc66a75 added a query method to CompositeName class.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2217
diff changeset
175
2236
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
176 def get_root_fqname(self):
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
177 """
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
178 Set value to the item_root of that namespace, and return
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
179 the new CompisteName.
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
180 """
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
181 return CompositeName(self.namespace, NAME_EXACT, app.cfg.root_mapping.get(self.namespace, app.cfg.default_root))
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
182
5f2fb2d7ed87 Replaced a single item_root to namespace-root mapping to add support for namespace specific redirecting.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2223
diff changeset
183
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
184 def split_fqname(url):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
185 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
186 Split a fully qualified url into namespace, field and pagename
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
187 url -> [NAMESPACE/][@FIELD/]NAME
2590
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
188
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
189 :param url: the url to split
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
190 :returns: a namedtuple CompositeName(namespace, field, itemname)
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
191 Examples::
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
192
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
193 url: u'ns1/ns2/@itemid/Page' return u'ns1/ns2', u'itemid', u'Page'
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
194 url: u'@revid/OtherPage' return u'', u'revid', u'OtherPage'
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
195 url: u'ns1/Page' return u'ns1', u'', u'Page'
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
196 url: u'ns1/ns2/@notfield' return u'ns1/ns2', u'', u'@notfield'
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
197 """
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
198 if not url:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
199 return CompositeName(u'', NAME_EXACT, u'')
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
200 namespaces = {namespace.rstrip('/') for namespace, _ in app.cfg.namespace_mapping}
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
201 namespace, url = _split_namespace(namespaces, url)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
202 field = NAME_EXACT
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
203 if url.startswith('@'):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
204 tokens = url[1:].split('/', 1)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
205 if tokens[0] in FIELDS:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
206 field = tokens[0]
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
207 url = tokens[1] if len(tokens) > 1 else u''
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
208 return CompositeName(namespace, field, url)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
209
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
210
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
211 def split_interwiki(wikiurl):
2590
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
212 """
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
213 Split a interwiki name, into wikiname and pagename, e.g::
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
214
2590
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
215 'MoinMoin/FrontPage' -> "MoinMoin", "", "", "FrontPage"
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
216 'FrontPage' -> "Self", "", "", "FrontPage"
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
217 'MoinMoin/Page with blanks' -> "MoinMoin", "", "", "Page with blanks"
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
218 'MoinMoin/' -> "MoinMoin", "", "", ""
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
219 'MoinMoin/@Someid/SomeValue' -> "MoinMoin", "", "Someid", "SomeValue" if Someid field exists or "MoinMoin", "", "", "Someid/SomePage" if not
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
220 'MoinMoin/interwikins/AnyPage' -> "MoinMoin", "interwikins", "", "AnyPage"
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
221 'ns/AnyPage' -> "Self", "ns", "", "AnyPage" if ns namespace exists or "Self", "", "", "ns:AnyPage" if not.
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
222 'ns1/ns2/AnyPage' -> "Self", "ns1/ns2", "", "AnyPage" if ns1/ns2 namespace exists OR
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
223 "Self", "ns1", "", "ns2/AnyPage" if ns1 namespace exists OR
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
224 "Self", "", "", "ns1/ns2/AnyPage" else.
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
225 'MoinMoin/ns/@Somefield/AnyPage' -> "MoinMoin", "ns", "", "@Somefield/AnyPage" if ns namespace exists and field Somefield does not OR
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
226 "MoinMoin", "ns", "Somefield", "AnyPage" if ns namespace and field Somefield exist OR
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
227 "MoinMoin", "", "", "ns/@Somefield/AnyPage" else.
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
228 :param wikiurl: the url to split
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
229 :rtype: tuple
92a9c6ef1ec7 reduce number of warning and error messages when running "m docs"
RogerHaase <haaserd@gmail.com>
parents: 2589
diff changeset
230 :returns: (wikiname, namespace, field, pagename)
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
231 """
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
232 if not isinstance(wikiurl, unicode):
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
233 wikiurl = wikiurl.decode('utf-8')
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
234 # Base case: no colon in wikiurl
2589
ee404f81afe9 fix new pep8 errors generated after pep8 is upgraded to 1.5.1
RogerHaase <haaserd@gmail.com>
parents: 2279
diff changeset
235 if '/' not in wikiurl:
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
236 return u'Self', u'', NAME_EXACT, wikiurl
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
237 wikiname = field = namespace = u''
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
238 if not wikiurl.startswith('/'):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
239 interwiki_mapping = set()
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
240 for interwiki_name in app.cfg.interwiki_map:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
241 interwiki_mapping.add(interwiki_name.split('/')[0])
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
242 wikiname, item_name = _split_namespace(interwiki_mapping, wikiurl)
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
243 if wikiname:
2279
49bc6f85f918 Fixed pep8 errors.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2236
diff changeset
244 wikiurl = wikiurl[len(wikiname) + 1:]
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
245 namespace, field, item_name = split_fqname(wikiurl)
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
246 if not wikiname:
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
247 wikiname = u'Self'
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
248 return wikiname, namespace, field, item_name
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
249 else:
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
250 namespace, field, item_name = split_fqname(wikiurl.split('/', 1)[1])
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
251 return u'Self', namespace, field, item_name
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
252
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
253
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
254 def join_wiki(wikiurl, wikitail, field, namespace):
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
255 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
256 Add a (url_quoted) page name to an interwiki url.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
257
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
258 Note: We can't know what kind of URL quoting a remote wiki expects.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
259 We just use a utf-8 encoded string with standard URL quoting.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
260
38
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
261 :param wikiurl: wiki url, maybe including a $PAGE placeholder
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
262 :param wikitail: page name
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
263 :param namespace: namespace
38
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
264 :rtype: string
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
265 :returns: generated URL of the page in the other wiki
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
266 """
1973
286e315935ae refactor usage of constants
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
267 wikitail = url_quote(wikitail, charset=CHARSET, safe='/')
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
268 field = url_quote(field, charset=CHARSET, safe='/')
1973
286e315935ae refactor usage of constants
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1832
diff changeset
269 namespace = url_quote(namespace, charset=CHARSET, safe='/')
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
270 if not('$PAGE' in wikiurl or '$NAMESPACE' in wikiurl or '$FIELD' in wikiurl):
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
271 return wikiurl + get_fqname(wikitail, field, namespace)
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
272 if '$PAGE' in wikiurl:
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
273 wikiurl = wikiurl.replace('$PAGE', wikitail)
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
274 if '$FIELD' in wikiurl:
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
275 wikiurl = wikiurl.replace('$FIELD', field)
1315
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
276 if '$NAMESPACE' in wikiurl:
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
277 wikiurl = wikiurl.replace('$NAMESPACE', namespace)
005e12de8b6a interwiki and namespaces related url generation and split adapted to namespaces. Proper documentation still missing.
Bruno Martin <bruno.martin@hacklab.com.br>
parents: 969
diff changeset
278 return wikiurl
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
279
1975
384555088cab lots of pep8 fixes
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1973
diff changeset
280
536
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
281 def getInterwikiName(item_name):
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
282 """
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
283 Get the (fully qualified) interwiki name of a local item name.
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
284
1983
bb2f526d961c fix some stuff discovered by pycharm code inspection
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 1975
diff changeset
285 :param item_name: item name (unicode)
536
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
286 :rtype: unicode
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
287 :returns: wiki_name:item_name
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
288 """
2217
431815d4f4a6 Fixed interwiki patch to deal with new composite name.
Ashutosh Singla <ashu1461@gmail.com>
parents: 2117
diff changeset
289 return u"{0}/{1}".format(app.cfg.interwikiname, item_name)
536
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
290
99426ee77abb revert cs 77f05fbc7d7e
Akash Sinha <akash2607@gmail.com>
parents: 532
diff changeset
291
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
292 def getInterwikiHome(username):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
293 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
294 Get a user's homepage.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
295
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
296 cfg.user_homewiki influences behaviour of this:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
297 'Self' does mean we store user homepage in THIS wiki.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
298 When set to our own interwikiname, it behaves like with 'Self'.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
299
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
300 'SomeOtherWiki' means we store user homepages in another wiki.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
301
38
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
302 :param username: the user's name
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
303 :rtype: tuple
1d8e9b1153d6 docstrings: convert epydoc fields to rst/sphinx fields
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 26
diff changeset
304 :returns: (wikiname, itemname)
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
305 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
306 homewiki = app.cfg.user_homewiki
313
8fa4a76d71ff cleanup interwiki code, new easier/more powerful functions
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents: 311
diff changeset
307 if is_local_wiki(homewiki):
0
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
308 homewiki = u'Self'
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
309 return homewiki, username
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
310
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
311
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
312 class InterWikiMap(object):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
313 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
314 Parse a valid interwiki map file/string, transforming into a simple python dict
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
315 object.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
316 Provides a set of utilities for parsing and checking a interwiki maps.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
317 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
318
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
319 SKIP = '#'
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
320
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
321 def __init__(self, s):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
322 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
323 Check for 's' to be a valid interwiki map string,
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
324 then parses it and stores to a dict.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
325 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
326 self.iwmap = dict()
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
327
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
328 for line in s.splitlines():
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
329 if self.SKIP in line:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
330 line = line.split(self.SKIP, 1)[0]
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
331 # remove trailing spaces (if any)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
332 line = line.rstrip()
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
333 if not line:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
334 continue
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
335
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
336 try:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
337 name, url = line.split(None, 1)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
338 self.iwmap[name] = url
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
339 except ValueError:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
340 raise ValueError('malformed interwiki map string: {0}'.format(
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
341 line))
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
342
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
343 @staticmethod
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
344 def from_string(ustring):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
345 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
346 Load and parse a valid interwiki map "unicode" object.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
347 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
348 return InterWikiMap(ustring)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
349
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
350 @staticmethod
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
351 def from_file(filename):
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
352 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
353 Load and parse a valid interwiki map file.
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
354 """
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
355 filename = os.path.expanduser(filename)
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
356 with open(filename) as f:
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
357 parser = InterWikiMap(f.read())
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
358
5568cf133caf create moin/2.0 repo, drop all history (see notes below)
Thomas Waldmann <tw AT waldmann-edv DOT de>
parents:
diff changeset
359 return parser