#!/web/soft/iw-home/TeamSite/iw-perl/bin/iwperl # load modules # restricts improper syntax and helps to debug error use strict; # require variables to be declared use diagnostics; # expand warnings (-w) expanation BEGIN { use mi0::LogManager; mi0::LogManager::init(level=>'info'); mi0::LogManager::info("********************************"); mi0::LogManager::info("Starting mi0_mgi_suite_cloning.cgi"); }; use TeamSite::Config; use CGI qw(:standard); # Include standard HTML and CGI +functions use CGI::Carp qw(fatalsToBrowser); # Send error messages to b +rowser use File::Find; use File::Copy; use File::Path; # global vars my $IWHOME = TeamSite::Config::iwgethome(); my $IWMOUNT = TeamSite::Config::iwgetmount(); mi0::LogManager::info("IWHOME: $IWHOME"); mi0::LogManager::info("IWMOUNT: $IWMOUNT"); # The location of the PERL executable my $iwperl = "$IWHOME/iw-perl/bin/iwperl -pi.bak -e"; # The location of the TeamSite extended attributes utility my $iwextattr = "$IWHOME/bin/iwextattr"; my $cgi=new CGI(); my $areapath = $cgi->param('area_path'); my $user = $cgi->param('user_name'); my $workarea = $areapath; $workarea=~s|^/iwmnt(.*)|$1|; mi0::LogManager::info("areapath: $areapath"); mi0::LogManager::info("user: $user"); mi0::LogManager::info("workarea: $workarea"); my @root= ( ## Workarea ### "$areapath", ); # array of starting directory my %duplicates; # remove duplicate values my $cmd = ""; my $unlinkStatus = 0; # file delete status my @PASSTHROUGH; # get all branches under /default/main/mgi_suite/sites/ opendir(DIR, "/iwmnt/default/main/mgi_suite/sites/"); my @cloningBranches = grep(/\.*$/,readdir(DIR)); closedir(DIR); @cloningBranches = sort @cloningBranches; my $copyFromBranch = $& if $workarea =~ m|\w+$|; my $cnt = 0; my (@pageExist,@htmlFiles,@selectedComponents,@copyFiles,@availableClo +ningBranches) = qw(); foreach my $dir (@cloningBranches) { next if ($dir =~ m|\.$| || $dir =~ m|\.\.$| || $dir =~ m|STAGING| +|| $dir =~ m|WORKAREA| || $dir =~ m|EDITION| || $dir =~ m|$copyFromBr +anch|); $cnt++; push(@availableCloningBranches, $dir); mi0::LogManager::info("branch $cnt: $dir"); } # walk (search) through directories and files my %options = (preprocess =>\&new_dir, wanted=>\&wanted); find ( \%options, @root); #Start HTML print $cgi->header(); buildHeader("Copy pages, modules and components from current branch ($ +copyFromBranch) to 1 or more available branches"); my $action = "/iw-bin/iw_cgi_wrapper.cgi/mi0_mgi_suite_cloning.cgi" ; print $cgi->startform(-action=>$action),map({hidden($_)} @PASSTHROUGH) +,; print $cgi->hidden(-name=>'formRequest',); performFormRequest(); passparams(); print $cgi->endform; print "<br />"; print $cgi->end_html; ###################################################################### +### # process files per directory ###################################################################### +### sub new_dir { my @files = @_; # the readdir() output # contains both (files and directories) # find html files under pages directory if ($File::Find::dir =~ m|pages|) { # sort files @files = sort(@files); # process files and directories foreach my $name (@files) { # scan files if (-e $name) { # skipped if not html file next if ($name !~ m|\.html$|); # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/Pr +imaryDCR "$name"`; # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/ +PrimaryDocumentType "$name"`; mi0::LogManager::info("page_generated_filename: $File: +:Find::dir/$name") if (($templateType ne "" && $templateType !~ m|mi\ +_navigation\/navigation|) && $primaryDCR ne ""); push (@pageExist, "$File::Find::dir/$name") if (($temp +lateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && +$primaryDCR ne ""); push (@htmlFiles, "$name"."_PAGE") if (($templateType +ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryD +CR ne ""); # Get full path of primaryDCR my $primaryDCR_path_name = $& if $File::Find::dir =~ m +|.*WORKAREA\/\w+\/|; my $workAreaPath = $primaryDCR_path_name; $primaryDCR_path_name = $primaryDCR_path_name."templat +edata/".$templateType."/data/".$primaryDCR; # mi0::LogManager::info("page_primaryDCR: $primaryDCR_ +path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation +|); push (@pageExist, "$primaryDCR_path_name") if ($primar +yDCR_path_name !~ m|mi\_navigation\/navigation|); if ($primaryDCR eq "sites.individual_testing.pages.2_c +olumn_hero_banner.ami_workshop1_2_col_with_hero") { my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$pri +maryDCR_path_name cannot be copied: $!"; open(WEBFILE, $bakFile) || die "can't open $bakFil +e: $!"; # read file my @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\/components\/|g) { $cmd = "$iwperl \"s|\/sites|\n\/sites|g\" +\"$bakFile\""; # replace cmd `$cmd`; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \" +$bakFile\""; # replace cmd `$cmd`; } } open(WEBFILE, $bakFile) || die "can't open $bakFil +e: $!"; # read file @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { chomp $line; if ($line =~ m|^\/sites\/.*|) { push(@htmlFiles, $line); if ($line =~ m|.*\/modules\/.*|) { # mi0::LogManager::info("module: $line +"); modulesComponentsExist($workAreaPath,$ +line); } else { # mi0::LogManager::info("component: $l +ine"); modulesComponentsExist($workAreaPath,$ +line); } } } } } } } return @files; #return the list to continue processing } # End of sub new_dir { ###################################################################### +### # needed for sytax, extra's for getting size etc. if needed ###################################################################### +### sub wanted { # not used here, but dummy sub is needed for syntax # this routine is called for each file/directory # if we wanted to do something special for each one # maybe get a size or whatever... # the full path name of this file would be in $File::Find::name } # End of sub wanted { ###################################################################### +### # verify if modules or components exist ###################################################################### +### sub modulesComponentsExist ($$) { my $workAreaPath = $_[0]; my $name = $_[1]; $name = $workAreaPath.$name; my $fileChanged = "no"; # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/PrimaryDCR "$n +ame"`; next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/PrimaryDocum +entType "$name"`; # Get full path of primaryDCR my $primaryDCR_path_name = $workAreaPath."templatedata/".$template +Type."/data/".$primaryDCR; if ($name =~ m|.*module.*|) { $name =~ s|\/\/|\/|g; mi0::LogManager::info("module_generated_filename: $name") if ( +($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation +|) && $primaryDCR ne ""); push (@pageExist, "$name") if (($templateType ne "" && $templa +teType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); + my $module = $& if $name =~ m|\w+.html|; push (@htmlFiles, "$module"."_MODULE") if (($templateType ne " +" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR n +e ""); # mi0::LogManager::info("module_primaryDCR: $primaryDCR_path_n +ame") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@pageExist, "$primaryDCR_path_name") if ($primaryDCR_pat +h_name !~ m|mi\_navigation\/navigation|); } else { $name =~ s|\/\/|\/|g; mi0::LogManager::info("component_generated_filename: $name") i +f (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigat +ion|) && $primaryDCR ne ""); push (@pageExist, "$name") if (($templateType ne "" && $templa +teType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); + my $component = $& if $name =~ m|\w+.html|; push (@htmlFiles, "$component"."_COMPONENT") if (($templateTyp +e ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primar +yDCR ne ""); # mi0::LogManager::info("component_primaryDCR: $primaryDCR_pat +h_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@pageExist, "$primaryDCR_path_name") if ($primaryDCR_pat +h_name !~ m|mi\_navigation\/navigation|); } my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path_nam +e cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read +file my @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\/components\/|g) { $cmd = "$iwperl \"s|\/sites|\n\/sites|g\" \"$bakFile\""; # + replace cmd `$cmd`; $fileChanged = "yes"; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bakFile\""; # r +eplace cmd `$cmd`; $fileChanged = "yes"; } } open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read +file @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { chomp $line; if ($line =~ m|.*\/sites\/.*|) { push(@htmlFiles, $line); if ($line =~ m|.*module.*|) { mi0::LogManager::info("module2: $line"); $name = $line; return $name; } else { my $subComponent = $& if $line =~ m|sites.*|; $name = $subComponent; $name = $workAreaPath.$name; # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/Pr +imaryDCR "$name"`; next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/ +PrimaryDocumentType "$name"`; mi0::LogManager::info("component_generated_filename: $ +name") if (($templateType ne "" && $templateType !~ m|mi\_navigation\ +/navigation|) && $primaryDCR ne ""); my $component = $& if $name =~ m|\w+.html|; push (@pageExist, "$name") if (($templateType ne "" && + $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne "" +); push (@htmlFiles, "$component"."_COMPONENT") if (($tem +plateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && + $primaryDCR ne ""); # Get full path of primaryDCR $primaryDCR_path_name = $workAreaPath."templatedata/". +$templateType."/data/".$primaryDCR; # mi0::LogManager::info("component_primaryDCR: $primar +yDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navig +ation|); push (@pageExist, "$primaryDCR_path_name") if ($primar +yDCR_path_name !~ m|mi\_navigation\/navigation|); my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primary +DCR_path_name cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $ +!"; # read file my @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\<componentID\>|g) { $cmd = "$iwperl \"s|\<componentID\>|\<componen +tID\>\n|g\" \"$bakFile\""; # replace cmd `$cmd`; $fileChanged = "yes"; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bak +File\""; # replace cmd `$cmd`; $fileChanged = "yes"; } } } } } if ($fileChanged eq "yes") { $unlinkStatus = unlink("$bakFile"); # delete .bak file created + from replace cmd $unlinkStatus = unlink("$bakFile.bak"); # delete .bak file cre +ated from replace cmd if ($unlinkStatus == 1) { # mi0::LogManager::info("Deleted (unlinked) .bak file succ +essfully: $bakFile"); # mi0::LogManager::info("Deleted (unlinked) .bak file succ +essfully: $bakFile.bak"); } else { mi0::LogManager::info("Failed to delete - '$bakFile': <$!> +. Received $unlinkStatus instead of 1."); mi0::LogManager::info("Failed to delete - '$bakFile.bak': +<$!>. Received $unlinkStatus instead of 1."); } } } # End of sub modulesComponentsExist { ###################################################################### +### # build header for form ###################################################################### +### sub buildHeader { my ($title) = @_ ; my $JSCRIPT = buildJavaScript(); my $HEADER =<<HEADEND; <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>$title</title> <SCRIPT LANGUAGE="JavaScript" SRC="/iw/mi_custom/CalendarPopup.js">< +/SCRIPT> <script type="text/javascript">$JSCRIPT</script> <link rel="stylesheet" TYPE="text/css" HREF="/iw-cc/base/styles/iw.c +ss" /> <link rel="stylesheet" TYPE="text/css" HREF="/iw-cc/base/styles/cust +om.css" /> <link rel="stylesheet" TYPE="text/css" HREF="/iw-cc/ccpro/styles/cus +tom.css" /> </head> <body bgcolor="#FFFFFF"> <table cellspacing=3 cellpadding=3 class="iw-base-heading" style="wi +dth:100%;margin-bottom:4px;"> <tr valign='top' width='100%'> <td class="iw-base-heading-title">$t +itle</td></tr> </table> HEADEND print $HEADER; } ###################################################################### +### # build javascript for header ###################################################################### +### sub buildJavaScript { my $JSCRIPT = <<JSEND; var x = 0; var colour = new initArray("#ffffff","#ffdfe2","#ffa6ae","#ff717e","#f +f3e51","#ff0000","#ff3e51","#ff717e","#ffa6ae","#ffdfe2","#ffffff"); function chcolor(layer){ findObj(layer).style.color = colour[x]; (x < colour.length-1) ? x++ : x = 0; } function initArray() { this.length = initArray.arguments.length; for (var i = 0; i < this.length; i++) { this[i] = initArray.arguments[i]; } } // RESIZE WINDOW window.resizeTo(700,700); function findObj(n, d) { var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) { d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p); } if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n]; for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i] +.document); if(!x && document.getElementById) x=document.getElementById(n); retu +rn x; } function formCancel() { formProcess("cancel"); } function formComplete() { formProcess("complete"); } function formSubmit() { formProcess("continue"); } function formStart() { formProcess(""); } function formProcess(process) { findObj('formRequest').value = process; document.forms[0].submit(); } var x = 0; var colour = new initArray("#ffffff","#ffdfe2","#ffa6ae","#ff717e","#f +f3e51","#ff0000","#ff3e51","#ff717e","#ffa6ae","#ffdfe2","#ffffff"); function chcolor(layer){ findObj(layer).style.color = colour[x]; (x < colour.length-1) ? x++ : x = 0; } function initArray() { this.length = initArray.arguments.length; for (var i = 0; i < this.length; i++) { this[i] = initArray.arguments[i]; } } function hideDiv(divName) { findObj(divName).style.visibility="hidden"; } JSEND return $JSCRIPT; } ###################################################################### +### # Create form to be processed ###################################################################### +### sub performFormRequest { my $formRequest=$cgi->param('formRequest'); if ($formRequest eq "cancel") { print $cgi->script('window.close()'); } if ($areapath eq ""){ print "<br /><i><strong>Please Navigate to the WORKAREA that you +want to copy pages, modules or components to</strong></i><br />"; } else { if ($formRequest eq '') { print "<br />"; getAvailableComponents(@htmlFiles); getAvailableBranches(@availableCloningBranches); buildButton("Submit","formSubmit()"); } if ($formRequest eq "continue") { getSelections(); } buildButton("Close","formCancel()"); } } ###################################################################### +### # build submit and close buttons ###################################################################### +### sub buildButton { my ($buttonValue, $buttonJS) = @_ ; my $BUTTON = '<table cellspacing="0" cellpadding="0" border="0">'; $BUTTON .= '<tr>'; $BUTTON .= ' <td width="1"><img src="/iw-cc/base/images/dialog_b +tn_left.gif"/></td>'; $BUTTON .= ' <td nowrap valign="middle" align="middle" backgroun +d="/iw-cc/base/images/dialog_btn_mid.gif">'; $BUTTON .= ' <a href="javascript:'.$buttonJS.';" class="iw-base +-actionlist-link">'; $BUTTON .= $buttonValue; $BUTTON .= ' </a>'; $BUTTON .= ' </td>'; $BUTTON .= ' <td width="1"><img src="/iw-cc/base/images/dialog_b +tn_right.gif"/></td>'; $BUTTON .= ' </tr>'; $BUTTON .= ' </table>'; print $BUTTON; } ###################################################################### +########################### # Show available pages, modules and components to copy ###################################################################### +########################### sub getAvailableComponents { my $checked = " =''"; my $componentsHeader.=qq(<b>Select html files (pages, modules and +components) to copy from current branch to selected branches? </b>\n) +; $componentsHeader.=qq(<br /><br />); $componentsHeader.=qq(<table>); $componentsHeader.=qq(<tr><td><b>pages</b></td><td><b>modules/comp +onents</b></td></tr>); print $componentsHeader if !$duplicates { $componentsHeader }++; + my $cnt = 0; my $tempcount = 0; foreach my $file (@htmlFiles) { if ($file =~ m|_PAGE$|g) { $file =~ s|_PAGE$||g; $cnt++; if ($cnt < 10) { $cnt = " $cnt"; } elsif ($cnt < 10) { $cnt = " $cnt"; } else { $cnt = "$cnt"; } print "<tr><td><label>$cnt)<input type=\"checkbox\" $check +ed id=\"$file\" name=\"html_files\" value=\"$file\" />page: $file</la +bel></td>\n" if !$duplicates {$file}++; } if ($file =~ m|_MODULE$|g) { $file =~ s|_MODULE$||g; print "<tr><td></td><td><label><input type=\"checkbox\" $c +hecked id=\"$file\" name=\"html_files\" value=\"$file\" />module: $fi +le</label></td></tr><tr>\n" if !$duplicates {$file}++; } if ($file =~ m|_COMPONENT$|g) { $file =~ s|_COMPONENT$||g; print "<tr><td></td><td><label><input type=\"checkbox\" $c +hecked id=\"$file\" name=\"html_files\" value=\"$file\" />component: +$file</label></td></tr>\n" if !$duplicates {$file}++; } } } ###################################################################### +########################################### # Show available branches to receive copied files ###################################################################### +########################### sub getAvailableBranches { print "</table><br /><br /><b><a name=\"branches\">Select Branches +</a> below to copy selected pages, modules and components? </b>\n"; print "<br /><br />"; print '<table><tr>'; my $cnt = 0; my $tempcount = 0; my $checked = " =''"; my $branch_value = "not valid"; foreach my $branch (@availableCloningBranches) { if (!$duplicates { $branch }++) { print "<td><label><input type=\"checkbox\" $checked id=\"$ +branch\" name=\"branch\" value=\"$branch\" />$branch</label></td>\n"; $tempcount++; $branch_value = "valid"; } if ($tempcount > 4 && $branch_value eq "valid") { print "</tr><tr>"; $tempcount = 0; } if ($branch eq $availableCloningBranches[$#availableCloningBra +nches] && $branch_value eq "valid") { # last branch in array print "</tr>\n"; } } print "</table>"; print "<br />"; print '<script type="text/javascript">'; print checkUncheckAll(); print checkUncheckAllPages(); print checkUncheckAllModules(); print checkUncheckAllComponents(); print checkUncheckAllBranches(); print "</script></td></tr><tr><td><a class='iw-base-link' href='#' + onclick='uncheckAll();'>Uncheck All</a></td> | <td><a class='iw-base +-link' href='#' onclick='checkAll();'>Check All</a></td><b> || </b><t +d><a class='iw-base-link' href='#' onclick='uncheckAllPages();'>Unche +ck All Pages</a></td> | <td><a class='iw-base-link' href='#' onclick= +'checkAllPages();'>Check All Pages</a></td><b> || </b><td><a class='i +w-base-link' href='#' onclick='uncheckAllModules();'>Uncheck All Modu +les</a></td> | <td><a class='iw-base-link' href='#' onclick='checkAll +Modules();'>Check All Modules</a></td><b> || </b><td><a class='iw-bas +e-link' href='#' onclick='uncheckAllComponents();'>Uncheck All Compon +ents</a></td> | <td><a class='iw-base-link' href='#' onclick='checkAl +lComponents();'>Check All Components</a></td><b> || </b><td><a class= +'iw-base-link' href='#branches' onclick='uncheckAllBranches();'>Unche +ck All Branches</a></td> | <td><a class='iw-base-link' href='#branche +s' onclick='checkAllBranches();'>Check All Branches</a></td></tr>"; print "<br /><br />"; } ###################################################################### +####### # build functions for CheckAll and UncheckAll links for available sele +ctions ###################################################################### +####### sub checkUncheckAll { my $checkAll = "function checkAll() {\n"; my $uncheckAll = "function uncheckAll() {\n"; foreach my $name (@htmlFiles) { if ($name =~ m|.*_PAGE$|g || $name =~ m|.*_MODULE$|g || $name =~ m +|.*_COMPONENT$|g) { $name =~ s|_PAGE$||g; $name =~ s|_MODULE$||g; $name =~ s|_COMPONENT$||g; $checkAll .= "findObj('$name').checked = true;\n"; $uncheckAll .= "findObj('$name').checked = false;\n"; } } foreach my $name (@availableCloningBranches) { $checkAll .= "findObj('$name').checked = true;\n"; $uncheckAll .= "findObj('$name').checked = false;\n"; } $checkAll .="}\n"; $uncheckAll .="}\n"; return $checkAll.$uncheckAll; } ###################################################################### +#################### # build functions for CheckAllPages and UncheckAllPages links for avai +lable pages ###################################################################### +#################### sub checkUncheckAllPages { my $checkAllPages = "function checkAllPages() {\n"; my $uncheckAllPages = "function uncheckAllPages() {\n"; for (my $i=0; $i<=$#htmlFiles; $i++) { if ($htmlFiles[$i] =~ m|^\w+.html|g && $htmlFiles[$i-1] !~ m|.*\/\ +w+.html|g) { $checkAllPages .= "findObj('$htmlFiles[$i]').checked = true;\n +"; $uncheckAllPages .= "findObj('$htmlFiles[$i]').checked = false +;\n"; } } $checkAllPages .="}\n"; $uncheckAllPages .="}\n"; return $checkAllPages.$uncheckAllPages; } ###################################################################### +#################### # build functions for CheckAllModules and UncheckAllModules links for +available modules ###################################################################### +#################### sub checkUncheckAllModules { my $checkAllModules = "function checkAllModules() {\n"; my $uncheckAllModules = "function uncheckAllModules() {\n"; for (my $i=0; $i<=$#htmlFiles; $i++) { if ($htmlFiles[$i] =~ m|^\w+.html|g && $htmlFiles[$i-1] =~ m|.*\/m +odules\/.*|g) { $checkAllModules .= "findObj('$htmlFiles[$i]').checked = true; +\n"; $uncheckAllModules .= "findObj('$htmlFiles[$i]').checked = fal +se;\n"; } } $checkAllModules .="}\n"; $uncheckAllModules .="}\n"; return $checkAllModules.$uncheckAllModules; } ###################################################################### +#################### # build functions for CheckAllComponents and UncheckAllComponents link +s for components ###################################################################### +#################### sub checkUncheckAllComponents { my $checkAllComponents = "function checkAllComponents() {\n"; my $uncheckAllComponents = "function uncheckAllComponents() {\n"; + for (my $i=0; $i<=$#htmlFiles; $i++) { if ($htmlFiles[$i] =~ m|^\w+.html|g && $htmlFiles[$i-1] =~ m|.*\/c +omponents\/.*|g) { $checkAllComponents .= "findObj('$htmlFiles[$i]').checked = tr +ue;\n"; $uncheckAllComponents .= "findObj('$htmlFiles[$i]').checked = +false;\n"; } } $checkAllComponents .="}\n"; $uncheckAllComponents .="}\n"; return $checkAllComponents.$uncheckAllComponents; } ###################################################################### +#################### # build functions for CheckAllBranches and UncheckAllBranches links fo +r branches ###################################################################### +#################### sub checkUncheckAllBranches { my $checkAllBranches = "function checkAllBranches() {\n"; my $uncheckAllBranches = "function uncheckAllBranches() {\n"; foreach my $name (@availableCloningBranches) { $checkAllBranches .= "findObj('$name').checked = true;\n"; $uncheckAllBranches .= "findObj('$name').checked = false;\n"; } $checkAllBranches .="}\n"; $uncheckAllBranches .="}\n"; return $checkAllBranches.$uncheckAllBranches; } ###################################################################### +### # Determine if files and branches are selected to copy files ###################################################################### +### sub getSelections { my $cnt = 0; my $tempcount = 0; @selectedComponents = param('html_files'); # returns a list of the + selected checkboxes my @selectedBranches = param('branch'); # returns a list of the se +lected checkboxes my @components = qw(); foreach my $html (@htmlFiles) { if ($html =~ m|.*_PAGE$|g || $html =~ m|.*_MODULE$|g || $html +=~ m|.*_COMPONENT$|g) { $html =~ s|_PAGE$||g; $html =~ s|_MODULE$||g; $html =~ s|_COMPONENT$||g; $cnt++; push(@components, $html); } } # checks whether or not checkboxes are selected if ((@selectedBranches == 0 && @selectedComponents == 0) || (@sele +ctedBranches > 0 && @selectedComponents == 0) || (@selectedBranches = += 0 && @selectedComponents > 0)) { #if array is empty print "<br />"; print "Submit not valid, you must select from available html f +iles and branches, press \"Back\" browser button to return to previou +s entry screen\n"; print "<br /><br />"; } else { print "<br />"; print "\"Selected files\" from <b>$copyFromBranch</b> branch a +re copied to \"selected branches\" ...\n"; print "<br /><br />"; print '<u>Selected Files</u>:'; print '<table cellpadding="5"><tr>'; # verify if files are selected my $html_value = "not valid"; foreach my $html_files (@selectedComponents) { if ($html_files ne "") { print "<td><label>$html_files</label></td>\n"; $cnt++; $html_value = "valid"; } $tempcount++; if ($tempcount > 4) { print "</tr><tr>"; $tempcount = 0; } if ($html_files eq $components[$#components]) { print "</tr>\n"; } } if ($html_value eq "valid") { print "</table>"; print "<br /><br />\n"; } # verify if branches are selected $tempcount = 0; print '<u>Selected Branches</u>:'; print '<table cellpadding="5"><tr>'; foreach my $branch (@selectedBranches) { if ($branch ne "") { print "<td><label>$branch</label></td>\n"; $cnt++; } $tempcount++; if ($tempcount > 4) { print "</tr><tr>"; $tempcount = 0; } if ($branch eq $availableCloningBranches[$#availableClonin +gBranches]) { print "</tr>\n"; } } print "</table>"; print "<br /><br />\n"; selected_files(\@selectedComponents); copyFromBranchToSelectedBranches(\@selectedBranches); } } ###################################################################### +### # process selected files ###################################################################### +### sub selected_files { my $files = $_[0];; # the readdir() output # contains both (files and directories) my @selectedFiles = qw(); foreach my $selectedFile (@$files) { foreach my $copyFile (@pageExist) { if ($copyFile =~ m|$selectedFile|) { # mi0::LogManager::info("found: $copyFile"); push(@selectedFiles, $copyFile); } } } # process files and directories foreach my $name (@selectedFiles) { # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/PrimaryDCR + "$name"`; # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/PrimaryD +ocumentType "$name"`; mi0::LogManager::info("page_generated_filename: $name") if (($ +templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) + && $primaryDCR ne ""); push (@copyFiles, "$name") if (($templateType ne "" && $templa +teType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); # Get full path of primaryDCR my $primaryDCR_path_name = $& if $name =~ m|.*WORKAREA\/\w+\/| +; my $workAreaPath = $primaryDCR_path_name; $primaryDCR_path_name = $primaryDCR_path_name."templatedata/". +$templateType."/data/".$primaryDCR; mi0::LogManager::info("page_primaryDCR: $primaryDCR_path_name" +) if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@copyFiles, "$primaryDCR_path_name") if ($primaryDCR_pat +h_name !~ m|mi\_navigation\/navigation|); my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path +_name cannot be copied: $!"; open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # r +ead file my @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\/components\/|g) { $cmd = "$iwperl \"s|\/sites|\n\/sites|g\" \"$bakFile\" +"; # replace cmd `$cmd`; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bakFile\""; + # replace cmd `$cmd`; } } open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # r +ead file @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { chomp $line; if ($line =~ m|^\/sites\/.*|) { if ($line =~ m|.*\/modules\/.*|) { # mi0::LogManager::info("module: $line"); + modulesComponentsCopy($workAreaPath,$line); } else { # mi0::LogManager::info("component: $line"); modulesComponentsCopy($workAreaPath,$line); } } } copyFromBranchToSelectedBranches(\@copyFiles); } } # End of sub selected_files { ###################################################################### +### # get modules or components to be copied ###################################################################### +### sub modulesComponentsCopy ($$) { my $workAreaPath = $_[0]; my $name = $_[1]; $name = $workAreaPath.$name; my $fileChanged = "no"; # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/PrimaryDCR "$n +ame"`; next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/PrimaryDocum +entType "$name"`; # Get full path of primaryDCR my $primaryDCR_path_name = $workAreaPath."templatedata/".$template +Type."/data/".$primaryDCR; if ($name =~ m|.*module.*|) { $name =~ s|\/\/|\/|g; mi0::LogManager::info("module_generated_filename: $name") if ( +($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation +|) && $primaryDCR ne ""); push (@copyFiles, "$name") if (($templateType ne "" && $templa +teType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); + my $module = $& if $name =~ m|\w+.html|; mi0::LogManager::info("module_primaryDCR: $primaryDCR_path_nam +e") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@copyFiles, "$primaryDCR_path_name") if ($primaryDCR_pat +h_name !~ m|mi\_navigation\/navigation|); } else { $name =~ s|\/\/|\/|g; mi0::LogManager::info("component_generated_filename: $name") i +f (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigat +ion|) && $primaryDCR ne ""); push (@copyFiles, "$name") if (($templateType ne "" && $templa +teType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); + my $component = $& if $name =~ m|\w+.html|; mi0::LogManager::info("component_primaryDCR: $primaryDCR_path_ +name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@copyFiles, "$primaryDCR_path_name") if ($primaryDCR_pat +h_name !~ m|mi\_navigation\/navigation|); } my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path_nam +e cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read +file my @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\/components\/|g) { $cmd = "$iwperl \"s|\/sites|\n\/sites|g\" \"$bakFile\""; # + replace cmd `$cmd`; $fileChanged = "yes"; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bakFile\""; # r +eplace cmd `$cmd`; $fileChanged = "yes"; } } open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read +file @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { chomp $line; if ($line =~ m|.*\/sites\/.*|) { if ($line =~ m|.*module.*|) { mi0::LogManager::info("module2: $line"); $name = $line; return $name; } else { my $subComponent = $& if $line =~ m|sites.*|; $name = $subComponent; $name = $workAreaPath.$name; # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/Pr +imaryDCR "$name"`; next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/ +PrimaryDocumentType "$name"`; mi0::LogManager::info("component_generated_filename: $ +name") if (($templateType ne "" && $templateType !~ m|mi\_navigation\ +/navigation|) && $primaryDCR ne ""); my $component = $& if $name =~ m|\w+.html|; push (@copyFiles, "$name") if (($templateType ne "" && + $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne "" +); # Get full path of primaryDCR $primaryDCR_path_name = $workAreaPath."templatedata/". +$templateType."/data/".$primaryDCR; mi0::LogManager::info("component_primaryDCR: $primaryD +CR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigat +ion|); push (@copyFiles, "$primaryDCR_path_name") if ($primar +yDCR_path_name !~ m|mi\_navigation\/navigation|); my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primary +DCR_path_name cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $ +!"; # read file my @file = <WEBFILE>; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\<componentID\>|g) { $cmd = "$iwperl \"s|\<componentID\>|\<componen +tID\>\n|g\" \"$bakFile\""; # replace cmd `$cmd`; $fileChanged = "yes"; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bak +File\""; # replace cmd `$cmd`; $fileChanged = "yes"; } } } } } mi0::LogManager::info("\@copyFiles: @copyFiles"); copyFromBranchToSelectedBranches(\@copyFiles); if ($fileChanged eq "yes") { $unlinkStatus = unlink("$bakFile"); # delete .bak file created + from replace cmd $unlinkStatus = unlink("$bakFile.bak"); # delete .bak file cre +ated from replace cmd if ($unlinkStatus == 1) { # mi0::LogManager::info("Deleted (unlinked) .bak file succ +essfully: $bakFile"); # mi0::LogManager::info("Deleted (unlinked) .bak file succ +essfully: $bakFile.bak"); } else { mi0::LogManager::info("Failed to delete - '$bakFile': <$!> +. Received $unlinkStatus instead of 1."); mi0::LogManager::info("Failed to delete - '$bakFile.bak': +<$!>. Received $unlinkStatus instead of 1."); } } } # End of sub modulesComponentsCopy { ###################################################################### +########################## # Get selected files and branches to copy from current branch to desti +nation branch or branches ###################################################################### +########################## sub copyFromBranchToSelectedBranches ($) { my $selectedComponents = $_[0]; my @selectedFiles = qw(); my @preSelectedFiles = qw(); my @branchesSelected = qw(); foreach my $selected (@$selectedComponents) { if ($selected =~ m|\/|g) { mi0::LogManager::info("files selected: $selected"); push(@preSelectedFiles,$selected); # files to be copied fr +om current branch } else { push(@branchesSelected, $selected); } } my $allSelectedFiles = "@selectedFiles" if(@selectedFiles != 0); # mi0::LogManager::info("allSelectedFiles : $allSelectedFiles +"); for (my $i=0; $i<=$#preSelectedFiles; $i++) { if($preSelectedFiles[$i] ne "") { mi0::LogManager::info("selectedFiles[i] : $preSelectedFile +s[$i]"); push(@selectedFiles, $preSelectedFiles[$i]); } } foreach my $selectedBranch (@branchesSelected) { mi0::LogManager::info("branches selected: $selectedBranch"); + mi0::LogManager::info("2\@selectedFiles: @selectedFiles"); foreach my $copyFromFiles (@selectedFiles) { my $copyToFiles = $copyFromFiles; $copyToFiles =~ s|$copyFromBranch|$selectedBranch|g; mi0::LogManager::info("copy file path created: $copyToFile +s"); my $copyToDir = $& if $copyToFiles =~ m|\/.*\/|; if ( ! -e $copyToFiles) { # if file exist eval { mkpath("$copyToDir"); }; if ($@) { abort("Couldn't create $copyToDir: $@"); } $cmd = "$IWHOME/bin/./iwcp $copyFromFiles $copyToFiles +"; # copy cmd for files to another branch `$cmd`; if ($?) { abort("Couldn't run $cmd"); } else { mi0::LogManager::info("file copied successfully: $ +copyToFiles"); } } else { mi0::LogManager::info("file already exists: $copyToFil +es"); } open(DCR, $copyToFiles) || die "can't open $copyToFiles: $ +!" if($copyToFiles !~ m|\.html$|); # read file if not html + my @file = <DCR>; close(DCR); my $fileChanged = "no"; foreach my $line (@file) { if ($line =~ m|\/sites\/|g) { $cmd = "$iwperl \"s|$copyFromBranch|$selectedBranc +h|g\" \"$copyToFiles\""; # replace cmd `$cmd`; $fileChanged = "yes"; } } if ($copyToFiles !~ m|\.html$| && $fileChanged eq "yes") { $unlinkStatus = unlink("$copyToFiles.bak"); # delete . +bak file created from replace cmd if ($unlinkStatus == 1) { # mi0::LogManager::info("Deleted (unlinked) .bak f +ile successfully: $copyToFiles.bak"); } else { mi0::LogManager::info("Failed to delete - '$copyTo +Files.bak': <$!>. Received $unlinkStatus instead of 1."); } } if ($copyToFiles =~ m|\.html$|) { # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/Pr +imaryDCR "$copyToFiles"`; $primaryDCR =~ s|$copyFromBranch|$selectedBranch|g; + next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/ +PrimaryDocumentType "$copyToFiles"`; mi0::LogManager::info("copied_generated_filename: $cop +yToFiles") if (($templateType ne "" && $templateType !~ m|mi\_navigat +ion\/navigation|) && $primaryDCR ne ""); # push (@pageExist, "$name") if (($templateType ne "" +&& $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne +""); # Retrieve primaryPT from TeamSite metadata my $primaryPT = `$iwextattr -g TeamSite/Templating/Pri +maryPT "$copyToFiles"`; # mi0::LogManager::info("copied_primaryPT: $primaryPT" +); # Get full path of primaryDCR my $workAreaPath = $& if $copyToFiles =~ m|.*WORKAREA\ +/\w+\/|; my $primaryDCR_path_name = $workAreaPath."templatedata +/".$templateType."/data/".$primaryDCR; mi0::LogManager::info("copied_primaryDCR: $primaryDCR_ +path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation +|); # Get full path of primaryPT my $primaryPT_path_name = $workAreaPath."templatedata/ +".$templateType."/presentation/".$primaryPT; $primaryPT_path_name =~ s|\/iwmnt||; $primaryDCR_path_name =~ s|\/iwmnt||; $copyToFiles =~ s|\/iwmnt||; $cmd = "$IWHOME/bin/./iwgen -t $primaryPT_path_name -r + $primaryDCR_path_name $copyToFiles"; # iwgen cmd `$cmd`; if ($?) { abort("Couldn't run $cmd"); } else { mi0::LogManager::info("generated successfully: $co +pyToFiles"); } } } } } ###################################################################### +############### # prints abort error message if cmd fails ###################################################################### +############### sub abort { my $message = $_[0]; mi0::LogManager::info("Aborting: $message"); } # End of sub abort { ###################################################################### +############### # Passes arguments around if necessary ###################################################################### +############### sub passparams{ #Passes useful parameters #task id print $cgi->hidden(-name=>'task_id', -value=>$cgi->param('task_id'), ); #user name print $cgi->hidden(-name=>'user_name', -value=>$cgi->param('user_name'), ); #user role print $cgi->hidden(-name=>'user_role', -value=>$cgi->param('user_role'), ); #area name print $cgi->hidden(-name=>'area_name', -value=>$cgi->param('area_name'), ); #area path print $cgi->hidden(-name=>'area_path', -value=>$cgi->param('area_path'), ); #directory path print $cgi->hidden(-name=>'directory_path', -value=>$cgi->param('directory_path'), ); #directory name print $cgi->hidden(-name=>'directory_name', -value=>$cgi->param('directory_name'), ); #page type print $cgi->hidden(-name=>'page_type', -value=>$cgi->param('page_type'), ); #subpage type print $cgi->hidden(-name=>'subpage_type', -value=>$cgi->param('subpage_type'), ); #Mount path print $cgi->hidden(-name=>'mount_path', -value=>$cgi->param('mount_path'), ); #Branch name print $cgi->hidden(-name=>'branch_name', -value=>$cgi->param('branch_name'), ); #Branch path print $cgi->hidden(-name=>'branch_path', -value=>$cgi->param('branch_path'), ); }

