def mark_link(self, reference): """ Create markup to indicate the link. We just add an overlay to represent the link. """ if not reference: return '' postfix = '' ## Absolute reference if reference.startswith('http'): pass elif reference.startswith("/"): path = normpath("%s" % (reference)) reference="%s://%s%s" % (self.method, self.host, path) elif self.method: ## FIXME: This leads to references without methods: reference="%s/%s" % (self.base_url, reference) if reference.startswith("http://"): reference='http:/'+FlagFramework.normpath(reference[6:]) reference = url_unquote(decode_entity(unquote(reference))) dbh = DB.DBO(self.case) dbh.execute("select mtime from inode where inode_id=%r", self.inode_id) row = dbh.fetch() dbh.execute("select inode.inode_id, inode.mtime, datediff(inode.mtime, %r) as diff, url "\ "from http join inode on "\ "inode.inode_id=http.inode_id where url=%r and not "\ # "isnull(http.inode_id) and size > 0 and inode.mtime >= %r "\ "isnull(http.inode_id) "\ "order by inode.mtime asc limit 1", (row['mtime'], reference, )) row = dbh.fetch() if row: print "Fetched %s %s ago" % (row['url'], row['diff']) postfix = "<div class='overlay'>Linked <a href=%s>%s</a><br>After %s</div>" % ( self.make_reference_to_inode(row['inode_id'],None), row['url'][:50], row['diff']) return postfix
def mark_link(self, reference): """ Create markup to indicate the link. We just add an overlay to represent the link. """ if not reference: return '' postfix = '' ## Absolute reference if reference.startswith('http'): pass elif reference.startswith("/"): path = normpath("%s" % (reference)) reference = "%s://%s%s" % (self.method, self.host, path) elif self.method: ## FIXME: This leads to references without methods: reference = "%s/%s" % (self.base_url, reference) if reference.startswith("http://"): reference = 'http:/' + FlagFramework.normpath(reference[6:]) reference = url_unquote(decode_entity(unquote(reference))) dbh = DB.DBO(self.case) dbh.execute("select mtime from inode where inode_id=%r", self.inode_id) row = dbh.fetch() dbh.execute("select inode.inode_id, inode.mtime, datediff(inode.mtime, %r) as diff, url "\ "from http join inode on "\ "inode.inode_id=http.inode_id where url=%r and not "\ # "isnull(http.inode_id) and size > 0 and inode.mtime >= %r "\ "isnull(http.inode_id) "\ "order by inode.mtime asc limit 1", (row['mtime'], reference, )) row = dbh.fetch() if row: print "Fetched %s %s ago" % (row['url'], row['diff']) postfix = "<div class='overlay'>Linked <a href=%s>%s</a><br>After %s</div>" % ( self.make_reference_to_inode( row['inode_id'], None), row['url'][:50], row['diff']) return postfix
def resolve_reference(self, reference, hint='', build_reference=True): original_reference = reference ## Absolute reference if re.match("(http|ftp)", reference, re.I): pass elif reference.startswith("/"): path = normpath("%s" % (reference)) reference="%s://%s%s" % (self.method, self.host, path) elif self.method: ## FIXME: This leads to references without methods: reference="%s://%s%s" % (self.method, self.host, FlagFramework.normpath("%s/%s" % (self.base_url, reference))) if reference.startswith("http://"): reference='http:/'+FlagFramework.normpath(reference[6:]) ## If we get here the reference is not absolute, and we dont ## have a method - chances are that its in the VFS: else: fsfd = FileSystem.DBFS(self.case) new_reference = decode_entity(url_unquote(reference)) url = posixpath.normpath(posixpath.join(posixpath.dirname(self.base_url), new_reference)) try: path, inode, inode_id = fsfd.lookup(path = url) if inode_id: return self.make_reference_to_inode(inode_id) except RuntimeError: pass ## Try to make reference more url friendly: reference = reference.strip(" \"'\t") reference = url_unquote(decode_entity(unquote(reference))) ## print reference, self.method, self.host, self.base_url, original_reference dbh = DB.DBO(self.case) dbh.execute("select http.status,http.inode_id from http join inode on "\ "inode.inode_id=http.inode_id where url=%r and not "\ "isnull(http.inode_id) and size > 0 limit 1", reference) row = dbh.fetch() if row and row['inode_id']: ## If the target was redirected - take care of that: ## (DANGER - a circular redirection could be problematic) ## FIXME - do this (we need to store the location header) if row['status'] == 302: inode_id = self.follow_redirect(dbh, row['inode_id']) else: inode_id = row['inode_id'] ## This is needed to stop dbh leaks due to the highly ## recursive nature of this function. del dbh result = self.make_reference_to_inode(inode_id, hint) if build_reference: result += " reference=\"%s\" " % reference return result ## Maybe its in the sundry table: dbh.execute("select id from http_sundry where url = %r and present = 'yes'", reference) row = dbh.fetch() if row and row['id']: del dbh result = self.make_reference_to_inode(row['id'], hint) if build_reference: result += " reference=\"%s\" " % reference return result ## We could not find it, so we try to insert to the sundry table dbh.check_index('http_sundry','url') dbh.execute("select * from http_sundry where url=%r", reference) row = dbh.fetch() if not row: dbh.insert("inode", inode = "x", _fast=True) inode_id = dbh.autoincrement() dbh.execute("update inode set inode = 'xHTTP%s' where inode_id = %s " %(inode_id, inode_id)) dbh.insert("file", inode_id = inode_id, inode = "xHTTP%s" % inode_id, path = "/http_sundry/", name = "xHTTP%s" % inode_id) dbh.insert('http_sundry', url = reference, id=inode_id) result = "images/spacer.png" if build_reference: result += " reference=\"%s\" " % reference print "Not found '%s' (%s + %s)" % (reference,original_reference, self.url) return result