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

Revision 24944, 9.3 KB (checked in by davidb, 8 years ago)

More careful recursive traversal to classifier nodes; further developed of CSS to Expeditee attributes (background colour); and slightly less cludgy way to deal with full img URLS

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          var visitedURLs = {};
111
112          outstandingURLs.push(url);
113          visitedURLs[url] = 1;
114
115          while (outstandingURLs.length>0) {
116          url = outstandingURLs.shift();
117         
118          var clHtml = urlGetSync(url);
119                 
120          var aElems;
121          var workingTrav = document.getElementById("workingTraverse");
122          workingTrav.innerHTML = clHtml;
123          aElems = workingTrav.getElementsByTagName("a");
124         
125         
126          /* any links with a=(b|d) ... cl=??? => outstandingURLS */
127          /* any links with a=d ... d=???  => docOIDS */
128         
129          var actionRE = new RegExp("(\\\\?|&)a=(?:d|b)(&|\$)");
130          var clRE     = new RegExp("(\\\\?|&)cl=" + cl + "(\\\\.\\\\d+)+(&|\$)");
131          var docRE    = new RegExp("(?:\\\\?|&)d=(.*?)(?:&|\$)");
132
133          for (var i=0; i<aElems.length; i++) {
134              var aElem = aElems[i];
135              var href=aElem.href;
136              if (href && href.match(actionRE)) {
137              if (href.match(clRE)) {
138                  if (!visitedURLs[href]) {     
139                      // console.log("found a new CL line: " + href);
140                      outstandingURLs.push(href);
141                  visitedURLs[href] = 1;
142                  }
143              }
144              else if (href.match(docRE)) {
145
146                  var docMatch = docRE.exec(href);
147                  var docOID = docMatch[1];
148
149                  // console.log("found a new doc line: " + docOID);
150
151                  docOIDs.push(docOID);
152              }
153              }
154          }
155          }
156
157          numDocOIDs = docOIDs.length;
158
159          var iframe = document.getElementById("iframe");
160
161          var docOID = docOIDs.shift();
162          //console.log("doc oid = " + docOID);
163         
164          var url;
165          if (gs2) {
166              url = "library.cgi";
167              url += "?c="+collect +"&a=d&d=" + docOID;
168          }
169          else {
170              url = "dev";
171              url += "?c="+collect +"&a=d&d=" + docOID;
172              url += "&ed=1&dt=hierarchy";
173              // url += "&excerptid=gs_content";
174          }
175         
176          /* load iframe with document drawn from Greenstone collection */
177          iframe.src = url;
178         
179          var progressbar = document.getElementById("progressbar");
180          progressbar.style.display = "block";
181
182
183          return false; });
184     });
185     
186      function pageLoaded()
187      {
188      var iframe = document.getElementById("iframe");     
189     
190      if (iframe.src) {
191          if (iframe.style.display != "block") {
192          iframe.height = "90%";
193          iframe.style.display = "block";
194          }
195
196          var iframeDoc;
197          if ( iframe.contentDocument )
198          { /* FF */
199            iframeDoc = iframe.contentDocument;
200          }
201          else if ( iframe.contentWindow )
202          { /* IE */
203            iframeDoc = iframe.contentWindow.document;
204          }
205
206          var frameID = (numDocOIDs - docOIDs.length);
207          var progressPercent = frameID/numDocOIDs * 100;
208
209          \$(function() {
210          \$('#progressbar').progressbar({ value: progressPercent })
211         });
212
213          var gsContent = iframeDoc.getElementById("gs_content");
214
215          /*
216          var gsContentPos = getElementPosition(gsContent);
217          var pxl = gsContentPos.xl;
218          var pxr = gsContentPos.xr;
219          */
220
221          var expFrameTree = htmlToExpeditee(gsContent);
222
223          var expFrame = JSON.stringify(expFrameTree);
224          //alert(expFrame);
225
226          var collect = document.getElementById("collect").value;
227          var site    = document.getElementById("site").value;
228
229          var url = "cgi-bin/html-to-expeditee.pl";
230          var params = "c=" + collect;
231          if (site.match(/\\w/)) {
232        params += "&site=" + site;
233          }
234          params += "&a=generate-frame&fn=" + frameID;
235          params += "&json=" + escape(expFrame);
236
237          var clHtml = urlPostSync(url,params);
238
239          if (!clHtml.match(/html-to-expeditee saved frame/)) {
240          alert("Error processing url: " + url);
241          }
242
243          if (docOIDs.length>0) {
244          var docOID = docOIDs.shift();
245         
246          // console.log("doc oid = " + docOID);
247
248         // is the following line used ????
249          var cl = document.getElementById("cl").value;
250         
251          var gs2 = $isGSDL2;
252              var url;
253
254              if (gs2) {
255                  url = "library.cgi";
256                  url += "?c="+collect +"&a=d&d=" + docOID;
257              }
258              else {
259                  url = "dev";
260                  url += "?c="+collect +"&a=d&d=" + docOID;
261                  url += "&ed=1&dt=hierarchy";
262                  // url += "&excerptid=gs_content";
263              }
264
265          iframe.src = url;
266          }
267          else {
268          var progressbar = document.getElementById("progressbar");
269          progressbar.style.display = "none";
270
271          iframe.style.display = "none";
272          delete iframe.src;
273          }
274      }
275     
276      }
277
278    </script>
279
280    <div id="progressbar" width="100%"
281          style="display: none; margin: 10px; height: 10px;"></div>
282    <div id="workingTraverse" style="display: none"></div>
283
284    <hr style="margin: 10px;">
285
286    <iframe width="100%" id="iframe" style="display: none;"
287            onload="pageLoaded()"></iframe>
288
289  </body>
290</html>
291EOT
292
293    print "Content-type:text/html\n\n";
294    print $html_form;
295}
296
297sub main
298{
299
300#    $ENV{'QUERY_STRING'} = "a=...";set-import-metadata&c=espresso-music&d=HASH012d6f72cde5dc48162f4a1d.1&metaname=annotation&metapos=0&metavalue=adfadfad";
301#    $ENV{'REQUEST_METHOD'} = "GET";
302
303    my $gsdl_cgi = new gsdlCGI();
304
305    # Load the Greenstone modules that we need to use
306    $gsdl_cgi->setup_gsdl();
307
308    my $gsdlhome = $ENV{'GSDLHOME'};   
309    $gsdl_cgi->checked_chdir($gsdlhome);
310
311    require cgiactions::HtmlToExpediteeAction;
312   
313    # Useful debug statement for seeing what packages have been included
314####    printf("%-45s%-s\n",$_,$INC{$_}) foreach (sort keys %INC);
315   
316    $gsdl_cgi->parse_cgi_args();
317
318    # We don't want the gsdlCGI module to return errors and warnings in XML
319    $gsdl_cgi->{'xml'} = 0;
320
321    my $fn = $gsdl_cgi->clean_param("fn");     # frame number
322
323    if (defined $fn) {
324
325    my $action = new HtmlToExpediteeAction($gsdl_cgi,$iis6_mode);
326
327    $action->do_action();
328    }
329    else {
330    # generate form, pre-filled out with any useful values such
331    # as the collection and classifier value
332
333    my $collect = $gsdl_cgi->clean_param("c");   
334    my $cl = $gsdl_cgi->clean_param("cl");   
335
336    # Establish collect_dir using defining 'site' along the way if GS3
337
338    my $site = undef;
339    my $isGSDL2 = undef;
340
341    if ($gsdl_cgi->greenstone_version() == 2) {
342        $isGSDL2 = 1;
343    }
344    else {
345        $isGSDL2 = 0;
346       
347        # GS3 (and possible future versions) make use of 'site'
348        $site = $gsdl_cgi->clean_param("site");   
349        if (!defined $site) {
350        $gsdl_cgi->generate_error("No site specified.");
351        }
352    }
353
354    generate_html_form($isGSDL2,$site,$collect,$cl);
355    }
356}
357
358
359
360&main();
Note: See TracBrowser for help on using the browser.