Back to index

moin  1.9.0~rc2
WantedPages.py
Go to the documentation of this file.
00001 # -*- coding: iso-8859-1 -*-
00002 """
00003     MoinMoin - WantedPages Macro
00004 
00005     @copyright: 2001 Juergen Hermann <jh@web.de>
00006     @license: GNU GPL, see COPYING for details.
00007 """
00008 
00009 from MoinMoin import wikiutil
00010 
00011 Dependencies = ["pages"]
00012 
00013 def macro_WantedPages(macro):
00014     request = macro.request
00015     _ = request.getText
00016 
00017     # prevent recursion
00018     if request.mode_getpagelinks:
00019         return ''
00020     if request.isSpiderAgent: # reduce bot cpu usage
00021         return ''
00022 
00023     # Get allpages switch from the form
00024     allpages = int(request.values.get('allpages', 0)) != 0
00025 
00026     # Control bar - filter the list of pages
00027     # TODO: we should make this a widget and use on all page listing pages
00028     label = (_('Include system pages'), _('Exclude system pages'))[allpages]
00029     page = macro.formatter.page
00030     controlbar = macro.formatter.div(1, css_class="controlbar") + \
00031                  page.link_to(request, label, querystr={'allpages': '%s' % (allpages and '0' or '1')}) + \
00032                  macro.formatter.div(0)
00033 
00034     # Get page dict readable by current user
00035     pages = request.rootpage.getPageDict()
00036 
00037     # build a dict of wanted pages
00038     wanted = {}
00039     deprecated_links = []
00040     for name, page in pages.items():
00041         # Skip system pages, because missing translations are not wanted pages,
00042         # unless you are a translator and clicked "Include system pages"
00043         if not allpages and wikiutil.isSystemPage(request, name):
00044             continue
00045 
00046         # Add links to pages which do not exist in pages dict
00047         links = page.getPageLinks(request)
00048         is_deprecated = page.parse_processing_instructions(
00049                 ).get('deprecated', False)
00050 
00051         for link in links:
00052             if not link in pages and request.user.may.read(link):
00053                 if is_deprecated:
00054                     deprecated_links.append(link)
00055                 if link in wanted:
00056                     wanted[link][name] = 1
00057                 else:
00058                     wanted[link] = {name: 1}
00059 
00060     for link in deprecated_links:
00061         if len(wanted[link]) == 1:
00062             del wanted[link]
00063 
00064     # Check for the extreme case when there are no wanted pages
00065     if not wanted:
00066         return u"%s<p>%s</p>" % (controlbar, _("No wanted pages in this wiki."))
00067 
00068     # Return a list of page links
00069     wantednames = wanted.keys()
00070     wantednames.sort()
00071     result = []
00072     result.append(macro.formatter.number_list(1))
00073     for name in wantednames:
00074         if not name:
00075             continue
00076         result.append(macro.formatter.listitem(1))
00077         # Add link to the wanted page
00078         result.append(macro.formatter.pagelink(1, name, generated=1))
00079         result.append(macro.formatter.text(name))
00080         result.append(macro.formatter.pagelink(0, name))
00081 
00082         # Add links to pages that want this page, highliting
00083         # the link in those pages.
00084         where = wanted[name].keys()
00085         where.sort()
00086         if macro.formatter.page.page_name in where:
00087             where.remove(macro.formatter.page.page_name)
00088         wherelinks = [pages[pagename].link_to(request, querystr={'highlight': name}, rel='nofollow')
00089                       for pagename in where]
00090         result.append(": " + ', '.join(wherelinks))
00091         result.append(macro.formatter.listitem(0))
00092     result.append(macro.formatter.number_list(0))
00093 
00094     return u'%s%s' % (controlbar, u''.join(result))
00095