[LON-CAPA-cvs] cvs: loncom /interface londocs.pm /lonnet/perl lonnet.pm

raeburn lon-capa-cvs@mail.lon-capa.org
Tue, 26 Jul 2005 13:30:36 -0000


raeburn		Tue Jul 26 09:30:36 2005 EDT

  Modified files:              
    /loncom/interface	londocs.pm 
    /loncom/lonnet/perl	lonnet.pm 
  Log:
  Re-enabling Export IMS button.  Embedded media in html pages and problem files will now be included in archive.
  
  
Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.196 loncom/interface/londocs.pm:1.197
--- loncom/interface/londocs.pm:1.196	Sun Jul 24 22:35:29 2005
+++ loncom/interface/londocs.pm	Tue Jul 26 09:30:34 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.196 2005/07/25 02:35:29 raeburn Exp $
+# $Id: londocs.pm,v 1.197 2005/07/26 13:30:34 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -249,7 +249,6 @@
 # ------------------------------------------------------ Generate "export" button
 
 sub exportbutton {
-    return '';
     return '</td><td bgcolor="#DDDDCC">'.
             '<input type="submit" name="exportcourse" value="'.
             &mt('Export Course to IMS').'" />'.
@@ -748,11 +747,13 @@
         if ($copiedfile = Apache::File->new('>'.$destination)) {
             my $content;
             if ($caller eq 'resource') {
-                $content = &Apache::lonnet::getfile('/home/httpd/html/res/'.$url);
+                my $respath =  $Apache::lonnet::perlvar{'lonDocRoot'}.'/res';
+                my $filepath = &Apache::lonnet::filelocation($respath,$url);
+                $content = &Apache::lonnet::getfile($filepath);
                 if ($content eq -1) {
                     $$message = 'Could not copy file '.$filename;
                 } else {
-                    &extract_media($content,$count,$tempexport,$href,'resource');
+                    &extract_media($url,$cdom,$cnum,\$content,$count,$tempexport,$href,$message,'resource');
                     $repstatus = 'ok';
                 }
             } elsif ($caller eq 'uploaded' || $caller eq 'templateupload') {
@@ -760,10 +761,10 @@
                 $repstatus = &Apache::lonnet::getuploaded('GET',$url,$cdom,$cnum,\$content,$rtncode);
                 if ($repstatus eq 'ok') {
                     if ($url =~ /\.html?$/i) {
-                        &extract_media(\$content,$count,$tempexport,$href,'uploaded');
+                        &extract_media($url,$cdom,$cnum,\$content,$count,$tempexport,$href,$message,'uploaded');
                     }
                 } else {
-                    $$message = 'Could not render '.$url.' server message - '.$rtncode;
+                    $$message = 'Could not render '.$url.' server message - '.$rtncode."<br />\n";
                 }
             } elsif ($caller eq 'noedit') {
 # Need to render the resource without the LON-CAPA Internal header and the Post discussion footer, and then set $content equal to this. 
@@ -775,10 +776,10 @@
             }
             close($copiedfile);
         } else {
-            $$message = 'Could not open destination file for '.$filename."\n";
+            $$message = 'Could not open destination file for '.$filename."<br />\n";
         }
     } else {
-        $$message = 'Could not determine name of file for '.$symb."\n";
+        $$message = 'Could not determine name of file for '.$symb."<br />\n";
     }
     if ($repstatus eq 'ok') {
         $content_name = $count.'/'.$filename;
@@ -787,11 +788,66 @@
 }
 
 sub extract_media {
-    my ($content,$count,$tempexport,$href,$caller) = @_;
-# @$href will contain path to any embedded resources in the content.
-# For LON-CAPA problems this would be images. applets etc. 
-# For uploaded HTML files this would be images etc.
-# paths will be in the form $count/res/$file, and urls in the $content will be rewritten with the new paths. 
+    my ($url,$cdom,$cnum,$content,$count,$tempexport,$href,$message,$caller) = @_;
+    my %allfiles = ();
+    my %codebase = ();
+    $url =~ s#([^/]+)$##;
+    &Apache::lonnet::extract_embedded_items(undef,undef,\%allfiles,\%codebase,$content);
+    foreach my $embed_file (keys(%allfiles)) {
+        my $filename;
+        if ($embed_file =~ m#([^/]+)$#) {
+            $filename = $1;
+        } else {
+            $filename = $embed_file;
+        }
+        my $newname = 'res/'.$filename;
+        my ($rtncode,$embed_content,$repstatus);
+        my $embed_url;
+        if ($embed_file =~ m-^/-) {
+            $embed_url = $embed_file;           # points to absolute path
+        } else {
+            if ($embed_file =~ m-https?://-) {
+                next;                           # points to url
+            } else {
+                $embed_url = $url.$embed_file;  # points to relative path
+            }
+        }
+        if ($caller eq 'resource') {
+            my $respath =  $Apache::lonnet::perlvar{'lonDocRoot'}.'/res';  
+            my $embed_path = &Apache::lonnet::filelocation($respath,$embed_url); 
+            $embed_content = &Apache::lonnet::getfile($embed_path);
+            unless ($embed_content eq -1) {
+                $repstatus = 'ok';
+            }
+        } elsif ($caller eq 'uploaded') {
+            
+            $repstatus = &Apache::lonnet::getuploaded('GET',$embed_url,$cdom,$cnum,\$embed_content,$rtncode);
+        }
+        if ($repstatus eq 'ok') {
+            my $destination = $tempexport.'/resources/'.$count.'/res';
+            if (!-e "$destination") {
+                mkdir($destination,0755);
+            }
+            $destination .= '/'.$filename;
+            my $copiedfile;
+            if ($copiedfile = Apache::File->new('>'.$destination)) {
+                print $copiedfile $embed_content;
+                push @{$href}, .'resources/'.$count.'/res/'.$filename;
+                my $attrib_regexp = '';
+                if (@{$allfiles{$embed_file}} > 1) {
+                    $attrib_regexp = join('|',@{$allfiles{$embed_file}});
+                } else {
+                    $attrib_regexp = $allfiles{$embed_file}[0];
+                }
+                $$content =~ s#($attrib_regexp\s*=\s*['"]?)\Q$embed_file\E(['"]?)#$1$newname$2#gi;
+                if ($caller eq 'resource' && $url =~ /\.(problem|library)$/) {
+                    $$content =~ s#\Q$embed_file\E#$newname#gi;
+                }
+            }
+        } else {
+            $$message .= 'replication of embedded file - '.$embed_file.' in '.$url.' failed, reason -'.$rtncode."<br />\n";
+        }
+    }
     return;
 }
 
