source: gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/html-to-expeditee.pl.in@ 24926

Last change on this file since 24926 was 24926, checked in by davidb, 12 years ago

shifted generation of the initial HTML form into a subroutine

File size: 8.8 KB
Line 
1#!@FULL_PERL_EXE@ -w
2
3# Need to specify the full path of Perl above, e.g. for Windows something like
4#!C:\\Perl32\\bin\\perl -w
5
6
7use strict;
8
9# Set this to 1 to work around IIS 6 craziness
10my $iis6_mode = 0;
11
12
13# IIS 6: for some reason, IIS runs this script with the working directory set to the Greenstone
14# directory rather than the cgi-bin directory, causing lots of stuff to fail
15if ($iis6_mode)
16{
17 # Change into cgi-bin directory
18 chdir("cgi-bin");
19}
20
21
22# We use require and an eval here (instead of "use") to catch any errors loading the module (for IIS)
23eval("require \"gsdlCGI.pm\"");
24if ($@)
25{
26 print STDOUT "Content-type:text/plain\n\n";
27 print STDOUT "ERROR: $@\n";
28 exit 0;
29}
30
31
32sub generate_html_form
33{
34 my ($isGSDL2,$site,$collect,$cl) = @_;
35
36 my $html_form = <<EOT;
37<html>
38 <head>
39 <title>HTML to Expeditee Frames</title>
40
41 <base href=".." />
42
43 <link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
44 <script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
45 <script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
46 <script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
47 <script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
48
49
50
51 </head>
52 <body>
53 <form class="ui-widget">
54 Convert the collection
55 <input type="text"
56 class="ui-corner-all" style="padding: 4px;"
57 name="collect" value="$collect" id="collect" />
58 to Expeditee frames by traversing the classifier
59 <input type="text"
60 class="ui-corner-all" style="padding: 4px;"
61 name="cl" value="$cl" id="cl" />
62
63 <input value="$site" id="site" type="hidden">
64 <input value="Go" id="go"
65 class="ui-button ui-widget ui-state-default ui-corner-all"
66 type="submit">
67
68 </form>
69 <script type="text/javascript">
70 var docOIDs = [];
71 var numDocOIDs;
72
73 \$(function() {
74 \$('#progress').progressbar();
75
76 \$('#go').button().click(function() {
77 var collect = document.getElementById("collect").value;
78 if (collect.match(/^\\s*\$/)) {
79 alert("No collection specified");
80 return false;
81 }
82
83 var cl = document.getElementById("cl").value;
84 if (cl.match(/^\\s*\$/)) {
85 alert("No classifier specified");
86 return false;
87 }
88 if (cl.match(/^\\d+\$/)) {
89 cl = "CL" + cl;
90 }
91
92 var gs2=$isGSDL2;
93
94 var url;
95 if (gs2) {
96 url = "library.cgi";
97 url += "?c="+collect +"&a=d&cl=" + cl;
98 }
99 else {
100 url = "dev";
101 url += "?c="+collect +"&a=b&rt=s&s=ClassifierBrowse&cl=" + cl;
102 url += "&excerptid=gs_content";
103 }
104
105
106 /* processing animation */
107
108 docOIDs = [];
109 var outstandingURLs = [];
110 outstandingURLs.push(url);
111
112 while (outstandingURLs.length>0) {
113 url = outstandingURLs.shift();
114
115 var clHtml = urlGetSync(url);
116
117 var aElems;
118 var workingTrav = document.getElementById("workingTraverse");
119 workingTrav.innerHTML = clHtml;
120 aElems = workingTrav.getElementsByTagName("a");
121
122
123 /* any links with a=d ... cl=??? => outstandingURLS */
124 /* any links with a=d ... d=??? => docOIDS */
125
126 var actionRE = new RegExp("(\\\\?|&)a=(?:d|b)(&|\$)");
127 var clRE = new RegExp("(\\\\?|&)cl=" + cl + "\\\\.");
128 var docRE = new RegExp("(?:\\\\?|&)d=(.*?)(?:&|\$)");
129
130 for (var i=0; i<aElems.length; i++) {
131 var aElem = aElems[i];
132 var href=aElem.href;
133 if (href && href.match(actionRE)) {
134 if (href.match(clRE)) {
135 outstandingURLs.push(href);
136 }
137 else if (href.match(docRE)) {
138
139 var docMatch = docRE.exec(href);
140 var docOID = docMatch[1];
141
142 docOIDs.push(docOID);
143 }
144 }
145 }
146 }
147
148 numDocOIDs = docOIDs.length;
149
150 var iframe = document.getElementById("iframe");
151
152 var docOID = docOIDs.shift();
153 console.log("doc oid = " + docOID);
154
155 var url;
156 if (gs2) {
157 url = "library.cgi";
158 url += "?c="+collect +"&a=d&d=" + docOID;
159 }
160 else {
161 url = "dev";
162 url += "?c="+collect +"&a=d&d=" + docOID;
163 url += "&ed=1&dt=hierarchy";
164 }
165
166 iframe.src = url;
167
168 var progressbar = document.getElementById("progressbar");
169 progressbar.style.display = "block";
170
171
172 return false; });
173 });
174
175 function pageLoaded()
176 {
177 var iframe = document.getElementById("iframe");
178
179 if (iframe.src) {
180 if (iframe.style.display != "block") {
181 iframe.height = "90%";
182 iframe.style.display = "block";
183 }
184
185 var iframeDoc;
186 if ( iframe.contentDocument )
187 { /* FF */
188 iframeDoc = iframe.contentDocument;
189 }
190 else if ( iframe.contentWindow )
191 { /* IE */
192 iframeDoc = iframe.contentWindow.document;
193 }
194
195 var frameID = (numDocOIDs - docOIDs.length);
196 var progressPercent = frameID/numDocOIDs * 100;
197
198 \$(function() {
199 \$('#progressbar').progressbar({ value: progressPercent })
200 });
201
202 var expFrameTree = htmlToExpeditee(iframeDoc.body);
203 var expFrame = JSON.stringify(expFrameTree);
204 //alert(expFrame);
205
206 var collect = document.getElementById("collect").value;
207 var site = document.getElementById("site").value;
208
209 var url = "cgi-bin/html-to-expeditee.pl";
210 var params = "c=" + collect;
211 if (site.match(/\\w/)) {
212 params += "&site=" + site;
213 }
214 params += "&a=generate-frame&fn=" + frameID;
215 params += "&json=" + escape(expFrame);
216
217 var clHtml = urlPostSync(url,params);
218
219 if (!clHtml.match(/html-to-expeditee saved frame/)) {
220 alert("Error processing url: " + url);
221 }
222
223 if (docOIDs.length>0) {
224 var docOID = docOIDs.shift();
225
226 // console.log("doc oid = " + docOID);
227
228 // is the following line used ????
229 var cl = document.getElementById("cl").value;
230
231 var gs2 = $isGSDL2;
232 var url;
233
234 if (gs2) {
235 url = "library.cgi";
236 url += "?c="+collect +"&a=d&d=" + docOID;
237 }
238 else {
239 url = "dev";
240 url += "?c="+collect +"&a=d&d=" + docOID;
241 url += "&ed=1&dt=hierarchy";
242 }
243
244 iframe.src = url;
245 }
246 else {
247 var progressbar = document.getElementById("progressbar");
248 progressbar.style.display = "none";
249
250 iframe.style.display = "none";
251 delete iframe.src;
252 }
253 }
254
255 }
256
257 </script>
258
259 <div id="progressbar" width="100%"
260 style="display: none; margin: 10px; height: 10px;"></div>
261 <div id="workingTraverse" style="display: none"></div>
262
263 <hr style="margin: 10px;">
264
265 <iframe width="100%" id="iframe" style="display: none;"
266 onload="pageLoaded()"></iframe>
267
268 </body>
269</html>
270EOT
271
272 print "Content-type:text/html\n\n";
273 print $html_form;
274}
275
276sub main
277{
278
279# $ENV{'QUERY_STRING'} = "a=set-import-metadata&c=espresso-music&d=HASH012d6f72cde5dc48162f4a1d.1&metaname=annotation&metapos=0&metavalue=adfadfad";
280# $ENV{'QUERYSTRING'} = "a=set-import-metadata&c=espresso-music&d=HASH012d6f72cde5dc48162f4a1d.1&metaname=annotation&metapos=0&metavalue=adfadfad";
281# $ENV{'REQUEST_METHOD'} = "GET";
282
283
284 my $gsdl_cgi = new gsdlCGI();
285
286 # Load the Greenstone modules that we need to use
287 $gsdl_cgi->setup_gsdl();
288
289 my $gsdlhome = $ENV{'GSDLHOME'};
290 $gsdl_cgi->checked_chdir($gsdlhome);
291
292 require cgiactions::HtmlToExpediteeAction;
293
294 # Useful debug statement for seeing what packages have been included
295#### printf("%-45s%-s\n",$_,$INC{$_}) foreach (sort keys %INC);
296
297 $gsdl_cgi->parse_cgi_args();
298
299 # We don't want the gsdlCGI module to return errors and warnings in XML
300 $gsdl_cgi->{'xml'} = 0;
301
302 my $fn = $gsdl_cgi->clean_param("fn"); # frame number
303
304 if (defined $fn) {
305
306 my $action = new HtmlToExpediteeAction($gsdl_cgi,$iis6_mode);
307
308 $action->do_action();
309
310 }
311 else {
312 # generate form, pre-filled out with any useful values such
313 # as the collection and classifier value
314
315 my $collect = $gsdl_cgi->clean_param("c");
316 my $cl = $gsdl_cgi->clean_param("cl");
317
318 # Establish collect_dir using defining 'site' along the way if GS3
319
320 my $site = undef;
321 my $isGSDL2 = undef;
322
323 if ($gsdl_cgi->greenstone_version() == 2) {
324 $isGSDL2 = 1;
325 }
326 else {
327 $isGSDL2 = 0;
328
329 # GS3 (and possible future versions) make use of 'site'
330 $site = $gsdl_cgi->clean_param("site");
331 if (!defined $site) {
332 $gsdl_cgi->generate_error("No site specified.");
333 }
334 }
335
336 generate_html_form($isGSDL2,$site,$collect,$cl);
337 }
338}
339
340
341
342&main();
Note: See TracBrowser for help on using the repository browser.