Skip to content

Instantly share code, notes, and snippets.

@madphilosopher
Last active March 29, 2020 21:29
Show Gist options
  • Save madphilosopher/6125e819c2a42d607341b62886e7fe46 to your computer and use it in GitHub Desktop.
Save madphilosopher/6125e819c2a42d607341b62886e7fe46 to your computer and use it in GitHub Desktop.
tracwiki - Dump the current version of each page in a Trac wiki
#!/usr/bin/env python3
"""Dump the current version of each Trac wiki page."""
__RCS__ = '$Id: tracwiki 1975 2020-03-19 14:37:50Z darren $'
__initialdate__ = 'January 2020'
__author__ = 'Darren Paul Griffith <http://madphilosopher.ca/>'
import sqlite3
import argparse
DATABASE = '/home/trac/darren/db/trac.db'
KILLFILE = ('CamelCase',
'PageTemplates',
'InterMapTxt',
'InterTrac',
'InterWiki',
'RecentChanges',
'SandBox',
)
if __name__ == '__main__':
# get command-line arguments
parser = argparse.ArgumentParser()
parser.add_argument('-t', '--titles', help='Show wiki page names', action='store_true')
args = parser.parse_args()
# connect to the Trac database
conn = sqlite3.connect(DATABASE)
c = conn.cursor()
# setup before main loop
version_table = {} # to store the latest version number of each page
text_table = {} # to store the text of the page
max_name = 0 # length of the longest page name; used for formatting name column
# get all the versions of all the wiki pages
for row in c.execute('SELECT name,version,text from wiki;'):
name, version, text = row
if (name[0:4] != 'Trac') and (name[0:4] != 'Wiki') and (name not in KILLFILE): # ignore Trac wiki pages
version_table[name] = version
if not args.titles:
text_table[(name, version)] = text
if len(name) > max_name:
max_name = len(name)
# main output begins here
if args.titles:
# print all titles (wiki page names)
for name in sorted(version_table.keys()):
print(name)
else:
# output all wiki pages with the page name in the left-hand column
for name in sorted(version_table.keys()):
version = version_table[name]
key = (name, version)
text = text_table[key]
for line in text.splitlines():
print("{0:{1}} | {2}".format(name, max_name, line))
print()
print()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment