#!/usr/bin/ruby require "cgi" load "unity" Domain = 'nobell.org' DB = "lenses" $StartTime = Time.now def mark secs = Time.now.to_i - $StartTime.to_i "#{secs/60}:#{format('%02d',secs%60)}" end db = UnityDB.readDB(DB) ALL_NAMES = db.colGroup.names QUERY_STRING = ENV.fetch('QUERY_STRING', ARGV[0]) SCRIPT_NAME = ENV.fetch('SCRIPT_NAME', '/cgi-bin/uprint') QUERY_ARGS = CGI.parse(QUERY_STRING) #POST_ARGS = CGI.parse(STDIN.read) ALL_ARGS = Hash.new QUERY_ARGS.each{|key, value| ALL_ARGS[key] = value } #POST_ARGS.each{|key, value| ALL_ARGS[key] = value } DEBUG = ALL_ARGS.fetch('debug', false) order = ALL_ARGS.fetch('order', [ALL_NAMES.join(' ')])[0].split(/\s/) sort = ALL_ARGS.fetch('sort', [''])[0].split(/\s/) query = ALL_ARGS.fetch('query', [''])[0] query = CGI.unescape(query) htmlQuery = CGI.escapeHTML(query) show = ALL_ARGS.fetch('show', [''])[0] show = CGI.unescape(show) column = ALL_ARGS.fetch('column', [''])[0] action = ALL_ARGS.fetch('action', [''])[0] STDERR.print "order:#{order.inspect}\n" if DEBUG STDERR.print "sort:#{sort.inspect}\n" if DEBUG STDERR.print "#{mark} query:#{query}\n" if DEBUG case show when '' when '(hide all)' order = [] else order = order + [show] if !order.index(show) end case action when 'sortup' reColumn = /^-?#{column}$/ sort = [column] + sort.collect{|x| x !~ reColumn ? x : nil }.compact when 'sortdown' reColumn = /^-?#{column}$/ sort = ['-' + column] + sort.collect{|x| x !~ reColumn ? x : nil }.compact when 'hide' order = order - [column] when 'left' i = order.index(column) order = order[0,i-1] + [column] + [order[i-1]] + order[i+1...order.length] if i && i > 0 when 'right' i = order.index(column) order = order[0,i] + [order[i+1]] + [column] + order[i+2...order.length] if i && i < order.length - 1 when 'first' order = [column] + (order - [column]) when 'last' order = (order - [column]) + [column] end dbo = db.select(order) rea, reError = reQueryParse(query, dbo.colGroup.names) STDERR.print "#{mark} rea:#{rea.inspect}\n" if DEBUG dbw = dbo.select(nil, rea) STDERR.print "#{mark} sort:#{sort.inspect}\n" if DEBUG dba = dbw.asort(sort) #dba = sort.length > 0 ? dbw.asort(sort) : dbw rea, reError = reQueryParse(query, dba.colGroup.names) print <<-EOF Content-Type: text/html
ALL_ARGS=#{ALL_ARGS.sort.collect{|item| item.inspect + ((item[0] =~ /shell/) ? "\n" : " ") } } #{ENV.to_a.sort.collect{|var,val| "#{var}=#{val}\n"}}EOF print <<-EOF