Edgewall Software

MacroBazaar: SubWiki.3.py

File SubWiki.3.py, 2.7 KB (added by Muness Alrubaie <muness@…>, 6 years ago)

Yet another version SubWiki - supports levels and extracting first heading (as a description)

Line 
1"""
2Inserts an alphabetic list of sub-wiki pages into the output.
3A sub-wiki page is a page that is is deeper in the hierachy than the current page.  e.g. if the current page is People, the this will return a list of all wiki entries that start with "People/"
4
5Accepts a prefix string as parameter: if provided, only pages with names that
6start with the prefix are included in the resulting list. If this parameter is
7omitted, all pages are listed.
8
9This now takes the text of the first heading and displays it as the link name.
10
11"""
12
13from StringIO import StringIO
14import re
15import string
16
17def execute(hdf, args, env):
18    # Args seperated by commas:
19    # prefix,level
20    #
21    # Page Name prefix to search for.
22    # how many 'levels' in the hierarchy to go down.
23    db = env.get_db_cnx()
24    cursor = db.cursor()
25    cs = db.cursor()
26
27    prefix = hdf.getValue('wiki.page_name', '') + '/'
28    level = None
29    if args:
30        args = args.replace('\'', '\'\'')
31        args = args.split(',')
32        if args[0] != 'None':
33                prefix = args[0]
34        if len(args) > 1 and args[1] != 'None':
35                level = args[1]
36
37    sql = 'SELECT DISTINCT name FROM wiki '
38    if prefix:
39        sql += 'WHERE name LIKE \'%s%%\' ' % prefix
40    sql += 'ORDER BY name'
41    cursor.execute(sql)
42
43    buf = StringIO()
44    buf.write('<ul>')
45    while 1:
46        row = cursor.fetchone()
47        if row == None:
48            break
49        name = row[0]
50        if level:
51                len_name = name.split('/')
52                #buf.write(len_name)
53                if len(len_name) > int(level)+1:
54                        continue
55        # Get the latest revision only.
56        sql = 'SELECT name,text from wiki where name = \'%s\' order by version desc limit 1' % name
57        cs.execute(sql)
58        while 1:
59                csrow = cs.fetchone()
60                if csrow == None:
61                        break
62                name = csrow[0]
63                text = csrow[1]
64                (linktext,title,desc) = getInfo(db,name)
65
66                link = env.href.wiki(name)
67
68                buf.write('<li><a title="%s" href="%s">' % (title,link))
69                buf.write(linktext)
70                buf.write('</a> %s</li>\n' % desc)
71    buf.write('</ul>')
72
73    return buf.getvalue()
74
75def getInfo(db,name):
76    cs = db.cursor()
77    desc = name
78    # Get the latest revision only.
79    cs.execute('SELECT text from wiki where name = \'%s\' order by version desc limit 1' % name)
80    csrow = cs.fetchone()
81    prefix = ''
82
83    if csrow != None:
84        text = csrow[0]
85        m  = re.search('=+\s([^=]*)=+',text)
86        if m != None:
87            desc = string.strip(m.group(1))
88    else:
89        prefix = "Create "
90
91    title = StringIO()
92    title.write("%s%s"%(prefix, desc))
93    if prefix != '' or desc == name:
94       desc = ''
95
96    return (name,title.getvalue(),desc)