root/gs3-extensions/html-to-expeditee/trunk/src/src/cgi-bin/html-to-expeditee.pl.in @ 24922

Revision 24922, 9.2 KB (checked in by davidb, 8 years ago)

Introduction of base tag in HTML to help with 'dumping' AJAX retrieved HTML to be resolved correctly, along with a few other improvments

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 = "/greenstone/cgi-bin/library.cgi";
172          //url += "?c="+collect +"&a=d&cl=" + cl;
173         
174          var url;
175          if (gs2) {
176              url = "library.cgi";
177          url += "?c="+collect +"&a=d&cl=" + cl;
178          }
179          else {
180              url = "dev";
181              url += "?c="+collect +"&a=b&rt=s&s=ClassifierBrowse&cl=" + cl;
182              url += "&excerptid=gs_content";
183          }
184
185
186          /* processing animation */
187         
188          docOIDs = [];
189          var outstandingURLs = [];
190          outstandingURLs.push(url);
191
192          while (outstandingURLs.length>0) {
193          url = outstandingURLs.shift();
194         
195          var clHtml = urlGetSync(url);
196                 
197          var aElems;
198          var workingTrav = document.getElementById("workingTraverse");
199          workingTrav.innerHTML = clHtml;
200          aElems = workingTrav.getElementsByTagName("a");
201         
202         
203          /* any links with a=d ... cl=??? => outstandingURLS */
204          /* any links with a=d ... d=???  => docOIDS */
205         
206          var actionRE = new RegExp("(\\\\?|&)a=(?:d|b)(&|\$)");
207          var clRE     = new RegExp("(\\\\?|&)cl=" + cl + "\\\\.");
208          var docRE    = new RegExp("(?:\\\\?|&)d=(.*?)(?:&|\$)");
209
210          for (var i=0; i<aElems.length; i++) {
211              var aElem = aElems[i];
212              var href=aElem.href;
213              if (href && href.match(actionRE)) {
214              if (href.match(clRE)) {
215                  outstandingURLs.push(href);
216              }
217              else if (href.match(docRE)) {
218
219                  var docMatch = docRE.exec(href);
220                  var docOID = docMatch[1];
221
222                  docOIDs.push(docOID);
223              }
224              }
225          }
226          }
227
228          numDocOIDs = docOIDs.length;
229
230          var iframe = document.getElementById("iframe");
231
232          var docOID = docOIDs.shift();
233          console.log("doc oid = " + docOID);
234         
235          var url;
236          if (gs2) {
237              url = "library.cgi";
238              url += "?c="+collect +"&a=d&d=" + docOID;
239         }
240         else {
241              url = "dev";
242              url += "?c="+collect +"&a=d&d=" + docOID;
243              url += "&ed=1&dt=hierarchy";
244         }
245         
246          iframe.src = url;
247         
248          var progressbar = document.getElementById("progressbar");
249          progressbar.style.display = "block";
250
251
252          return false; });
253     });
254     
255      function pageLoaded()
256      {
257      var iframe = document.getElementById("iframe");     
258     
259      if (iframe.src) {
260          if (iframe.style.display != "block") {
261          iframe.height = "90%";
262          iframe.style.display = "block";
263          }
264
265          var iframeDoc;
266          if ( iframe.contentDocument )
267          { /* FF */
268            iframeDoc = iframe.contentDocument;
269          }
270          else if ( iframe.contentWindow )
271          { /* IE */
272            iframeDoc = iframe.contentWindow.document;
273          }
274
275          var frameID = (numDocOIDs - docOIDs.length);
276          var progressPercent = frameID/numDocOIDs * 100;
277
278          \$(function() {
279          \$('#progressbar').progressbar({ value: progressPercent })
280         });
281
282          expFrame = htmlToExpeditee(iframeDoc.body);
283          //alert(expFrame);
284
285          var collect = document.getElementById("collect").value;
286          var site    = document.getElementById("site").value;
287
288          var url = "cgi-bin/html-to-expeditee.pl";
289          var params = "c=" + collect;
290          if (site.match(/\\w/)) {
291        params += "&site=" + site;
292          }
293          params += "&fn=" + frameID;
294          params += "&json=" + escape(expFrame);
295          //params += "&json=" + "testing123";
296
297          var clHtml = urlPostSync(url,params);
298
299          if (!clHtml.match(/html-to-expeditee saved frame/)) {
300          alert("Error processing url: " + url);
301          }
302
303          if (docOIDs.length>0) {
304          var docOID = docOIDs.shift();
305         
306          // console.log("doc oid = " + docOID);
307
308          var cl = document.getElementById("cl").value;
309         
310
311          //var url = "/greenstone/cgi-bin/library.cgi";
312          //url += "?c="+collect +"&a=d&d=" + docOID;
313         
314          var gs2 = $isGSDL2;
315              var url;
316
317              if (gs2) {
318                  url = "library.cgi";
319                  url += "?c="+collect +"&a=d&d=" + docOID;
320              }
321              else {
322                  url = "dev";
323                  url += "?c="+collect +"&a=d&d=" + docOID;
324                  url += "&ed=1&dt=hierarchy";
325              }
326
327          iframe.src = url;
328          }
329          else {
330          var progressbar = document.getElementById("progressbar");
331          progressbar.style.display = "none";
332
333          iframe.style.display = "none";
334          delete iframe.src;
335          }
336      }
337     
338      }
339
340    </script>
341
342    <div id="progressbar" width="100%"
343          style="display: none; margin: 10px; height: 10px;"></div>
344    <div id="workingTraverse" style="display: none"></div>
345
346    <hr style="margin: 10px;">
347
348    <iframe width="100%" id="iframe" style="display: none;"
349            onload="pageLoaded()"></iframe>
350
351  </body>
352</html>
353EOT
354
355
356        print "Content-type:text/html\n\n";
357        print $html_form;
358
359    }
360}
361
362
363
364&main();
Note: See TracBrowser for help on using the browser.