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

raeburn raeburn at source.lon-capa.org
Fri Jan 3 20:34:57 EST 2025


raeburn		Sat Jan  4 01:34:57 2025 EDT

  Modified files:              
    /loncom/interface	londocs.pm 
  Log:
  - Copying files from Course Authoring Space to User's Authoring Space.
    Accommodate case where file has been published to Resource Space, and
    corresponding file in Authoring Space has been modified. 
    - If "Yes" was selected for "Publish copied files" the existing published 
      file in /res for Course Authoring is copied to /res for User Authoring.
  
  
-------------- next part --------------
Index: loncom/interface/londocs.pm
diff -u loncom/interface/londocs.pm:1.716 loncom/interface/londocs.pm:1.717
--- loncom/interface/londocs.pm:1.716	Fri Jan  3 02:10:55 2025
+++ loncom/interface/londocs.pm	Sat Jan  4 01:34:57 2025
@@ -1,7 +1,7 @@
 # The LearningOnline Network
 # Documents
 #
-# $Id: londocs.pm,v 1.716 2025/01/03 02:10:55 raeburn Exp $
+# $Id: londocs.pm,v 1.717 2025/01/04 01:34:57 raeburn Exp $
 #
 # Copyright Michigan State University Board of Trustees
 #
@@ -995,231 +995,159 @@
                                                                    '<span class="LC_filename">'.$desturl.'/'.$subdir.'/'.$file.'</span>').
                                       '</p>'."\n");
                         } elsif (($src ne '') && ($dest ne '')) {
-                            if ($is_course_home) {
-                                if (&File::Copy::copy($src,$dest)) {
-                                    $newfile{$file} = 1;
-                                }
-                            } else {
-                                if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file,$dest) eq 'ok') {
-                                    $newfile{$file} = 1;
-                                }
-                            }
-                            if ($newfile{$file}) {
-                                my $gotmeta;
-                                if ($is_course_home) {
-                                    if ((-e $src.'.meta') && (!-e $dest.'.meta')) {
-                                        if (&File::Copy::copy($src.'.meta',$dest.'.meta')) {
-                                            $gotmeta = 1;
-                                        }
-                                    }
-                                } else {
-                                    if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file.'.meta',$dest.'.meta') eq 'ok') {
-                                        $gotmeta = 1;
+                            my $ressrc = $docroot.$resurl.'/'.$file;
+                            my $ressrcmeta = $ressrc.'.meta';
+                            my ($ext) = ($file =~ /\.(\w+)$/);
+                            my $embstyle=&Apache::loncommon::fileembstyle($ext);
+                            my ($getres,$getresmeta);
+                            if ($respublish) {
+                                if ($path eq '') {
+                                    if ((ref($resfiles{'/'}) eq 'HASH') &&
+                                        (exists($resfiles{'/'}{$fname}))) {
+                                        $getres = 1;
+                                        $getresmeta = 1;
                                     }
+                                } elsif ((ref($resfiles{$path}) eq 'HASH') &&
+                                         (exists($resfiles{$path}{$fname}))) {
+                                    $getres = 1;
+                                    $getresmeta = 1;
                                 }
-                                if ($gotmeta) {
-                                    if (open(my $fh,'<',$dest.'.meta')) {
-                                        my ($output,$now,$setsourceavail);
-                                        $now = time;
-                                        if (($file =~ /\.(xml|html|htm|xhtml|xhtm)$/i) || ($file =~ /$LONCAPA::assess_re/)) {
-                                            $setsourceavail = 1;
-                                        }
-                                        while (my $line=<$fh>) {
-                                            chomp($line);
-                                            if ($line eq "<authorspace>$coursenum:$coursedom</authorspace>") {
-                                                $output .= "<authorspace>$ca:$cd</authorspace>\n";
-                                            } elsif ($line eq '<copyright>custom</copyright>') {
-                                                $output .= "<copyright>$copyright</copyright>\n";
-                                            } elsif ($line =~ m{^<creationdate>\d+</creationdate>$}) {
-                                                $output .= "<creationdate>$now</creationdate>\n";
-                                            } elsif ($line eq "<customdistributionfile>/res/$coursedom/$coursenum/default.rights</customdistributionfile>") {
-                                                $output .= "<customdistributionfile>$customdistfile</customdistributionfile>\n";
-                                            } elsif ($line =~ m{^<sourceavail>(open|closed)</sourceavail>$}) {
-                                                if ($setsourceavail) {
-                                                    $output .= "<sourceavail>$sourceavail</sourceavail>\n";
+                            }
+                            if ($is_course_home) {
+                                my ($needpriv,$needprivmeta);
+                                if ($respublish) {
+                                    if ($getres) {
+                                        if (&Apache::londiff::are_different_files($src,$ressrc)) {
+                                            $needpriv = 1;
+                                            if (&File::Copy::copy($ressrc,$dest)) {
+                                                if ($embstyle eq 'ssi') {
+                                                    &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd);
                                                 }
-                                            } elsif ($line eq "<domain>$coursedom</domain>") {
-                                                $output .= "<domain>$cd</domain>\n";
-                                            } elsif ($line =~ m{^<lastrevisiondate>\d+</lastrevisiondate>$}) {
-                                                $output .= "<lastrevisiondate>$now</lastrevisiondate>\n";
-                                            } elsif ($line =~ m{^<modifyinguser>$match_username:$match_domain</modifyinguser>$}) {
-                                                $output .= "<modifyinguser>$env{'user.name'}:$env{'user.domain'}</modifyinguser>\n";
-                                            } elsif ($line eq "<owner>$coursenum:$coursedom</owner>") {
-                                                $output .= "<owner>$ca:$cd</owner>\n";
-                                            } elsif ($line =~ m{^<dependencies>(.+)</dependencies>$}) {
-                                                my @deps = split(/\s*,\s*/,$1);
-                                                my @newdeps;
-                                                my $changed = 0;
-                                                foreach my $dep (@deps) {
-                                                    if ($dep =~ m{^/res/$coursedom/$coursenum/(.+)$}) {
-                                                        my $rest = $1;
-                                                        push(@newdeps,"/res/$cd/$ca/$rest");
-                                                        $checkdeps{$rest} = 1;
-                                                        $changed ++;
-                                                    } else {
-                                                        push(@newdeps,$dep);
-                                                    }
+                                            }
+                                        } else {
+                                            if (&File::Copy::copy($src,$dest)) {
+                                                $newfile{$file} = 1;
+                                                if ($embstyle eq 'ssi') {
+                                                    &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir);
                                                 }
-                                                if ($changed) {
-                                                    $output .= '<dependencies>'.join(',', at newdeps).'</dependencies>'."\n";
+                                            }
+                                        }
+                                    } else {
+                                        $needpriv = 1;
+                                    }
+                                    if ($getresmeta) {
+                                        if ((-e $src.'.meta') && (!-e $dest.'.meta')) {
+                                            if (&Apache::londiff::are_different_files($src.'.meta',$ressrc.'.meta')) {
+                                                if (&File::Copy::copy($ressrc.'.meta',$dest.'.meta')) {
+                                                    &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright,
+                                                                       $customdistfile,$sourceavail,\%checkdeps);
                                                 }
+                                                $needprivmeta = 1;
                                             } else {
-                                                $output .= "$line\n";
+                                                if (&File::Copy::copy($src.'.meta',$dest.'.meta')) {
+                                                    &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright,
+                                                                       $customdistfile,$sourceavail,\%checkdeps);
+                                                }
                                             }
                                         }
-                                        close($fh);
-                                        if (open(my $fh,'>',$dest.'.meta')) {
-                                            print $fh $output;
-                                            close($fh);
+                                    }
+                                    if ($getres) {
+                                        my $destresfile = $docroot.$destresurl.'/'.$subdir.'/'.$file;
+                                        if (-e $dest) {
+                                            my $output = &Apache::lonpublisher::batchpublish($r,$dest,$destresfile,$nokeyref,1);
                                         }
                                     }
+                                } else {
+                                    $needpriv = 1;
+                                    if ((-e $src.'.meta') && (!-e $dest.'.meta')) {
+                                        $needprivmeta = 1;
+                                    }  
                                 }
-                                my ($ext) = ($file =~ /\.(\w+)$/);
-                                my $embstyle=&Apache::loncommon::fileembstyle($ext);
-                                if ($embstyle eq 'ssi') {
-                                    my $outstring='';
-                                    my $changes = 0;
-                                    my @parser;
-                                    $parser[0]=HTML::LCParser->new($dest);
-                                    $parser[-1]->xml_mode(1);
-                                    my $token;
-                                    while (@parser) {
-                                        while ($token=$parser[-1]->get_token) {
-                                            if ($token->[0] eq 'S') {
-                                                my $tag=$token->[1];
-                                                my $lctag=lc($tag);
-                                                my %parms=%{$token->[2]};
-                                                foreach my $type ('src','href','background','bgimg') {
-                                                    foreach my $key (keys(%parms)) {
-                                                        if ($key =~ /^$type$/i) {
-                                                            next if (($lctag eq 'img') && ($type eq 'src') &&
-                                                                     ($parms{$key} =~ m{^data\:image/gif;base64,}));
-                                                            if ($parms{$key} =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                                $parms{$key} =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/}si;
-                                                                $changes ++;
-                                                            }
-                                                        }
-                                                    }
-                                                }
-                                                # probably a <randomlabel> image type <label>
-                                                # or a <image> tag inside <imageresponse> or <drawimage>
-                                                if (($lctag eq 'label' && defined($parms{'description'}))
-                                                     || ($lctag eq 'image') || ($lctag eq 'import')) {
-                                                    my $next_token=$parser[-1]->get_token();
-                                                    if ($next_token->[0] eq 'T') {
-                                                        $next_token->[1] =~ s/[\n\r\f]+//g;
-                                                        if ($next_token->[1] =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                            $next_token->[1] =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/}si;
-                                                            $changes ++;
-                                                        }
-                                                    }
-                                                    $parser[-1]->unget_token($next_token);
-                                                }
-                                                if ($lctag eq 'applet') {
-                                                    my $havecodebase=0;
-                                                    foreach my $key (keys(%parms)) {
-                                                        if (lc($key) eq 'codebase') {
-                                                            if ($parms{$key} =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                                $parms{$key} =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/}si;
-                                                                $changes ++;
-                                                            }
-                                                            $havecodebase = 1;
-                                                        }
-                                                    }
-                                                    unless ($havecodebase) {
-                                                        foreach my $key (keys(%parms)) {
-                                                            if ($key =~ /(archive|code|object)/i) {
-                                                                if ($parms{$key} =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                                    $parms{$key} =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/si};
-                                                                    $changes ++;
-                                                                }
-                                                            }
-                                                        }
-                                                    }
-                                                }
-                                                my $newparmstring='';
-                                                my $endtag='';
-                                                foreach my $parkey (keys(%parms)) {
-                                                    if ($parkey eq '/') {
-                                                        $endtag=' /';
-                                                    } else {
-                                                        my $quote=($parms{$parkey}=~/\"/?"'":'"');
-                                                        $newparmstring.=' '.$parkey.'='.$quote.$parms{$parkey}.$quote;
+                                if ($needpriv) {
+                                    if (&File::Copy::copy($src,$dest)) {
+                                        $newfile{$file} = 1;
+                                        if ($embstyle eq 'ssi') {
+                                            &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir);
+                                        }
+                                    }
+                                }
+                                if ($needprivmeta) {
+                                    if (&File::Copy::copy($src.'.meta',$dest.'.meta')) {
+                                         &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright,
+                                                            $customdistfile,$sourceavail,\%checkdeps);
+                                    }
+                                }
+                            } else {
+                                my ($needpriv,$needprivmeta);
+                                if ($respublish) {
+                                    if ($getres) {
+                                        &Apache::lonnet::repcopy($docroot.$resurl.'/'.$file);
+                                    }
+                                    if ($getresmeta) {
+                                        &Apache::lonnet::repcopy($docroot.$resurl.'/'.$file.'.meta');
+                                    }
+                                    if (-e $docroot.$resurl.'/'.$file) {
+                                        if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file,$dest) eq 'ok') {
+                                            if (&Apache::londiff::are_different_files($docroot.$resurl.'/'.$file,$dest)) {
+                                                $needpriv = 1;
+                                                if (&File::Copy::copy($docroot.$resurl.'/'.$file,$dest)) {
+                                                    if ($embstyle eq 'ssi') {
+                                                        &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd);
                                                     }
                                                 }
-                                                if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
-                                                $outstring.='<'.$tag.$newparmstring.$endtag.'>';
-                                                if ($lctag eq 'm' || $lctag eq 'answer' || $lctag eq 'display' ||
-                                                    $lctag eq 'tex') {
-                                                    $outstring.=&Apache::lonxml::get_all_text_unbalanced('/'.$lctag,\@parser);
-                                                } elsif ($lctag eq 'script') {
-                                                    if ($parms{'type'} eq 'loncapa/perl') {
-                                                        $outstring.=&Apache::lonxml::get_all_text_unbalanced('/'.$lctag,\@parser);
-                                                    } else {
-                                                        my $needsupdate;
-                                                        my $script = &Apache::lonxml::get_all_text_unbalanced('/'.$lctag,\@parser);
-                                                        if ($script =~ m{\.addMediaSrc\((["'])((?!\1).+)\1\);}) {
-                                                            my $src = $2;
-                                                            if ($src =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                                $needsupdate = 1;
-                                                            }
-                                                        }
-                                                        if ($script =~ /\(document,\s*(['"])script\1,\s*\[([^\]]+)\]\);/s) {
-                                                            my $scriptslist = $2;
-                                                            my $needsupdate = 1;
-                                                            my @srcs = split(/\s*,\s*/,$scriptslist);
-                                                            foreach my $src (@srcs) {
-                                                                if ($src =~ /(["'])(?:(?!\1).)+\.js\1/) {
-                                                                    my $quote = $1;
-                                                                    my ($url) = ($src =~ m/\Q$quote\E([^$quote]+)\Q$quote\E/);
-                                                                    if ($url =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                                        $needsupdate = 1;
-                                                                    }
-                                                                }
-                                                            }
-                                                        }
-                                                        if ($script =~ m{loadScript\(\s*(['"])((?:(?!\1).)+\.js)\1,\s*function}is) {
-                                                            my $src = $2;
-                                                            if ($src =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
-                                                                $needsupdate = 1;
-                                                            }
-                                                        }
-                                                        if ($needsupdate) {
-                                                            $script =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/gsi};
-                                                            $changes ++;
-                                                        }
-                                                        $outstring .= $script;
-                                                    }
+                                            } else {
+                                                if ($embstyle eq 'ssi') {
+                                                    &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir);
                                                 }
-                                            } elsif ($token->[0] eq 'E') {
-                                                if ($token->[2]) {
-                                                    unless ($token->[1] eq 'allow') {
-                                                        $outstring.='</'.$token->[1].'>';
-                                                    }
+                                                $newfile{$file} = 1;
+                                            }
+                                        }
+                                    } else {
+                                        $needpriv = 1;
+                                    }
+                                    if (-e $docroot.$resurl.'/'.$file.'.meta') {
+                                        if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file.'.meta',$dest.'.meta') eq 'ok') {
+                                            if (&Apache::londiff::are_different_files($docroot.$resurl.'/'.$file.'.meta',$dest.'.meta')) {
+                                                $needprivmeta = 1;
+                                                if (&File::Copy::copy($docroot.$resurl.'/'.$file.'.meta',$dest.'.meta')) {
+                                                    &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright,
+                                                                       $customdistfile,$sourceavail,\%checkdeps);
                                                 }
                                             } else {
-                                                $outstring.=$token->[1];
+                                                &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright,
+                                                                   $customdistfile,$sourceavail,\%checkdeps);
                                             }
                                         }
-                                        pop(@parser);
+                                    } else {
+                                        if (!-e $dest.'.meta') {
+                                            $needprivmeta = 1;
+                                        }
                                     }
-                                    if ($changes) {
-                                        if (open(my $fh,'>',$dest)) {
-                                            print $fh $outstring;
-                                            close($fh);
+                                    if ($getres) {
+                                        my $destresfile = $docroot.$destresurl.'/'.$subdir.'/'.$file;
+                                        if (-e $dest) {
+                                            my $output = &Apache::lonpublisher::batchpublish($r,$dest,$destresfile,$nokeyref,1);
+#FIXME publish meta file also?
                                         }
                                     }
+                                } else {
+                                    $needpriv = 1;
+                                    if (!-e $dest.'.meta') {
+                                        $needprivmeta = 1;
+                                    }
                                 }
-                                if ($respublish) {
-                                    my $destresfile = $docroot.$destresurl.'/'.$subdir.'/'.$file;
-                                    if ($path eq '') {
-                                        if ((ref($resfiles{'/'}) eq 'HASH') &&
-                                            (exists($resfiles{'/'}{$fname}))) {
-                                            my $output = &Apache::lonpublisher::batchpublish($r,$dest,$destresfile,$nokeyref,1);
+                                if ($needpriv) {
+                                    if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file,$dest) eq 'ok') {
+                                        if ($embstyle eq 'ssi') {
+                                            &crsres_fixup($dest,$coursenum,$coursedom,$ca,$cd,$subdir);
                                         }
-                                    } elsif ((ref($resfiles{$path}) eq 'HASH') &&
-                                             (exists($resfiles{$path}{$fname}))) {
-                                        my $output = &Apache::lonpublisher::batchpublish($r,$dest,$destresfile,$nokeyref,1);
+                                        $newfile{$file} = 1;
+                                    }
+                                }
+                                if ($needprivmeta) {
+                                    if (&Apache::lonnet::repcopy_crsprivfile($srcurl.'/'.$file.'.meta',$dest.'.meta') eq 'ok') {
+                                        &crsres_fixup_meta($dest,$coursenum,$coursedom,$ca,$cd,$copyright,
+                                                           $customdistfile,$sourceavail,\%checkdeps);
                                     }
                                 }
                             }
@@ -1506,6 +1434,201 @@
     return $output;
 }
 
+sub crsres_fixup_meta {
+    my ($dest,$coursenum,$coursedom,$ca,$cd,$copyright,$customdistfile,$sourceavail,$checkdeps) = @_;
+    return unless (ref($checkdeps) eq 'HASH');
+    if (open(my $fh,'<',$dest.'.meta')) {
+        my ($output,$now,$setsourceavail);
+        $now = time;
+        if (($dest =~ /\.(xml|html|htm|xhtml|xhtm)$/i) || ($dest =~ /$LONCAPA::assess_re/)) {
+            $setsourceavail = 1;
+        }
+        while (my $line=<$fh>) {
+            chomp($line);
+            if ($line eq "<authorspace>$coursenum:$coursedom</authorspace>") {
+                $output .= "<authorspace>$ca:$cd</authorspace>\n";
+            } elsif ($line eq '<copyright>custom</copyright>') {
+                $output .= "<copyright>$copyright</copyright>\n";
+            } elsif ($line =~ m{^<creationdate>\d+</creationdate>$}) {
+                $output .= "<creationdate>$now</creationdate>\n";
+            } elsif ($line eq "<customdistributionfile>/res/$coursedom/$coursenum/default.rights</customdistributionfile>") {
+                $output .= "<customdistributionfile>$customdistfile</customdistributionfile>\n";
+            } elsif ($line =~ m{^<sourceavail>(open|closed)</sourceavail>$}) {
+                if ($setsourceavail) {
+                    $output .= "<sourceavail>$sourceavail</sourceavail>\n";
+                }
+            } elsif ($line eq "<domain>$coursedom</domain>") {
+                $output .= "<domain>$cd</domain>\n";
+            } elsif ($line =~ m{^<lastrevisiondate>\d+</lastrevisiondate>$}) {
+                $output .= "<lastrevisiondate>$now</lastrevisiondate>\n";
+            } elsif ($line =~ m{^<modifyinguser>$match_username:$match_domain</modifyinguser>$}) {
+                $output .= "<modifyinguser>$env{'user.name'}:$env{'user.domain'}</modifyinguser>\n";
+            } elsif ($line eq "<owner>$coursenum:$coursedom</owner>") {
+                $output .= "<owner>$ca:$cd</owner>\n";
+            } elsif ($line =~ m{^<dependencies>(.+)</dependencies>$}) {
+                my @deps = split(/\s*,\s*/,$1);
+                my @newdeps;
+                my $changed = 0;
+                foreach my $dep (@deps) {
+                    if ($dep =~ m{^/res/$coursedom/$coursenum/(.+)$}) {
+                        my $rest = $1;
+                        push(@newdeps,"/res/$cd/$ca/$rest");
+                        $checkdeps->{$rest} = 1;
+                        $changed ++;
+                    } else {
+                        push(@newdeps,$dep);
+                    }
+                }
+                if ($changed) {
+                    $output .= '<dependencies>'.join(',', at newdeps).'</dependencies>'."\n";
+                }
+            } else {
+                $output .= "$line\n";
+            }
+        }
+        close($fh);
+        if (open(my $fh,'>',$dest.'.meta')) {
+            print $fh $output;
+            close($fh);
+        }
+    }
+}
+
+sub crsres_fixup {
+    my ($dest,$coursenum,$coursedom,$ca,$cd,$subdir) = @_;
+    my $outstring='';
+    my $changes = 0;
+    my @parser;
+    $parser[0]=HTML::LCParser->new($dest);
+    $parser[-1]->xml_mode(1);
+    my $token;
+    while (@parser) {
+        while ($token=$parser[-1]->get_token) {
+            if ($token->[0] eq 'S') {
+                my $tag=$token->[1];
+                my $lctag=lc($tag);
+                my %parms=%{$token->[2]};
+                foreach my $type ('src','href','background','bgimg') {
+                    foreach my $key (keys(%parms)) {
+                        if ($key =~ /^$type$/i) {
+                            next if (($lctag eq 'img') && ($type eq 'src') &&
+                                     ($parms{$key} =~ m{^data\:image/gif;base64,}));
+                            if ($parms{$key} =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                                $parms{$key} =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/}si;
+                                $changes ++;
+                            }
+                        }
+                    }
+                }
+                # probably a <randomlabel> image type <label>
+                # or a <image> tag inside <imageresponse> or <drawimage>
+                if (($lctag eq 'label' && defined($parms{'description'}))
+                    || ($lctag eq 'image') || ($lctag eq 'import')) {
+                    my $next_token=$parser[-1]->get_token();
+                    if ($next_token->[0] eq 'T') {
+                        $next_token->[1] =~ s/[\n\r\f]+//g;
+                        if ($next_token->[1] =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                            $next_token->[1] =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/}si;
+                            $changes ++;
+                        }
+                    }
+                    $parser[-1]->unget_token($next_token);
+                }
+                if ($lctag eq 'applet') {
+                    my $havecodebase=0;
+                    foreach my $key (keys(%parms)) {
+                        if (lc($key) eq 'codebase') {
+                            if ($parms{$key} =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                                $parms{$key} =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/}si;
+                                $changes ++;
+                            }
+                            $havecodebase = 1;
+                        }
+                    }
+                    unless ($havecodebase) {
+                        foreach my $key (keys(%parms)) {
+                            if ($key =~ /(archive|code|object)/i) {
+                                if ($parms{$key} =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                                    $parms{$key} =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/si};
+                                    $changes ++;
+                                }
+                            }
+                        }
+                    }
+                }
+                my $newparmstring='';
+                my $endtag='';
+                foreach my $parkey (keys(%parms)) {
+                    if ($parkey eq '/') {
+                        $endtag=' /';
+                    } else {
+                        my $quote=($parms{$parkey}=~/\"/?"'":'"');
+                        $newparmstring.=' '.$parkey.'='.$quote.$parms{$parkey}.$quote;
+                    }
+                }
+                if (!$endtag) { if ($token->[4]=~m:/>$:) { $endtag=' /'; }; }
+                $outstring.='<'.$tag.$newparmstring.$endtag.'>';
+                if ($lctag eq 'm' || $lctag eq 'answer' || $lctag eq 'display' ||
+                    $lctag eq 'tex') {
+                    $outstring.=&Apache::lonxml::get_all_text_unbalanced('/'.$lctag,\@parser);
+                } elsif ($lctag eq 'script') {
+                    if ($parms{'type'} eq 'loncapa/perl') {
+                        $outstring.=&Apache::lonxml::get_all_text_unbalanced('/'.$lctag,\@parser);
+                    } else {
+                        my $needsupdate;
+                        my $script = &Apache::lonxml::get_all_text_unbalanced('/'.$lctag,\@parser);
+                        if ($script =~ m{\.addMediaSrc\((["'])((?!\1).+)\1\);}) {
+                            my $src = $2;
+                            if ($src =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                                $needsupdate = 1;
+                            }
+                        }
+                        if ($script =~ /\(document,\s*(['"])script\1,\s*\[([^\]]+)\]\);/s) {
+                            my $scriptslist = $2;
+                            my @srcs = split(/\s*,\s*/,$scriptslist);
+                            foreach my $src (@srcs) {
+                                if ($src =~ /(["'])(?:(?!\1).)+\.js\1/) {
+                                    my $quote = $1;
+                                    my ($url) = ($src =~ m/\Q$quote\E([^$quote]+)\Q$quote\E/);
+                                    if ($url =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                                        $needsupdate = 1;
+                                    }
+                                }
+                            }
+                        }
+                        if ($script =~ m{loadScript\(\s*(['"])((?:(?!\1).)+\.js)\1,\s*function}is) {
+                            my $src = $2;
+                            if ($src =~ m{^\Q/res/$coursedom/$coursenum/\E}si) {
+                                $needsupdate = 1;
+                            }
+                        }
+                        if ($needsupdate) {
+                            $script =~ s{^\Q/res/$coursedom/$coursenum/\E}{/res/$cd/$ca/$subdir/gsi};
+                            $changes ++;
+                        }
+                        $outstring .= $script;
+                    }
+                }
+            } elsif ($token->[0] eq 'E') {
+                if ($token->[2]) {
+                    unless ($token->[1] eq 'allow') {
+                        $outstring.='</'.$token->[1].'>';
+                    }
+                }
+            } else {
+                $outstring.=$token->[1];
+            }
+        }
+        pop(@parser);
+    }
+    if ($changes) {
+        if (open(my $fh,'>',$dest)) {
+            print $fh $outstring;
+            close($fh);
+        }
+    }
+}
+
 sub group_import {
     my ($coursenum, $coursedom, $folder, $container, $caller, $ltitoolsref, @files) = @_;
     my ($donechk,$allmaps,%hierarchy,%titles,%addedmaps,%removefrommap,


More information about the LON-CAPA-cvs mailing list