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

Revision 24924, 8.6 KB (checked in by davidb, 9 years ago)

Introduction of action to support frame writing

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::HtmlToExpediteeAction;
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    my $collect = $gsdl_cgi->clean_param("c");   
59    my $cl = $gsdl_cgi->clean_param("cl");   
60
61    # Establish collect_dir using defining 'site' along the way if GS3
62
63    my $site = undef;
64    my $isGSDL2 = undef;
65
66    if ($gsdl_cgi->greenstone_version() == 2) {
67        $isGSDL2 = 1;
68    }
69    else {
70        $isGSDL2 = 0;
71
72    # GS3 (and possible future versions) make use of 'site'
73    $site = $gsdl_cgi->clean_param("site");   
74    if (!defined $site) {
75        $gsdl_cgi->generate_error("No site specified.");
76    }
77    }
78
79    my $fn = $gsdl_cgi->clean_param("fn");     # frame number
80
81    if (defined $fn) {
82
83    my $action = new HtmlToExpediteeAction($gsdl_cgi,$iis6_mode);
84
85    $action->do_action();
86
87
88    }
89    else {
90   
91    my $html_form = <<EOT;
92<html>
93  <head>
94    <title>HTML to Expeditee Frames</title>
95
96    <base href=".." />
97
98    <link type="text/css" href="ext/html-to-expeditee/jquery/css/le-frog/jquery-ui-1.8.16.custom.css" rel="stylesheet" />
99    <script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-1.6.2.min.js"></script>
100    <script type="text/javascript" src="ext/html-to-expeditee/jquery/js/jquery-ui-1.8.16.custom.min.js"></script>
101    <script type="text/javascript" src="ext/html-to-expeditee/js/gsajax-min.js"></script>
102    <script type="text/javascript" src="ext/html-to-expeditee/js/html-to-expeditee.js"></script>
103
104
105
106  </head>
107  <body>
108    <form class="ui-widget">
109      Convert the collection
110        <input type="text"
111           class="ui-corner-all" style="padding: 4px;"
112           name="collect" value="$collect" id="collect" />
113      to Expeditee frames by traversing the classifier
114        <input type="text"
115           class="ui-corner-all" style="padding: 4px;"
116               name="cl" value="$cl" id="cl" />
117
118        <input value="$site" id="site" type="hidden">
119        <input value="Go" id="go"
120               class="ui-button ui-widget ui-state-default ui-corner-all"
121               type="submit">
122
123    </form>
124    <script type="text/javascript">
125      var docOIDs = [];
126      var numDocOIDs;
127
128      \$(function() {
129      \$('#progress').progressbar();
130     
131      \$('#go').button().click(function() {
132          var collect = document.getElementById("collect").value;
133          if (collect.match(/^\\s*\$/)) {
134          alert("No collection specified");
135          return false;
136          }
137
138          var cl = document.getElementById("cl").value;
139          if (cl.match(/^\\s*\$/)) {
140          alert("No classifier specified");
141          return false;
142          }
143          if (cl.match(/^\\d+\$/)) {
144              cl = "CL" + cl;
145          }
146         
147          var gs2=$isGSDL2;
148
149          var url;
150          if (gs2) {
151              url = "library.cgi";
152          url += "?c="+collect +"&a=d&cl=" + cl;
153          }
154          else {
155              url = "dev";
156              url += "?c="+collect +"&a=b&rt=s&s=ClassifierBrowse&cl=" + cl;
157              url += "&excerptid=gs_content";
158          }
159
160
161          /* processing animation */
162         
163          docOIDs = [];
164          var outstandingURLs = [];
165          outstandingURLs.push(url);
166
167          while (outstandingURLs.length>0) {
168          url = outstandingURLs.shift();
169         
170          var clHtml = urlGetSync(url);
171                 
172          var aElems;
173          var workingTrav = document.getElementById("workingTraverse");
174          workingTrav.innerHTML = clHtml;
175          aElems = workingTrav.getElementsByTagName("a");
176         
177         
178          /* any links with a=d ... cl=??? => outstandingURLS */
179          /* any links with a=d ... d=???  => docOIDS */
180         
181          var actionRE = new RegExp("(\\\\?|&)a=(?:d|b)(&|\$)");
182          var clRE     = new RegExp("(\\\\?|&)cl=" + cl + "\\\\.");
183          var docRE    = new RegExp("(?:\\\\?|&)d=(.*?)(?:&|\$)");
184
185          for (var i=0; i<aElems.length; i++) {
186              var aElem = aElems[i];
187              var href=aElem.href;
188              if (href && href.match(actionRE)) {
189              if (href.match(clRE)) {
190                  outstandingURLs.push(href);
191              }
192              else if (href.match(docRE)) {
193
194                  var docMatch = docRE.exec(href);
195                  var docOID = docMatch[1];
196
197                  docOIDs.push(docOID);
198              }
199              }
200          }
201          }
202
203          numDocOIDs = docOIDs.length;
204
205          var iframe = document.getElementById("iframe");
206
207          var docOID = docOIDs.shift();
208          console.log("doc oid = " + docOID);
209         
210          var url;
211          if (gs2) {
212              url = "library.cgi";
213              url += "?c="+collect +"&a=d&d=" + docOID;
214         }
215         else {
216              url = "dev";
217              url += "?c="+collect +"&a=d&d=" + docOID;
218              url += "&ed=1&dt=hierarchy";
219         }
220         
221          iframe.src = url;
222         
223          var progressbar = document.getElementById("progressbar");
224          progressbar.style.display = "block";
225
226
227          return false; });
228     });
229     
230      function pageLoaded()
231      {
232      var iframe = document.getElementById("iframe");     
233     
234      if (iframe.src) {
235          if (iframe.style.display != "block") {
236          iframe.height = "90%";
237          iframe.style.display = "block";
238          }
239
240          var iframeDoc;
241          if ( iframe.contentDocument )
242          { /* FF */
243            iframeDoc = iframe.contentDocument;
244          }
245          else if ( iframe.contentWindow )
246          { /* IE */
247            iframeDoc = iframe.contentWindow.document;
248          }
249
250          var frameID = (numDocOIDs - docOIDs.length);
251          var progressPercent = frameID/numDocOIDs * 100;
252
253          \$(function() {
254          \$('#progressbar').progressbar({ value: progressPercent })
255         });
256
257          var expFrameTree = htmlToExpeditee(iframeDoc.body);
258          var expFrame = JSON.stringify(expFrameTree);
259          //alert(expFrame);
260
261          var collect = document.getElementById("collect").value;
262          var site    = document.getElementById("site").value;
263
264          var url = "cgi-bin/html-to-expeditee.pl";
265          var params = "c=" + collect;
266          if (site.match(/\\w/)) {
267        params += "&site=" + site;
268          }
269          params += "&a=generate-frame&fn=" + frameID;
270          params += "&json=" + escape(expFrame);
271
272          var clHtml = urlPostSync(url,params);
273
274          if (!clHtml.match(/html-to-expeditee saved frame/)) {
275          alert("Error processing url: " + url);
276          }
277
278          if (docOIDs.length>0) {
279          var docOID = docOIDs.shift();
280         
281          // console.log("doc oid = " + docOID);
282
283         // is the following line used ????
284          var cl = document.getElementById("cl").value;
285         
286          var gs2 = $isGSDL2;
287              var url;
288
289              if (gs2) {
290                  url = "library.cgi";
291                  url += "?c="+collect +"&a=d&d=" + docOID;
292              }
293              else {
294                  url = "dev";
295                  url += "?c="+collect +"&a=d&d=" + docOID;
296                  url += "&ed=1&dt=hierarchy";
297              }
298
299          iframe.src = url;
300          }
301          else {
302          var progressbar = document.getElementById("progressbar");
303          progressbar.style.display = "none";
304
305          iframe.style.display = "none";
306          delete iframe.src;
307          }
308      }
309     
310      }
311
312    </script>
313
314    <div id="progressbar" width="100%"
315          style="display: none; margin: 10px; height: 10px;"></div>
316    <div id="workingTraverse" style="display: none"></div>
317
318    <hr style="margin: 10px;">
319
320    <iframe width="100%" id="iframe" style="display: none;"
321            onload="pageLoaded()"></iframe>
322
323  </body>
324</html>
325EOT
326
327
328        print "Content-type:text/html\n\n";
329        print $html_form;
330
331    }
332}
333
334
335
336&main();
Note: See TracBrowser for help on using the browser.