Edgewall Software

MacroBazaar: AutoNav.3.py

File AutoNav.3.py, 2.1 KB (added by anonymous, 6 years ago)

Fix truncated page list (don't stop enumerating pages after encountering thispage)

Line 
1"""     Trac Macro for inclusion of reverse navigation
2
3        COPYRIGHT(C): Anders Jansson, 2005, anders.jansson@kastanj.net
4
5        You are hereby free to do whatever You find appropriate with this code,
6        except sue me, even if it screws up Your life. If You find it useful
7        and appreciate it, send me an email and tell me so, otherwise don't
8        bother. Please leave a reminder of the original author if it resembles
9        the original code.
10"""
11
12from StringIO import StringIO
13
14def execute(hdf, args, env):
15    """ AutoNav(arg) finds all references in the wiki section to this Document
16        and shows them in a sorted list.
17
18        Used with no arguments only produces a list from the database. Arguments
19        sent to AnutoNav will be merged inside the list too. Separate the
20        arguments with comma.
21
22        Example:
23            [[AutoNav()]]       -> only references
24            [[AutoNav(MyPage)]] -> references merged and sorted with MyPage
25            [[AutoNav(MyPage, MyPageToo, MyPageThree)]]
26                                -> references merged with MyPage(|Too|Three)
27    """
28
29    db = env.get_db_cnx()
30    cursor = db.cursor()
31    buf = StringIO()
32
33    # get the refere page name
34    thispage = hdf.getValue('wiki.page_name', '')
35
36    # process arguments
37    pages = []
38    if args:
39        pages = args.split(','); # this is the split condition
40
41    # query to get the latest version of a page
42    query = """
43        SELECT w1.name
44        FROM wiki w1,
45            (
46                SELECT name, MAX(version) AS version
47                FROM wiki
48                GROUP BY name
49            ) w2
50        WHERE
51            w1.version = w2.version AND
52            w1.name = w2.name AND
53            w1.text LIKE \'%%%s%%\'
54        ORDER BY w1.name""" % thispage
55
56    # TODO: use named parameters
57    cursor.execute(query)
58
59    # for each answer store in page
60    while 1:
61        row = cursor.fetchone()
62        if row == None:
63            break
64        if row[0] == thispage:
65            continue
66        pages.append(row[0])
67
68    pages.sort()
69
70    # get the references to each list
71    outlist = []
72    for page in pages:
73        out = '<a href="%s">%s</a>' % (env.href.wiki(page), page)
74        outlist.append(out)
75
76    # format the list
77    buf.write('<strong>Navigation:</strong> ( ')
78    buf.write(", ".join(outlist))
79    buf.write(')\n')
80
81    return buf.getvalue()
82