#!/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 browser 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,@availableCloningBranches) = qw(); foreach my $dir (@cloningBranches) { next if ($dir =~ m|\.$| || $dir =~ m|\.\.$| || $dir =~ m|STAGING| || $dir =~ m|WORKAREA| || $dir =~ m|EDITION| || $dir =~ m|$copyFromBranch|); $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 "
"; 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/PrimaryDCR "$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 (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); push (@htmlFiles, "$name"."_PAGE") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR 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."templatedata/".$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 ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); if ($primaryDCR eq "sites.individual_testing.pages.2_column_hero_banner.ami_workshop1_2_col_with_hero") { 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: $!"; # read file my @file = ; 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: $!"; # read file @file = ; 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: $line"); 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 "$name"`; next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/PrimaryDocumentType "$name"`; # Get full path of primaryDCR my $primaryDCR_path_name = $workAreaPath."templatedata/".$templateType."/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 "" && $templateType !~ 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 ne ""); # mi0::LogManager::info("module_primaryDCR: $primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@pageExist, "$primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); } else { $name =~ s|\/\/|\/|g; mi0::LogManager::info("component_generated_filename: $name") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); push (@pageExist, "$name") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); my $component = $& if $name =~ m|\w+.html|; push (@htmlFiles, "$component"."_COMPONENT") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); # mi0::LogManager::info("component_primaryDCR: $primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@pageExist, "$primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); } my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path_name cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read file my @file = ; 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\""; # replace cmd `$cmd`; $fileChanged = "yes"; } } open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read file @file = ; 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/PrimaryDCR "$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 (($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: $primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@pageExist, "$primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path_name cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read file my @file = ; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\|g) { $cmd = "$iwperl \"s|\|\\n|g\" \"$bakFile\""; # replace cmd `$cmd`; $fileChanged = "yes"; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bakFile\""; # 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 created from replace cmd if ($unlinkStatus == 1) { # mi0::LogManager::info("Deleted (unlinked) .bak file successfully: $bakFile"); # mi0::LogManager::info("Deleted (unlinked) .bak file successfully: $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 =< $title
$title
HEADEND print $HEADER; } ######################################################################### # build javascript for header ######################################################################### sub buildJavaScript { my $JSCRIPT = <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&&iparam('formRequest'); if ($formRequest eq "cancel") { print $cgi->script('window.close()'); } if ($areapath eq ""){ print "
Please Navigate to the WORKAREA that you want to copy pages, modules or components to
"; } else { if ($formRequest eq '') { print "
"; 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 = ''; $BUTTON .= ''; $BUTTON .= ' '; $BUTTON .= ' '; $BUTTON .= ' '; $BUTTON .= ' '; $BUTTON .= '
'; $BUTTON .= ' '; $BUTTON .= $buttonValue; $BUTTON .= ' '; $BUTTON .= '
'; print $BUTTON; } ################################################################################################# # Show available pages, modules and components to copy ################################################################################################# sub getAvailableComponents { my $checked = " =''"; my $componentsHeader.=qq(Select html files (pages, modules and components) to copy from current branch to selected branches? \n); $componentsHeader.=qq(

); $componentsHeader.=qq(); $componentsHeader.=qq(); 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 "\n" if !$duplicates {$file}++; } if ($file =~ m|_MODULE$|g) { $file =~ s|_MODULE$||g; print "\n" if !$duplicates {$file}++; } if ($file =~ m|_COMPONENT$|g) { $file =~ s|_COMPONENT$||g; print "\n" if !$duplicates {$file}++; } } } ################################################################################################################# # Show available branches to receive copied files ################################################################################################# sub getAvailableBranches { print "
pagesmodules/components


Select Branches below to copy selected pages, modules and components? \n"; print "

"; print ''; my $cnt = 0; my $tempcount = 0; my $checked = " =''"; my $branch_value = "not valid"; foreach my $branch (@availableCloningBranches) { if (!$duplicates { $branch }++) { print "\n"; $tempcount++; $branch_value = "valid"; } if ($tempcount > 4 && $branch_value eq "valid") { print ""; $tempcount = 0; } if ($branch eq $availableCloningBranches[$#availableCloningBranches] && $branch_value eq "valid") { # last branch in array print "\n"; } } print "
"; print "
"; print 'Uncheck All | Check All || Uncheck All Pages | Check All Pages || Uncheck All Modules | Check All Modules || Uncheck All Components | Check All Components || Uncheck All Branches | Check All Branches"; print "

"; } ############################################################################# # build functions for CheckAll and UncheckAll links for available selections ############################################################################# 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 available 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|.*\/modules\/.*|g) { $checkAllModules .= "findObj('$htmlFiles[$i]').checked = true;\n"; $uncheckAllModules .= "findObj('$htmlFiles[$i]').checked = false;\n"; } } $checkAllModules .="}\n"; $uncheckAllModules .="}\n"; return $checkAllModules.$uncheckAllModules; } ########################################################################################## # build functions for CheckAllComponents and UncheckAllComponents links 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|.*\/components\/.*|g) { $checkAllComponents .= "findObj('$htmlFiles[$i]').checked = true;\n"; $uncheckAllComponents .= "findObj('$htmlFiles[$i]').checked = false;\n"; } } $checkAllComponents .="}\n"; $uncheckAllComponents .="}\n"; return $checkAllComponents.$uncheckAllComponents; } ########################################################################################## # build functions for CheckAllBranches and UncheckAllBranches links for 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 selected 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) || (@selectedBranches > 0 && @selectedComponents == 0) || (@selectedBranches == 0 && @selectedComponents > 0)) { #if array is empty print "
"; print "Submit not valid, you must select from available html files and branches, press \"Back\" browser button to return to previous entry screen\n"; print "

