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

Last change on this file since 24923 was 24923, checked in by davidb, 9 years ago

Tidy up of code, removing commented lines of code

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