| 1 |
# |
|---|
| 2 |
# BackLinksMenu plugin for Trac 1.0 |
|---|
| 3 |
# |
|---|
| 4 |
# Author: Trapanator trap@trapanator.com |
|---|
| 5 |
# Website: http://www.trapanator.com/blog/archives/category/trac |
|---|
| 6 |
# |
|---|
| 7 |
# |
|---|
| 8 |
from StringIO import StringIO |
|---|
| 9 |
from genshi.builder import tag |
|---|
| 10 |
from trac.wiki.macros import WikiMacroBase |
|---|
| 11 |
import string |
|---|
| 12 |
from trac.core import * |
|---|
| 13 |
from trac.wiki.formatter import format_to_html |
|---|
| 14 |
from trac.util import TracError |
|---|
| 15 |
from trac.util.text import to_unicode |
|---|
| 16 |
from trac.wiki.model import WikiPage |
|---|
| 17 |
|
|---|
| 18 |
|
|---|
| 19 |
class BackLinksMenuMacro(WikiMacroBase): |
|---|
| 20 |
"""Backlinks |
|---|
| 21 |
|
|---|
| 22 |
Note that the name of the class is meaningful: |
|---|
| 23 |
- it must end with "Macro" |
|---|
| 24 |
- what comes before "Macro" ends up being the macro name |
|---|
| 25 |
|
|---|
| 26 |
The documentation of the class (i.e. what you're reading) |
|---|
| 27 |
will become the documentation of the macro, as shown by |
|---|
| 28 |
the !MacroList macro (usually used in the WikiMacros page). |
|---|
| 29 |
""" |
|---|
| 30 |
|
|---|
| 31 |
revision = "$Rev$" |
|---|
| 32 |
url = "$URL$" |
|---|
| 33 |
|
|---|
| 34 |
def expand_macro(self, formatter, name, args): |
|---|
| 35 |
db = self.env.get_db_cnx() |
|---|
| 36 |
cursor = db.cursor() |
|---|
| 37 |
|
|---|
| 38 |
thispage = None |
|---|
| 39 |
context = formatter.context |
|---|
| 40 |
resource = context.resource |
|---|
| 41 |
|
|---|
| 42 |
if args: |
|---|
| 43 |
thispage = args.replace('\'', '\'\'') |
|---|
| 44 |
else: |
|---|
| 45 |
#thispage = self.hdf.getValue('wiki.page_name', '') |
|---|
| 46 |
thispage = WikiPage(self.env, resource).name |
|---|
| 47 |
|
|---|
| 48 |
sql = 'SELECT w1.name FROM wiki w1, ' + \ |
|---|
| 49 |
'(SELECT name, MAX(version) AS VERSION FROM WIKI GROUP BY NAME) w2 ' + \ |
|---|
| 50 |
'WHERE w1.version = w2.version AND w1.name = w2.name ' |
|---|
| 51 |
|
|---|
| 52 |
if thispage: |
|---|
| 53 |
#sql += 'AND (w1.text LIKE \'%%[wiki:%s %%\' ' % (to_unicode(thispage, "utf-8").encode ("utf-8")) |
|---|
| 54 |
sql += 'AND (w1.text LIKE \'%%[wiki:%s %%\' ' % thispage |
|---|
| 55 |
sql += 'OR w1.text LIKE \'%%[wiki:%s]%%\')' % thispage |
|---|
| 56 |
#sql += 'OR w1.text LIKE \'%%[wiki:%s]%%\')' % (to_unicode (thispage, "utf-8").encode ("utf-8")) |
|---|
| 57 |
|
|---|
| 58 |
cursor.execute(sql) |
|---|
| 59 |
|
|---|
| 60 |
buf = StringIO() |
|---|
| 61 |
buf.write('<div class="wiki-toc">') |
|---|
| 62 |
buf.write('Pages linking to %s:<br />\n' % thispage ) |
|---|
| 63 |
|
|---|
| 64 |
while 1: |
|---|
| 65 |
row = cursor.fetchone() |
|---|
| 66 |
if row == None: |
|---|
| 67 |
break |
|---|
| 68 |
#s2 = unicode (thispage, "utf-8") |
|---|
| 69 |
s2 = thispage |
|---|
| 70 |
if row[0] == s2: |
|---|
| 71 |
pass |
|---|
| 72 |
else: |
|---|
| 73 |
buf.write('<a href="%s">' % self.env.href.wiki(row[0])) |
|---|
| 74 |
buf.write(row[0]) |
|---|
| 75 |
buf.write('</a><br />\n') |
|---|
| 76 |
|
|---|
| 77 |
buf.write('</div>') |
|---|
| 78 |
return buf.getvalue() |
|---|