1 # =====================================================================
2 # groupYearList.awk: RPC I/O function for rpclib/groupYearList.
3 #
4 # Copyright (c) 2007-2011 Carlo Strozzi
5 #
6 # This program is free software; you can redistribute it and/or modify
7 # it under the terms of the GNU General Public License as published by
8 # the Free Software Foundation; version 2 dated June, 1991.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program; if not, write to the Free Software
17 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 #
19 # =====================================================================
20
21 # =====================================================================
22 # void _userproc(int mode)
23 # =====================================================================
24
25 function _userproc(mode, value,outfile,blksize,\
26 i,j,url,a,fmt,tmp,e,f,\
27 blkno,curr,first,last,\
28 tot,fmt1) {
29
30 if (mode == _O_REQUEST) { # request.
31
32 # target group
33 value = _request("1",1)
34
35 # group must not be null and it may not contain the
36 # unescaped ``.'' character.
37
38 if (value != _NULL && value !~ /\./) {
39 _rcset("cgi.group",unixify(value))
40 _rcset("cgi.group.literal",value)
41 }
42
43 # year to list.
44
45 value = _request("2",1)
46
47 # add more types as needed.
48 if (value ~ /^[1-9][0-9]+$/) _rcset("cgi.year",value)
49
50 # Optional starting page no. for paged results.
51 if ((value=_request("3",1)/1) > 0) _rcset("cgi.paging",value)
52
53 # Optional target meta-category within group, provided it
54 # is at least 3 characters in length. If it is shorter
55 # (but not null), then it is set to a bogus value. Note that
56 # the "too.short" keyword contains one dot, to make sure it
57 # does not collide with an existing subcat named "tooshort"
58
59 if ((value=_request("4",1)) != _NULL) {
60 _rcset("cgi.subcat.literal",value)
61 if (length((value=unixify(value,1))) < 3) value = "too.short"
62 _rcset("cgi.subcat",value)
63 }
64
65 # The following test is necessary since the address could,
66 # at least in theory, have been set to any string by the
67 # remote user, due to how it is handled to cope with stunnel(8)
68 # and the lack of transproxy support in kernel 2.4.x.
69
70 if (_isipaddr(ENVIRON["REMOTE_ADDR"]) == _TRUE)
71 value = ENVIRON["REMOTE_ADDR"]
72 else value = "0.0.0.0"
73
74 _rcset("REMOTE_ADDR",value)
75 }
76
77 else { # response
78
79 outfile = _rcget("tpl.include.tw.page")
80 if (outfile !~ /^\/\.*[a-zA-Z0-9]/)
81 return(_sys("csaExit.fault 0041 outfile"))
82
83 # Set output format string.
84 fmt = readfmt("tw-group-yearlist")
85 gsub(/%/,"%%",fmt) # turn plain '%' into '%%'.
86 gsub(/\\/,"\\\\&",fmt) # turn '\' into '\\'.
87 gsub(/[\n\r]+/,"",fmt) # just in case.
88 tmp1 = fmt
89
90 # Handle custom positioning of output tokens.
91 sub(/.*\[:/,_NULL,tmp1); sub(/:].*/,_NULL,tmp1)
92 tmp1 = _strip(tmp1,_O_MIDDLE)
93 if (tmp1 !~ /^[0-3 ]+$/) tmp1 = "1 2 3"
94
95 # pad missing arg specs with "0".
96 if ((i=split(tmp1,f," ")) < 3) {
97 while (i++ <= 3) tmp1 = tmp1 " 0"
98 i = split(tmp1,f," ")
99 }
100
101 tmp1 = _NULL
102
103 for (j=1; j<=i; j++) {
104 if (j > 3) break # ignore excess arg specs.
105 if (!sub(//,"%s",fmt)) fmt = fmt ""
106 tmp1 = tmp1 " " f[j]
107 }
108
109 # encode any extra markers.
110 gsub(//,"\\<tw:s/\\>",fmt)
111
112 fmt = fmt "\n"
113
114 split(_strip(tmp1),f," "); f[0] = 0
115
116 url = ENVIRON["CSA_RPC_URI"] "/" \
117 ENVIRON["CSA_LANG"] "/" \
118 _rcget("tbl_group.g_uri") "/"
119
120 # trigger paging if appropriate.
121 if ((blkno=_rcget("cgi.paging"))) {
122 blksize = _rcget("TNS_PAGER_BLKSIZE",1)
123 if ((blksize/=1) <= 0) blksize = 10
124 first = blkno * blksize - blksize + 1
125 last = first + blksize - 1
126 }
127
128 # Make sure the output file is cleared, in case we are re-using
129 # a previous workfile and we have nothing new to write to it,
130 # i.e. in case _TBLS[] is empty.
131 _creat(outfile,_O_TRUNC)
132
133 # read input table.
134 # p_vtime, p_name, p_uri, p_descr
135
136 i=j=1; e[0] = _NULL
137 while (split(_TBLS[1,i++],a,"\t")) {
138
139 # Handle output paging.
140 if (blkno && ++tot && (++curr < first || curr > last)) continue
141
142 # Pages with nil descriptions will appear in the
143 # listing only to editors.
144
145 if (a[4] ~ /^ *- *$/ && \
146 "," ENVIRON["TNS_AUTH_GRP"] "," !~ /,editor,/) continue
147
148 sub(/:..$/,"",a[1]) # drop seconds.
149
150 e[1] = a[1]
151 e[2] = _xmlencode(url a[3])
152 e[3] = _xmlencode(a[2])
153 printf(fmt,e[f[1]],e[f[2]],e[f[3]]) > outfile
154
155 # Provide also a machine-readable format.
156 _wsresponse(1,j++,url a[3])
157 }
158
159 close(outfile)
160
161 # show the pager if appropriate.
162 if (blkno && tot > blksize) {
163
164 # Set pager output format string.
165
166 fmt1 = readfmt("tw-pager")
167 gsub(/%/,"%%",fmt1) # turn plain '%' into '%%'.
168 gsub(/\\/,"\\\\&",fmt1) # turn '\' into '\\'.
169 gsub(/[\n\r]+/,"",fmt1) # just in case.
170
171 # the pager format string is currently not customizable.
172 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
173 if (!sub(//,"%s",fmt1)) fmt1 = fmt1 ""
174
175 # encode any extra markers.
176 gsub(//,"\\<tw:s/\\>",fmt1)
177
178 fmt1 = fmt1 "\n"
179
180 url = ENVIRON["CSA_RPC_URI"] "/" ENVIRON["CSA_LANG"] "/" \
181 _rcget("tbl_group.g_uri") "/4/" _rcget("cgi.year")
182
183 # modify here to include the search parameters (if any)
184 # in the pager links..
185 #if ((tmp=_rcget("cgi.subcat")) != _NULL) url = url "/" tmp "/"
186 #else url = url "?4="
187
188 url = url "?3="
189
190 tot % blksize ? tmp = 1 : tmp = 0
191
192 _response("tpl.var.tw.pager",\
193 pager(blkno,int(tot/blksize)+tmp,url,fmt1))
194 }
195
196 # Set fixed page title for the specified view.
197 _response("tpl.var.html.title",\
198 _rcget("tbl_group.g_descr") " - " \
199 _rcget("tpl.var.tw.year") ": " \
200 _nlsmap(_NULL,"tw-group-yearlist"))
201
202 # generic template conditionals.
203
204 ifsections()
205 }
206 }
207
208 # EOF