Exemplo n.º 1
0
def send_human_readable(id, request):
    mimetype = None
    request.cursor.execute(
        """SELECT human_readable_secret from captchas
           where id=%(id)s and written_time > %(timevalid)s""", {
            'id': id,
            'timevalid': (time.time() - CAPTCHA_VALID_TIME)
        })
    result = request.cursor.fetchone()
    if not result:
        request.http_headers()
        request.write("No captcha for this id?  "
                      "The captcha could have expired, so you may want to "
                      "try again.")
        return

    human_readable = wikidb.binaryToString(result[0])
    mimetype = mimetypes.guess_type(id)[0]
    if not mimetype:
        mimetype = "application/octet-stream"

    request.do_gzip = False
    request.http_headers([("Content-Type", mimetype),
                          ("Content-Length", len(human_readable))])
    #output file
    request.write(human_readable, raw=True)
Exemplo n.º 2
0
def send_human_readable(id, request):
    mimetype = None
    request.cursor.execute(
        """SELECT human_readable_secret from captchas
           where id=%(id)s and written_time > %(timevalid)s""",
        {"id": id, "timevalid": (time.time() - CAPTCHA_VALID_TIME)},
    )
    result = request.cursor.fetchone()
    if not result:
        request.http_headers()
        request.write("No captcha for this id?  " "The captcha could have expired, so you may want to " "try again.")
        return

    human_readable = wikidb.binaryToString(result[0])
    mimetype = mimetypes.guess_type(id)[0]
    if not mimetype:
        mimetype = "application/octet-stream"

    request.do_gzip = False
    request.http_headers([("Content-Type", mimetype), ("Content-Length", len(human_readable))])
    # output file
    request.write(human_readable, raw=True)
Exemplo n.º 3
0
    def update(self, content, links):
        links = self._consider_talk_link(links)
        cached_time = time.time()
        self.request.cursor.execute("""UPDATE curPages set
            cachedText=%(cached_content)s, cachedTime=%(cached_time)s
            where name=%(key)s and wiki_id=%(wiki_id)s""",
            {'cached_content':wikidb.dbapi.Binary(content),
             'cached_time':cached_time, 'key':self.key,
             'wiki_id':self.request.config.wiki_id},
            isWrite=True)
        self.request.cursor.execute("""DELETE from links where
            source_pagename=%(key)s and wiki_id=%(wiki_id)s""",
            {'key':self.key, 'wiki_id':self.request.config.wiki_id},
            isWrite=True)
        for link in links:
          self.request.cursor.execute("""INSERT into links
            (source_pagename, destination_pagename,
             destination_pagename_propercased, wiki_id)
            values (%(key)s, %(link)s, %(link_propercased)s, %(wiki_id)s)""",
            {'key':self.key, 'link':link.lower(), 'link_propercased':link,
             'wiki_id':self.request.config.wiki_id}, isWrite=True)
        page_info = pageInfo(Page(self.key, self.request),
                             get_from_cache=False, cached_content=content,
                             cached_time=cached_time)

        text = wikidb.binaryToString(content)
        page_info.cached_text = (text, cached_time)
        if config.memcache:
           if self.request.set_cache:
             self.request.mc.set("page_info:%s" % wikiutil.mc_quote(self.key),
                                 page_info)
           else:
             self.request.mc.add("page_info:%s" % wikiutil.mc_quote(self.key),
                                 page_info)

        self.request.req_cache['page_info'][(wikiutil.quoteFilename(self.key),
            self.request.config.wiki_id)] = page_info
