Short description

If you want to synchronizing the contents of two wikis, it's often necessary to check if the content of two attachments differ. One possible solution would be to request the attachments via getAttachment over the network and byte compare them. But this would create lots of network traffic.

A better solution would be to compare just the hash values which were created from the Moin system out of the attachment's content before.

The new XMLRPC procedure is called getAttachmentHash, gets the page name and attachment's name as parameters and returns the hash value in a string.

Code

Here is how I do it. I added this code to MoinMoin/xmlrpc/init.py

   1     def xmlrpc_getAttachmentHash(self, pagename, attachname):
   2         """
   3         Get hash about contents of attachment <attachname> of page <pagename>
   4 
   5         @param pagename: pagename (utf-8)
   6         @param attachname: attachment name (utf-8)
   7         @rtype: base64
   8         @return: base64 data
   9         """
  10         pagename = self._instr(pagename)
  11         # User may read page?
  12         if not self.request.user.may.read(pagename):
  13             return self.notAllowedFault()
  14 
  15         attachname = wikiutil.taintfilename(self._instr(attachname))
  16         filename = AttachFile.getFilename(self.request, pagename, attachname)
  17         if not os.path.isfile(filename):
  18             return self.noSuchPageFault()
  19 
  20         # Calculate MD5 checksum from attachment.
  21         content = open(filename, 'rb').read()
  22         hash = hashlib.md5()
  23         hash.update(content)
  24         return hash.hexdigest()

Discussion

please use open() and close() for file access. Not any py version on any platform will close automaticly


CategoryFeatureRequest

MoinMoin: FeatureRequests/XMLRPCGetAttachmentHash (last edited 2010-04-14 15:33:08 by JosefMeier)