Attachment 'lenyaimport.pl'

Download

   1 #!/usr/bin/perl
   2 #
   3 # Copyleft 2005, qMax, Ind
   4 #
   5 # Not licensed under any license, you may use this file any way you want.
   6 #
   7 # Software distributed on an "AS WAS" BASIS, WITHOUT ANY WARRANTIES OR
   8 # CONDITIONS OF ANY KIND, either express or implied.
   9 #
  10 # Dedicated to Apache Lenya developers.
  11 #
  12 # v1.0 <qmax@mail.ru>
  13 #
  14 
  15 use strict;
  16 use File::Copy;
  17 use vars qw(%args);
  18 
  19 sub usage {
  20 	print STDERR "Usage: $0 [options] NODEPATH FILENAME...\n".
  21 							 "Args:\n".
  22 							 "  NODEPATH = absolute path of PARENT node, under wich new nodes to be created\n".
  23 							 "  FILENAME = path to files to be imported into node\n\n".
  24 							 "All source .xml files imported as documents at new nodes \$NODEPATH/\$filename/\n".
  25 							 "Other files are uploaded as assets\n\n".
  26 							 "Be carefull, there's no undo other then sitetree backup!\n\n".
  27 							 "Options:\n".
  28 							 "  --pub-path= path to lenya publication (e.g \$TOMCAT/webapps/lenya/lenya/pubs/default)\n".
  29 							 "  --area=     [authoring] area (authoring|live|share)\n".
  30 							 "  --visible=  [true] visibileinnav flag\n".
  31 							 "  --workflow= [workflow.xml] workflow scheme to generate workflow state file.\n".
  32 							 "  --dcformat= dc:format of assets, seems like required\n".
  33 							 "  --lang=     [ru] language, to create index_\$LN.xml\n".
  34 							 "  -t          test run, show what is to be done\n";
  35 }
  36 
  37 # defaults
  38 $args{'area'}="authoring";
  39 $args{'visible'}="true";
  40 $args{'lang'}="en";
  41 $args{'workflow'}="workflow.xml";
  42 
  43 while(my $arg=shift(@ARGV)) {
  44 	if( $arg =~ /^-(\w)/ ) {
  45 		$args{$1} = 1;
  46 		next;
  47 		}
  48 	elsif( $arg =~ /^--([\w-]+)=(.+)$/ ) {
  49 		$args{$1}=$2;
  50 		next;
  51 		}
  52 	else {
  53 		unless( $args{'NODEPATH'} ) {
  54 			$args{'NODEPATH'} = $arg;
  55 			next;
  56 			}
  57 		else {
  58 			push @{$args{'FILENAMES'}}, $arg;
  59 			next;
  60 			}
  61 		}
  62 }
  63 
  64 unless( $args{'FILENAMES'} and $args{'NODEPATH'} and $args{'pub-path'} ) {
  65 	usage();
  66 	die "Some required arguments missing (pub-path, NODEPATH, FILENAME)\n";
  67 	}
  68 
  69 my $treefile     = $args{'pub-path'}."/content/".$args{'area'}."/sitetree.xml";
  70 
  71 ### common temporary files
  72 our $tmp_workflowfile = create_wflowfile();
  73 our $tmp_patchxsltfile = create_patchxslt();
  74 
  75 if( $args{'t'} ) {
  76 	print STDERR "Backing up $treefile to $treefile.old\n";
  77 	}
  78 copyordie("$treefile","$treefile.old");
  79 
  80 foreach my $filename (@{$args{'FILENAMES'}}) {
  81 	if( $filename =~ /\.xml$/ ) {
  82 		importdoc($filename);
  83 		}
  84 	else {
  85 		uploadasset($filename);
  86 		}
  87 	}
  88 
  89 unlink($tmp_workflowfile);
  90 unlink($tmp_patchxsltfile);
  91 
  92 sub create_wflowfile {
  93 	my $tmpwflowfile = "/tmp/lenyaimport-$$-wflow.xml";
  94 	if( $args{'t'} ) {
  95 		print STDERR "Creating temporary workflow file at $tmpwflowfile\n";
  96 		}
  97 	open(F,">$tmpwflowfile") 
  98 		or die "Cannot create file $tmpwflowfile, but i realy need it.\n";
  99 	my $date=`date +"%Y-%m-%d %H:%M:%S"`;
 100 	chomp($date);
 101 	print F "<?xml version='1.0' encoding='UTF-8'?>
 102 <wf:history xmlns:wf='http://apache.org/cocoon/lenya/workflow/1.0' workflow='".$args{'workflow'}."'>
 103 <wf:version date='$date' state='".$args{'area'}."'>
 104 <wf:identity/>
 105 </wf:version>
 106 <wf:variable name='is_live' value='".($args{'area'} eq 'authoring' ? 'false' : 'true')."'/>
 107 </wf:history>";
 108 	close(F);
 109 	return $tmpwflowfile;
 110 }
 111 
 112 sub create_patchxslt {
 113 	my $tmpxsltfile = "/tmp/lenyaimport-$$-patch.xslt";
 114 	if( $args{'t'} ) {
 115 		print STDERR "Creating temporary xslt patch at $tmpxsltfile\n";
 116 		}
 117 	open(F,">$tmpxsltfile") or die "Cannot create file $tmpxsltfile, giving up.\n";
 118 	print F '<?xml version="1.0"?>
 119 <xsl:stylesheet 
 120   xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"
 121 	xmlns:tree="http://apache.org/cocoon/lenya/sitetree/1.0"
 122 	exclude-result-prefixes="tree"
 123 >
 124 <xsl:output encoding="utf-8" indent="yes"/>
 125 <xsl:param name="nodepath" select="\''.$args{'NODEPATH'}.'\'"/>
 126 <xsl:param name="nodename"/>
 127 <xsl:template match="@*|*|text()|comment()|processing-instruction()" priority="-1">
 128 	<xsl:copy><xsl:apply-templates select="@*|*|text()|comment()|processing-instruction()"/></xsl:copy>
 129 </xsl:template>
 130 <xsl:template match="text()[normalize-space()=\'\']"/>
 131 <xsl:template match="tree:node">
 132 	<xsl:param name="path" select="\'\'"/>
 133 	<xsl:variable name="curpath" select="concat($path,\'/\',@id)"/>
 134 	<xsl:choose>
 135 		<xsl:when test="$curpath = $nodepath">
 136 			<xsl:copy>
 137 				<xsl:apply-templates select="@*|*"/>
 138 				<tree:node id="{$nodename}" visibleinnav="'.$args{'visible'}.'">
 139 					<tree:label xml:lang="'.$args{'lang'}.'"><xsl:value-of select="$nodename"/></tree:label>
 140 				</tree:node>
 141 			</xsl:copy>
 142 		</xsl:when>
 143 		<xsl:otherwise>
 144 			<xsl:copy>
 145 				<xsl:copy-of select="@*"/>
 146 				<xsl:apply-templates select="*">
 147 					<xsl:with-param name="path" select="$curpath"/>
 148 				</xsl:apply-templates>
 149 			</xsl:copy>
 150 		</xsl:otherwise>
 151 	</xsl:choose>
 152 </xsl:template>
 153 </xsl:stylesheet>
 154 ';
 155 	close(F);
 156 	return $tmpxsltfile;
 157 }
 158 
 159 sub importdoc {
 160 	my ($filename) = @_;
 161 	my $nodename = $filename;
 162 	$nodename =~ s/\.xml$//i;
 163 
 164 	my $pubdir   = $args{'pub-path'};
 165 	my $nodepath = $args{'NODEPATH'}."/$nodename";
 166 	
 167 	my $docfilename = "index_".$args{'lang'}.".xml";
 168 	my $docfiledir  = "$pubdir/content/".$args{'area'}."/$nodepath";
 169 	my $docfile     = "$docfiledir/$docfilename";
 170 
 171 	# workflow only stored in authoring area
 172 	my $wflowfiledir = "$pubdir/content/workflow/history/authoring".$nodepath;
 173 	my $wflowfile    = "$wflowfiledir/$docfilename";
 174 	
 175 	print STDERR "Importing $filename into $nodepath\n";
 176 	
 177 	mkdir($docfiledir);
 178 	#copyordie($filename,$docfile);
 179         `/usr/bin/xsltproc --novalid --output $docfile simp.xsl $filename`;
 180 	mkdir($wflowfiledir);
 181 	copyordie($tmp_workflowfile,$wflowfile);
 182 	
 183 	if( $args{'t'} ) {
 184 		print STDERR "Patching sietree.xml\n";
 185 		}
 186 	copyordie($treefile,"$treefile.tmp");
 187 	tryordie("/usr/bin/xsltproc --stringparam nodename $nodename --output $treefile $tmp_patchxsltfile $treefile.tmp");
 188 	unlink("$treefile.tmp");
 189 }
 190 
 191 sub uploadasset {
 192 	my ($filename) = @_;
 193 	
 194 	my $pubdir   = $args{'pub-path'};
 195 	my $nodepath = $args{'NODEPATH'};
 196 	
 197 	my $assetdir = "$pubdir/resources/".$args{'area'}."/$nodepath";
 198 	my $assetfile = "$assetdir/$filename";
 199 	my $metafilename = "$filename.meta";
 200 	my $metafile = "$assetdir/$metafilename";
 201 	
 202 	print STDERR "Uploading asset $filename to $nodepath\n";
 203 	
 204 	copyordie($filename,$assetfile);
 205 	mkdir($assetdir);
 206 	open(F,">$metafile")
 207 		or die "Cannot create file $metafile.\nSomething wrong.";
 208 	my $date=`LC_ALL=POSIX date +"%a %b %d %H:%M:%S %Z %Y"`;
 209 	chomp($date);
 210 	print F '<?xml version="1.0" encoding="UTF-8"?>
 211 <dc:metadata xmlns:dc="http://purl.org/dc/elements/1.1/">
 212 <dc:title>'.$filename.'</dc:title>
 213 <dc:type></dc:type>
 214 <dc:rights>All rights reserved.</dc:rights>
 215 <dc:publisher></dc:publisher>
 216 <dc:date>'.$date.'</dc:date>
 217 <dc:language></dc:language>
 218 <dc:creator>Vasiliy Pupkin</dc:creator>
 219 <dc:coverage></dc:coverage>
 220 <dc:description></dc:description>
 221 <dc:extent>21372</dc:extent>
 222 <dc:identifier></dc:identifier>
 223 <dc:subject></dc:subject>
 224 <dc:relation></dc:relation>
 225 <dc:contributor></dc:contributor>
 226 <dc:source></dc:source>
 227 <dc:format>'.$args{'dcformat'}.'</dc:format>
 228 </dc:metadata>';
 229 	close(F);
 230 
 231 }
 232 
 233 sub copyordie {
 234 	my ($src,$dst) = @_;
 235 	if( $args{'t'} ) {
 236 		print STDERR "cp $src $dst\n";
 237 		}
 238 	copy($src,$dst)
 239 		or die("\nFailed to copy $src to $dst\nShit happens: $?: $!\n");
 240 }
 241 
 242 sub tryordie {
 243 	my $command = shift;
 244 	if( $args{'t'} ) {
 245 		print STDERR "running: $command\n";
 246 		}
 247 	system ($command) == 0
 248 		or die("\nFailed to execute: $command\nAin't no way to run: $?: $!\n");
 249 }

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2005-08-12 01:31:36, 7.2 KB) [[attachment:lenyaimport.pl]]
 All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.