Parent: [503f86] (diff)

Download this file

history.py    106 lines (85 with data), 3.7 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
import os
import json
from ming.orm.ormsession import ThreadLocalORMSession
from forgewiki.command.wiki2markdown.base import BaseImportUnit
from allura.command import base as allura_base
from pylons import c
from allura import model as M
from forgewiki import model as WM
from forgewiki import converters
class HistoryImportUnit(BaseImportUnit):
def _export_pages(self, p):
if p.neighborhood is None:
return
wiki_app = p.app_instance('wiki')
discussion_app = p.app_instance('discussion')
pid = "%s" % p._id
out_dir = os.path.join(self.options.output_dir, pid)
if not os.path.exists(out_dir):
os.mkdir(out_dir)
file_path = os.path.join(out_dir, "history.json")
json_history = {'pages': {}, 'discuss': {}}
if wiki_app is not None:
pages = WM.Page.query.find(dict(app_config_id=wiki_app.config._id)).all()
else:
pages = []
for page in pages:
for hist in page.history().all():
hist_id = "%s" % hist._id
json_history['pages'][hist_id] = {'text': converters.mediawiki2markdown(hist.data['text'])}
if discussion_app is not None:
discussions = M.Discussion.query.find(app_config_id=discussion_app.config._id).all()
else:
discussions = []
for discuss in discussions:
for post in discuss.posts:
for hist in post.history().all():
hist_id = "%s" % hist._id
json_history['discuss'][hist_id] = {'text': converters.mediawiki2markdown(hist.data['text'])}
if len(json_history['pages']) > 0 or len(json_history['discuss']) > 0:
with open(file_path, 'w') as history_file:
json.dump(json_history, history_file)
def _load_pages(self, p):
if p.neighborhood is None:
return
wiki_app = p.app_instance('wiki')
discussion_app = p.app_instance('discussion')
pid = "%s" % p._id
file_path = os.path.join(self.options.output_dir, pid, "history.json")
if not os.path.isfile(file_path):
return
json_history = {}
with open(file_path, 'r') as history_file:
json_history = json.load(history_file)
c.project = None
if wiki_app is not None:
pages = WM.Page.query.find(dict(app_config_id=wiki_app.config._id)).all()
else:
pages = []
for page in pages:
for hist in page.history().all():
hist_id = "%s" % hist._id
if hist_id in json_history['pages']:
hist.data['text'] = json_history['pages'][hist_id]['text']
if discussion_app is not None:
discussions = M.Discussion.query.find(app_config_id=discussion_app.config._id).all()
else:
discussions = []
for discuss in discussions:
for post in discuss.posts:
for hist in post.history().all():
hist_id = "%s" % hist._id
if hist_id in json_history['discuss']:
hist.data['text'] = json_history['discuss'][hist_id]['text']
ThreadLocalORMSession.flush_all()
ThreadLocalORMSession.close_all()
def extract(self):
projects = M.Project.query.find().all()
for p in projects:
self._export_pages(p)
allura_base.log.info("Export history complete")
def load(self):
projects = M.Project.query.find().all()
for p in projects:
self._load_pages(p)
allura_base.log.info("Load history complete")