def run(yielder, sp): # XXX add locking, acquire read-lock on sp if debug: self.log_status(ActionClass.INFO, raw_suffix="Processing %r" % sp) local_pagename = sp.local_name if not self.request.user.may.write(local_pagename): self.log_status(ActionClass.WARN, _("Skipped page %s because of no write access to local page."), (local_pagename, )) return current_page = PageEditor(self.request, local_pagename) # YYY direct access comment = u"Local Merge - %r" % (remote.get_interwiki_name() or remote.get_iwid()) tags = TagStore(current_page) matching_tags = tags.fetch(iwid_full=remote.iwid_full, direction=match_direction) matching_tags.sort() if debug: self.log_status(ActionClass.INFO, raw_suffix="Tags: %r <<BR>> All: %r" % (matching_tags, tags.tags)) # some default values for non matching tags normalised_name = None remote_rev = None local_rev = sp.local_rev # merge against the newest version old_contents = "" if matching_tags: newest_tag = matching_tags[-1] local_change = newest_tag.current_rev != sp.local_rev remote_change = newest_tag.remote_rev != sp.remote_rev # handle some cases where we cannot continue for this page if not remote_change and (direction == DOWN or not local_change): return # no changes done, next page if sp.local_deleted and sp.remote_deleted: return if sp.remote_deleted and not local_change: msg = local.delete_page(sp.local_name, comment) if not msg: self.log_status(ActionClass.INFO, _("Deleted page %s locally."), (sp.name, )) else: self.log_status(ActionClass.ERROR, _("Error while deleting page %s locally:"), (sp.name, ), msg) return if sp.local_deleted and not remote_change: if direction == DOWN: return yield remote.delete_page_pre(sp.remote_name, sp.remote_rev, local_full_iwid) msg = remote.delete_page_post(yielder.fetch_result()) if not msg: self.log_status(ActionClass.INFO, _("Deleted page %s remotely."), (sp.name, )) else: self.log_status(ActionClass.ERROR, _("Error while deleting page %s remotely:"), (sp.name, ), msg) return if sp.local_mime_type != MIMETYPE_MOIN and not (local_change ^ remote_change): self.log_status(ActionClass.WARN, _("The item %s cannot be merged automatically but was changed in both wikis. Please delete it in one of both wikis and try again."), (sp.name, )) return if sp.local_mime_type != sp.remote_mime_type: self.log_status(ActionClass.WARN, _("The item %s has different mime types in both wikis and cannot be merged. Please delete it in one of both wikis or unify the mime type, and try again."), (sp.name, )) return if newest_tag.normalised_name != sp.name: self.log_status(ActionClass.WARN, _("The item %s was renamed locally. This is not implemented yet. Therefore the full synchronisation history is lost for this page."), (sp.name, )) # XXX implement renames else: normalised_name = newest_tag.normalised_name local_rev = newest_tag.current_rev remote_rev = newest_tag.remote_rev old_contents = Page(self.request, local_pagename, rev=newest_tag.current_rev).get_raw_body_str() # YYY direct access else: if (sp.local_deleted and not sp.remote_rev) or ( sp.remote_deleted and not sp.local_rev): return self.log_status(ActionClass.INFO, _("Synchronising page %s with remote page %s ..."), (local_pagename, sp.remote_name)) if direction == DOWN: remote_rev = None # always fetch the full page, ignore remote conflict check patch_base_contents = "" else: patch_base_contents = old_contents # retrieve remote contents diff if remote_rev != sp.remote_rev: if sp.remote_deleted: # ignore remote changes current_remote_rev = sp.remote_rev is_remote_conflict = False diff = None self.log_status(ActionClass.WARN, _("The page %s was deleted remotely but changed locally."), (sp.name, )) else: yield remote.get_diff_pre(sp.remote_name, remote_rev, None, normalised_name) diff_result = remote.get_diff_post(yielder.fetch_result()) if diff_result is None: self.log_status(ActionClass.ERROR, _("The page %s could not be synced. The remote page was renamed. This is not supported yet. You may want to delete one of the pages to get it synced."), (sp.remote_name, )) return is_remote_conflict = diff_result["conflict"] assert diff_result["diffversion"] == 1 diff = diff_result["diff"] current_remote_rev = diff_result["current"] else: current_remote_rev = remote_rev if sp.local_mime_type == MIMETYPE_MOIN: is_remote_conflict = wikiutil.containsConflictMarker(old_contents.decode("utf-8")) else: is_remote_conflict = NotImplemented diff = None # do not sync if the conflict is remote and local, or if it is local # and the page has never been synchronised if (sp.local_mime_type == MIMETYPE_MOIN and wikiutil.containsConflictMarker(current_page.get_raw_body()) # YYY direct access and (remote_rev is None or is_remote_conflict)): self.log_status(ActionClass.WARN, _("Skipped page %s because of a locally or remotely unresolved conflict."), (local_pagename, )) return if remote_rev is None and direction == BOTH: self.log_status(ActionClass.INFO, _("This is the first synchronisation between the local and the remote wiki for the page %s."), (sp.name, )) # calculate remote page contents from diff if sp.remote_deleted: remote_contents = "" elif diff is None: remote_contents = old_contents else: remote_contents = patch(patch_base_contents, decompress(diff)) if diff is None: # only a local change if debug: self.log_status(ActionClass.INFO, raw_suffix="Only local changes for %r" % sp.name) merged_text_raw = current_page.get_raw_body_str() if sp.local_mime_type == MIMETYPE_MOIN: merged_text = merged_text_raw.decode("utf-8") elif local_rev == sp.local_rev: if debug: self.log_status(ActionClass.INFO, raw_suffix="Only remote changes for %r" % sp.name) merged_text_raw = remote_contents if sp.local_mime_type == MIMETYPE_MOIN: merged_text = merged_text_raw.decode("utf-8") else: # this is guaranteed by a check above assert sp.local_mime_type == MIMETYPE_MOIN remote_contents_unicode = remote_contents.decode("utf-8") # here, the actual 3-way merge happens merged_text = diff3.text_merge(old_contents.decode("utf-8"), remote_contents_unicode, current_page.get_raw_body(), 1, *conflict_markers) # YYY direct access if debug: self.log_status(ActionClass.INFO, raw_suffix="Merging %r, %r and %r into %r" % (old_contents.decode("utf-8"), remote_contents_unicode, current_page.get_raw_body(), merged_text)) merged_text_raw = merged_text.encode("utf-8") # generate binary diff diff = textdiff(remote_contents, merged_text_raw) if debug: self.log_status(ActionClass.INFO, raw_suffix="Diff against %r" % remote_contents) # XXX upgrade to write lock try: local_change_done = True current_page.saveText(merged_text, sp.local_rev or 0, comment=comment) # YYY direct access except PageEditor.Unchanged: local_change_done = False except PageEditor.EditConflict: local_change_done = False assert False, "You stumbled on a problem with the current storage system - I cannot lock pages" new_local_rev = current_page.get_real_rev() # YYY direct access def rollback_local_change(): # YYY direct local access comment = u"Wikisync rollback" rev = new_local_rev - 1 revstr = '%08d' % rev oldpg = Page(self.request, sp.local_name, rev=rev) pg = PageEditor(self.request, sp.local_name) if not oldpg.exists(): pg.deletePage(comment) else: try: savemsg = pg.saveText(oldpg.get_raw_body(), 0, comment=comment, extra=revstr, action="SAVE/REVERT") except PageEditor.Unchanged: pass return sp.local_name if local_change_done: self.register_rollback(rollback_local_change) if direction == BOTH: yield remote.merge_diff_pre(sp.remote_name, compress(diff), new_local_rev, current_remote_rev, current_remote_rev, local_full_iwid, sp.name) try: very_current_remote_rev = remote.merge_diff_post(yielder.fetch_result()) except NotAllowedException: self.log_status(ActionClass.ERROR, _("The page %s could not be merged because you are not allowed to modify the page in the remote wiki."), (sp.name, )) return else: very_current_remote_rev = current_remote_rev if local_change_done: self.remove_rollback(rollback_local_change) # this is needed at least for direction both and cgi sync to standalone for immutable pages on both # servers. It is not needed for the opposite direction try: tags.add(remote_wiki=remote_full_iwid, remote_rev=very_current_remote_rev, current_rev=new_local_rev, direction=direction, normalised_name=sp.name) except: self.log_status(ActionClass.ERROR, _("The page %s could not be merged because you are not allowed to modify the page in the remote wiki."), (sp.name, )) return if sp.local_mime_type != MIMETYPE_MOIN or not wikiutil.containsConflictMarker(merged_text): self.log_status(ActionClass.INFO, _("Page %s successfully merged."), (sp.name, )) elif is_remote_conflict: self.log_status(ActionClass.WARN, _("Page %s contains conflicts that were introduced on the remote side."), (sp.name, )) else: self.log_status(ActionClass.WARN, _("Page %s merged with conflicts."), (sp.name, ))
def xmlrpc_getDiff(self, pagename, from_rev, to_rev, n_name=None): """ Gets the binary difference between two page revisions. @param pagename: unicode string qualifying the page name @param fromRev: integer specifying the source revision. May be None to refer to a virtual empty revision which leads to a diff containing the whole page. @param toRev: integer specifying the target revision. May be None to refer to the current revision. If the current revision is the same as fromRev, there will be a special error condition "ALREADY_CURRENT" @param n_name: do a tag check verifying that n_name was the normalised name of the last tag If both fromRev and toRev are None, this function acts similar to getPage, i.e. it will diff("",currentRev). @return: Returns a dict: * status (not a field, implicit, returned as Fault if not SUCCESS): * "SUCCESS" - if the diff could be retrieved successfully * "NOT_EXIST" - item does not exist * "FROMREV_INVALID" - the source revision is invalid * "TOREV_INVALID" - the target revision is invalid * "INTERNAL_ERROR" - there was an internal error * "INVALID_TAG" - the last tag does not match the supplied normalised name * "ALREADY_CURRENT" - this not merely an error condition. It rather means that there is no new revision to diff against which is a good thing while synchronisation. * current: the revision number of the current revision (not the one which was diff'ed against) * diff: Binary object that transports a zlib-compressed binary diff (see bdiff.py, taken from Mercurial) * conflict: if there is a conflict on the page currently """ from MoinMoin.util.bdiff import textdiff, compress from MoinMoin.wikisync import TagStore pagename = self._instr(pagename) if n_name is not None: n_name = self._instr(n_name) # User may read page? if not self.request.user.may.read(pagename): return self.notAllowedFault() def allowed_rev_type(data): if data is None: return True return isinstance(data, int) and data > 0 if not allowed_rev_type(from_rev): return xmlrpclib.Fault("FROMREV_INVALID", "Incorrect type for from_rev.") if not allowed_rev_type(to_rev): return xmlrpclib.Fault("TOREV_INVALID", "Incorrect type for to_rev.") currentpage = Page(self.request, pagename) if not currentpage.exists(): return xmlrpclib.Fault("NOT_EXIST", "Page does not exist.") revisions = currentpage.getRevList() if from_rev is not None and from_rev not in revisions: return xmlrpclib.Fault("FROMREV_INVALID", "Unknown from_rev.") if to_rev is not None and to_rev not in revisions: return xmlrpclib.Fault("TOREV_INVALID", "Unknown to_rev.") # use lambda to defer execution in the next lines if from_rev is None: oldcontents = lambda: "" else: oldpage = Page(self.request, pagename, rev=from_rev) oldcontents = lambda: oldpage.get_raw_body_str() if to_rev is None: newpage = currentpage newcontents = lambda: currentpage.get_raw_body_str() else: newpage = Page(self.request, pagename, rev=to_rev) newcontents = lambda: newpage.get_raw_body_str() if oldcontents() and oldpage.get_real_rev() == newpage.get_real_rev(): return xmlrpclib.Fault("ALREADY_CURRENT", "There are no changes.") if n_name is not None: tags = TagStore(newpage) last_tag = tags.get_last_tag() if last_tag is not None and last_tag.normalised_name != n_name: return xmlrpclib.Fault( "INVALID_TAG", "The used tag is incorrect because the normalised name does not match." ) newcontents = newcontents() conflict = wikiutil.containsConflictMarker(newcontents) diffblob = xmlrpclib.Binary( compress(textdiff(oldcontents(), newcontents))) return { "conflict": conflict, "diff": diffblob, "diffversion": 1, "current": currentpage.get_real_rev() }
def xmlrpc_getDiff(self, pagename, from_rev, to_rev, n_name=None): """ Gets the binary difference between two page revisions. @param pagename: unicode string qualifying the page name @param fromRev: integer specifying the source revision. May be None to refer to a virtual empty revision which leads to a diff containing the whole page. @param toRev: integer specifying the target revision. May be None to refer to the current revision. If the current revision is the same as fromRev, there will be a special error condition "ALREADY_CURRENT" @param n_name: do a tag check verifying that n_name was the normalised name of the last tag If both fromRev and toRev are None, this function acts similar to getPage, i.e. it will diff("",currentRev). @return: Returns a dict: * status (not a field, implicit, returned as Fault if not SUCCESS): * "SUCCESS" - if the diff could be retrieved successfully * "NOT_EXIST" - item does not exist * "FROMREV_INVALID" - the source revision is invalid * "TOREV_INVALID" - the target revision is invalid * "INTERNAL_ERROR" - there was an internal error * "INVALID_TAG" - the last tag does not match the supplied normalised name * "ALREADY_CURRENT" - this not merely an error condition. It rather means that there is no new revision to diff against which is a good thing while synchronisation. * current: the revision number of the current revision (not the one which was diff'ed against) * diff: Binary object that transports a zlib-compressed binary diff (see bdiff.py, taken from Mercurial) * conflict: if there is a conflict on the page currently """ from MoinMoin.util.bdiff import textdiff, compress from MoinMoin.wikisync import TagStore pagename = self._instr(pagename) if n_name is not None: n_name = self._instr(n_name) # User may read page? if not self.request.user.may.read(pagename): return self.notAllowedFault() def allowed_rev_type(data): if data is None: return True return isinstance(data, int) and data > 0 if not allowed_rev_type(from_rev): return xmlrpclib.Fault("FROMREV_INVALID", "Incorrect type for from_rev.") if not allowed_rev_type(to_rev): return xmlrpclib.Fault("TOREV_INVALID", "Incorrect type for to_rev.") currentpage = Page(self.request, pagename) if not currentpage.exists(): return xmlrpclib.Fault("NOT_EXIST", "Page does not exist.") revisions = currentpage.getRevList() if from_rev is not None and from_rev not in revisions: return xmlrpclib.Fault("FROMREV_INVALID", "Unknown from_rev.") if to_rev is not None and to_rev not in revisions: return xmlrpclib.Fault("TOREV_INVALID", "Unknown to_rev.") # use lambda to defer execution in the next lines if from_rev is None: oldcontents = lambda: "" else: oldpage = Page(self.request, pagename, rev=from_rev) oldcontents = lambda: oldpage.get_raw_body_str() if to_rev is None: newpage = currentpage newcontents = lambda: currentpage.get_raw_body_str() else: newpage = Page(self.request, pagename, rev=to_rev) newcontents = lambda: newpage.get_raw_body_str() if oldcontents() and oldpage.get_real_rev() == newpage.get_real_rev(): return xmlrpclib.Fault("ALREADY_CURRENT", "There are no changes.") if n_name is not None: tags = TagStore(newpage) last_tag = tags.get_last_tag() if last_tag is not None and last_tag.normalised_name != n_name: return xmlrpclib.Fault("INVALID_TAG", "The used tag is incorrect because the normalised name does not match.") newcontents = newcontents() conflict = wikiutil.containsConflictMarker(newcontents) diffblob = xmlrpclib.Binary(compress(textdiff(oldcontents(), newcontents))) return {"conflict": conflict, "diff": diffblob, "diffversion": 1, "current": currentpage.get_real_rev()}