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

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

Original script worked with Greenstone2. These edits are are the result of getting a basic system running for Greenstone3.

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    <link type="text/css" href="../ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
118    <script type="text/javascript" src="../ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
119    <script type="text/javascript" src="../ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
120    <script type="text/javascript" src="../ext/html-to-expeditee/js/gsajax-min.js"></script>
121    <script type="text/javascript" src="../ext/html-to-expeditee/js/html-to-expeditee.js"></script>
122
123  </head>
124  <body>
125    <form>
126      Convert the collection <input type="text" name="collect" value="$collect" id="collect" />
127      to Expeditee frames by traversing the classifier <input type="text" name="cl" value="$cl" id="cl" />
128
129      <input value="$site" id="site" type="hidden">
130      <input value="Go" id="go"
131             class="ui-button ui-widget ui-state-default ui-corner-all"
132             type="submit">
133
134    </form>
135    <script type="text/javascript">
136      var docOIDs = [];
137      var numDocOIDs;
138
139      \$(function() {
140      \$('#progress').progressbar();
141     
142      \$('#go').button().click(function() {
143          var collect = document.getElementById("collect").value;
144          if (collect.match(/^\\s*\$/)) {
145          alert("No collection specified");
146          return false;
147          }
148
149          var cl = document.getElementById("cl").value;
150          if (cl.match(/^\\s*\$/)) {
151          alert("No classifier specified");
152          return false;
153          }
154          if (cl.match(/^\\d+\$/)) {
155              cl = "CL" + cl;
156          }
157         
158          var gs2=$isGSDL2;
159
160          //var url = "/greenstone/cgi-bin/library.cgi";
161          //url += "?c="+collect +"&a=d&cl=" + cl;
162         
163          var url;
164          if (gs2) {
165              url = "library.cgi";
166          url += "?c="+collect +"&a=d&cl=" + cl;
167          }
168          else {
169              url = "../dev";
170              url += "?c="+collect +"&a=b&rt=s&s=ClassifierBrowse&cl=" + cl;
171          }
172
173
174          /* processing animation */
175         
176          docOIDs = [];
177          var outstandingURLs = [];
178          outstandingURLs.push(url);
179
180          while (outstandingURLs.length>0) {
181          url = outstandingURLs.shift();
182         
183          var clHtml = urlGetSync(url);
184                 
185          var aElems;
186          //if (gs2) {
187              var workingTrav = document.getElementById("workingTraverse");
188              workingTrav.innerHTML = clHtml;
189              aElems = workingTrav.getElementsByTagName("a");
190          //}
191          //else {
192              /* gs3 */
193            //  var parser = new DOMParser();
194             // cl_doc = parser.parseFromString(clHtml,"text/html");
195             // var aElems = cl_doc.getElementsByTagName("a");
196          //}
197         
198         
199          /* any links with a=d ... cl=??? => outstandingURLS */
200          /* any links with a=d ... d=???  => docOIDS */
201         
202          var actionRE = new RegExp("(\\\\?|&)a=(?:d|b)(&|\$)");
203          var clRE     = new RegExp("(\\\\?|&)cl=" + cl + "\\\\.");
204          var docRE    = new RegExp("(?:\\\\?|&)d=(.*?)(?:&|\$)");
205
206          for (var i=0; i<aElems.length; i++) {
207              var aElem = aElems[i];
208              var href=aElem.href;
209              if (href && href.match(actionRE)) {
210              if (href.match(clRE)) {
211                  outstandingURLs.push(href);
212              }
213              else if (href.match(docRE)) {
214
215                  var docMatch = docRE.exec(href);
216                  var docOID = docMatch[1];
217
218                  docOIDs.push(docOID);
219              }
220              }
221          }
222          }
223
224          numDocOIDs = docOIDs.length;
225
226          var iframe = document.getElementById("iframe");
227
228          var docOID = docOIDs.shift();
229          console.log("doc oid = " + docOID);
230         
231          var url;
232          if (gs2) {
233              url = "library.cgi";
234              url += "?c="+collect +"&a=d&d=" + docOID;
235         }
236         else {
237              url = "../dev";
238              url += "?c="+collect +"&a=d&d=" + docOID;
239              url += "&ed=1";
240         }
241         
242          iframe.src = url;
243         
244          var progressbar = document.getElementById("progressbar");
245          progressbar.style.display = "block";
246
247
248          return false; });
249     });
250     
251      function pageLoaded()
252      {
253      var iframe = document.getElementById("iframe");     
254     
255      if (iframe.src) {
256          if (iframe.style.display != "block") {
257          iframe.height = "90%";
258          iframe.style.display = "block";
259          }
260
261          var iframeDoc;
262          if ( iframe.contentDocument )
263          { /* FF */
264            iframeDoc = iframe.contentDocument;
265          }
266          else if ( iframe.contentWindow )
267          { /* IE */
268            iframeDoc = iframe.contentWindow.document;
269          }
270
271          var frameID = (numDocOIDs - docOIDs.length);
272          var progressPercent = frameID/numDocOIDs * 100;
273
274          \$(function() {
275          \$('#progressbar').progressbar({ value: progressPercent })
276         });
277
278          expFrame = htmlToExpeditee(iframeDoc.body);
279          //alert(expFrame);
280
281          var collect = document.getElementById("collect").value;
282          var site    = document.getElementById("site").value;
283
284          var url = "html-to-expeditee.pl";
285          var params = "c=" + collect;
286          if (site.match(/\\w/)) {
287        params += "&site=" + site;
288          }
289          params += "&fn=" + frameID;
290          params += "&json=" + escape(expFrame);
291          //params += "&json=" + "testing123";
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
307          //var url = "/greenstone/cgi-bin/library.cgi";
308          //url += "?c="+collect +"&a=d&d=" + docOID;
309         
310          var gs2 = $isGSDL2;
311              var url;
312
313              if (gs2) {
314                  url = "library.cgi";
315                  url += "?c="+collect +"&a=d&d=" + docOID;
316              }
317              else {
318                  url = "../dev";
319                  url += "?c="+collect +"&a=d&d=" + docOID;
320                  url += "&ed=1";
321              }
322
323
324
325          iframe.src = url;
326          }
327          else {
328          var progressbar = document.getElementById("progressbar");
329          progressbar.style.display = "none";
330
331          iframe.style.display = "none";
332          delete iframe.src;
333          }
334      }
335     
336      }
337
338    </script>
339
340    <div id="progressbar" width="100%"
341          style="display: none; margin: 10px; height: 10px;"></div>
342    <div id="workingTraverse" style="display: none"></div>
343
344    <hr style="margin: 10px;">
345
346    <iframe width="100%" id="iframe" style="display: none;"
347            onload="pageLoaded()"></iframe>
348
349  </body>
350</html>
351EOT
352
353
354        print "Content-type:text/html\n\n";
355        print $html_form;
356
357    }
358}
359
360
361
362&main();
Note: See TracBrowser for help on using the browser.