Edgewall Software

MacroBazaar: BackLinks.3.py

File BackLinks.3.py, 0.9 KB (added by Muness Alrubaie <muness@…>, 4 years ago)

Another version of BackLinks. This one only matches [wiki:... ] based links

Line 
1from StringIO import StringIO
2
3def execute(hdf, args, env):
4    db = env.get_db_cnx()
5    cursor = db.cursor()
6
7    thispage = None
8
9    if args:
10        thispage = args.replace('\'', '\'\'')
11    else :
12        thispage = hdf.getValue('wiki.page_name', '')
13
14
15    sql = 'SELECT w1.name FROM wiki w1, ' + \
16          '(SELECT name, MAX(version) AS VERSION FROM WIKI GROUP BY NAME) w2 ' + \
17          'WHERE w1.version = w2.version AND w1.name = w2.name '
18
19    if thispage:
20            sql += 'AND w1.text LIKE \'%%[wiki:%s %%\' ' % thispage
21
22    cursor.execute(sql)
23
24    buf = StringIO()
25
26    buf.write('Pages linking to %s:\n' % thispage)
27    buf.write('<ul>')
28
29    while 1:
30        row = cursor.fetchone()
31        if row == None:
32            break
33        if row[0] != thispage:
34            buf.write('<li><a href="%s">' % env.href.wiki(row[0]))
35            buf.write(row[0])
36            buf.write('</a></li>\n')
37
38    buf.write('</ul>')
39
40    return buf.getvalue()