def print_exception(self, type=None, value=None, tb=None, limit=None): if type is None: type, value, tb = sys.exc_info() import traceback self.write("<h2>request.print_exception handler</h2>\n") self.write("<h3>Traceback (most recent call last):</h3>\n") list = traceback.format_tb(tb, limit) + \ traceback.format_exception_only(type, value) self.write("<pre>%s<strong>%s</strong></pre>\n" % ( wikiutil.escape("".join(list[:-1])), wikiutil.escape(list[-1]),)) del tb
def _table_repl(self, word): """ Handle table cell separator. """ if self.in_table: # check for attributes attrs, attrerr = self._getTableAttrs(word) # start the table row? if self.table_rowstart: self.table_rowstart = 0 leader = self.formatter.table_row(1, attrs) else: leader = self.formatter.table_cell(0) # check for adjacent cell markers if word.count("|") > 2: if not attrs.has_key('align'): attrs['align'] = '"center"' if not attrs.has_key('colspan'): attrs['colspan'] = '"%d"' % (word.count("|")/2) # return the complete cell markup return leader + self.formatter.table_cell(1, attrs) + attrerr else: return wikiutil.escape(word)
def anchorlink(self, name, text, id = None, escape=True): extra = '' if id: extra = ' id="%s"' % id if escape: text = wikiutil.escape(text) return '<a href="#%s"%s>%s</a>' % (name, extra, text)
def url(self, url, text=None, css=None, show_image=True, **kw): """ Keyword params: title - title attribute ... some more (!!! TODO) """ url = wikiutil.mapURL(url) pretty = kw.get('pretty_url', 0) title = kw.get('title', None) if show_image and not pretty and wikiutil.isPicture(url): return (u'<img src="%s" alt="%s">' % (url, url)).encode(config.charset) if text is None: text = url # create link str = '<a' if css: str = '%s class="%s"' % (str, css) if title: str = '%s title="%s"' % (str, title) str = '%s href="%s">%s</a>' % (str, wikiutil.escape(url, 1), text) if type(str) == unicode: str = str.encode(config.charset) return str
def get_map_info(request): """ Grabs the point and page information from the request form. """ page_locs = [] nearby_locs = [] for key in request.form: item = request.form[key][0] if key.startswith('pname'): id = int(key[len('pname'):]) locs = page_locs elif key.startswith('near'): id = int(key[len('near'):]) locs = nearby_locs else: continue pagename = urllib.unquote(item) address = wikiutil.escape( urllib.unquote(request.form['addr%s' % id][0])) latitude = float(request.form['lat%s' % id][0]) longitude = float(request.form['long%s' % id][0]) locs.append(mapItem(pagename, address, latitude, longitude)) wiki_name = request.form['wiki'][0] return (wiki_name, page_locs, nearby_locs)
def html_head(self, d): """ Assemble html head @param d: parameter dictionary @rtype: string @return: html head """ dict = { 'stylesheets_html': self.html_stylesheets(d), } dict.update(d) dict['theme_last_modified'] = self.last_modified dict['newtitle'] = None dict['newtitle'] = dict['title'] if dict['title'] == 'Front Page': dict['newtitle'] = wikiutil.escape(self.request.config.catchphrase) dict['web_dir'] = config.web_dir if d['page'].hasMapPoints(): dict['wiki_name'] = self.request.config.wiki_name if config.wiki_farm: dict['map_base'] = farm.getBaseFarmURL(self.request) else: dict['map_base'] = self.request.getBaseURL() dict['gmaps_api_key'] = (self.request.config.gmaps_api_key or config.gmaps_api_key) dict['map_html'] = ( '<script type="text/javascript">' 'var gmaps_src="http://maps.google.com/maps?file=api&v=2&' 'key=%(gmaps_api_key)s";' '</script>' '<script src="%(web_dir)s/wiki/gmap.js?tm=%(theme_last_modified)s" ' 'type="text/javascript">' '</script>' '<script type="text/javascript">' 'var map_url="%(map_base)s?action=gmaps&wiki=%(wiki_name)s";' 'var point_count=1;</script>' % dict) else: dict['map_html'] = '' if dict['newtitle'] is self.request.config.catchphrase: if self.request.config.catchphrase: html = ('<title>%(sitename)s - %(newtitle)s</title>\n' '%(map_html)s\n' '%(stylesheets_html)s\n' % dict) else: html = ('<title>%(sitename)s</title>\n' '%(map_html)s\n' '%(stylesheets_html)s\n' % dict) else: html = ('<title>%(newtitle)s - %(sitename)s</title>' '%(map_html)s\n' '%(stylesheets_html)s\n' % dict) return html
def title(self, d): """ Assemble the title @param d: parameter dictionary @rtype: string @return: title html """ _ = self.request.getText html = ['<div id="title">'] if d['title_link']: html.append('<h1><a title="%s" href="%s">%s</a></h1>' % ( _('Click here to get more information about this page'), d['title_link'], wikiutil.escape(d['title_text']))) else: html.append('<h1>%s</h1>' % wikiutil.escape(d['title_text'])) html.append('</div>') return ''.join(html)
def format(self, formatter): """ Send the text. """ #!!! send each line via the usual formatter calls text = wikiutil.escape(self.raw) text = text.expandtabs() text = text.replace('\n', '<br>\n') text = text.replace(' ', ' ') self.request.write(text)
def dumpFormData(form): """ Dump the form data for debugging purposes """ from Sycamore import wikiutil result = '<dt><strong>Form entries</strong></dt>' for k in form.keys(): v = form.get(k, ["<empty>"]) v = "|".join(v) result = result + '<dd><em>%s</em>=%s</dd>' % (k, wikiutil.escape(v)) return result
def image(self, **kw): """ Take HTML <IMG> tag attributes in `attr`. Attribute names have to be lowercase! """ result = '<img' for attr, value in kw.items(): if attr=='html_class': attr='class' result = result + ' %s="%s"' % (attr, wikiutil.escape(str(value))) return result + '>'
def username(self, d): """ Assemble the username / userprefs link @param d: parameter dictionary @rtype: string @return: username html """ html = '<div id="username">%s</div>' % wikiutil.link_tag( self.request, wikiutil.quoteWikiname(d['page_user_prefs']), wikiutil.escape(d['user_prefs'])) return html
def lang(self, lang_name, text): """ Insert text with specific lang and direction. Enclose within span tag if lang_name is different from the current lang """ if lang_name != self.request.current_lang: dir = i18n.getDirection(lang_name) text = wikiutil.escape(text) return ('<span lang="%(lang_name)s" dir="%(dir)s">' '%(text)s</span>' % {'lang_name': lang_name, 'dir': dir, 'text': text}) return text
def _openingtag(self): result = [self.tagname()] attrs = self.attrs.items() if _SORT_ATTRS: attrs.sort() for key, val in attrs: key = key.lower() if type(val) == unicode: val = val.encode(config.charset) if self._BOOL_ATTRS.has_key(key): if val: result.append(key) else: result.append('%s="%s"' % (key, wikiutil.escape(str(val), 1))) return " ".join(result)
def title(self, d): """ Assemble the title @param d: parameter dictionary @rtype: string @return: title html """ _ = self.request.getText html = [] if d['title_link']: if d['polite_msg']: polite_html = ('<div style="font-size: 10px; color: #404040; ' 'clear:both;">' ' (%s)' '</div>' % d['polite_msg']) else: polite_html = '' # deal with subpages and links to them # cut it up for subpage display page_title_links = subpagelinks.SubpageLinks( self.request, d['title_text']).render() subpage_title = [] for pagename, display_pagename in page_title_links[:-1]: subpage_title.append(Page(pagename, self.request).link_to( know_status=True, know_status_exists=True, text=display_pagename)) # the most current pagename can be styled differently, if desired subpage_title.append( Page(page_title_links[-1][0], self.request).link_to( know_status=True, know_status_exists=True, text=page_title_links[-1][1], css_class="currentChild")) pagename_html = '/'.join(subpage_title) html.append('<td id="title_text"><h1>%s</h1>%s</td>' % ( pagename_html, polite_html)) html.append(self.new_iconbar(d)) else: html.append('<td id="title_text"><h1>%s</h1></td>' % wikiutil.escape(d['title_text'])) return ''.join(html)
def banner(self,d): """ Assemble the banner @rtype: string @return: banner html """ if d['script_name']: html = ['<a class="nostyle" href="%(script_name)s">' % d] else: html = ['<a class="nostyle" href="%s/Front_Page">' % self.request.getScriptname()] if has_file(self.request, self.images_pagename, 'logo.png'): if not self.request.config.logo_sizes.has_key('logo.png'): wikiutil.init_logo_sizes(self.request) if not self.request.config.theme_files_last_modified.has_key( 'logo.png'): wikiutil.init_theme_files_last_modified(self.request) width, height = self.request.config.logo_sizes['logo.png'] last_modified = self.request.config.theme_files_last_modified[ 'logo.png'] if self.request.isSSL(): html.append( '<img align="middle" src="%s" alt="wiki logo" style="%s" ' 'height="%s" width="%s"></a>' % (self.request.getQualifiedURL( uri=getAttachUrl( self.images_pagename, 'logo.png', self.request, ts=last_modified), ), self.png_behavior, height, width)) else: html.append( '<img align="middle" src="%s" alt="wiki logo" style="%s" ' 'height="%s" width="%s"></a>' % (getAttachUrl(self.images_pagename, 'logo.png', self.request, ts=last_modified), self.png_behavior, height, width)) else: html.append('<div id="logo_text">%s</div></a>' % wikiutil.escape(self.request.config.sitename)) return ''.join(html)
def get_map_info(request): """ Grabs the point and page information from the request form. """ page_locs = [] nearby_locs = [] for key in request.form: item = request.form[key][0] if key.startswith("pname"): id = int(key[len("pname") :]) locs = page_locs elif key.startswith("near"): id = int(key[len("near") :]) locs = nearby_locs else: continue pagename = urllib.unquote(item) address = wikiutil.escape(urllib.unquote(request.form["addr%s" % id][0])) latitude = float(request.form["lat%s" % id][0]) longitude = float(request.form["long%s" % id][0]) locs.append(mapItem(pagename, address, latitude, longitude)) wiki_name = request.form["wiki"][0] return (wiki_name, page_locs, nearby_locs)
def text(self, text): if self._in_code: return wikiutil.escape(text).replace(' ', self.hardspace) return wikiutil.escape(text)
def sysmsg(self, text, **kw): return '\n<div class="message">%s</div>\n' % wikiutil.escape(text)
def handleData(self, new_user=None): from Sycamore.Page import MAX_PAGENAME_LENGTH as MAX_USERNAME_LENGTH _ = self._ form = self.request.form msg = '' isdisabled = False if form.get('disabled', [0])[0] == '1': isdisabled = True self.from_wiki = None if self.request.form.has_key('from_wiki'): self.from_wiki = self.request.form['from_wiki'][0].lower().strip() if not wikiutil.isInFarm(self.from_wiki, self.request): self.from_wiki = None new_user = int(form.get('new_user', [0])[0]) # wiki farm authentication # we want them to be able to sign back in right after they # click the 'logout' GET link, hence this test is_form_logout = (form.has_key('qs') and urllib.unquote(form['qs'][0]) == 'action=userform&logout=Logout') if (self.request.form.has_key('badlogin') and self.request.form['badlogin'][0]): _create_nologin_cookie(self.request) if config.wiki_farm: wiki_base_url = farm.getBaseFarmURL(self.request) else: wiki_base_url = '%s/' % self.request.getScriptname() return_string = ('Unknown username or wrong password.<br/><br/>' 'New user? <a href="%s%s?new_user=1">Click here ' 'to create an account!</a><br/><br/>' 'Forgot your password? We\'ll email it to you.' '<form action="%s" method="POST">' '<input type="hidden" name="action" ' 'value="userform">' 'Email address: <input class="formfields" ' 'type="text" name="email">' ' <input type="submit" class="formbutton" ' 'name="login_sendmail" ' 'value="Mail me my account data">' '</form>' % ( wiki_base_url, wikiutil.quoteWikiname( config.page_user_preferences), wiki_base_url)) return return_string if (form.has_key('login_check') and form['login_check'][0] and form.has_key('backto_wiki') and form.has_key('backto_page')): backto_wiki = form['backto_wiki'][0] backto_page = form['backto_page'][0].encode(config.charset) if form.has_key('qs') and not is_form_logout: q_query_string = form['qs'][0] else: q_query_string = '' if self.request.user.valid: secret, stored_expire_time, session = \ self.request.user.cookie_dough if q_query_string: url = ('%s?action=userform&backto_page=%s&qs=%s' '&secret=%s&expire_time=%s&uid=%s&session=%s' % ( backto_page, urllib.quote(backto_page), urllib.quote(q_query_string), urllib.quote(secret), stored_expire_time, self.request.user.id, urllib.quote(session))) else: url = ('%s?action=userform&backto_page=%s&secret=%s' '&expire_time=%s&uid=%s&session=%s' % ( backto_page, urllib.quote(backto_page), urllib.quote(secret), stored_expire_time, self.request.user.id, urllib.quote(session))) else: if q_query_string: url = ('%s?action=userform&backto_page=%s¬_logged_in=1' '&qs=%s' % (backto_page, urllib.quote(backto_page), urllib.quote(q_query_string))) else: url = ('%s?action=userform&backto_page=%s¬_logged_in=1' % (backto_page, urllib.quote(backto_page))) self.request.http_redirect(url) return # bounce-back wiki farm authentication if form.has_key('not_logged_in') and form.has_key('backto_page'): backto = urllib.unquote(form['backto_page'][0].encode( config.charset)) if form.has_key('qs') and not is_form_logout: query_string = '?%s' % (urllib.unquote(form['qs'][0] ).encode(config.charset)) else: query_string = '' url = '%s%s' % (backto, query_string) _create_nologin_cookie(self.request) self.request.http_redirect(url) return # bounce-back wiki farm authentication elif (form.has_key('uid') and form.has_key('secret') and form.has_key('session') and form.has_key('expire_time')): uid = form['uid'][0] secret = urllib.unquote(form['secret'][0]) session = form['session'][0] expire_time = float(form['expire_time'][0]) if form.has_key('backto_page'): backto_page = form['backto_page'][0] else: backto_page = '/' if form.has_key('qs') and not is_form_logout: query_string = '?%s' % urllib.unquote(form['qs'][0]) else: query_string = '' url = '%s%s' % (backto_page, query_string) self.request.http_redirect(url) self.request.user.sendCookie(self.request, expire=expire_time, sessionid=session, secret=secret, id=uid) self.request.user.clearNologinCookie(self.request) return if form.has_key('logout') or isdisabled: msg = '' if isdisabled: if not self.request.isPOST(): return "Use the interactive interface to change settings!" # disable the account self.request.user.disabled = 1 # save user's profile self.request.user.save() msg = '<p>%s</p>' % _("Your account has been disabled.") # clear the cookie in the browser and locally try: cookie = Cookie.SimpleCookie(self.request.saved_cookie) except Cookie.CookieError: # ignore invalid cookies cookie = None else: if config.wiki_farm: cookie_id = wikiutil.quoteCookiename( config.wiki_base_domain + ',ID') else: cookie_id = wikiutil.quoteCookiename( config.sitename + ',ID') if cookie.has_key(cookie_id): self.removeSession(cookie[cookie_id].value) cookie_dir = config.web_dir if not cookie_dir: cookie_dir = '/' domain = wikiutil.getCookieDomain(self.request) expirestr = time.strftime("%A, %d-%b-%Y %H:%M:%S GMT", time.gmtime(0)) self.request.setHttpHeader(('Set-Cookie', ('%s="%s"; domain=%s; path=%s; ' 'expires=%s' % ( (cookie_id, cookie[cookie_id].value, domain, cookie_dir, expirestr))))) self.request.saved_cookie = '' self.request.auth_username = '' self.request.user = user.User(self.request) return msg + _("Cookie deleted. You are now logged out.") if form.has_key('login_sendmail'): if not self.request.isPOST(): return "Use the interactive interface to change settings!" if not config.mail_smarthost: return _('This wiki is not enabled for mail processing. ' 'Contact the owner of the wiki, who can either enable ' 'email, or remove the "Subscribe" icon.') try: email = form['email'][0] except KeyError: return _("Please provide a valid email address!") text = '' uid = user.getUserIdByEmail(email, self.request) if uid: theuser = user.User(self.request, uid) if theuser.valid: code = self.createCode(theuser.id) sitename = farm.getBaseWikiFullName(self.request) if config.wiki_farm: url = farm.getBaseFarmURL(self.request) else: url = '%s/' % self.request.getBaseURL() text = ("Go here to automatically log into %s: " "%s%s?action=userform&uid=%s&code=%s\n" "Once you're logged in, you should change your " "password in your settings " "(you forgot your password, right?).\n\n" "(The link in this email is good for " "one use only.)" % ( sitename, url, wikiutil.quoteWikiname( config.page_user_preferences), theuser.id, code)) if not text: return _("Found no account matching the given email address " "'%(email)s'!") % {'email': email} mailok, msg = util.mail.sendmail(self.request, [email], 'Your wiki account data', text, mail_from=config.mail_from) return wikiutil.escape(msg) if form.has_key('login') or form.has_key('uid'): uid = None if form.has_key('code') and form.has_key('uid'): given_uid = form['uid'][0].strip() given_code = form['code'][0].strip() given_code if self.isValidCode(given_uid, given_code): uid = given_uid if uid: # we were given account information so let's # create an account -> log them in theuser = user.User(self.request, id=uid) msg = _("You are now logged in! " "Please change your password below!") # send the cookie theuser.sendCookie(self.request) self.request.user = theuser return msg else: # we weren't given information, so let's see # if they gave us a login/password # try to get the user name try: name = form['username'][0].replace('\t', ' ').strip() except KeyError: name = '' # try to get the password password = form.get('password',[''])[0] # load the user data and check for validness if name: theuser = user.User(self.request, name=name, password=password, is_login=True) else: theuser = user.User(self.request, id=uid, name=name, password=password, is_login=True) if config.wiki_farm: wiki_base_url = farm.getBaseFarmURL(self.request) else: wiki_base_url = '%s/' % self.request.getScriptname() if not theuser.valid: if (not self.request.form.has_key('backto_wiki') or not self.request.form['backto_wiki'][0]): return_string = ('Unknown username or wrong password.' '<br/><br/>New user? ' '<a href="%s%s?new_user=1">' 'Click here to create an account!</a>' '<br/><br/>Forgot your password? ' 'We\'ll email it to you.' '<form action="%s" method="POST">' '<input type="hidden" name="action" ' 'value="userform">' 'Email address: ' '<input class="formfields" ' 'type="text" name="email">' ' ' '<input type="submit" ' 'class="formbutton" ' 'name="login_sendmail" ' 'value="Mail me my account ' 'data">' '</form>' % ( wiki_base_url, wikiutil.quoteWikiname( config.page_user_preferences), wiki_base_url)) return return_string else: self.request.http_redirect( urllib.unquote( self.request.form['backto_page'][0].encode( config.charset)) + '?action=userform&badlogin=1') return # send the cookie theuser.sendCookie(self.request) self.request.user = theuser send_back_home(self.request, msg="You are now logged in!") else: if not self.request.isPOST(): return """Use the interactive interface to change settings!""" # save user's profile, first get user instance theuser = user.User(self.request) # try to get the name # if name is empty or missing, return an error msg if (form.has_key('username') and form['username'][0].replace('\t', '').strip()): theuser.propercased_name = \ form['username'][0].replace('\t', ' ').strip() theuser.name = theuser.propercased_name.lower() elif form.has_key('username'): raise BadData, (_("Please enter a user name!"), new_user) if (self.request.user.name and (self.request.user.name != theuser.name)): # they are still logged on and are trying to make a new account raise BadData, (_("Please log out before creating an account."), new_user) if user.getUserId(theuser.name, self.request): if theuser.name != self.request.user.name: raise BadData, (_("User name already exists!"), new_user) else: new_user = False if form.has_key('save') and form['save'][0] == 'Change password': # change password setting # try to get the password and pw repeat password = form.get('password', [''])[0] password2 = form.get('password2',[''])[0] # Check if password is given and matches with password repeat if password != password2: raise BadData, (_("Passwords don't match!"), new_user) if not password and new_user: raise BadData, (_("Please specify a password!"), new_user) if password: theuser.enc_password = user.generate_hash(password) self._clear_all_sessions_except_current() msg = _("Password changed!") else: # process general settings # tr to get the email theuser.email = form.get('email', [''])[0] if theuser.email: email_user_id = user.getUserIdByEmail(theuser.email, self.request) else: email_user_id = None if not theuser.email or not re.match(".+@.+\..{2,}", theuser.email): raise BadData, (_("Please provide your email address - " "without that you could not " "get your login data via email just in " "case you lose it."), new_user) elif email_user_id and email_user_id != theuser.id: raise BadData, (_("Somebody else has already registered " "with the email address \"%s\", please " "pick something else." % theuser.email), new_user) # editor size theuser.edit_rows = util.web.getIntegerInput(self.request, 'edit_rows', theuser.edit_rows, 10, 60) theuser.edit_cols = util.web.getIntegerInput(self.request, 'edit_cols', theuser.edit_cols, 30, 100) # time zone tz = form.get('tz', theuser.tz)[0] if tz not in pytz.common_timezones: tz = theuser.tz theuser.tz = tz wiki_for_userpage = form.get('wiki_for_userpage', [''])[0].lower() if (wiki_for_userpage and not wikiutil.isInFarm(wiki_for_userpage, self.request)): raise BadData, (_('"%s" is not the name of a wiki.' % ( wiki_for_userpage)), new_user) if wiki_for_userpage != theuser.wiki_for_userpage: # they have changed the wiki! time to do # something differently msg = _("<p>User preferences saved!</p>" "<p><strong>Note:</strong> It may take a bit for " "all links to your name to point to the " "new wiki.</p>") theuser.wiki_for_userpage = wiki_for_userpage # User CSS URL theuser.css_url = form.get('css_url', [''])[0] # try to get the (optional) preferred language #theuser.language = form.get('language', [''])[0] # checkbox options keys = [] for key in user_checkbox_fields: value = form.get(key, [0])[0] try: value = int(value) except ValueError: pass setattr(theuser, key, value) if new_user: # strip spaces, we don't allow them anyway theuser.propercased_name = theuser.propercased_name.strip() theuser.name = theuser.propercased_name.lower() if not theuser.name.strip(): raise BadData, (_("Please provide a user name!"), new_user) elif theuser.propercased_name.find(' ') != -1: raise BadData, (_("Invalid username: spaces are not " "allowed in user names"), new_user) elif re.search('[%s]' % re.escape(USER_NOT_ALLOWED_CHARS), theuser.propercased_name): raise BadData, (_("Invalid username: the characters " "%s are not allowed in usernames." % ( wikiutil.escape( USER_NOT_ALLOWED_CHARS))), new_user) # messes up subpages. # '/' isn't allowed, so we just disallow this and we're cool. elif theuser.name == '..' or theuser.name == '.': raise BadData, (_("Invalid username: okay, seriously, " "that's a pretty lame name. " "Pick something better!"), new_user) elif len(theuser.propercased_name) > MAX_USERNAME_LENGTH: raise BadData, (_("Invalid username: a username can be " "at most %s characters long." % ( MAX_USERNAME_LEGNTH)), new_user) elif (not theuser.email or not re.match(".+@.+\..{2,}", theuser.email)): raise BadData, (_("Please provide your email address - " "without that you could not get your " "login data via email just in case you " "lose it."), new_user) name_exists = user.getUserId(theuser.name, self.request) if name_exists: raise BadData, (_("This user name already belongs to " "somebody else."), new_user) email_exists = user.getUserIdByEmail(theuser.email, self.request) if email_exists: raise BadData, (_("This email already belongs to somebody " "else."), new_user) # try to get the password and pw repeat password = form.get('password', [''])[0] password2 = form.get('password2',[''])[0] # Check if password is given and matches with password repeat if password != password2: raise BadData, (_("Passwords don't match!"), new_user) if not password and new_user: raise BadData, (_("Please specify a password!"), new_user) if password: theuser.enc_password = user.generate_hash(password) theuser.anonymous = False # save data and send cookie theuser.save(new_user=new_user) theuser.sendCookie(self.request) self.request.user = theuser from Sycamore.formatter.text_html import Formatter formatter = Formatter(self.request) if not new_user: if not msg: msg = _("User preferences saved!") if self.from_wiki: go_back_to_wiki = farm.link_to_wiki(self.from_wiki, formatter) msg = ('%s<br/><br/>Wanna go ' 'back to %s?' % (msg, go_back_to_wiki)) else: msg = _("Account created! You are now logged in.") self.request.user.valid = 1 if (self.from_wiki and self.from_wiki.lower() != farm.getBaseWikiName()): go_back_to_wiki = farm.link_to_wiki(self.from_wiki, formatter) msg = ('%s<br/><br/>Head back over to %s and your new account ' 'should work there!' % (msg, go_back_to_wiki)) if _debug: msg = msg + util.dumpFormData(form) return msg
def sendEditor(self, **kw): """ Send the editor form page. @keyword preview: if given, show this text in preview mode @keyword staytop: don't go to #preview @keyword comment: comment field (when preview is true) @keyword had_conflict: we had an edit conflict on a save. """ import re try: from Sycamore.action import SpellCheck except ImportError: SpellCheck = None form = self.request.form _ = self._ self.request.http_headers([("Content-Type", "text/html; charset=%s" % config.charset)] + self.request.nocache) msg = None preview = kw.get('preview', None) emit_anchor = not kw.get('staytop', 0) proper_name = self.proper_name() from Sycamore.formatter.text_html import Formatter self.request.formatter = Formatter(self.request, store_pagelinks=1, preview=preview) base_uri = "%s/%s?action=edit" % (self.request.getScriptname(), wikiutil.quoteWikiname( self.proper_name())) backto = form.get('backto', [None])[0] if backto: base_uri += '&' + util.web.makeQueryString(backto=backto) # check edit permissions if not self.request.user.may.edit(self): msg = _('You are not allowed to edit this page.') elif self.prev_date: # Trying to edit an old version, this is not possible via # the web interface, but catch it just in case... msg = _('Cannot edit old revisions!') # Is the IP address a Tor exit note? if config.block_tor_edits: tor = torcheck.torcheck() if tor.query(self.request.remote_addr): msg = _('You are not allowed to edit this page, %s.' % self.request.remote_addr) # Test against a number of DNSBLs if config.block_dnsbl_edits: # Test: is this an IP address? re1='((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?))(?![\\d])' rg = re.compile(re1,re.IGNORECASE|re.DOTALL) m = rg.search(self.request.remote_addr) if m: # this is an IP address # reverse IP address octets dq = self.request.remote_addr.split('.') dq.reverse() for dnsbl in config.block_dnsbl_edits: querystring = '.'.join(dq) querystring += '.' + dnsbl + '.' try: result = socket.gethostbyname(querystring) except socket.gaierror: # probably nxdomain result = '0.0.0.0' if result.startswith('127.0.0'): # utoh msg = _('You are not allowed to edit this page: %s listed on %s' % (self.request.remote_addr, dnsbl)) # Did one of the prechecks fail? if msg and not kw.get('had_conflict', None): self.send_page(msg=msg) return # check for preview submit if preview is None: title = _('Edit "%(pagename)s"') else: title = _('Preview of "%(pagename)s"') self.set_raw_body(preview.replace("\r", ""), 1) page_needle = self.page_name if config.allow_subpages and page_needle.count('/'): page_needle = '/' + page_needle.split('/')[-1] wikiutil.send_title(self.request, self.proper_name(), pagename=self.proper_name(), has_link=True, strict_title='Editing "%s"' % self.proper_name(), body_onload = "sizeForIE('savetext', 100, 'editorComment', 99);" ) # start content div self.request.write('<div id="content" class="content">\n') # get request parameters text_rows = None if form.has_key('rows'): text_rows = int(form['rows'][0]) if self.request.user.valid: # possibly update user's pref if text_rows != self.request.user.edit_rows: self.request.user.edit_rows = text_rows self.request.user.save() else: text_rows = config.edit_rows if self.request.user.valid: text_rows = int(self.request.user.edit_rows) if form.has_key('cols'): text_cols = int(form['cols'][0]) if self.request.user.valid: # possibly update user's pref if text_rows != self.request.user.edit_rows: self.request.user.edit_rows = text_rows self.request.user.save() else: text_cols = 80 if self.request.user.valid: text_cols = int(self.request.user.edit_cols) # check datestamp (version) of the page our edit is based on if preview is not None: # propagate original datestamp mtime = float(form['datestamp'][0]) # did someone else change the page while we were editing? conflict_msg = None if not self.exists(): # page does not exist, are we creating it? if mtime: conflict_msg = _('<p>Someone else <b>deleted</b> this ' 'page while you were editing!') elif mtime != self.mtime(): conflict_msg = _('<p>Someone else changed this page while ' 'you were editing.') # merge conflicting versions allow_conflicts = 1 from Sycamore.util import diff3 savetext = self.get_raw_body() oldpg = Page(self.page_name, self.request, prev_date=mtime) original_text = oldpg.get_raw_body() saved_text = Page(self.page_name, self.request).get_raw_body() verynewtext, had_conflict = diff3.text_merge( original_text, saved_text, savetext, marker1='----- /!\ Edit conflict! Your version: -----\n', marker2='----- /!\ Edit conflict! Other version: -----\n', marker3='----- /!\ End of edit conflict -----\n') if had_conflict and self.request.user.valid and ( self.request.user.id == self.last_edit_info()[1]): # user pressed back button or did something weird conflict_msg =None elif had_conflict: conflict_msg = _(conflict_msg + 'There was an <b>edit conflict between ' 'your changes!</b></p>' '<p>Please review the conflicts and ' 'merge the changes.</p>') mtime = self.mtime() self.set_raw_body(verynewtext, 1) else: conflict_msg = _(conflict_msg + 'Your changes were sucessfully merged!') mtime = self.mtime() self.set_raw_body(verynewtext) if conflict_msg: self.request.write('<div id="message"><div>%s' '</div></div>'% conflict_msg) emit_anchor = 0 # make this msg visible! elif self.exists(): # datestamp of existing page mtime = self.mtime() else: # page creation mtime = 0 # output message message = kw.get('msg', '') if message: self.request.write('<div id="message">%s</div>' % (message)) # get the text body for the editor field if form.has_key('template'): # "template" parameter contains the name of the template page template_page = wikiutil.unquoteWikiname(form['template'][0]) raw_body = Page(template_page, self.request).get_raw_body() else: raw_body = self.get_raw_body() # send text above text area # button toolbar self.request.write('<div id="editArea">') self.request.write("<script type=\"text/javascript\">" "var buttonRoot = '%s';</script>" % ( os.path.join(config.url_prefix, self.request.theme.name, 'img', 'buttons'))) if self.request.user.name: if config.user_page_prefix: self.request.write("<script type=\"text/javascript\">" "var userPageLink = '[\"%s%s\"]';</script>" % ( config.user_page_prefix, self.request.user.propercased_name)) else: self.request.write("<script type=\"text/javascript\">" "var userPageLink = '[\"%s\"]';</script>" % ( config.user_page_prefix, self.request.user.propercased_name)) else: self.request.write("<script type=\"text/javascript\">" "var userPageLink = '%s';</script>" % ( self.request.remote_addr)) if config.wiki_farm: self.request.write("<script type=\"text/javascript\" " "src=\"http://%s%s%s/edit.js\"></script>\n" % ( config.wiki_base_domain, config.web_dir, config.url_prefix)) else: self.request.write("<script type=\"text/javascript\" " "src=\"%s%s/edit.js\"></script>\n" % ( config.web_dir, config.url_prefix)) # send form self.request.write('<form name="editform" id="editform" ' 'method="post" action="%s/%s#preview">' % ( self.request.getScriptname(), wikiutil.quoteWikiname(proper_name))) self.request.write(str(html.INPUT(type="hidden", name="action", value="savepage"))) if backto: self.request.write(str(html.INPUT(type="hidden", name="backto", value=backto))) # generate default content if not raw_body: if self.isTalkPage(): raw_body = _('This page is for discussing the ' 'contents of ["%s"].') % self.proper_name()[:-5] else: raw_body = _('Describe %s here.') % (self.proper_name(),) # replace CRLF with LF raw_body = self._normalize_text(raw_body) # send datestamp (version) of the page our edit is based on self.request.write('<input type="hidden" name="datestamp" ' 'value="%s">' % repr(mtime)) # Print the editor textarea and the save button self.request.write('<textarea id="savetext" name="savetext" ' 'rows="%d" cols="%d" style="width:100%%;">%s' '</textarea>' % (text_rows, text_cols, wikiutil.escape(raw_body))) # make sure we keep the template notice on a resize of the editor template_param = '' if form.has_key('template'): template_param = '&template=' + form['template'][0] # draw edit size links self.request.write(_('<div class="pageEditInfo" id="editorSize">' 'editor size:')) self.request.write('<a href="#" onclick="return sizeEditor(\'bigger\',' '\'%s&preview=1&cols=60%s\')">%s' '</a>' % (base_uri, template_param, '+')) self.request.write(',<a href="#" onclick="return sizeEditor(\'smaller\',' '\'%s&preview=1&cols=60%s\')">%s' '</a>' % (base_uri, template_param, '-')) self.request.write('</div>') self.request.write('</p>') # close textarea self.request.write('<div id="editComment" id="editorResizeButtons"> ' '%s<br><input type="text" class="formfields" ' 'name="comment" id="editorComment" value="%s" ' 'size="%d" maxlength="80" style="width:99%%;">' '</div>' % (_("<font size=\"+1\">Please comment " "about this change:</font>"), wikiutil.escape(kw.get('comment', ''), 1), text_cols)) spam_catch_button = ( '<span style="position: absolute; top: 0px; left: 0px;' 'height: 0px; width: 0px; overflow: hidden;">' 'dont enter into this box:' '<input type="text" name="text_dont"/>' '</span>' '<span style="position: absolute; top: 0px; left: 0px;' 'height: 0px; width: 0px; overflow: hidden;">' '<input class="formbutton" type="submit" name="button_dont" ' 'value="Dont press me">\n' '</span>') self.request.write(spam_catch_button) # button bar button_spellcheck = (SpellCheck and '<input type="submit" class="formbutton" name="button_spellcheck"' ' value="%s">' % _('Check Spelling')) or '' save_button_text = _('Save Changes') cancel_button_text = _('Cancel') self.request.write("</div>") if self.request.user.may.admin(self): security_button = ('<input type="button" class="formbutton" ' 'onClick="location.href=' '\'%s/%s?action=Security\'" ' 'value="Security">') % ( self.request.getScriptname(), wikiutil.quoteWikiname(proper_name)) else: security_button = '' if self.request.user.may.delete(self): delete_button = ('<input type="button" class="formbutton" ' 'onClick="location.href=' '\'%s/%s?action=DeletePage\'" value="Delete">') % ( self.request.getScriptname(), wikiutil.quoteWikiname(proper_name)) rename_button = ('<input type="button" class="formbutton" ' 'onClick="location.href=\'%s/%s?action=Rename\'"' ' value="Rename">') % ( self.request.getScriptname(), wikiutil.quoteWikiname(proper_name)) else: delete_button = '' rename_button = '' self.request.write('<div id="editButtonRow">' '<span>' '<input type="submit" class="formbutton" ' 'name="button_preview" value="%s"> ' '<input type="submit" class="formbutton" ' 'name="button_save" value="%s"> ' '<input type="submit" class="formbutton" ' 'name="button_cancel" value="%s"> ' '</span>' '<span class="editActions">' '<input type="button" class="formbutton" ' 'onClick="window.open(\'%s/%s?action=Files\', ' '\'files\', \'width=800,height=600,' 'scrollbars=1\')" value="Upload Files"> ' '%s ' '%s ' '%s ' '%s</span>' '</div>' % ( _('Preview'), save_button_text, cancel_button_text, self.request.getScriptname(), wikiutil.quoteWikiname(proper_name), button_spellcheck, delete_button, rename_button, security_button)) if self.request.config.edit_agreement_text: self.request.write(self.request.config.edit_agreement_text) badwords_re = None if preview is not None: if SpellCheck and ( form.has_key('button_spellcheck') or form.has_key('button_newwords')): badwords, badwords_re, msg = SpellCheck.checkSpelling(self, self.request, own_form=0) self.request.write("<p>%s</p>" % msg) self.request.write("</form>") if config.wiki_farm: from Sycamore import farm help_link = farm.link_to_page(farm.getBaseWikiName(), "Help with Editing", self.request.formatter, force_farm=True) else: help_link = Page("Help with Editing", self.request).link_to() # QuickHelp originally by Georg Mischler <*****@*****.**> self.request.write('<h2>Editing quick-help</h2>\n' '<dl><div style="float: right; margin: 10px; border: 1px solid; ' 'padding: 3pt;">See <b>%s</b> for more information.</div>' % ( help_link) + _("<dt>Emphasis:</dt>\n" "<dd>''<em>italics</em>''; '''<strong>bold</strong>''';" "'''''<strong><em>bold italics</em></strong>''''';" "''<em>mixed '''<strong>bold</strong>''' and " "italics</em>''; ---- horizontal rule.</dd>" "<dt>Headings:</dt>" "<dd>= Title 1 =; == Title 2 ==; === Title 3 ===;" "==== Title 4 ====; ===== Title 5 =====.</dd>" "<dt>Lists:</dt>" "<dd>space and one of * bullets; 1., a., A., i., I. " "numbered items;" " 1.#n start numbering at n; space alone indents.</dd>" "<dt>Links:</dt>" "<dd>[\"brackets and double quotes\"]; [\"the exact " "page name\" label];" " url; [url]; [url label].</dd>" "<dt>Tables:</dt>" "<dd>|| cell text |||| cell text spanning two columns ||;" " no trailing white space allowed after tables or titles." "</dd></dl>")) if preview is not None: if not emit_anchor: preview_name = "previewHide" else: preview_name = "preview" self.request.write('<div id="%s" class="preview">' % preview_name) self.send_page(content_only=1, hilite_re=badwords_re, preview=preview) self.request.write('</div>') self.request.write('</div>\n') # end content div wikiutil.send_after_content(self.request) self.request.theme.emit_custom_html(config.page_footer1) self.request.theme.emit_custom_html(config.page_footer2) self.request.write('</body></html>')
def _write_to_db(self, text, action, comment, ip, proper_name): """ Write the text to the page tables in the database. """ ourtime = time.time() self.request.save_time = ourtime self.request.cursor.execute("""SELECT name, propercased_name from curPages where name=%(page_name)s and wiki_id=%(wiki_id)s""", {'page_name':self.page_name, 'wiki_id':self.request.config.wiki_id}) exists = self.request.cursor.fetchone() if not proper_name: if exists: proper_name = exists[1] else: proper_name = self.given_name if not self.request.user.id: user_id = 'anon:%s' % i else: user_id = self.request.user.id if exists: self.request.cursor.execute("""UPDATE curPages set name=%(page_name)s, text=%(text)s, editTime=%(ourtime)s, userEdited=%(id)s, propercased_name=%(proper_name)s where name=%(page_name)s and wiki_id=%(wiki_id)s""", {'page_name': self.page_name, 'text': text, 'ourtime': ourtime, 'id': user_id, 'proper_name':proper_name, 'wiki_id':self.request.config.wiki_id}, isWrite=True) else: self.request.cursor.execute("""INSERT into curPages (name, text, cachedText, editTime, cachedTime, userEdited, propercased_name, wiki_id) values (%(page_name)s, %(text)s, NULL, %(ourtime)s, NULL, %(id)s, %(proper_name)s, %(wiki_id)s)""", {'page_name':self.page_name, 'text':text, 'ourtime':ourtime, 'id':user_id, 'proper_name':proper_name, 'wiki_id':self.request.config.wiki_id}, isWrite=True) # then we need to update the allPages table for Recent Changes # and page-centric info. self.request.cursor.execute("""INSERT into allPages (name, text, editTime, userEdited, editType, comment, userIP, propercased_name, wiki_id) values (%(page_name)s, %(text)s, %(ourtime)s, %(id)s, %(action)s, %(comment)s, %(ip)s, %(proper_name)s, %(wiki_id)s)""", {'page_name':self.page_name, 'proper_name':proper_name, 'text':text, 'ourtime':ourtime, 'id':user_id, 'action':action, 'comment':wikiutil.escape(comment),'ip':ip, 'wiki_id':self.request.config.wiki_id}, isWrite=True) # set in-memory page text/cached page text self.set_raw_body(text, set_cache=True) import caching cache = caching.CacheEntry(self.page_name, self.request) if config.memcache and not exists: pagecount = wikidb.getPageCount(self.request) + 1 self.request.mc.set('active_page_count', pagecount) # set trigger for clearing possible dependencies (e.g. [[Include]]) # we want this to be a post-commit trigger so that we don't # have stale data for pagename in caching.depend_on_me(self.page_name, self.request, exists, action=action): self.request.postCommitActions.append( (caching.CacheEntry(pagename, self.request).clear,)) if exists: type = 'page save' else: type = 'page save new' self.buildCache(type=type) # check if this is a user page and keep track of it, if so changed_state = (action == 'SAVENEW' or action == 'DELETE') is_user_page = self.page_name.startswith( config.user_page_prefix.lower()) if is_user_page and changed_state: user.userPageChangedState(self, action)
def checkSpelling(page, request, own_form=1): """ Do spell checking, return a tuple with the result. """ _ = request.getText # first check to see if we we're called with a "newwords" parameter if request.form.has_key('button_newwords'): _addLocalWords(request) # load words wordsdict = _loadDict(request) localwords = {} lsw_page = Page(request.config.page_local_spelling_words, request) if lsw_page.exists(): _loadWordsString(request, localwords, lsw_page.get_raw_body()) # init status vars & load page badwords = {} text = page.get_raw_body() # checker regex and matching substitute function word_re = re.compile(r'([%s]?[%s]+)' % ( config.upperletters, config.lowerletters)) def checkword(match, wordsdict=wordsdict, badwords=badwords, localwords=localwords, num_re=re.compile(r'^\d+$')): word = match.group(1) if len(word) == 1: return "" if not (wordsdict.has_key(word) or wordsdict.has_key(word.lower()) or localwords.has_key(word) or localwords.has_key(word.lower()) ): if not num_re.match(word): badwords[word] = 1 return "" # do the checking for line in text.encode('utf-8').split('\n'): if line == '' or line[0] == '#': continue word_re.sub(checkword, line) if badwords: badwords = badwords.keys() badwords.sort(lambda x,y: cmp(x.lower(), y.lower())) # build regex recognizing the bad words badwords_re = r'(^|(?<!\w))(%s)(?!\w)' badwords_re = badwords_re % ("|".join(map(re.escape, badwords)),) # XXX UNICODE re.UNICODE !? badwords_re = re.compile(badwords_re) lsw_msg = '' if localwords: lsw_msg = ' ' + _('(including %(localwords)d %(pagelink)s)') % { 'localwords': len(localwords), 'pagelink': lsw_page.link_to()} msg = _('The following %(badwords)d words could not be found ' 'in the dictionary of ' '%(totalwords)d words%(localwords)s and are ' 'highlighted below:') % { 'badwords': len(badwords), 'totalwords': len(wordsdict)+len(localwords), 'localwords': lsw_msg} + "<br>" # figure out what this action is called action_name = os.path.splitext(os.path.basename(__file__))[0] # add a form containing the bad words if own_form: msg = msg + ( '<form method="POST" action="%s">' '<input type="hidden" name="action" value="%s">' % (page.url(request), action_name,)) checkbox = ('<input type="checkbox" name="newwords" ' 'value="%(word)s">%(word)s ') msg = msg + ( " ".join(map( lambda w, cb=checkbox: cb % {'word': wikiutil.escape(w),}, badwords)) + '<p><input type="submit" name="button_newwords" value="%s"></p>' % _('Add checked words to dictionary') ) if own_form: msg = msg + '</form>' else: badwords_re = None msg = _("No spelling errors found!") return badwords, badwords_re, msg
def html_head(self, d): """ Assemble html head @param d: parameter dictionary @rtype: string @return: html head """ dict = { 'stylesheets_html': self.html_stylesheets(d), } dict.update(d) dict['theme_last_modified'] = self.last_modified dict['newtitle'] = None dict['newtitle'] = dict['title'] if dict['title'] == 'Front Page': dict['newtitle'] = wikiutil.escape(self.request.config.catchphrase) dict['web_dir'] = config.web_dir if d['page'].hasMapPoints(): dict['wiki_name'] = self.request.config.wiki_name if config.wiki_farm: dict['map_base'] = farm.getBaseFarmURL(self.request) else: dict['map_base'] = self.request.getBaseURL() dict['gmaps_api_key'] = (self.request.config.gmaps_api_key or config.gmaps_api_key) dict['map_html'] = ( '<script type="text/javascript">' 'var gmaps_src="http://maps.google.com/maps?file=api&v=2&' 'key=%(gmaps_api_key)s";' '</script>' '<script src="%(web_dir)s/wiki/gmap.js?tm=%(theme_last_modified)s" ' 'type="text/javascript">' '</script>' '<script type="text/javascript">' 'var map_url="%(map_base)s?action=gmaps&wiki=%(wiki_name)s";' 'var point_count=1;</script>' % dict) else: dict['map_html'] = '' dict['searchsuggest_html'] = '' dict['searchsuggest_html'] += '<script type="text/javascript" src="/wiki/jquery.js"></script>' dict['searchsuggest_html'] += '<script type="text/javascript" src="/wiki/jquery.qtip-1.0.0-rc3.min.js"></script>' if config.opensearch_suggest_url: dict['searchsuggest_html'] += '<script type="text/javascript" src="/wiki/suggest/dimensions.js"></script>' dict['searchsuggest_html'] += '<script type="text/javascript" src="/wiki/suggest/autocomplete.js"></script>' dict['stylesheets_html'] += '<link rel="stylesheet" type="text/css" charset="iso-8859-1" media="all" href="/wiki/suggest/autocomplete.css">\n' if dict['newtitle'] is self.request.config.catchphrase: if self.request.config.catchphrase: html = ('<title>%(sitename)s - %(newtitle)s</title>\n' '%(map_html)s\n' '%(stylesheets_html)s\n' '%(searchsuggest_html)s\n' % dict) else: html = ('<title>%(sitename)s</title>\n' '%(map_html)s\n' '%(stylesheets_html)s\n' '%(searchsuggest_html)s\n' % dict) else: html = ('<title>%(newtitle)s - %(sitename)s</title>' '%(map_html)s\n' '%(stylesheets_html)s\n' '%(searchsuggest_html)s\n' % dict) return html
def diff(request, old, new, text_mode=False): """ Find changes between old and new and return HTML markup visualising them. """ _ = request.getText t_line = _("Line") + " " seq1 = old.splitlines() seq2 = new.splitlines() seqobj = difflib.SequenceMatcher(None, seq1, seq2) linematch = seqobj.get_matching_blocks() if len(seq1) == len(seq2) and linematch[0] == (0, 0, len(seq1)): # No differences. return _("No differences found!") lastmatch = (0, 0) if not text_mode: result = [ '<table class="diff">\n' '<tr>\n' '<td class="diff-removed">\n' '<span>\n' '%s\n' '</span>\n' '</td>\n' '<td class="diff-added">\n' '<span>\n' '%s\n' '</span>\n' '</td>\n' '</tr>\n' % (_('Deletions are marked like this.'), _('Additions are marked like this.'))] else: result = [ '<table>\n' '<tr>\n' '<td>\n' '<span>\n' '%s\n' '</span>\n' '</td>\n' '<td>\n' '<span>\n' '%s\n' '</span>\n' '</td>\n' '</tr>\n' % (_('Deletions are marked with - .'), _('Additions are marked with +.'))] # Print all differences for match in linematch: # Starts of pages identical? if lastmatch == match[0:2]: lastmatch = (match[0] + match[2], match[1] + match[2]) continue if not text_mode: result.append('<tr class="diff-title">\n' '<td>\n' '%s %s:\n' '</td>\n' '<td>\n' '%s %s:\n' '</td>\n' '</tr>\n' % (t_line, str(lastmatch[0] + 1), t_line, str(lastmatch[1] + 1))) else: result.append('<tr>\n' '<td>\n' '%s %s:\n' '</td>\n' '<td>\n' '%s %s:\n' '</td>\n' '</tr>\n' % (t_line, str(lastmatch[0] + 1), t_line, str(lastmatch[1] + 1))) leftpane = [] rightpane = [] linecount = max(match[0] - lastmatch[0], match[1] - lastmatch[1]) for line in range(linecount): if line < match[0] - lastmatch[0]: if line > 0: leftpane.append('\n') if not text_mode: leftpane.append(seq1[lastmatch[0] + line]) else: leftpane.append("- %s" % seq1[lastmatch[0] + line]) if line < match[1] - lastmatch[1]: if line > 0: rightpane.append('\n') if not text_mode: rightpane.append(seq2[lastmatch[1] + line]) else: rightpane.append("+ %s" % seq2[lastmatch[1] + line]) charobj = difflib.SequenceMatcher(None, ''.join(leftpane), ''.join(rightpane)) charmatch = charobj.get_matching_blocks() if charobj.ratio() < 0.5: # Insufficient similarity. if leftpane: leftresult = '<span>%s</span>' % indent(escape( ''.join(leftpane))) else: leftresult = '' if rightpane: rightresult = '<span>%s</span>' % indent(escape( ''.join(rightpane))) else: rightresult = '' else: # Some similarities; markup changes. charlast = (0, 0) leftresult = '' rightresult = '' for thismatch in charmatch: if thismatch[0] - charlast[0] != 0: leftresult += """<span>%s</span>""" % indent( escape(''.join(leftpane)[charlast[0]:thismatch[0]])) if thismatch[1] - charlast[1] != 0: rightresult += """<span>%s</span>""" % indent( escape(''.join(rightpane)[charlast[1]:thismatch[1]])) leftresult += escape(''.join( leftpane)[thismatch[0]:thismatch[0] + thismatch[2]]) rightresult += escape(''.join( rightpane)[thismatch[1]:thismatch[1] + thismatch[2]]) charlast = (thismatch[0] + thismatch[2], thismatch[1] + thismatch[2]) leftpane = '<br>\n'.join(map(indent, leftresult.splitlines())) rightpane = '<br>\n'.join(map(indent, rightresult.splitlines())) if not text_mode: result.append('<tr>\n' '<td class="diff-removed">\n' '%s\n' '</td>\n' '<td class="diff-added">\n' '%s\n' '</td>\n' '</tr>\n' % (leftpane, rightpane)) else: result.append('<tr>\n' '<td>\n' '%s\n' '</td>\n' '<td>\n' '%s\n' '</td>\n' '</tr>\n' % (leftpane, rightpane)) lastmatch = (match[0] + match[2], match[1] + match[2]) result.append('</table>\n') return ''.join(result)
def append(self, child): if isinstance(child, type("")): child = wikiutil.escape(child) self.children.append(child) return self
def asHTML(self): """ Create the complete HTML form code. """ _ = self._ self.make_form() # different form elements depending on login state html_uid = '' html_sendmail = '' security_pagename = "%s/%s" % (config.wiki_settings_page, config.wiki_settings_page_security_defaults) if not self.request.user.name in wikiacl.Group("Admin", self.request): return '' else: self._inner.append(html.Raw('<div class="securitySettings">')) self.make_row(_("Everybody may:"), [ html.INPUT(type="checkbox", name="All_may_read", value=1, checked=self.request.config.acl_rights_default['All'][ ACL_RIGHTS_TABLE['read']]), 'read', html.INPUT(type="checkbox", name="All_may_edit", value=1, checked=self.request.config.acl_rights_default['All'][ ACL_RIGHTS_TABLE['edit']]), 'edit', html.INPUT(type="checkbox", name="All_may_delete", value=1, checked=self.request.config.acl_rights_default['All'][ ACL_RIGHTS_TABLE['delete']]), 'delete', ]) self.make_row(_("Logged in people may:"), [ html.INPUT(type="checkbox", name="Known_may_read", value=1, checked=self.request.config.acl_rights_default['Known'][ ACL_RIGHTS_TABLE['read']]), 'read', html.INPUT(type="checkbox", name="Known_may_edit", value=1, checked=self.request.config.acl_rights_default['Known'][ ACL_RIGHTS_TABLE['edit']]), 'edit', html.INPUT(type="checkbox", name="Known_may_delete", value=1, checked=self.request.config.acl_rights_default['Known'][ ACL_RIGHTS_TABLE['delete']]), 'delete', ]) self.make_row(_("Banned people may:"), [ html.INPUT(type="checkbox", name="Banned_may_read", value=1, checked=self.request.config.acl_rights_default['Banned'][ ACL_RIGHTS_TABLE['read']]), 'read', html.INPUT(type="checkbox", name="Banned_may_edit", value=1, checked=self.request.config.acl_rights_default['Banned'][ ACL_RIGHTS_TABLE['edit']]), 'edit', html.INPUT(type="checkbox", name="Banned_may_delete", value=1, checked=self.request.config.acl_rights_default['Banned'][ ACL_RIGHTS_TABLE['delete']]), 'delete', ]) custom_groups = user.getGroupList(self.request, exclude_special_groups=True) for groupname in custom_groups: group = wikiacl.Group(groupname, self.request, fresh=True) self.make_row(_("People in the %s group may:" % ( wikiutil.escape(groupname))), [ html.INPUT(type="checkbox", name="%s_may_read" % quoteWikiname(groupname), value=1, checked=group.default_rights()[ ACL_RIGHTS_TABLE['read']]), 'read', html.INPUT(type="checkbox", name="%s_may_edit" % quoteWikiname(groupname), value=1, checked=group.default_rights()[ ACL_RIGHTS_TABLE['edit']]), 'edit', html.INPUT(type="checkbox", name="%s_may_delete" % quoteWikiname(groupname), value=1, checked=group.default_rights()[ ACL_RIGHTS_TABLE['delete']]), 'delete', html.INPUT(type="checkbox", name="%s_may_admin" % quoteWikiname(groupname), value=1, checked=group.default_rights()[ ACL_RIGHTS_TABLE['admin']]), 'change security' ]) self._inner.append(html.Raw("</div>")) # close securitySettings div buttons = [ ('save', _('Save Settings')), ] # Add buttons button_cell = [] for name, label in buttons: button_cell.extend([ html.INPUT(type="submit", name=name, value=label), ' ', ]) self.make_row('', button_cell) return str(self._form)
def _word_repl(self, word, text=None): """ Handle linked wiki page names. """ # check for parent links # !!! should use wikiutil.AbsPageName here, but setting `text` # correctly prevents us from doing this for now if (self.request.config.allow_subpages and word.startswith(self.PARENT_PREFIX)): alt_text = True # for making error prettier if not text: text = word alt_text = False base_pagename = self.formatter.page.proper_name() split_base_pagename = base_pagename.split('/') split_pagename = word.split('/') for entry in split_pagename: if entry == '..': try: split_base_pagename.pop() except IndexError: # Their link makes no sense if alt_text: return '["%s" %s]' % (word, text) else: return '["%s"]' % word else: split_base_pagename.append(entry) if split_base_pagename: word = split_base_pagename[0] if len(word) > 1: for entry in split_base_pagename[1:]: if entry: word += '/' + entry # is this a link to a user page? userpage_link = user.unify_userpage(self.request, word, text) if not text: text = word # if a simple, self-referencing link, emit it as plain text if self.is_a_page: if word.lower() == self.formatter.page.page_name: # Don't want to display ["Users/Myname"] as "Users/Myname" on # their Myname's user page. We display "Myname." if word.lower().startswith(config.user_page_prefix.lower()): text = word[len(config.user_page_prefix):] return text if (self.request.config.allow_subpages and word.startswith(wikiutil.CHILD_PREFIX)): word = self.formatter.page.proper_name() + word text = self.highlight_text(text) # need to do this for the side-effect of adding to pagelinks try: pagelink = self.formatter.pagelink(word, text) except Page.InvalidPageName, msg: from Sycamore.wikiaction import NOT_ALLOWED_CHARS not_allowed = ' '.join(NOT_ALLOWED_CHARS) msg = ('<em style="background-color: #ffffaa; padding: 2px;">' 'Invalid pagename: %s — The characters %s are not allowed in page' ' names.</em>' % (wikiutil.escape(word), wikiutil.escape(not_allowed))) pagelink = msg
def __str__(self): return wikiutil.escape(self.text)
def execute(macro, args, formatter=None): if not formatter: formatter = macro.formatter if line_has_just_macro(macro, args, formatter): macro.parser.inhibit_br = 2 macro_text = '' baseurl = macro.request.getScriptname() action = 'Files' # name of the action that does the file stuff html = [] ticketString = None # for temporary thumbnail generation pagename = formatter.page.page_name urlpagename = wikiutil.quoteWikiname(formatter.page.proper_name()) if not args: macro_text += formatter.rawHTML( '<b>Please supply at least an image name, e.g. ' '[[Image(image.jpg)]], where image.jpg is an image that\'s been ' 'uploaded to this page.</b>') return macro_text # image.jpg, "caption, here, yes", 20, right --- in any order # (filename first) # the number is the 'max' size (width or height) in pixels # parse the arguments try: (image_name, caption, thumbnail, px_size, alignment, border) = getArguments(args) except: macro_text += formatter.rawHTML('[[Image(%s)]]' % wikiutil.escape(args)) return macro_text if not wikiutil.isImage(image_name): macro_text += "%s does not seem to be an image file." % image_name return macro_text url_image_name = urllib.quote(image_name.encode(config.charset)) if (macro.formatter.processed_thumbnails.has_key( (pagename, image_name)) and (thumbnail or caption)): macro_text += ('<em style="background-color: #ffffaa; padding: 2px;">' 'A thumbnail or caption may be displayed only once per ' 'image.</em>') return macro_text macro.formatter.processed_thumbnails[(pagename, image_name)] = True #is the original image even on the page? macro.request.cursor.execute("""SELECT name FROM files WHERE name=%(image_name)s and attached_to_pagename=%(pagename)s and wiki_id=%(wiki_id)s""", {'image_name':image_name, 'pagename':pagename.lower(), 'wiki_id':macro.request.config.wiki_id}) result = macro.request.cursor.fetchone() image_exists = result if not image_exists: # lets make a link telling them they can upload the image, # just like the normal attachment linktext = 'Upload new image "%s"' % (image_name) macro_text += wikiutil.attach_link_tag(macro.request, '%s?action=Files&rename=%s#uploadFileArea' % ( wikiutil.quoteWikiname(formatter.page.proper_name()), url_image_name), linktext) return macro_text full_size_url = (baseurl + "/" + urlpagename + "?action=" + action + "&do=view&target=" + url_image_name) # put the caption in the db if it's new and if we're not in preview mode if not formatter.isPreview(): touchCaption(pagename, pagename, image_name, caption, macro.request) if caption: # parse the caption string caption = wikiutil.stripOuterParagraph(wikiutil.wikifyString( caption, formatter.request, formatter.page, formatter=formatter)) if thumbnail: # let's generated the thumbnail or get the dimensions if it's # already been generated if not px_size: px_size = default_px_size (x, y), ticketString = touchThumbnail(macro.request, pagename, image_name, px_size, formatter) d = {'right':'floatRight', 'left':'floatLeft', '':'noFloat'} floatSide = d[alignment] if caption and border: html.append('<span class="%s thumb" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" style="display:block;"/></a>' '<span>%s</span>' '</span>' % (floatSide, int(x)+2, full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, thumb=True, size=px_size, ticket=ticketString), image_name, caption)) elif border: html.append('<span class="%s thumb" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" style="display:block;"/></a>' '</span>' % (floatSide, int(x)+2, full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, thumb=True, size=px_size, ticket=ticketString), image_name)) elif caption and not border: html.append('<span class="%s thumb noborder" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" style="display:block;"/></a>' '<span>%s</span></span>' % (floatSide, int(x)+2, full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, thumb=True, size=px_size, ticket=ticketString), image_name, caption)) else: html.append('<span class="%s thumb noborder" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" style="display:block;"/></a>' '</span>' % (floatSide, int(x)+2, full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, thumb=True, size=px_size, ticket=ticketString), image_name)) else: x, y = getImageSize(pagename, image_name, macro.request) if not x and not y: # image has no size..something went amuck setImageSize(pagename, image_name, macro.request) x, y = getImageSize(pagename, image_name, macro.request) if not border and not caption: img_string = ('<a href="%s">' '<img class="borderless" src="%s" alt="%s"/></a>' % (full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, ticket=ticketString), image_name)) elif border and not caption: img_string = ('<a href="%s">' '<img class="border" src="%s" alt="%s"/></a>' % (full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, ticket=ticketString), image_name)) elif border and caption: img_string = ('<a href="%s">' '<img class="border" src="%s" alt="%s"/></a>' '<div style="width: %spx;">' '<p class="normalCaption">%s</p></div>' % (full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, ticket=ticketString), image_name, x, caption)) elif not border and caption: img_string = ('<a href="%s">' '<img class="borderless" src="%s" alt="%s"/></a>' '<div style="width: %spx;">' '<p class="normalCaption">%s</p></div>' % (full_size_url, Files.getAttachUrl(pagename, image_name, macro.request, ticket=ticketString), image_name, x, caption)) if alignment == 'right': img_string = '<span class="floatRight">' + img_string + '</span>' elif alignment == 'left': img_string = '<span class="floatLeft">' + img_string + '</span>' html.append(img_string) macro_text += ''.join(html) return macro_text
def _write_to_db(self, text, action, comment, ip, proper_name): """ Write the text to the page tables in the database. """ ourtime = time.time() self.request.save_time = ourtime self.request.cursor.execute( """SELECT name, propercased_name from curPages where name=%(page_name)s and wiki_id=%(wiki_id)s""", { 'page_name': self.page_name, 'wiki_id': self.request.config.wiki_id }) exists = self.request.cursor.fetchone() if not proper_name: if exists: proper_name = exists[1] else: proper_name = self.given_name if not self.request.user.id: user_id = 'anon:%s' % i else: user_id = self.request.user.id if exists: self.request.cursor.execute("""UPDATE curPages set name=%(page_name)s, text=%(text)s, editTime=%(ourtime)s, userEdited=%(id)s, propercased_name=%(proper_name)s where name=%(page_name)s and wiki_id=%(wiki_id)s""", { 'page_name': self.page_name, 'text': text, 'ourtime': ourtime, 'id': user_id, 'proper_name': proper_name, 'wiki_id': self.request.config.wiki_id }, isWrite=True) else: self.request.cursor.execute("""INSERT into curPages (name, text, cachedText, editTime, cachedTime, userEdited, propercased_name, wiki_id) values (%(page_name)s, %(text)s, NULL, %(ourtime)s, NULL, %(id)s, %(proper_name)s, %(wiki_id)s)""", { 'page_name': self.page_name, 'text': text, 'ourtime': ourtime, 'id': user_id, 'proper_name': proper_name, 'wiki_id': self.request.config.wiki_id }, isWrite=True) # then we need to update the allPages table for Recent Changes # and page-centric info. self.request.cursor.execute("""INSERT into allPages (name, text, editTime, userEdited, editType, comment, userIP, propercased_name, wiki_id) values (%(page_name)s, %(text)s, %(ourtime)s, %(id)s, %(action)s, %(comment)s, %(ip)s, %(proper_name)s, %(wiki_id)s)""", { 'page_name': self.page_name, 'proper_name': proper_name, 'text': text, 'ourtime': ourtime, 'id': user_id, 'action': action, 'comment': wikiutil.escape(comment), 'ip': ip, 'wiki_id': self.request.config.wiki_id }, isWrite=True) # set in-memory page text/cached page text self.set_raw_body(text, set_cache=True) import caching cache = caching.CacheEntry(self.page_name, self.request) if config.memcache and not exists: pagecount = wikidb.getPageCount(self.request) + 1 self.request.mc.set('active_page_count', pagecount) # set trigger for clearing possible dependencies (e.g. [[Include]]) # we want this to be a post-commit trigger so that we don't # have stale data for pagename in caching.depend_on_me(self.page_name, self.request, exists, action=action): self.request.postCommitActions.append( (caching.CacheEntry(pagename, self.request).clear, )) if exists: type = 'page save' else: type = 'page save new' self.buildCache(type=type) # check if this is a user page and keep track of it, if so changed_state = (action == 'SAVENEW' or action == 'DELETE') is_user_page = self.page_name.startswith( config.user_page_prefix.lower()) if is_user_page and changed_state: user.userPageChangedState(self, action)
def asHTML(self): """ Create the complete HTML form code. """ _ = self._ self.make_form() # different form elements depending on login state html_uid = '' html_sendmail = '' groups_pagename = "%s/%s" % (config.wiki_settings_page, config.wiki_settings_page_security_defaults) if self.request.user.name in wikiacl.Group("Admin", self.request): group_admin = wikiacl.Group("Admin", self.request, fresh=True) self.make_row(_("Admins"), [ html.TEXTAREA(name="group_Admin", rows="6", cols="40", id="group_Admin").append( '\n'.join(map(wikiutil.escape, group_admin.users(proper_names=True)))) ], option_text=_("(one per line)")) group_banned = wikiacl.Group("Banned", self.request, fresh=True) self.make_row(_("Banned Users"), [ html.TEXTAREA(name="group_Banned", rows="6", cols="40", id="group_Banned").append( '\n'.join(map(wikiutil.escape, group_banned.users(proper_names=True)))) ], option_text=_("(one per line)")) self.make_row(_("Banned IP Addresses"), [ html.TEXTAREA(name="ips_banned", rows="6", cols="40", id="ips_banned").append( '\n'.join(map(wikiutil.escape, group_banned.get_ips().keys()))) ], option_text=_("(one per line)")) custom_groups = user.getGroupList(self.request, exclude_special_groups=True) for groupname in custom_groups: group = wikiacl.Group(groupname, self.request, fresh=True) delete_label = ('<span class="minorActionBox">[<a href="%s/' '%s?action=usergroupsettings&delete=%s">' 'delete group</a>]</span>') % ( self.request.getScriptname(), quoteWikiname(groups_pagename), quoteWikiname(groupname)) self.make_row('%s %s' % (wikiutil.escape(groupname), delete_label), [ html.TEXTAREA( name="group_%s" % quoteWikiname(groupname), rows="6", cols="40", id="group_%s" % quoteWikiname(groupname) ).append('\n'.join(map(wikiutil.escape, group.users(proper_names=True)))) ], option_text=_("(one per line)")) buttons = [ ('save', _('Save Groups')), ] # Add buttons button_cell = [] for name, label in buttons: button_cell.extend([ html.INPUT(type="submit", name=name, value=label), ' ', ]) self.make_row('', button_cell) self._inner.append(html.H2().append("Create a new group")) self.make_row(_("Group name"), [ html.INPUT(type="text", size="40", name="new_group_name"), ]) self.make_row('Group users', [ html.TEXTAREA(name="new_group_users", rows="6", cols="40") ]) buttons = [ ('save', _('Add new group')), ] # Add buttons button_cell = [] for name, label in buttons: button_cell.extend([ html.INPUT(type="submit", name=name, value=label), ' ', ]) self.make_row('', button_cell) return str(self._form)
def sendEditor(self, **kw): """ Send the editor form page. @keyword preview: if given, show this text in preview mode @keyword staytop: don't go to #preview @keyword comment: comment field (when preview is true) @keyword had_conflict: we had an edit conflict on a save. """ import re try: from Sycamore.action import SpellCheck except ImportError: SpellCheck = None form = self.request.form _ = self._ self.request.http_headers([("Content-Type", "text/html; charset=%s" % config.charset)] + self.request.nocache) msg = None preview = kw.get('preview', None) emit_anchor = not kw.get('staytop', 0) proper_name = self.proper_name() from Sycamore.formatter.text_html import Formatter self.request.formatter = Formatter(self.request, store_pagelinks=1, preview=preview) base_uri = "%s/%s?action=edit" % (self.request.getScriptname(), wikiutil.quoteWikiname( self.proper_name())) backto = form.get('backto', [None])[0] if backto: base_uri += '&' + util.web.makeQueryString(backto=backto) # check edit permissions if not self.request.user.may.edit(self): msg = _('You are not allowed to edit this page.') elif self.prev_date: # Trying to edit an old version, this is not possible via # the web interface, but catch it just in case... msg = _('Cannot edit old revisions!') # Did one of the prechecks fail? if msg and not kw.get('had_conflict', None): self.send_page(msg=msg) return # check for preview submit if preview is None: title = _('Edit "%(pagename)s"') else: title = _('Preview of "%(pagename)s"') self.set_raw_body(preview.replace("\r", ""), 1) page_needle = self.page_name if config.allow_subpages and page_needle.count('/'): page_needle = '/' + page_needle.split('/')[-1] wikiutil.send_title( self.request, self.proper_name(), pagename=self.proper_name(), has_link=True, strict_title='Editing "%s"' % self.proper_name(), body_onload="sizeForIE('savetext', 100, 'editorComment', 99);") # start content div self.request.write('<div id="content" class="content">\n') # get request parameters text_rows = None if form.has_key('rows'): text_rows = int(form['rows'][0]) if self.request.user.valid: # possibly update user's pref if text_rows != self.request.user.edit_rows: self.request.user.edit_rows = text_rows self.request.user.save() else: text_rows = config.edit_rows if self.request.user.valid: text_rows = int(self.request.user.edit_rows) if form.has_key('cols'): text_cols = int(form['cols'][0]) if self.request.user.valid: # possibly update user's pref if text_rows != self.request.user.edit_rows: self.request.user.edit_rows = text_rows self.request.user.save() else: text_cols = 80 if self.request.user.valid: text_cols = int(self.request.user.edit_cols) # check datestamp (version) of the page our edit is based on if preview is not None: # propagate original datestamp mtime = float(form['datestamp'][0]) # did someone else change the page while we were editing? conflict_msg = None if not self.exists(): # page does not exist, are we creating it? if mtime: conflict_msg = _('<p>Someone else <b>deleted</b> this ' 'page while you were editing!') elif mtime != self.mtime(): conflict_msg = _('<p>Someone else changed this page while ' 'you were editing.') # merge conflicting versions allow_conflicts = 1 from Sycamore.util import diff3 savetext = self.get_raw_body() oldpg = Page(self.page_name, self.request, prev_date=mtime) original_text = oldpg.get_raw_body() saved_text = Page(self.page_name, self.request).get_raw_body() verynewtext, had_conflict = diff3.text_merge( original_text, saved_text, savetext, marker1='----- /!\ Edit conflict! Your version: -----\n', marker2='----- /!\ Edit conflict! Other version: -----\n', marker3='----- /!\ End of edit conflict -----\n') if had_conflict and self.request.user.valid and ( self.request.user.id == self.last_edit_info()[1]): # user pressed back button or did something weird conflict_msg = None elif had_conflict: conflict_msg = _(conflict_msg + 'There was an <b>edit conflict between ' 'your changes!</b></p>' '<p>Please review the conflicts and ' 'merge the changes.</p>') mtime = self.mtime() self.set_raw_body(verynewtext, 1) else: conflict_msg = _(conflict_msg + 'Your changes were sucessfully merged!') mtime = self.mtime() self.set_raw_body(verynewtext) if conflict_msg: self.request.write('<div id="message"><div>%s' '</div></div>' % conflict_msg) emit_anchor = 0 # make this msg visible! elif self.exists(): # datestamp of existing page mtime = self.mtime() else: # page creation mtime = 0 # output message message = kw.get('msg', '') if message: self.request.write('<div id="message">%s</div>' % (message)) # get the text body for the editor field if form.has_key('template'): # "template" parameter contains the name of the template page template_page = wikiutil.unquoteWikiname(form['template'][0]) raw_body = Page(template_page, self.request).get_raw_body() else: raw_body = self.get_raw_body() # send text above text area # button toolbar self.request.write('<div id="editArea">') self.request.write( "<script type=\"text/javascript\">" "var buttonRoot = '%s';</script>" % (os.path.join( config.url_prefix, self.request.theme.name, 'img', 'buttons'))) if self.request.user.name: if config.user_page_prefix: self.request.write( "<script type=\"text/javascript\">" "var userPageLink = '[\"%s%s\"]';</script>" % (config.user_page_prefix, self.request.user.propercased_name)) else: self.request.write("<script type=\"text/javascript\">" "var userPageLink = '[\"%s\"]';</script>" % (config.user_page_prefix, self.request.user.propercased_name)) else: self.request.write("<script type=\"text/javascript\">" "var userPageLink = '%s';</script>" % (self.request.remote_addr)) if config.wiki_farm: self.request.write( "<script type=\"text/javascript\" " "src=\"http://%s%s%s/edit.js\"></script>\n" % (config.wiki_base_domain, config.web_dir, config.url_prefix)) else: self.request.write("<script type=\"text/javascript\" " "src=\"%s%s/edit.js\"></script>\n" % (config.web_dir, config.url_prefix)) # send form self.request.write('<form name="editform" id="editform" ' 'method="post" action="%s/%s#preview">' % (self.request.getScriptname(), wikiutil.quoteWikiname(proper_name))) self.request.write( str(html.INPUT(type="hidden", name="action", value="savepage"))) if backto: self.request.write( str(html.INPUT(type="hidden", name="backto", value=backto))) # generate default content if not raw_body: if self.isTalkPage(): raw_body = _('This page is for discussing the ' 'contents of ["%s"].') % self.proper_name()[:-5] else: raw_body = _('Describe %s here.') % (self.proper_name(), ) # replace CRLF with LF raw_body = self._normalize_text(raw_body) # send datestamp (version) of the page our edit is based on self.request.write('<input type="hidden" name="datestamp" ' 'value="%s">' % repr(mtime)) # Print the editor textarea and the save button self.request.write('<textarea id="savetext" name="savetext" ' 'rows="%d" cols="%d" style="width:100%%;">%s' '</textarea>' % (text_rows, text_cols, wikiutil.escape(raw_body))) # make sure we keep the template notice on a resize of the editor template_param = '' if form.has_key('template'): template_param = '&template=' + form['template'][0] # draw edit size links self.request.write( _('<div class="pageEditInfo" id="editorSize">' 'editor size:')) self.request.write('<a href="#" onclick="return sizeEditor(\'bigger\',' '\'%s&preview=1&cols=60%s\')">%s' '</a>' % (base_uri, template_param, '+')) self.request.write( ',<a href="#" onclick="return sizeEditor(\'smaller\',' '\'%s&preview=1&cols=60%s\')">%s' '</a>' % (base_uri, template_param, '-')) self.request.write('</div>') self.request.write('</p>') # close textarea self.request.write( '<div id="editComment" id="editorResizeButtons"> ' '%s<br><input type="text" class="formfields" ' 'name="comment" id="editorComment" value="%s" ' 'size="%d" maxlength="80" style="width:99%%;">' '</div>' % (_("<font size=\"+1\">Please comment " "about this change:</font>"), wikiutil.escape(kw.get('comment', ''), 1), text_cols)) spam_catch_button = ( '<span style="position: absolute; top: 0px; left: 0px;' 'height: 0px; width: 0px; overflow: hidden;">' 'dont enter into this box:' '<input type="text" name="text_dont"/>' '</span>' '<span style="position: absolute; top: 0px; left: 0px;' 'height: 0px; width: 0px; overflow: hidden;">' '<input class="formbutton" type="submit" name="button_dont" ' 'value="Dont press me">\n' '</span>') self.request.write(spam_catch_button) # button bar button_spellcheck = ( SpellCheck and '<input type="submit" class="formbutton" name="button_spellcheck"' ' value="%s">' % _('Check Spelling')) or '' save_button_text = _('Save Changes') cancel_button_text = _('Cancel') self.request.write("</div>") if self.request.user.may.admin(self): security_button = ('<input type="button" class="formbutton" ' 'onClick="location.href=' '\'%s/%s?action=Security\'" ' 'value="Security">') % ( self.request.getScriptname(), wikiutil.quoteWikiname(proper_name)) else: security_button = '' if self.request.user.may.delete(self): delete_button = ('<input type="button" class="formbutton" ' 'onClick="location.href=' '\'%s/%s?action=DeletePage\'" value="Delete">' ) % (self.request.getScriptname(), wikiutil.quoteWikiname(proper_name)) rename_button = ('<input type="button" class="formbutton" ' 'onClick="location.href=\'%s/%s?action=Rename\'"' ' value="Rename">') % (self.request.getScriptname( ), wikiutil.quoteWikiname(proper_name)) else: delete_button = '' rename_button = '' self.request.write( '<div id="editButtonRow">' '<span>' '<input type="submit" class="formbutton" ' 'name="button_preview" value="%s"> ' '<input type="submit" class="formbutton" ' 'name="button_save" value="%s"> ' '<input type="submit" class="formbutton" ' 'name="button_cancel" value="%s"> ' '</span>' '<span class="editActions">' '<input type="button" class="formbutton" ' 'onClick="window.open(\'%s/%s?action=Files\', ' '\'files\', \'width=800,height=600,' 'scrollbars=1\')" value="Upload Files"> ' '%s ' '%s ' '%s ' '%s</span>' '</div>' % (_('Preview'), save_button_text, cancel_button_text, self.request.getScriptname(), wikiutil.quoteWikiname(proper_name), button_spellcheck, delete_button, rename_button, security_button)) if self.request.config.edit_agreement_text: self.request.write(self.request.config.edit_agreement_text) badwords_re = None if preview is not None: if SpellCheck and (form.has_key('button_spellcheck') or form.has_key('button_newwords')): badwords, badwords_re, msg = SpellCheck.checkSpelling( self, self.request, own_form=0) self.request.write("<p>%s</p>" % msg) self.request.write("</form>") if config.wiki_farm: from Sycamore import farm help_link = farm.link_to_page(farm.getBaseWikiName(), "Help with Editing", self.request.formatter, force_farm=True) else: help_link = Page("Help with Editing", self.request).link_to() # QuickHelp originally by Georg Mischler <*****@*****.**> self.request.write( '<h2>Editing quick-help</h2>\n' '<dl><div style="float: right; margin: 10px; border: 1px solid; ' 'padding: 3pt;">See <b>%s</b> for more information.</div>' % (help_link) + _("<dt>Emphasis:</dt>\n" "<dd>''<em>italics</em>''; '''<strong>bold</strong>''';" "'''''<strong><em>bold italics</em></strong>''''';" "''<em>mixed '''<strong>bold</strong>''' and " "italics</em>''; ---- horizontal rule.</dd>" "<dt>Headings:</dt>" "<dd>= Title 1 =; == Title 2 ==; === Title 3 ===;" "==== Title 4 ====; ===== Title 5 =====.</dd>" "<dt>Lists:</dt>" "<dd>space and one of * bullets; 1., a., A., i., I. " "numbered items;" " 1.#n start numbering at n; space alone indents.</dd>" "<dt>Links:</dt>" "<dd>[\"brackets and double quotes\"]; [\"the exact " "page name\" label];" " url; [url]; [url label].</dd>" "<dt>Tables:</dt>" "<dd>|| cell text |||| cell text spanning two columns ||;" " no trailing white space allowed after tables or titles." "</dd></dl>")) if preview is not None: if not emit_anchor: preview_name = "previewHide" else: preview_name = "preview" self.request.write('<div id="%s" class="preview">' % preview_name) self.send_page(content_only=1, hilite_re=badwords_re, preview=preview) self.request.write('</div>') self.request.write('</div>\n') # end content div wikiutil.send_after_content(self.request) self.request.theme.emit_custom_html(config.page_footer1) self.request.theme.emit_custom_html(config.page_footer2) self.request.write('</body></html>')
def checkSpelling(page, request, own_form=1): """ Do spell checking, return a tuple with the result. """ _ = request.getText # first check to see if we we're called with a "newwords" parameter if request.form.has_key('button_newwords'): _addLocalWords(request) # load words wordsdict = _loadDict(request) localwords = {} lsw_page = Page(request.config.page_local_spelling_words, request) if lsw_page.exists(): _loadWordsString(request, localwords, lsw_page.get_raw_body()) # init status vars & load page badwords = {} text = page.get_raw_body() # checker regex and matching substitute function word_re = re.compile(r'([%s]?[%s]+)' % (config.upperletters, config.lowerletters)) def checkword(match, wordsdict=wordsdict, badwords=badwords, localwords=localwords, num_re=re.compile(r'^\d+$')): word = match.group(1) if len(word) == 1: return "" if not (wordsdict.has_key(word) or wordsdict.has_key(word.lower()) or localwords.has_key(word) or localwords.has_key(word.lower())): if not num_re.match(word): badwords[word] = 1 return "" # do the checking for line in text.encode('utf-8').split('\n'): if line == '' or line[0] == '#': continue word_re.sub(checkword, line) if badwords: badwords = badwords.keys() badwords.sort(lambda x, y: cmp(x.lower(), y.lower())) # build regex recognizing the bad words badwords_re = r'(^|(?<!\w))(%s)(?!\w)' badwords_re = badwords_re % ("|".join(map(re.escape, badwords)), ) # XXX UNICODE re.UNICODE !? badwords_re = re.compile(badwords_re) lsw_msg = '' if localwords: lsw_msg = ' ' + _('(including %(localwords)d %(pagelink)s)') % { 'localwords': len(localwords), 'pagelink': lsw_page.link_to() } msg = _('The following %(badwords)d words could not be found ' 'in the dictionary of ' '%(totalwords)d words%(localwords)s and are ' 'highlighted below:') % { 'badwords': len(badwords), 'totalwords': len(wordsdict) + len(localwords), 'localwords': lsw_msg } + "<br>" # figure out what this action is called action_name = os.path.splitext(os.path.basename(__file__))[0] # add a form containing the bad words if own_form: msg = msg + ('<form method="POST" action="%s">' '<input type="hidden" name="action" value="%s">' % ( page.url(request), action_name, )) checkbox = ('<input type="checkbox" name="newwords" ' 'value="%(word)s">%(word)s ') msg = msg + ( " ".join( map(lambda w, cb=checkbox: cb % { 'word': wikiutil.escape(w), }, badwords)) + '<p><input type="submit" name="button_newwords" value="%s"></p>' % _('Add checked words to dictionary')) if own_form: msg = msg + '</form>' else: badwords_re = None msg = _("No spelling errors found!") return badwords, badwords_re, msg
def execute(macro, args, formatter=None): if not formatter: formatter = macro.formatter if line_has_just_macro(macro, args, formatter): macro.parser.inhibit_br = 2 if config.memcache: mc = macro.request.mc else: return formatter.rawHTML('<!-- Flickr support requires memcache -->') macro_text = '' baseurl = config.flickr_cgiurl html = [] pagename = formatter.page.page_name urlpagename = wikiutil.quoteWikiname(formatter.page.proper_name()) if not args: macro_text += formatter.rawHTML( '<b>Please supply at least an image ID, e.g. ' '[[Flickr(3046120549)]], where 3046120549 is a Flickr ' 'photo ID number.</b>') return macro_text # id, "caption, here, yes", Large, right --- in any order # (filename first) # the 'Large' is the size # parse the arguments try: (image_name, caption, thumbnail, px_size, alignment, border) = getArguments(args) except: macro_text += formatter.rawHTML('[[Flickr(%s)]]' % wikiutil.escape(args)) return macro_text url_image_name = urllib.quote(image_name.encode(config.charset)) photohandle = flickr.Photo(image_name) try: validsizes = mc.get('flickr-%s-validsizes' % url_image_name) if not validsizes: validsizes = [] for i in photohandle.getSizes(): validsizes.append(i['label'].lower()) mc.set('flickr-%s-validsizes' % url_image_name, validsizes, time=3*24*60*60) except flickr.FlickrError: macro_text += '%s does not seem to be a valid Flickr photo' % image_name return macro_text licensename = mc.get('flickr-license-%s' % photohandle.license) try: # sanity check on licensename tmpfoo1, tmpfoo2 = licensename if not (type(tmpfoo1) == type(tmpfoo2) == type(str())): licensename = None except: licensename = None if not licensename: licensename = licenses_getInfo(photohandle.license) if licensename: mc.set('flickr-license-%s' % photohandle.license, licensename, time=3*24*60*60) else: licensename = ('unknown license id %s', 'http://www.flickr.com/services/api/flickr.photos.licenses.getInfo.html') mc.set('flickr-license-%s' % photohandle.license, licensename, time=60*60) size = px_size oklicenses = ['1','2','3','4','5','6','7','8'] ok = mc.get('flickr-%s-%s-ok' % (url_image_name, size)) if not ok: if (photohandle.license in oklicenses) and (photohandle.ispublic): ok = True mc.set('flickr-%s-%s-ok' % (url_image_name, size), ok, time=60*60*12) else: ok = False if not ok: macro_text += 'License for image %s does not allow us to include this image.' % image_name return macro_text if px_size.lower() not in validsizes: macro_text += '%s is not a valid size for %s. Try: %s' % (px_size, image_name, string.join(validsizes, ', ')) return macro_text ownername = mc.get('flickr-%s-owner' % (url_image_name)) if not ownername: ownername = photohandle.owner.username mc.set('flickr-%s-owner' % (url_image_name), ownername) if not ownername: ownername = '(unknown)' if (macro.formatter.processed_thumbnails.has_key( (pagename, image_name)) and (thumbnail or caption)): macro_text += ('<em style="background-color: #ffffaa; padding: 2px;">' 'A thumbnail or caption may be displayed only once per ' 'image.</em>') return macro_text macro.formatter.processed_thumbnails[(pagename, image_name)] = True urls = mc.get('flickr-%s-%s-urls' % (url_image_name, size)) if not urls: urls = {} urls['img'] = photohandle.getURL(urlType='source',size=size) urls['link'] = photohandle.getURL(urlType='url',size=size) mc.set('flickr-%s-%s-urls' % (url_image_name, size), urls, time=60*60*24) full_size_url = urls['link'] # put the caption in the db if it's new and if we're not in preview mode if not formatter.isPreview(): touchCaption(pagename, pagename, image_name, caption, macro.request) if caption: caption += ' (by Flickr user %s [%s license info])' % (ownername, licensename[1]) # parse the caption string caption = wikiutil.stripOuterParagraph(wikiutil.wikifyString( caption, formatter.request, formatter.page, formatter=formatter)) if thumbnail: # let's generated the thumbnail or get the dimensions if it's # already been generated if not px_size: px_size = default_px_size x, y = getImageSize(pagename, photohandle, px_size, macro.request) d = {'right':'floatRight', 'left':'floatLeft', '':'noFloat'} floatSide = d[alignment] if caption and border: html.append('<span class="%s thumb" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" title="%s" style="display:block;"/></a>' '<span>%s</span>' '</span>' % (floatSide, int(x)+2, full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0], caption)) elif border: html.append('<span class="%s thumb" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" title="%s" style="display:block;"/></a>' '</span>' % (floatSide, int(x)+2, full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0])) elif caption and not border: html.append('<span class="%s thumb noborder" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" title="%s" style="display:block;"/></a>' '<span>%s</span></span>' % (floatSide, int(x)+2, full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0], caption)) else: html.append('<span class="%s thumb noborder" style="width: %spx;">' '<a style="color: black;" href="%s">' '<img src="%s" alt="%s" title="%s" style="display:block;"/></a>' '</span>' % (floatSide, int(x)+2, full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0])) else: x, y = getImageSize(pagename, photohandle, px_size, macro.request) if not border and not caption: img_string = ('<a href="%s">' '<img class="borderless" src="%s" alt="%s" title="%s"/></a>' % (full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0])) elif border and not caption: img_string = ('<a href="%s">' '<img class="border" src="%s" alt="%s" title="%s"/></a>' % (full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0])) elif border and caption: img_string = ('<a href="%s">' '<img class="border" src="%s" alt="%s" title="%s"/></a>' '<div style="width: %spx;">' '<p class="normalCaption">%s</p></div>' % (full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0], x, caption)) elif not border and caption: img_string = ('<a href="%s">' '<img class="borderless" src="%s" alt="%s" title="%s"/></a>' '<div style="width: %spx;">' '<p class="normalCaption">%s</p></div>' % (full_size_url, baseurl + '?id=' + image_name + '&size=' + px_size, image_name, licensename[0], x, caption)) if alignment == 'right': img_string = '<span class="floatRight">' + img_string + '</span>' elif alignment == 'left': img_string = '<span class="floatLeft">' + img_string + '</span>' html.append(img_string) macro_text += ''.join(html) return macro_text