# -*- coding: iso-8859-1 -*-
"""MoinMoin - Backlinks

A macro to embed backlinks into the current page.

Syntax:
    [[Backlinks]]

$Id: Backlinks.py,v 1.1.1.1 2007-06-07 20:36:15 gber Exp $
Copyright 2007 by Guido Berhoerster <guido+moinmoin@berhoerster.name>
Licensed under the GNU GPL, see COPYING for details.

$Log: Backlinks.py,v $
Revision 1.1.1.1  2007-06-07 20:36:15  gber
initial import into CVS


"""

from MoinMoin import search

# do not cache
Dependencies = ["namespace"]

def execute(macro, args):
    request = macro.request
    _ = request.getText
    content = []

    # do not show to spiders in order to avoid heavy cpu load
    if request.isSpiderAgent:
        return ''

    page_name = macro.formatter.page.page_name

    # backlinks get wrapped and a heading
    content.append(macro.formatter.div(1, css_class="backlinks"))
    content.append(macro.formatter.heading(1,1))
    content.append(macro.formatter.escapedText(_("Backlinks")))
    content.append(macro.formatter.heading(0,1))

    # search for pages linking to this one and sort results by name
    query = search.LinkSearch(page_name)
    results = search.searchPages(request, query)
    results.sortByPagename()

    content.append(results.pageList(request, request.formatter,
        numbered=0))
    content.append(macro.formatter.div(0))

    return "".join(content)

