# -*- coding: utf-8 -*-
#
# AutoNav macro for Trac 0.11
#
# Author: Anders Jansson <anders dot jansson at kastanj dot net>
# License: BSD
# Modified by: 
#   Andrew Stromnov <stromnov at gmail dot com>
#   Christian Boos <cboos at neuf fr>

from genshi.builder import tag

from trac.core import *
from trac.wiki.api import parse_args
from trac.wiki.macros import WikiMacroBase
from StringIO import StringIO

__all__ = ['AutoNavMacro']


class AutoNavMacro(WikiMacroBase):
    """AutoNav finds all references in the wiki section to this Document

    It then shows them in a sorted list.

    Used with no arguments only produces a list from the database. Arguments
    sent to AutoNav will be merged inside the list too. Separate the
    arguments with comma.
    
    Example:
    `[[AutoNav()]]`	-> only references
    
    `[[AutoNav(MyPage)]]` -> references merged and sorted with MyPage
         
    `[[AutoNav(MyPage, MyPageToo, MyPageThree)]]` -> references merged with MyPage, MyPageToo and MyPageThree
    """
    
    def render_macro(self, formatter, name, args):
        if formatter.context.resource != 'wiki':
            return ''
        
	cursor = formatter.db.cursor()
	
	# get the refere page name
	thispage = formatter.context.id
	
	# process arguments
	pages, kw = parse_args(args)
	
	# query to get the latest version of a page
	query = """
	    SELECT w1.name 
	    FROM wiki w1, 
	        ( 
		    SELECT name, MAX(version) AS version 
		    FROM wiki 
		    GROUP BY name 
		) w2 
	    WHERE 
	        w1.version = w2.version AND 
		w1.name = w2.name AND 
		w1.text LIKE \'%%%s%%\' 
	    ORDER BY w1.name""" % thispage
	
	# TODO: use named parameters
	cursor.execute(query)
	
	# for each answer store in page
        for page, in cursor:
	    if page == thispage:
	        continue
	    pages.append(page)
	
	pages.sort()
	
	# get the references to each list
        def link(page):
            return tag.a(page, href=formatter.href.wiki(page))
        
	return tag(tag.strong('Navigation:'), '(',
                   [[link(page), ', '] for page in pages[:-2]],
                   link(page), ')')

