Back to index

moin  1.9.0~rc2
LocalSiteMap.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - LocalSiteMap action
00004 
00005     The LocalSiteMap action gives you a page that shows
00006     nearby links.  This is an example of what appears on the
00007     page (names are linkable on the real page):
00008 
00009     MoinMoin
00010          GarthKidd
00011               OrphanedPages
00012               WantedPages
00013          JoeDoe
00014               CategoryHomepage
00015                    CategoryCategory
00016                    WikiHomePage
00017               JoeWishes
00018               WikiWiki
00019                    OriginalWiki
00020 
00021     @copyright: 2001 Steve Howell <showell@zipcon.com>,
00022                 2001-2004 Juergen Hermann <jh@web.de>
00023     @license: GNU GPL, see COPYING for details.
00024 """
00025 
00026 from MoinMoin import wikiutil
00027 from MoinMoin.Page import Page
00028 
00029 class MaxNodesReachedException(Exception):
00030     pass
00031 
00032 def execute(pagename, request):
00033     _ = request.getText
00034 
00035     # This action generate data using the user language
00036     request.setContentLanguage(request.lang)
00037 
00038     request.theme.send_title(_('Local Site Map for "%s"') % (pagename), pagename=pagename)
00039 
00040     # Start content - IMPORTANT - witout content div, there is no
00041     # direction support!
00042     request.write(request.formatter.startContent("content"))
00043 
00044     request.write(LocalSiteMap(pagename).output(request))
00045 
00046     request.write(request.formatter.endContent()) # end content div
00047     request.theme.send_footer(pagename)
00048     request.theme.send_closing_html()
00049 
00050 class LocalSiteMap:
00051     def __init__(self, name):
00052         self.name = name
00053         self.result = []
00054 
00055     def output(self, request):
00056         tree = PageTreeBuilder(request).build_tree(self.name)
00057         #self.append("<small>")
00058         tree.depth_first_visit(request, self)
00059         #self.append("</small>")
00060         return """
00061 <p>
00062 %s
00063 </p>
00064 """ % ''.join(self.result)
00065 
00066     def visit(self, request, name, depth):
00067         """ Visit a page, i.e. create a link.
00068         """
00069         if not name:
00070             return
00071         _ = request.getText
00072         pg = Page(request, name)
00073         action = __name__.split('.')[-1]
00074         self.append('&nbsp;' * (5*depth+1))
00075         self.append(pg.link_to(request, querystr={'action': action}))
00076         self.append("&nbsp;<small>[")
00077         self.append(pg.link_to(request, _('view')))
00078         self.append("</small>]<br>")
00079 
00080     def append(self, text):
00081         self.result.append(text)
00082 
00083 
00084 class PageTreeBuilder:
00085     def __init__(self, request):
00086         self.request = request
00087         self.children = {}
00088         self.numnodes = 0
00089         self.maxnodes = 35
00090 
00091     def mark_child(self, name):
00092         self.children[name] = 1
00093 
00094     def child_marked(self, name):
00095         return name in self.children
00096 
00097     def is_ok(self, child):
00098         if not self.child_marked(child):
00099             if not self.request.user.may.read(child):
00100                 return 0
00101             if Page(self.request, child).exists():
00102                 self.mark_child(child)
00103                 return 1
00104         return 0
00105 
00106     def new_kids(self, name):
00107         # does not recurse
00108         kids = []
00109         for child in Page(self.request, name).getPageLinks(self.request):
00110             if self.is_ok(child):
00111                 kids.append(child)
00112         return kids
00113 
00114     def new_node(self):
00115         self.numnodes = self.numnodes + 1
00116         if self.numnodes == self.maxnodes:
00117             raise MaxNodesReachedException
00118 
00119     def build_tree(self, name):
00120         self.mark_child(name)
00121         tree = Tree(name)
00122         try:
00123             self.recurse_build([tree], 1)
00124         except MaxNodesReachedException:
00125             pass
00126         return tree
00127 
00128     def recurse_build(self, trees, depth):
00129         all_kids = []
00130         for tree in trees:
00131             kids = self.new_kids(tree.node)
00132             for kid in kids:
00133                 newTree = Tree(kid)
00134                 tree.append(newTree)
00135                 self.new_node()
00136                 all_kids.append(newTree)
00137         if len(all_kids):
00138             self.recurse_build(all_kids, depth+1)
00139 
00140 class Tree:
00141     def __init__(self, node):
00142         self.node = node
00143         self.children = []
00144 
00145     def append(self, node):
00146         self.children.append(node)
00147 
00148     def depth_first_visit(self, request, visitor, depth=0):
00149         visitor.visit(request, self.node, depth)
00150         for c in self.children:
00151             c.depth_first_visit(request, visitor, depth+1)
00152