CopyImages.pl

From Organic Design wiki
Revision as of 07:07, 1 December 2008 by Sven (talk | contribs) (Maintenance script to copy wiki images)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
  1. !/usr/bin/perl -w

use strict; use Getopt::Long; use File::Copy;

  1. If there is no XML file the script should grab all the images and put them into
  2. /tmp/wikiimages Template:PERL
  1. 0) ------------------------------ Globals ----------------------------------- #

my $debug = 0; # debugging switch

my @matches; # A list of images obtained from 'xmlfile'

my %options = ( # Set options using Getopt::Long

 xmlfile		=> "",
 wikiImagePath => "",
 tmpdir		=> "/tmp/wikiimages", # Could use wiki/images path too
 debug			=> 0,
 rmtmpdir      => 0,

);

my @wantedPaths; # A list of wanted image paths which exist from 'xmlfile'

  1. 1) ------------------------------ Get options ------------------------------ #

GetOptions(

 \%options,
 'xmlfile=s',
 'wikiImagePath=s',
 'tmpdir=s',
 'debug',
 'rmtmpdir',

);

  1. Create tmp subdirectory

mkdir $options{'tmpdir'} || die("Could not create directory");

if ($options{'debug'}) {

 print "== Input arguements ==\n";
 foreach my$values ( keys %options ) {
   print "$values => $options{$values}\n";
 }

}

if( $options{'xmlfile'} eq "" ) {

 print "[ Copying ALL images to: $options{'tmpdir'} ]\n";
  1. find . | egrep '\..{3,4}$' | egrep -v '(thumb|archive)' | while read i; do cp $i /tmp/wikiimages/; done
 my $command = "find $options{'wikiImagePath'} "  # find files in image path
 . q(| egrep '\..{3,4}$' )          # 3-4 letter extensions only
 . q(| egrep -v '(thumb|archive)') # Remove thumb archive directories
 . q(| while read i; do cp $i ) . $options{'tmpdir'} . q(; done);
   
 if ($options{'debug'} ) {
   print"== Executing command ==\n";
   print "$command\n";
 }
 system($command);
  1. while read i; do cp $i' . $options{'tmpdir'} . '; done');
 exit;

}


  1. 2) ---------------- Open input XML file and list of images ------------------ #

open(XMLFILE, $options{'xmlfile'}) || die("Could not open file ($options{'xmlfile'}): $!\n");

  1. 3) ------------------------------ Grab images ------------------------------- #

{

 # Not scalable  
 local $/=undef;
 my $xmlImage = <XMLFILE>;
 # ($xmlImage =~ m/\<nowiki(.+?)\&nowiki\\gt;/) && (print $1);
 if ( $options{'debug'} ) {
   print "== XML nowiki sections ==\n";
   while ($xmlImage =~ m/\<nowiki\>(.+?)<\/nowiki\>/g) {print "$1\n"};
 }
 # Remove content in between <nowiki>'s
 $xmlImage =~ s/\<nowiki\>(.+?)<\/nowiki\>//g;
 # Grab image matches (don't forget |'s and ]]'s in image tags)
 @matches = $xmlImage =~ m/\[{2}Image:(.+?)(?=\||\]{2})/g; 

}

if($options{'debug'}) {

 local $" = "\n";
 print "== \@matches ==\n @matches\n";

}

  1. Not scalable

my @allImagePaths=<ALLPATHS>;

  1. 4) ----------------- Test $imageToMatch against $eachImagePath -------------- #

foreach my $imageToMatch (@matches) {

 chop $imageToMatch;
 # Convert space separated image names to '_'s
 $imageToMatch =~ s/ /_/g;
 # Matching XMLFILE images against images in ALLPATHS 
 foreach my $eachImagePath (@allImagePaths) {
   $eachImagePath =~ m/$imageToMatch/ && push @wantedPaths, $eachImagePath;
 }

}

if ( $options{'debug'} ) {

 print "==Wanted image paths==\n@wantedPaths";

}

if ( scalar(@wantedPaths) == 0 ) {

 die("[ No images found in XML file $options{'xmlfile'} ]\n");

}

print "[ Copying images found in XML file $options{'xmlfile'} to: $options{'tmpdir'} ]\n";

  1. 5) --------------------- Copy subset of wanted images ----------------------- #

foreach my $wantedPath ( @wantedPaths ) {

 chomp($wantedPath);
   
 my $fullPath = $options{'wikiImagePath'} . substr($wantedPath, 1, length $wantedPath); 
 print "$fullPath\n";    
   
 # Copy files
 copy($fullPath, $options{'tmpdir'}) || dir("Could not copy $fullPath files to $options{'tmpdir'}: $!\n")

}