2009-07-12 19:55:54 +02:00
|
|
|
from datetime import datetime
|
|
|
|
|
2009-07-07 18:16:58 +02:00
|
|
|
from Acquisition import aq_inner, aq_parent
|
|
|
|
|
|
|
|
from Products.Five.browser import BrowserView
|
|
|
|
from Products.Five.browser.pagetemplatefile import ViewPageTemplateFile
|
|
|
|
|
|
|
|
from Products.CMFCore.utils import getToolByName
|
|
|
|
|
|
|
|
from Products.CMFPlone import PloneMessageFactory as _
|
|
|
|
|
|
|
|
from Products.statusmessages.interfaces import IStatusMessage
|
|
|
|
|
|
|
|
from Products.CMFCore.interfaces import IContentish
|
|
|
|
|
2009-12-22 00:36:35 +01:00
|
|
|
from Products.CMFCore.interfaces._content import IDiscussionResponse
|
|
|
|
|
|
|
|
import transaction
|
|
|
|
|
2009-07-07 18:16:58 +02:00
|
|
|
from zope.component import createObject
|
|
|
|
|
2009-07-12 18:09:34 +02:00
|
|
|
from plone.app.discussion.comment import CommentFactory
|
|
|
|
|
2009-12-22 00:36:35 +01:00
|
|
|
from plone.app.discussion.interfaces import IConversation, IReplies, IComment
|
2009-07-07 18:16:58 +02:00
|
|
|
|
|
|
|
|
|
|
|
class View(BrowserView):
|
|
|
|
"""Migration View
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __call__(self):
|
|
|
|
|
|
|
|
context = aq_inner(self.context)
|
|
|
|
out = []
|
2009-07-12 18:09:34 +02:00
|
|
|
self.total_comments_migrated = 0
|
2009-07-13 22:22:37 +02:00
|
|
|
self.total_comments_deleted = 0
|
2010-01-03 18:36:01 +01:00
|
|
|
|
|
|
|
dry_run = self.request.has_key("dry_run")
|
|
|
|
|
|
|
|
# This is for testing only.
|
|
|
|
# Do not use transactions during a test.
|
|
|
|
test = self.request.has_key("test")
|
|
|
|
|
|
|
|
if not test:
|
|
|
|
transaction.begin()
|
2009-12-22 00:36:35 +01:00
|
|
|
|
2009-07-13 22:22:37 +02:00
|
|
|
catalog = getToolByName(context, 'portal_catalog')
|
|
|
|
dtool = context.portal_discussion
|
2009-12-22 00:36:35 +01:00
|
|
|
|
2009-07-07 18:16:58 +02:00
|
|
|
def log(msg):
|
|
|
|
context.plone_log(msg)
|
|
|
|
out.append(msg)
|
|
|
|
|
2009-07-12 18:09:34 +02:00
|
|
|
def migrate_replies(context, in_reply_to, replies, depth=0):
|
2009-07-13 22:22:37 +02:00
|
|
|
# Recursive function to migrate all direct replies
|
|
|
|
# of a comment. Returns True if there are no replies to
|
|
|
|
# this comment left, and therefore the comment can be removed.
|
|
|
|
if len(replies) == 0:
|
|
|
|
return True
|
2009-07-12 18:09:34 +02:00
|
|
|
|
|
|
|
for reply in replies:
|
|
|
|
|
|
|
|
# log
|
|
|
|
indent = " "
|
|
|
|
for i in range(depth):
|
|
|
|
indent += " "
|
|
|
|
log("%smigrate_reply: '%s'." % (indent, reply.title))
|
|
|
|
|
|
|
|
# create a reply object
|
|
|
|
comment = CommentFactory()
|
|
|
|
comment.title = reply.Title()
|
|
|
|
comment.text = reply.text
|
|
|
|
comment.creator = reply.Creator()
|
2009-07-12 19:55:54 +02:00
|
|
|
|
|
|
|
comment.creation_date = datetime.fromtimestamp(reply.creation_date)
|
|
|
|
comment.modification_date = datetime.fromtimestamp(reply.modification_date)
|
2009-07-12 18:09:34 +02:00
|
|
|
|
|
|
|
comment.reply_to = in_reply_to
|
|
|
|
|
2009-07-13 23:01:38 +02:00
|
|
|
if in_reply_to == 0:
|
|
|
|
# Direct reply to a content object
|
|
|
|
new_in_reply_to = conversation.addComment(comment)
|
|
|
|
else:
|
|
|
|
# Reply to another comment
|
|
|
|
comment_to_reply_to = conversation.get(in_reply_to)
|
|
|
|
replies = IReplies(comment_to_reply_to)
|
|
|
|
new_in_reply_to = replies.addComment(comment)
|
|
|
|
|
2009-07-12 18:09:34 +02:00
|
|
|
self.total_comments_migrated += 1
|
|
|
|
|
|
|
|
# migrate all talkbacks of the reply
|
|
|
|
talkback = getattr( reply, 'talkback', None )
|
2009-07-13 22:22:37 +02:00
|
|
|
no_replies_left = migrate_replies(context, new_in_reply_to, talkback.getReplies(), depth=depth+1)
|
|
|
|
if no_replies_left:
|
|
|
|
# remove reply and talkback
|
|
|
|
talkback.deleteReply(reply.id)
|
|
|
|
obj = aq_parent(talkback)
|
|
|
|
obj.talkback = None
|
2009-07-14 00:14:30 +02:00
|
|
|
log("%sremove %s" % (indent, reply.id))
|
2009-07-13 22:22:37 +02:00
|
|
|
self.total_comments_deleted += 1
|
2009-07-12 18:09:34 +02:00
|
|
|
|
2009-07-14 00:14:30 +02:00
|
|
|
# Return True when all comments on a certain level have been migrated.
|
|
|
|
return True
|
|
|
|
|
2009-07-07 18:16:58 +02:00
|
|
|
# Find content
|
|
|
|
brains = catalog.searchResults(
|
|
|
|
object_provides='Products.CMFCore.interfaces._content.IContentish')
|
2009-07-12 18:09:34 +02:00
|
|
|
log("Found %s content objects." % len(brains))
|
2009-07-07 18:16:58 +02:00
|
|
|
|
2009-12-22 00:36:35 +01:00
|
|
|
count_discussion_items = len(catalog.searchResults(Type='Discussion Item'))
|
|
|
|
count_comments_pad = len(catalog.searchResults(object_provides=IComment.__identifier__))
|
|
|
|
count_comments_old = len(catalog.searchResults(object_provides=IDiscussionResponse.__identifier__))
|
|
|
|
|
|
|
|
log("Found %s Discussion Item objects." % count_discussion_items)
|
|
|
|
log("Found %s old discussion items." % count_comments_old)
|
|
|
|
log("Found %s plone.app.discussion comments." % count_comments_pad)
|
|
|
|
|
|
|
|
log("\n")
|
|
|
|
log("Start comment migration.")
|
|
|
|
|
2009-07-13 22:22:37 +02:00
|
|
|
# This loop is necessary to get all contentish objects, but not
|
|
|
|
# the Discussion Items. This wouldn't be necessary if the
|
|
|
|
# zcatalog would support NOT expressions.
|
|
|
|
new_brains = []
|
2009-07-07 18:16:58 +02:00
|
|
|
for brain in brains:
|
|
|
|
if brain.portal_type != 'Discussion Item':
|
2009-07-13 22:22:37 +02:00
|
|
|
new_brains.append(brain)
|
|
|
|
|
|
|
|
# Recursively run through the comment tree and migrate all comments.
|
|
|
|
for brain in new_brains:
|
|
|
|
obj = brain.getObject()
|
|
|
|
talkback = getattr( obj, 'talkback', None )
|
|
|
|
if talkback:
|
|
|
|
replies = talkback.getReplies()
|
|
|
|
if replies:
|
|
|
|
conversation = IConversation(obj)
|
2009-12-22 00:36:35 +01:00
|
|
|
log("\n")
|
|
|
|
log("Migrate '%s' (%s)" % (obj.Title(), obj.absolute_url(relative=1)))
|
2009-07-13 22:22:37 +02:00
|
|
|
migrate_replies(context, 0, replies)
|
|
|
|
obj = aq_parent(talkback)
|
|
|
|
obj.talkback = None
|
|
|
|
|
|
|
|
if self.total_comments_deleted != self.total_comments_migrated:
|
|
|
|
log("Something went wrong during migration. The number of migrated comments (%s)\
|
|
|
|
differs from the number of deleted comments (%s)."
|
|
|
|
% (self.total_comments_migrated, self.total_comments_deleted))
|
2010-01-03 18:36:01 +01:00
|
|
|
if not test:
|
|
|
|
transaction.abort()
|
2009-12-22 00:36:35 +01:00
|
|
|
log("Abort transaction")
|
|
|
|
|
|
|
|
log("\n")
|
2009-07-07 18:16:58 +02:00
|
|
|
log("Comment migration finished.")
|
2009-12-22 00:36:35 +01:00
|
|
|
log("\n")
|
|
|
|
|
2009-07-12 18:09:34 +02:00
|
|
|
log("%s of %s comments migrated."
|
2010-01-02 20:45:05 +01:00
|
|
|
% (self.total_comments_migrated, count_comments_old))
|
|
|
|
|
|
|
|
if self.total_comments_migrated != count_comments_old:
|
|
|
|
log("%s comments could not be migrated." % (count_comments_old - self.total_comments_migrated))
|
|
|
|
log("Please make sure your portal catalog is up-to-date.")
|
2009-12-22 00:36:35 +01:00
|
|
|
|
2010-01-03 18:36:01 +01:00
|
|
|
if dry_run and not test:
|
2009-12-22 00:36:35 +01:00
|
|
|
transaction.abort()
|
|
|
|
log("Dry run")
|
|
|
|
log("Abort transaction")
|
2010-01-03 18:36:01 +01:00
|
|
|
if not test:
|
|
|
|
transaction.commit()
|
2009-07-12 18:09:34 +02:00
|
|
|
return '\n'.join(out)
|