")),
'pagelink': webapi.getQualifiedURL(self.url()),
'sitename': config.sitename or webapi.getBaseURL(),
}
if comment:
mailBody = mailBody + \
_("The comment on the change is:\n%(comment)s\n\n") % locals()
# append a diff
if not oldversions:
mailBody = mailBody + \
_("No older revisions of the page stored, diff not available.")
else:
rc, page_file, backup_file, lines = wikiutil.pagediff(self.page_name, oldversions[0])
if lines and len(lines) > 2:
mailBody = "%s%s\n%s" % (
mailBody, ("-" * 78), string.join(lines[2:], ''))
else:
mailBody = mailBody + _("No differences found!\n")
if rc:
mailBody = mailBody + '\n\n' + \
_('The external diff utility returned with error code %(rc)s!') % locals()
return util.sendmail(emails,
_('[%(sitename)s] Update of "%(pagename)s"') % {
'sitename': config.sitename or "Wiki",
'pagename': self.page_name,
},
mailBody, mail_from=request.user.email)
def _notifySubscribers(self, request, comment):
""" Send email to all subscribers of this page.
Return message, indicating success or errors.
"""
# extract categories of this page
pageList = self.getPageLinks(request)
CATEGORY_RE = re.compile("^Category")
pageList = filter(CATEGORY_RE.match, pageList)
# add current page name for list matching
pageList.append(self.page_name)
# get email addresses of the all wiki user which have a profile stored;
# add the address only if the user has subscribed to the page and
# the user is not the current editor
userlist = user.getUserList()
emails = {}
for uid in userlist:
if uid == request.user.id: continue # no self notification
subscriber = user.User(uid)
if not subscriber.email: continue # skip empty email address
if subscriber.isSubscribedTo(pageList):
lang = subscriber.language or 'en'
if not emails.has_key(lang): emails[lang] = []
emails[lang].append(subscriber.email)
if emails:
# get a list of old revisions, and append a diff
oldversions = wikiutil.getBackupList(config.backup_dir, self.page_name)
# send email to all subscribers
results = [_('Status of sending notification mails:')]
for lang in emails.keys():
status = self._sendNotification(request, comment, emails[lang], lang, oldversions)
recipients = string.join(emails[lang], ", ")
results.append(_('[%(lang)s] %(recipients)s: %(status)s') % locals())
return string.join(results, '
')
return _('Nobody subscribed to this page, no mail sent.')
def _user_variable(self, request):
"""If user has a profile return the user name from the profile
else return the remote address or "anonymous"
If the user name contains spaces it is wiki quoted to allow
links to the wiki user homepage (if one exists).
"""
username = request.user.name
if username and config.allow_extended_names and \
string.count(username, ' ') and Page(username).exists():
username = '["%s"]' % username
return username or os.environ.get('REMOTE_ADDR', 'anonymous')
def _expand_variables(self, request, text):
"""Expand @VARIABLE@ in `text`and return the expanded text."""
#!!! TODO: Allow addition of variables via moin_config (and/or a text file)
now = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(time.time()))
system_vars = {
'PAGE': lambda s=self: s.page_name,
'TIME': lambda t=now: "[[DateTime(%s)]]" % t,
'DATE': lambda t=now: "[[Date(%s)]]" % t,
'USERNAME': lambda s=self, r=request: s._user_variable(r),
'USER': lambda s=self, r=request: "-- %s" % (s._user_variable(r),),
'SIG': lambda s=self, r=request, t=now: "-- %s [[DateTime(%s)]]"
% (s._user_variable(r), t,),
}
if request.user.valid and request.user.name and request.user.email:
system_vars['MAILTO'] = lambda u=request.user: \
"[mailto:%s %s]" % (u.email, u.name)
#!!! TODO: Use a more stream-lined re.sub algorithm
for name, val in system_vars.items():
text = string.replace(text, '@' + name + '@', val())
return text
def _write_file(self, text):
is_deprecated = string.lower(text[:11]) == "#deprecated"
# save to tmpfile
tmp_filename = self._tmp_filename()
tmp_file = open(tmp_filename, 'wb')
tmp_file.write(text)
tmp_file.close()
page_filename = self._text_filename()
if not os.path.isdir(config.backup_dir):
os.mkdir(config.backup_dir, 0777 & config.umask)
if os.path.isfile(page_filename) \
and not is_deprecated:
os.rename(page_filename, os.path.join(config.backup_dir,
wikiutil.quoteFilename(self.page_name) + '.' + str(os.path.getmtime(page_filename))))
else:
if os.name == 'nt':
# Bad Bill! POSIX rename ought to replace. :-(
try:
os.remove(page_filename)
except OSError, er:
import errno
if er.errno <> errno.ENOENT: raise er
# set in-memory content
self.set_raw_body(text)
# replace old page by tmpfile
os.chmod(tmp_filename, 0666 & config.umask)
os.rename(tmp_filename, page_filename)
return os.path.getmtime(page_filename)
def save_text(self, request, newtext, datestamp, **kw):
""" Save new text for a page.
Keyword parameters:
stripspaces - strip whitespace from line ends (default: 0)
notify - send email notice tp subscribers (default: 0)
comment - comment field (when preview is true)
"""
msg = ""
if not request.user.may.edit:
msg = _("""You are not allowed to edit any pages.""")
if not newtext:
msg = _("""You cannot save empty pages.""")
elif datestamp == '0':
pass
elif datestamp != str(os.path.getmtime(self._text_filename())):
msg = _("""Sorry, someone else saved the page while you edited it.
Please do the following: Use the back button of your browser, and cut&paste
your changes from there. Then go forward to here, and click EditText again.
Now re-add your changes to the current page contents.
Do not just replace
the content editbox with your version of the page, because that would
delete the changes of the other person, which is excessively rude!
""")
# save only if no error occured (msg is empty)
if not msg:
# set success msg
msg = _("""Thank you for your changes.
Your attention to detail is appreciated.""")
# remove CRs (so Win32 and Unix users save the same text)
newtext = string.replace(newtext, "\r", "")
# possibly strip trailing spaces
if kw.get('stripspaces', 0):
newtext = string.join(map(string.rstrip, string.split(newtext, '\n')), '\n')
# add final newline if not present in textarea, better for diffs
# (does not include former last line when just adding text to
# bottom; idea by CliffordAdams)
if newtext and newtext[-1] != '\n':
newtext = newtext + '\n'
# expand variables, unless it's a template or form page
if not (wikiutil.isTemplatePage(self.page_name) or
wikiutil.isFormPage(self.page_name)):
newtext = self._expand_variables(request, newtext)
# write the page file
mtime = self._write_file(newtext)
# write the editlog entry
from MoinMoin import editlog
log = editlog.makeLogStore()
remote_name = os.environ.get('REMOTE_ADDR', '')
log.addEntry(self.page_name, remote_name, mtime, kw.get('comment', ''))
# add event log entry
request.getEventLogger().add('SAVEPAGE', {'pagename': self.page_name})
# send notification mails
if config.mail_smarthost and kw.get('notify', 0):
msg = msg + "
" + self._notifySubscribers(request, kw.get('comment', ''))
# change lists
msg = msg + "
" + self._commitMessage(request, kw.get('comment', '')) + "
"
return msg
def _commitMessage(self, request, comment, removed=0):
""" Send email to the mailing list associated with this wiki.
"""
fromAddr = ""+config.mail_commit_address;
toAddr = ""+config.mail_commit_address;
now = time.strftime("%Y-%m-%dT%H:%M:%S", time.localtime(time.time()))
# create body
mailBody = _(" Date: %(date)s\n"
" Editor: %(editor)s <%(email)s>\n"
" Wiki: %(sitename)s\n"
" Page: %(pagelink)s\n\n"
" %(comment)s\n\n") % {
'date': now,
'editor': request.user.name or os.environ.get('REMOTE_ADDR', 'unknown'),
'email': request.user.email or '',
'pagelink': webapi.getQualifiedURL(self.url()),
'sitename': config.sitename or webapi.getBaseURL(),
'comment': comment or "no comment"
}
subject = "[wiki] "
# get a list of old revisions, and append a diff
oldversions = wikiutil.getBackupList(config.backup_dir, self.page_name)
if removed:
mailBody = mailBody + "****Page Removed***"
subject = subject + "Removed: "
elif oldversions:
mailBody = mailBody + "Change Log:\n\n"
rc, page_file, backup_file, lines = wikiutil.pagediff(self.page_name, oldversions[0])
if lines and len(lines) > 2:
mailBody = "%s%s\n%s" % (
mailBody, ("-" * 78), string.join(lines[2:], ''))
else:
if rc:
mailBody = mailBody + '\n\n' + \
_('The external diff utility returned with error code %(rc)s!') % locals()
else:
#mailBody = mailBody + _("No differences found!\n")
return 0; # no change...okay!
subject = subject + "Updated: "
else:
mailBody = mailBody + "New Page:\n\n"
mailBody = mailBody + self.get_raw_body()
subject = subject + "New: "
subject = subject + _(" %(pagename)s") % { 'pagename': self.page_name }
util.sendmail2(toAddr,subject,
mailBody, mail_from=fromAddr)
return "Sent commit message to " + toAddr + " from " + fromAddr
]]>