Hi wise ones, I have an issue trying to pass array values to foreach loop, which I have done thousand of times. I cannot pass array values from line 937 to 944. Once I get these values in foreach loop at line at 944, I will be able to complete the copy process to destination folder from this code. Unfortunately it's a big code but all works except this small issue when resolved code/functionality will be complete.

Just please look at the lines closer to or linked to 937 and 944 please. I really need to complete for a demo tom. Code is attached, thanks a bunch for any help.

Here is sample output below I get from line 937:

2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/si +tes/individual_testing/pages/2_column_hero_banner/masthead_carousel.h +tml 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/te +mplatedata/mi_page/page_builder/data/masthead_carousel 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/si +tes/individual_testing/modules/hero_banner_module/test_hero_banner_mo +dule.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/te +mplatedata/mi_content/hero_banner_module/data/test_hero_banner_module +_dcr 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/si +tes/individual_testing/components/carousel/test_carousel.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/te +mplatedata/mi_content/carousel/data/test_carousel_dcr 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/si +tes/individual_testing/components/article/article_gradient_heading.ht +ml 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/te +mplatedata/mi_content/article/data/article_gradient_heading 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/si +tes/individual_testing/components/faqs/test_faqs.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/m +ain/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/te +mplatedata/mi_content/faqs/data/test_faqs_dcr


In reply to Array not accessible in foreach loop by begood321

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.