loona class: oops, mixed up table.remove and os.remove. Corrected. Slightly
authorTimm S. Mueller <tmueller@neoscientists.org>
Fri, 24 Oct 2008 01:19:42 +0200
changeset 2505107767c6c1e
parent 249 2407b197ed8b
child 251 2de5931b723d
loona class: oops, mixed up table.remove and os.remove. Corrected. Slightly
changed the way startup arguments are passed to app/loona.lua, as can be seen
in the fastcgi launcher.
cgi-bin/loona_fastcgi.lua
cgi-bin/tek/app/loona.lua
cgi-bin/tek/class/loona.lua
     1.1 --- a/cgi-bin/loona_fastcgi.lua	Mon Oct 20 00:56:02 2008 +0200
     1.2 +++ b/cgi-bin/loona_fastcgi.lua	Fri Oct 24 01:19:42 2008 +0200
     1.3 @@ -66,7 +66,14 @@
     1.4  end
     1.5  
     1.6  function FCGIServer:runapp(req)
     1.7 -	loona.main(self.buffer, self.request, self.document, self.userdata)
     1.8 +	loona.main
     1.9 +	{
    1.10 +		conffile = "etc/config.lua",
    1.11 +		buf = self.buffer,
    1.12 +		request = self.request,
    1.13 +		document = self.document,
    1.14 +		userdata = self.userdata
    1.15 +	}
    1.16  	self:endrequest(req) -- confirm
    1.17  end
    1.18  
     2.1 --- a/cgi-bin/tek/app/loona.lua	Mon Oct 20 00:56:02 2008 +0200
     2.2 +++ b/cgi-bin/tek/app/loona.lua	Fri Oct 24 01:19:42 2008 +0200
     2.3 @@ -18,24 +18,19 @@
     2.4  --	main: LOona application entrypoint
     2.5  -------------------------------------------------------------------------------
     2.6  
     2.7 -function main(buf, request, document, userdata)
     2.8 +function main(init)
     2.9  	local res, msg = -1, "No handler to serve the addressed document"
    2.10 -
    2.11 -	-- Create buffer, request and document objects:
    2.12 -	buf = buf or Buffer:new()
    2.13 -	request = request or Request:new()
    2.14 -	document = document or request:getdocument()
    2.15 -
    2.16 -	if document.Handler then
    2.17 -
    2.18 +	init = init or { }
    2.19 +	init.buf = init.buf or Buffer:new()
    2.20 +	local buf = init.buf
    2.21 +	init.request = init.request or Request:new()
    2.22 +	init.document = init.document or init.request:getdocument()
    2.23 +	if init.document.Handler then
    2.24  		local loona, errdetail, errtrace
    2.25 -
    2.26  		res, msg, errdetail, errtrace = catch(function()
    2.27 -			loona = Loona:new { buf = buf, request = request,
    2.28 -				document = document, userdata = userdata }
    2.29 +			loona = Loona:new(init)
    2.30  			loona:run()
    2.31  		end)
    2.32 -
    2.33  		if res ~= 0 then
    2.34  			buf:discard()
    2.35  			buf:addheader "Content-Type: text/html; charset=utf-8\n\n"
     3.1 --- a/cgi-bin/tek/class/loona.lua	Mon Oct 20 00:56:02 2008 +0200
     3.2 +++ b/cgi-bin/tek/class/loona.lua	Fri Oct 24 01:19:42 2008 +0200
     3.3 @@ -28,16 +28,20 @@
     3.4  
     3.5  local table, string, assert, unpack, ipairs, pairs, type, require, setfenv =
     3.6  	table, string, assert, unpack, ipairs, pairs, type, require, setfenv
     3.7 -local open, remove, rename, getenv, time, date =
     3.8 +local open, os_remove, rename, getenv, time, date =
     3.9  	io.open, os.remove, os.rename, os.getenv, os.time, os.date
    3.10  local setmetatable = setmetatable
    3.11 +local loadfile = loadfile
    3.12 +local insert = table.insert
    3.13 +local concat = table.concat
    3.14 +local remove = table.remove
    3.15  
    3.16  -------------------------------------------------------------------------------
    3.17  --	Module setup:
    3.18  -------------------------------------------------------------------------------
    3.19  
    3.20  module("tek.class.loona", tek.class)
    3.21 -_VERSION = "LOona Class 5.7"
    3.22 +_VERSION = "LOona Class 5.8"
    3.23  
    3.24  -------------------------------------------------------------------------------
    3.25  --	Markup:
    3.26 @@ -63,11 +67,11 @@
    3.27  		self.is_dynamic_content = true
    3.28  	end
    3.29  	local t = { '<%loona:include("', args[1], '"' }
    3.30 -	table.remove(args, 1)
    3.31 +	remove(args, 1)
    3.32  	for _, v in ipairs(args) do
    3.33 -		table.insert(t, ',' .. v)
    3.34 +		insert(t, ',' .. v)
    3.35  	end
    3.36 -	return table.concat(t), ')%>'
    3.37 +	return concat(t), ')%>'
    3.38  end
    3.39  
    3.40  function Markup:link(link)
    3.41 @@ -121,7 +125,7 @@
    3.42  end
    3.43  
    3.44  function Session:delete()
    3.45 -	remove(self.filename)
    3.46 +	os_remove(self.filename)
    3.47  end
    3.48  
    3.49  -------------------------------------------------------------------------------
    3.50 @@ -160,10 +164,10 @@
    3.51  function Loona:deleteprofile(p, lang)
    3.52  	p = self.config.contentdir .. "/" .. p .. "_" .. (lang or self.lang)
    3.53  	for e in util.readdir(p) do
    3.54 - 		local success, msg = remove(p .. "/" .. e)
    3.55 + 		local success, msg = os_remove(p .. "/" .. e)
    3.56  		assert(success, self:dbmsg("Error removing entry in profile", msg))
    3.57  	end
    3.58 -	return remove(p)
    3.59 +	return os_remove(p)
    3.60  end
    3.61  
    3.62  
    3.63 @@ -220,7 +224,7 @@
    3.64  	for e in util.readdir(self.config.contentdir) do
    3.65  		local l = e:match(lmatch)
    3.66  		if l then
    3.67 -			table.insert(plangs, l)
    3.68 +			insert(plangs, l)
    3.69  		end
    3.70  	end
    3.71  
    3.72 @@ -264,8 +268,8 @@
    3.73  	for e in util.readdir(cache) do
    3.74  		local f = e:match("^.*%.html%.?(%w*)$")
    3.75  		if f and f ~= "tmp" then
    3.76 -			local success, msg = remove(htdocs .. "/" .. e)
    3.77 -			success, msg = remove(cache .. "/" .. e)
    3.78 +			local success, msg = os_remove(htdocs .. "/" .. e)
    3.79 +			success, msg = os_remove(cache .. "/" .. e)
    3.80   			assert(success,
    3.81   				self:dbmsg("Could not purge cached HTML file", msg))
    3.82  		end
    3.83 @@ -334,7 +338,7 @@
    3.84  				tab[#tab].name = a
    3.85  				ss = s.subs
    3.86  				if ss then
    3.87 -					table.insert(tab, { entries = ss })
    3.88 +					insert(tab, { entries = ss })
    3.89  				end
    3.90  			else
    3.91  				ss = nil -- stop.
    3.92 @@ -344,7 +348,7 @@
    3.93  	if not self.section and not sectionpath then
    3.94  		sectionpath = self.sections[default]
    3.95  		if sectionpath then
    3.96 -			table.insert(tab, { entries = sectionpath.subs })
    3.97 +			insert(tab, { entries = sectionpath.subs })
    3.98  		end
    3.99  	end
   3.100  	return tab, sectionpath or false
   3.101 @@ -357,26 +361,26 @@
   3.102  	maxdepth = maxdepth or #self.submenus
   3.103  	for _, menu in ipairs(self.submenus) do
   3.104  		if menu.name then
   3.105 -			table.insert(t, menu.name)
   3.106 +			insert(t, menu.name)
   3.107  		end
   3.108  		d = d + 1
   3.109  		if d == maxdepth then
   3.110  			break
   3.111  		end
   3.112  	end
   3.113 -	return table.concat(t, delimiter or "/")
   3.114 +	return concat(t, delimiter or "/")
   3.115  end
   3.116  
   3.117  
   3.118  function Loona:deletesection(fname, all_bodies)
   3.119  	local fullname = self.contentdir .. "/" .. fname
   3.120 -	local success, msg = remove(fullname)
   3.121 +	local success, msg = os_remove(fullname)
   3.122  	if all_bodies then
   3.123  		local pat = "^" .. -- TODO: check
   3.124  			fname:gsub("%^%$%(%)%%%.%[%]%*%+%-%?", "%%%1") .. "%..*$"
   3.125  		for e in util.readdir(self.contentdir) do
   3.126  			if e:match(pat) then
   3.127 -				remove(self.contentdir .. "/" .. e)
   3.128 +				os_remove(self.contentdir .. "/" .. e)
   3.129  			end
   3.130  		end
   3.131  	end
   3.132 @@ -395,7 +399,7 @@
   3.133  				end
   3.134  				tab = s.subs
   3.135  			else
   3.136 -				table.insert(tab, e)
   3.137 +				insert(tab, e)
   3.138  				tab[a] = e
   3.139   				tab = nil -- stop
   3.140  			end
   3.141 @@ -425,7 +429,7 @@
   3.142  					parent = tab[idx]
   3.143  					tab = tab[idx].subs
   3.144  				else
   3.145 -					table.remove(tab, idx)
   3.146 +					remove(tab, idx)
   3.147  					tab[a] = nil
   3.148  					if #tab == 0 and parent then
   3.149  						parent.subs = nil
   3.150 @@ -492,7 +496,7 @@
   3.151  
   3.152  
   3.153  function Loona:unlockfile(file)
   3.154 -	return not self.session and true or remove(file .. ".LOCK")
   3.155 +	return not self.session and true or os_remove(file .. ".LOCK")
   3.156  end
   3.157  
   3.158  
   3.159 @@ -553,9 +557,9 @@
   3.160  	for _, a in ipairs(arg) do
   3.161  		local key, val = a:match("^([%w_]+)=(.*)$")
   3.162  		if key and val then -- "arg=val" sets/overrides argument
   3.163 -			table.insert(args2, { name = key, value = val })
   3.164 +			insert(args2, { name = key, value = val })
   3.165  		elseif self.args[a] then -- just "arg" propagates argument
   3.166 -			table.insert(args2, { name = a, value = self.args[a] })
   3.167 +			insert(args2, { name = a, value = self.args[a] })
   3.168  		end
   3.169  	end
   3.170  	local doc = self:getdocname(section, #args2 > 0)
   3.171 @@ -564,16 +568,16 @@
   3.172  	local href = { anch and url or doc }
   3.173  	for i, arg in ipairs(args2) do
   3.174  		if i > 1 or notfirst then
   3.175 -			table.insert(href, "&amp;")
   3.176 +			insert(href, "&amp;")
   3.177  		else
   3.178 -			table.insert(href, "?")
   3.179 +			insert(href, "?")
   3.180  		end
   3.181 -		table.insert(href, arg.name .. "=" .. cgi.encodeurl(arg.value))
   3.182 +		insert(href, arg.name .. "=" .. cgi.encodeurl(arg.value))
   3.183  	end
   3.184  	if anch then
   3.185 -		table.insert(href, anch)
   3.186 +		insert(href, anch)
   3.187  	end
   3.188 -	return table.concat(href)
   3.189 +	return concat(href)
   3.190  end
   3.191  
   3.192  
   3.193 @@ -581,11 +585,11 @@
   3.194  
   3.195  function Loona:href(section, ...)
   3.196  	if self.session then
   3.197 -		table.insert(arg, 1, "profile")
   3.198 -		table.insert(arg, 1, "session")
   3.199 +		insert(arg, 1, "profile")
   3.200 +		insert(arg, 1, "session")
   3.201  	end
   3.202  	if self.explicitlang then
   3.203 -		table.insert(arg, 1, "lang")
   3.204 +		insert(arg, 1, "lang")
   3.205  	end
   3.206  	return self:shref(section, arg)
   3.207  end
   3.208 @@ -641,7 +645,7 @@
   3.209  		if posix.lstat(dir .. "/" .. f, "mode") == "directory" then
   3.210  			f = func(f)
   3.211  			if f then
   3.212 -				table.insert(tab, f)
   3.213 +				insert(tab, f)
   3.214  			end
   3.215  		end
   3.216  	end
   3.217 @@ -680,11 +684,11 @@
   3.218  		local visible = { }
   3.219  		for _, e in ipairs(sub.entries) do
   3.220  			if not e.notvisible then
   3.221 -				table.insert(visible, e)
   3.222 +				insert(visible, e)
   3.223  			end
   3.224  		end
   3.225  		if addnew then
   3.226 -			table.insert(visible, newent)
   3.227 +			insert(visible, newent)
   3.228  		end
   3.229  		local numvis = #visible
   3.230  		if numvis > 0 then
   3.231 @@ -762,7 +766,7 @@
   3.232  
   3.233  	if self.authuser_edit or self.authuser_profile or self.authuser_menu then
   3.234  
   3.235 -		local hiddenvars = table.concat( {
   3.236 +		local hiddenvars = concat( {
   3.237  			self:hidden("lang", self.args.lang),
   3.238  			self:hidden("profile", self.profile),
   3.239  			self:hidden("session", self.session.id),
   3.240 @@ -1065,7 +1069,7 @@
   3.241  					if tab and tab.id == self.session.id then
   3.242  						-- lock already held and is mine - try to save:
   3.243  						local savec = c:gsub("&nbsp;", "\194\160") -- TODO
   3.244 -						remove(contentdir .. "/" .. savename .. ".html")
   3.245 +						os_remove(contentdir .. "/" .. savename .. ".html")
   3.246  						self:savebody(savename, savec)
   3.247  						-- TODO: error handling
   3.248  						self:unlockfile(lockfname)
   3.249 @@ -1206,8 +1210,8 @@
   3.250  	local t, path, section = { }
   3.251  	for _, menu in ipairs(self.submenus) do
   3.252  		if menu.entries and menu.entries[menu.name] then
   3.253 -			table.insert(t, menu.name)
   3.254 -			local fn = table.concat(t, "_")
   3.255 +			insert(t, menu.name)
   3.256 +			local fn = concat(t, "_")
   3.257  			if posix.stat(self.contentdir .. "/" .. fn .. ext,
   3.258  				"mode") == "file" then
   3.259  				path, section = fn, menu
   3.260 @@ -1235,15 +1239,12 @@
   3.261  	local l = self.requestlang or self.args.lang
   3.262  	self.langs = { l and l:match("^%w+$") }
   3.263  	local s = self.request.HTTP_ACCEPT_LANGUAGE
   3.264 -	while s do
   3.265 -		local l, r = s:match("^([%w.=]+)[,;](.*)$")
   3.266 -		l = l or s
   3.267 -		s = r
   3.268 -		if l:match("^%w+$") then
   3.269 -			table.insert(self.langs, l)
   3.270 +	if s then
   3.271 +		for l in (s .. ";"):gmatch("([%a-]+)[,;]") do
   3.272 +			insert(self.langs, l)
   3.273  		end
   3.274  	end
   3.275 -	table.insert(self.langs, self.config.deflang)
   3.276 +	insert(self.langs, self.config.deflang)
   3.277  
   3.278  	-- get list of possible profiles
   3.279  
   3.280 @@ -1395,8 +1396,8 @@
   3.281  							self:hidden("actionconfirm", "true")
   3.282  					}
   3.283  				else
   3.284 -					local item = table.remove(t, i)
   3.285 -					table.insert(t, i - 1, item)
   3.286 +					local item = remove(t, i)
   3.287 +					insert(t, i - 1, item)
   3.288  					save = true
   3.289  				end
   3.290  			end
   3.291 @@ -1416,8 +1417,8 @@
   3.292  							self:hidden("actionconfirm", "true")
   3.293  					}
   3.294  				else
   3.295 -					local item = table.remove(t, i)
   3.296 -					table.insert(t, i + 1, item)
   3.297 +					local item = remove(t, i)
   3.298 +					insert(t, i + 1, item)
   3.299  					save = true
   3.300  				end
   3.301  			end
   3.302 @@ -1592,8 +1593,8 @@
   3.303  	local t = { }
   3.304  	local is_dynamic = Markup:new { input = s,
   3.305  		features = "hespcadlintf", indentchar = " ",
   3.306 -		wrfunc = function(s) table.insert(t, s) end }:run()
   3.307 -	return table.concat(t), is_dynamic
   3.308 +		wrfunc = function(s) insert(t, s) end }:run()
   3.309 +	return concat(t), is_dynamic
   3.310  end
   3.311  
   3.312  
   3.313 @@ -1893,7 +1894,7 @@
   3.314  	local outbuf = { }
   3.315  	o = o or { }
   3.316  	o.nologin = true
   3.317 -	o.out = function(self, s) table.insert(outbuf, s) end
   3.318 +	o.out = function(self, s) insert(outbuf, s) end
   3.319  	o.addheader = function(self, s) end
   3.320  
   3.321  	o = self:new(o):run()