root/hodgestar/PythonCode/Tiki2Pm/importer.py

Revision 377, 4.4 kB (checked in by simon, 4 years ago)

Scripts for moving TikiWiki? data into PmWiki? and transforming the markup.

  • Property svn:mime-type set to text/python-source
  • Property svn:eol-style set to native
Line 
1import MySQLdb, sys
2import pmwiki, tikiwiki
3
4# Configuration
5
6dTikiDb = { 'host' : 'katfs', 'user' : 'katwiki', 'passwd' : '1rf2go',
7            'db': 'katwiki' }
8
9dConfig = { 'output_dir' : 'wiki.d', 'wiki_group' : 'Tiki',
10            'auto_group' : 'Auto', 'auto_author' : 'PmWikiImporter',
11            'pmwiki_version' : "2.2.0-beta65",
12            'uploads_dir' : 'uploads', 'uploads_prefix' : pmwiki.PmWikiWriter.PER_GROUP_UPLOAD }
13
14dBadAuthors = {
15}
16
17dBadPages = {
18}
19
20sys.setrecursionlimit(100000)
21
22#
23# Translation
24#
25
26def doWikiPages(oTiki,oTrans,oWriter):
27    global dConfig, dBadAuthors, dBadPages
28
29    for oPage in oTiki.pages():
30        if dBadAuthors.has_key(oPage.author): continue
31        if dBadPages.has_key(oPage.title): continue
32
33        sTitle = pmwiki.safePageName(oPage.title)
34        sData = oTrans.translate(sTitle,oPage.data)
35
36        for oComment in oPage.comments():
37            sData += "\n----" \
38                  + "\n!!!!Comment: " + oComment.title \
39                  + " (by [[~" + oComment.author + "]]" \
40                  + " on " + str(oComment.date) + ")\n" \
41                  + oTrans.translate(None,oComment.data)
42
43        attachments = list(oPage.attachments())
44        if len(attachments) > 0:
45            sData += "\n----" \
46                  + "\n!!!!Attachments:"
47
48            for oAttachment in attachments:
49                sData += "\n* [[Attach:" + oAttachment.filename + " | " + oAttachment.filename + "]] - added by " + oAttachment.author + " on " + str(oAttachment.date)
50                oWriter.writeFile(sTitle,dConfig['wiki_group'],oAttachment.data,oAttachment.filename)
51
52        oWriter.writePage(sTitle,dConfig['wiki_group'],oPage.author,sData,getattr(oPage,'date',None))
53
54def doResults(oTiki,oTrans,oWriter):
55    global dConfig
56
57    for sType, oPages in oTrans.matches():
58        aItems = list(oPages)
59        aItems.sort()
60        sText = "\n".join([ "*[[" + dConfig['wiki_group'] + "." + x + "]]" for x in aItems ])
61        oWriter.writePage(sType,dConfig['auto_group'],dConfig['auto_author'],sText)
62
63def doBlogs(oTiki,oTrans,oWriter):
64    global dConfig
65    sBlogMain = "(:pageList group={$Group} fmt=#include list=normal count=5 order=-ctime:)\n" \
66              + "*[[{$Group}.Archive]]\n" \
67              + "*Categories: [[!Blog]]\n"
68    sBlogArchive = "(:pageList group={$Group} fmt=#title list=normal order=-ctime:)\n" \
69              + "----\n" \
70              + "*[[{$Group}.{$Group}|{$Group}]]\n"
71
72    for oBlog in oTiki.blogs():
73        print oBlog.id, oBlog.title, oBlog.author
74
75        sGroup = pmwiki.safePageName(oBlog.title)
76        if not sGroup.endswith("Blog"):
77            sGroup = sGroup + "Blog"
78        oWriter.writePage(sGroup,sGroup,oBlog.author,sBlogMain,None,oBlog.date)
79        oWriter.writePage("Archive",sGroup,oBlog.author,sBlogArchive,oBlog.date)
80        iPostCnt = 0
81
82        for oPost in oBlog.posts():
83            print " [P] ", oPost.title
84            iPostCnt += 1
85
86            sTitle = pmwiki.safePageName(oPost.title)
87            if len(sTitle) == 0:
88                sTitle = sGroup + "Post" + str(iPostCnt)
89
90            sData = oTrans.translate(sTitle,oPost.data)
91
92            for oComment in oPost.comments():
93                print "   [C] ", oComment.title
94
95                sData += "\n----" \
96                      + "\n!!!!Comment: " + oComment.title \
97                      + " (by [[~" + oComment.author + "]]" \
98                      + " on " + str(oComment.date) + ")\n" \
99                      + oTrans.translate(None,oComment.data)
100       
101            oWriter.writePage(sTitle,sGroup,oPost.author,sData,None,oPost.date)
102
103def doPolls(oTiki,oTrans,oWriter):
104    for oPoll in oTiki.polls():
105        print oPoll.id, oPoll.title
106        for oOption in oPoll.options():
107            print " [O] ", oOption.title, oOption.votes
108        for oComment in oPoll.comments():
109            print " [C] ", oComment.title
110
111#
112# Main
113#
114
115def main(aArgs):
116    global dTikiDb, dConfig
117
118    oConn = MySQLdb.connect(**dTikiDb)
119    oWriter = pmwiki.PmWikiWriter(dConfig['output_dir'],dConfig['pmwiki_version'],dConfig['uploads_dir'],dConfig['uploads_prefix'])
120    oTrans = pmwiki.MarkUpTranslator()
121    oTiki = tikiwiki.Tiki(oConn)
122
123    for tRule in tikiwiki.Tiki2PmWiki.aRules:
124        oTrans.addRule(*tRule)
125
126    doWikiPages(oTiki,oTrans,oWriter)
127    #doResults(oTiki,oTrans,oWriter)
128    #doBlogs(oTiki,oTrans,oWriter)
129    #doPolls(oTiki,oTrans,oWriter)
130
131    oConn.close()
132
133if __name__ == '__main__':
134    sys.exit(main(sys.argv))
Note: See TracBrowser for help on using the browser.