Exemplo n.º 4
0
def pageInfo(page, get_from_cache=True, cached_content=None,
             cached_time=None):
    """
    Gets a group of related items for a page: last edited information,
    page cached text, meta-text (such as #redirect), and has_map.
    Returns an object with attributes edit_info, cached_text, meta_text,
    has_map.
    """

    pagename_key = wikiutil.mc_quote(page.page_name.lower())
    if page.prev_date:
        key = "%s,%s" % (pagename_key, repr(page.prev_date))
    else:
        key = pagename_key

    if get_from_cache:
        # check per-request cache
        if page.request.req_cache['page_info'].has_key(
            (key, page.request.config.wiki_id)):
          return page.request.req_cache['page_info'][
            (key, page.request.config.wiki_id)]
        
        # check memcache
        if config.memcache:
            page_info = page.request.mc.get("page_info:%s" % key)
            if page_info:
                page.request.req_cache['page_info'][
                    (key, page.request.config.wiki_id)] = page_info
                return page_info

    # memcache failed, this means we have to get all the information
    # from the database

    # last edit information 
    editUserID = None
    editTimeUnix = 0
    has_map = None
    if page.exists():
        if not page.prev_date:
            page.cursor.execute("""SELECT editTime, userEdited from curPages
                where name=%(page_name)s and wiki_id=%(wiki_id)s""",
                {'page_name':page.page_name,
                 'wiki_id':page.request.config.wiki_id})
            result = page.cursor.fetchone()
            if result:
                editTimeUnix = result[0]
                if result[1]:
                    editUserID = result[1].strip()
                else:
                    editUserID = result[1]
        else:
            page.cursor.execute("""SELECT userEdited from allPages
                where name=%(page_name)s and editTime=%(date)s and
                      wiki_id=%(wiki_id)s""",
                {'page_name':page.page_name, 'date':page.prev_date,
                 'wiki_id':page.request.config.wiki_id})
            result = page.cursor.fetchone()
            editUserID = result[0]
            editTimeUnix = page.prev_date
        edit_info = (editTimeUnix, editUserID)

        # cached text
        cached_text = ('', 0)
        if not page.prev_date:
              if not cached_content or not cached_time:
                  page.cursor.execute("""SELECT cachedText, cachedTime from
                      curPages where name=%(page)s and wiki_id=%(wiki_id)s""",
                      {'page':page.page_name,
                       'wiki_id':page.request.config.wiki_id})
                  result = page.cursor.fetchone()
                  if result:
                      if result[0] and result[1]:
                          text = wikidb.binaryToString(result[0])
                          cached_time = result[1]
                          cached_text = (text, cached_time)
              else:
                  cached_text = cached_content

        # meta_text
        meta_text = find_meta_text(page, fresh=True)
      
    else:
     # set some defaults.  These shouldn't be accessed.
     edit_info = (None, None)
     cached_text = ('', 0)
     meta_text = None
     has_map = False
     has_acl = True

    if not page.prev_date:
        if not config.has_old_wiki_map:
            currently_has_map = False
            page.cursor.execute("""SELECT count(pagename) from mapPoints
                where pagename=%(page_name)s and wiki_id=%(wiki_id)s""",
                {'page_name':page.page_name,
                 'wiki_id':page.request.config.wiki_id})
            result = page.cursor.fetchone()
            if result:
                if result[0]:
                    currently_has_map = True
            if page.request.save_time: # we are in a 'saving' request
                if page.request.addresses:
                    has_map = True
                else:
                    has_map = False
            else:
                has_map = currently_has_map 

        else:
            page.cursor.execute("""SELECT count(pagename) from mapPoints where
                pagename=%(page_name)s and wiki_id=%(wiki_id)s""",
                {'page_name':page.page_name,
                 'wiki_id':page.request.config.wiki_id})
            result = page.cursor.fetchone()
            if result:
                if result[0]:
                    has_map = True
        if not page.exists():
           page.cursor.execute("""SELECT latestEdit.editTime,
            allPages.userEdited from (
                SELECT max(editTime) as editTime from allPages
                    where name=%(page_name)s and wiki_id=%(wiki_id)s)
            as latestEdit, allPages
            where allPages.name=%(page_name)s and
            allPages.editTime=latestEdit.editTime and
            allPages.wiki_id=%(wiki_id)s""",
           {'page_name':page.page_name, 'wiki_id':page.request.config.wiki_id})
           result = page.cursor.fetchone()
           if result:
                editUserID = result[1]
                editTimeUnix = result[0]
                edit_info = (editTimeUnix, editUserID)

    else:
        page.cursor.execute("""SELECT userEdited from allPages
         where name=%(page_name)s and editTime=%(date)s and
               wiki_id=%(wiki_id)s""",
         {'page_name':page.page_name, 'date':page.prev_date,
          'wiki_id':page.request.config.wiki_id})
        result = page.cursor.fetchone()
        editUserID = result[0]
        editTimeUnix = page.prev_date
        edit_info = (editTimeUnix, editUserID)
        has_map = None

    page.cursor.execute("""SELECT groupname, may_read, may_edit, may_delete,
                                  may_admin from pageAcls
                                  where pagename=%(pagename)s and
                                  wiki_id=%(wiki_id)s""",
                        {'pagename':page.page_name,
                         'wiki_id':page.request.config.wiki_id})
    if page.cursor.fetchone():
        has_acl = True
    else:
        has_acl = False

    page_info = pageInfoObj(edit_info, cached_text, meta_text, has_acl,
                            has_map)

    if config.memcache and not page.request.set_cache:
        page.request.mc.add("page_info:%s" % key, page_info)
    elif config.memcache and page.request.set_cache:
        page.request.mc.set("page_info:%s" % key, page_info)

    page.request.req_cache['page_info'][
        (key, page.request.config.wiki_id)] = page_info
    return page_info