Index: loncom/lonnet/perl/lonnet.pm
diff -u loncom/lonnet/perl/lonnet.pm:1.647 loncom/lonnet/perl/lonnet.pm:1.648
--- loncom/lonnet/perl/lonnet.pm:1.647	Mon Jul 25 14:47:38 2005
+++ loncom/lonnet/perl/lonnet.pm	Tue Jul 26 09:30:34 2005
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # TCP networking package
 #
-# $Id: lonnet.pm,v 1.647 2005/07/25 18:47:38 raeburn Exp $
+# $Id: lonnet.pm,v 1.648 2005/07/26 13:30:34 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -1368,7 +1368,7 @@
 }
 
 sub extract_embedded_items {
-    my ($filepath,$file,$allfiles,$codebase) = @_;
+    my ($filepath,$file,$allfiles,$codebase,$content) = @_;
     my @state = ();
     my %javafiles = (
                       codebase => '',
@@ -1379,11 +1379,19 @@
                       src => '',
                       movie => '',
                      );
-    my $p = HTML::LCParser->new($filepath.'/'.$file);
+    my $p;
+    if ($content) {
+        $p = HTML::LCParser->new($content);
+    } else {
+        $p = HTML::LCParser->new($filepath.'/'.$file);
+    }
     while (my $t=$p->get_token()) {
 	if ($t->[0] eq 'S') {
 	    my ($tagname, $attr) = ($t->[1],$t->[2]);
 	    push (@state, $tagname);
+            if (lc($tagname) eq 'allow') {
+                &add_filetype($allfiles,$attr->{'src'},'src');
+            }
 	    if (lc($tagname) eq 'img') {
 		&add_filetype($allfiles,$attr->{'src'},'src');
 	    }