"""
Inserts an alphabetic list of sub-wiki pages into the output.
A 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/"

Accepts a prefix string as parameter: if provided, only pages with names that
start with the prefix are included in the resulting list. If this parameter is
omitted, all pages are listed.
"""

from StringIO import StringIO

def execute(hdf, args, env):
    db = env.get_db_cnx()
    cursor = db.cursor()

    prefix = None
    if args:
        prefix = args.replace('\'', '\'\'')
    else :
	prefix = hdf.getValue('wiki.page_name', '') + '/'

    sql = 'SELECT DISTINCT name FROM wiki '
    if prefix:
        sql += 'WHERE name LIKE \'%s%%\' ' % prefix
    sql += 'ORDER BY name'
    cursor.execute(sql)

    buf = StringIO()
    buf.write('<ul>')
    while 1:
        row = cursor.fetchone()
        if row == None:
            break
        buf.write('<li><a href="%s">' % env.href.wiki(row[0]))
        buf.write(row[0])
        buf.write('</a></li>\n')
    buf.write('</ul>')

    return buf.getvalue()

