一、服务端
1. 修改/usr/lib/python2.5/site-packages/MoinMoin/wikirpc.py,在class XmlRpcBase中增加如下函数:
def xmlrpc_putAttachment(self, pagename, attachname, data):
""" Set attachname associated with pagename to data
@param pagename: pagename (utf-8)
@param attachname: attachment name (utf-8)
@param data: file data (base64)
@rtype boolean
@return True if attachment was set
"""
pagename = self._instr(pagename)
# User may read page?
if not self.request.query_string.startswith('action=xmlrpc'):
if not self.request.user.may.read(pagename):
return self.notAllowedFault()
if not self.request.cfg.xmlrpc_putpage_enabled:
return xmlrpclib.Boolean(0)
if self.request.cfg.xmlrpc_putpage_trusted_only and not self.request.user.trusted:
return xmlrpclib.Fault(1, "You are not allowed to edit this page")
# also check ACLs
if not self.request.user.may.write(pagename):
return xmlrpclib.Fault(1, "You are not allowed to edit this page")
attachname = wikiutil.taintfilename(attachname)
filename = AttachFile.getFilename(self.request, pagename, attachname)
if os.path.exists(filename) and not os.path.isfile(filename):
return self.noSuchPageFault()
open(filename, 'wb+').write(base64.decodestring(data))
os.chmod(filename, 0666 & config.umask)
AttachFile._addLogEntry(self.request, 'ATTNEW', pagename, os.path.basename(filename))
return xmlrpclib.Boolean(1)
另外别忘记import相关库:
import os, base64
2. 修改/usr/lib/python2.5/site-packages/MoinMoin/PageEditor.py,对class PageEditor的saveText函数做如下修改:
找到如下行:
if not self.request.user.may.save(self, newtext, rev, **kw):
msg = _('You are not allowed to edit this page!')
raise self.AccessDenied, msg
修改为:
if not self.request.user.may.save(self, newtext, rev, **kw) and not self.request.query_string.startswith('action=xmlrpc'):
msg = _('You are not allowed to edit this page!')
raise self.AccessDenied, msg
这么做的主要原因是,有时候xmlrpc客户端无法认证,导致执行失败,所以内部用的话,干脆就取消xmlrpc的认证了…
3. 重启MoinMoin
二、客户端代码举例
import xmlrpclib
import base64
#import MoinMoin.auth as authmodule
#from MoinMoin.support.BasicAuthTransport import BasicAuthTransport
#username = 'will'
#password = 'mima'
#authtran = BasicAuthTransport(username, password)
wiki = xmlrpclib.ServerProxy("http://192.168.0.100/mywiki/?action=xmlrpc2")
file = open('ar01.png', 'r').read()
print wiki.putAttachment("pageName", "at01.png", base64.encodestring(file))
print wiki.WhoAmI()