"; } else { print "
"; print "\"Selected files\" from $copyFromBranch branch are copied to \"selected branches\" ...\n"; print "

"; print 'Selected Files:'; print ''; # verify if files are selected my $html_value = "not valid"; foreach my $html_files (@selectedComponents) { if ($html_files ne "") { print "\n"; $cnt++; $html_value = "valid"; } $tempcount++; if ($tempcount > 4) { print ""; $tempcount = 0; } if ($html_files eq $components[$#components]) { print "\n"; } } if ($html_value eq "valid") { print "
"; print "

\n"; } # verify if branches are selected $tempcount = 0; print 'Selected Branches:'; print ''; foreach my $branch (@selectedBranches) { if ($branch ne "") { print "\n"; $cnt++; } $tempcount++; if ($tempcount > 4) { print ""; $tempcount = 0; } if ($branch eq $availableCloningBranches[$#availableCloningBranches]) { print "\n"; } } print "
"; print "

\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/PrimaryDocumentType "$name"`; mi0::LogManager::info("page_generated_filename: $name") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); push (@copyFiles, "$name") if (($templateType ne "" && $templateType !~ 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_path_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: $!"; # read file my @file = ; 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: $!"; # read file @file = ; 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 "$name"`; next if ($primaryDCR eq ""); # Retrieve templateType from TeamSite metadata my $templateType = `$iwextattr -g TeamSite/Templating/PrimaryDocumentType "$name"`; # Get full path of primaryDCR my $primaryDCR_path_name = $workAreaPath."templatedata/".$templateType."/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 "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); my $module = $& if $name =~ m|\w+.html|; mi0::LogManager::info("module_primaryDCR: $primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@copyFiles, "$primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); } else { $name =~ s|\/\/|\/|g; mi0::LogManager::info("component_generated_filename: $name") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/navigation|) && $primaryDCR ne ""); push (@copyFiles, "$name") if (($templateType ne "" && $templateType !~ 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_path_name !~ m|mi\_navigation\/navigation|); } my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path_name cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read file my @file = ; 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\""; # replace cmd `$cmd`; $fileChanged = "yes"; } } open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read file @file = ; 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/PrimaryDCR "$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: $primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); push (@copyFiles, "$primaryDCR_path_name") if ($primaryDCR_path_name !~ m|mi\_navigation\/navigation|); my $bakFile = $primaryDCR_path_name.".bak"; copy($primaryDCR_path_name, $bakFile) or die "$primaryDCR_path_name cannot be copied: $!"; # create .bak file open(WEBFILE, $bakFile) || die "can't open $bakFile: $!"; # read file my @file = ; close(WEBFILE); foreach my $line (@file) { if ($line =~ m|\|g) { $cmd = "$iwperl \"s|\|\\n|g\" \"$bakFile\""; # replace cmd `$cmd`; $fileChanged = "yes"; } if ($line =~ m|\.html|g) { $cmd = "$iwperl \"s|\.html|\.html\n|g\" \"$bakFile\""; # 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 created from replace cmd if ($unlinkStatus == 1) { # mi0::LogManager::info("Deleted (unlinked) .bak file successfully: $bakFile"); # mi0::LogManager::info("Deleted (unlinked) .bak file successfully: $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 destination 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 from 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] : $preSelectedFiles[$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: $copyToFiles"); 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: $copyToFiles"); } open(DCR, $copyToFiles) || die "can't open $copyToFiles: $!" if($copyToFiles !~ m|\.html$|); # read file if not html my @file = ; close(DCR); my $fileChanged = "no"; foreach my $line (@file) { if ($line =~ m|\/sites\/|g) { $cmd = "$iwperl \"s|$copyFromBranch|$selectedBranch|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 file successfully: $copyToFiles.bak"); } else { mi0::LogManager::info("Failed to delete - '$copyToFiles.bak': <$!>. Received $unlinkStatus instead of 1."); } } if ($copyToFiles =~ m|\.html$|) { # Retrieve primaryDCR from TeamSite metadata my $primaryDCR = `$iwextattr -g TeamSite/Templating/PrimaryDCR "$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: $copyToFiles") if (($templateType ne "" && $templateType !~ m|mi\_navigation\/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/PrimaryPT "$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: $copyToFiles"); } } } } } ##################################################################################### # 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'), ); } #### 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/sites/individual_testing/pages/2_column_hero_banner/masthead_carousel.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/templatedata/mi_page/page_builder/data/masthead_carousel 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/sites/individual_testing/modules/hero_banner_module/test_hero_banner_module.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/templatedata/mi_content/hero_banner_module/data/test_hero_banner_module_dcr 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/sites/individual_testing/components/carousel/test_carousel.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/templatedata/mi_content/carousel/data/test_carousel_dcr 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/sites/individual_testing/components/article/article_gradient_heading.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/templatedata/mi_content/article/data/article_gradient_heading 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/sites/individual_testing/components/faqs/test_faqs.html 2013/07/24 20:03:19 main 937 INFO: selectedFiles[i] : /iwmnt/default/main/mgi_suite/sites/individual_testing/WORKAREA/individual_testing/templatedata/mi_content/faqs/data/test_faqs_dcr