update dev300-m58
[ooovba.git] / helpcontent2 / helpers / update_tree.pl
blobae7dddf0d6c01039a4a06168352bd117b6715e2d
2 eval 'exec perl -wS $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 #
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: update_tree.pl,v $
14 # $Revision: 1.13 $
16 # This file is part of OpenOffice.org.
18 # OpenOffice.org is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License version 3
20 # only, as published by the Free Software Foundation.
22 # OpenOffice.org is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU Lesser General Public License version 3 for more details
26 # (a copy is included in the LICENSE file that accompanied this code).
28 # You should have received a copy of the GNU Lesser General Public License
29 # version 3 along with OpenOffice.org. If not, see
30 # <http://www.openoffice.org/license.html>
31 # for a copy of the LGPLv3 License.
33 #*************************************************************************
35 use Cwd 'abs_path';
36 use File::Find;
37 use File::Copy qw/cp mv/;
38 use File::Basename;
39 use Benchmark;
41 my $is_win = "false";
43 $is_win = "true" , if ( defined $ENV{USE_SHELL} && $ENV{USE_SHELL} eq "4nt" );
45 $t0 = new Benchmark;
46 # update the tree files in <platform>/misc/*
48 $| = 1;
50 my $prj = $ENV{ENVPRJ};
52 my $inpath = $ENV{INPATH};
53 terminate() if ( ! defined $inpath );
55 my $destpath = $inpath;
56 my $with_lang = $ENV{WITH_LANG};
58 if ( defined $ENV{COMMON_OUTDIR} && $ENV{COMMON_OUTDIR} ne "" ) {
59 $destpath =~ s/$ENV{OUTPATH}/$ENV{COMMON_OUTDIR}/;
62 # Always use / directory separators
63 $prj =~ s/\\/\//g if defined($prj);
64 $inpath =~ s/\\/\//g;
65 $destpath =~ s/\\/\//g;
68 if ( ! defined $prj ) {
69 # do someting that works for manual call
70 ($scriptname = `pwd`) =~ s/\n/\/$0/;
71 ($tree_src = $scriptname) =~ s/\/update_tree.pl/\/..\/source\/auxiliary/;
72 ($tree_dest = $scriptname) =~ s/\/update_tree.pl/\/..\/$destpath\/misc/;
73 ($source_dir = $scriptname) =~ s/\/update_tree.pl/\/..\/source/;
74 if ( defined $ENV{TRYSDF} && defined $ENV{LOCALIZESDF} )
76 if( defined $ENV{LOCALIZATION_FOUND} && $ENV{LOCALIZATION_FOUND} eq "YES" )
78 $source_dir = $ENV{TRYSDF};
80 else
82 $source_dir = $ENV{LOCALIZESDF};
84 if( $is_win eq "false" ) { $source_dir =~ s/\/auxiliary\/localize.sdf$// ; }
85 else { $source_dir =~ s/\\auxiliary\\localize.sdf$// ; }
88 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";}
91 $treestrings = "$source_dir/text/shared/tree_strings.xhp";
92 } else {
93 $tree_src = "$prj\/source\/auxiliary";
94 $tree_dest = "$prj\/$destpath\/misc";
95 $source_dir = "$prj\/source";
96 $treestrings = "$source_dir/text/shared/tree_strings.xhp";
98 if ( defined $ENV{TRYSDF} && defined $ENV{LOCALIZESDF} )
100 if( defined $ENV{LOCALIZATION_FOUND} && $ENV{LOCALIZATION_FOUND} eq "YES" )
102 $source_dir = $ENV{TRYSDF};
104 else
106 $source_dir = $ENV{LOCALIZESDF};
108 if( $is_win eq "false" ) { $source_dir =~ s/\/auxiliary\/localize.sdf$// ; }
109 else { $source_dir =~ s/\\auxiliary\\localize.sdf$// ; }
111 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";}
115 # Get the English tree files as master
116 #-------------------------------
117 # Update English from xhp
118 #-------------------------------
119 &do_english;
120 #-------------------------------
121 # Update localizations from sdf
122 #-------------------------------
124 @langs = split /\s+/, $with_lang;
125 &read_loc;
126 print "################\nUpdating the treefiles for @langs \n";
127 for $l(@langs) {
128 if ($l ne "en-US") {
129 &do_lang($l);
133 #-------------------------------
135 $t1 = new Benchmark;
136 $td = timediff($t1, $t0);
137 print timestr($td),"\n";
139 ####################
140 # SUBS
141 ####################
142 sub terminate {
143 $err = shift;
144 print "$err\n\n";
145 $msg = <<"MSG";
147 update_tree.pl
148 all languages in WITH_LANG are processed. WITH_LANG=ALL is
149 not supported in manual calls.
151 Updates the *.tree files.
152 At first, the English file is updated based on the English
153 help topic titles as read from the help files. Then, the
154 localized tree files are written based on the English tree
155 file and the localized help topic titles.
157 Requires a valid SO/OOo environment.
159 print "$msg\n";
160 exit( -1 );
161 # die "$msg\n";
164 #---------------------------------------------------
166 sub do_english {
167 print "Processing en-US\n";
168 undef %helpsection; undef %node;
169 &readtreestrings;
170 &gettreefiles;
171 &processtreefiles('en-US');
174 #---------------------------------------------------
175 sub do_lang {
176 $lng = shift;
177 print "\n---------------------------------------------------\nProcessing $lng\n";
178 &processtreefiles($lng);
179 print "\n";
182 #---------------------------------------------------
183 sub readtreestrings {
184 print "Reading tree strings for en-US...";
185 if (open TREE, $treestrings) {
186 while (<TREE>) {
187 chomp;
188 s/<\/*help:productname>//gis;
189 if (/help_section/) {
190 s/^\s*<.*help_section//;
191 s/<\/.*$//;
192 ($id = $_) =~ s/^.*id=&quot;(\d+)&quot;.*$/$1/;
193 ($title = $_) =~ s/^.*title=&quot;(.*)&quot;.*$/$1/;
194 $helpsection{$id} = $title;
197 if (/node id=/) {
198 s/^\s*<.*node //;
199 s/<\/.*$//;
200 ($id = $_) =~ s/^.*id=&quot;(\d+)&quot;.*$/$1/;
201 ($title = $_) =~ s/^.*title=&quot;(.*)&quot;.*$/$1/;
202 $node{$id} = $title;
205 close TREE;
206 } else {
207 &terminate("Error opening $treestrings");
209 print "done\n";
212 #------------------------------------
213 sub gettreefiles {
214 # Read the tree files from the directory
215 # this list is also used for all foreign languages
216 print "Reading tree files...";
217 if (opendir ENUS, "$tree_src") {
218 @treeviews = grep /\.tree/, readdir ENUS;
219 closedir ENUS;
220 } else {
221 &terminate("Cannot open directory $tree_src");
223 print "done\n";
226 #------------------------------------
227 sub processtreefiles {
228 $lng = shift;
229 use File::Temp qw/ tempfile /;
230 use File::Spec;
232 for $tv(@treeviews) {
233 print "\nProcessing $tv\n";
234 @lines = &readtv("$tree_src/$tv");
235 print "Read ".scalar @lines." lines\n";
236 for $l(@lines) {
237 if ($l =~ /topic/) {
238 ($id = $l) =~ s/^.*id="([^"]*)".*$/$1/gis;
239 ($module = $id) =~ s/^([^\/]*).*$/$1/;
240 $id =~ s/^.*?\///;
241 $file = "$source_dir/$id";
243 if ($lng eq 'en-US') { # english comes from the file
244 if (open F,$file) {
245 print ".";
246 undef $/; $cnt = <F>; close F;
247 $cnt =~ s/^.*<title[^>]+id="tit"[^>]*>([^<]*)<\/title>.*$/$1/gis;
248 $cnt =~ s/&apos;/\'/gis; $cnt =~ s/&amp;/+/gis;
249 $cnt =~ s/&quot;/\'/gis; $cnt =~ s/&/+/gis;
250 $l = "<topic id=\"$module/$id\">$cnt</topic>\n";
251 } else {
252 print "!";
253 $l = "<!-- removed $module/$id -->\n";
255 } else { # localized comes from the localize sdf
256 #print "\nid: $id";
257 if (defined($loc_title{$lng}->{$id})) {
258 print ".";
259 $l = "<topic id=\"$module/$id\">$loc_title{$lng}->{$id}</topic>\n";
260 } else {
261 print "!";
266 if ($l =~/<node/) {
267 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis;
268 if ($lng eq 'en-US') {
269 if (defined($node{$id})) {
270 $l =~ s/title="(.*)"/title="$node{$id}"/;
271 } else {
272 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
274 } else {
275 if (defined($node{$lng}->{$id})) {
276 $l =~ s/title="(.*)"/title="$node{$lng}->{$id}"/;
281 if ($l =~/<help_section/) {
282 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis;
283 if ($lng eq 'en-US') {
284 if (defined($helpsection{$id})) {
285 $l =~ s/title="(.*)"/title="$helpsection{$id}"/;
286 } else {
287 print "#";
288 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
290 } else {
291 if (defined($helpsection{$lng}->{$id})) {
292 $l =~ s/title="(.*)"/title="$helpsection{$lng}->{$id}"/;
297 if ( ! -d "$tree_dest/$lng" ) {
298 mkdir "$tree_dest/$lng" or die "\nCouldn't create directory \"$tree_dest/$lng\"";
300 my $treeoutdir = "$tree_dest/$lng";
301 my $tmpname_template=$tv."_XXXXX";
302 my ( $treetmpfilehandle, $treetmpfile ) = tempfile($tmpname_template , DIR => File::Spec->tmpdir() );
303 close $treetmpfilehandle ;
304 if (open TV, ">$treetmpfile") {
305 for $line(@lines) {
306 $line =~ s/\$\[officename\]/%PRODUCTNAME/g;
307 $line =~ s/\$\[officeversion\]/%PRODUCTVERSION/g;
308 print TV $line;
310 close TV;
311 chmod 0664, $treetmpfile or &terminate("Cannot change rights on $treetmpfile");
312 if( $^O eq 'MSWin32' )
314 $tree_dest =~ s/\//\\/g ;
315 unlink "$tree_dest\\$lng\\$tv" ;
316 mv $treetmpfile , "$tree_dest\\$lng\\$tv" or &terminate("Cannot mv $treetmpfile to $tree_dest\\$lng\\$tv" );
318 else
320 unlink "$tree_dest/$lng/$tv" ;
321 my $ret=mv $treetmpfile , "$tree_dest/$lng/$tv$inpath" or &terminate("Cannot write to $tree_dest/$lng/$tv$inpath - Error $!");
322 my $ret=mv "$tree_dest/$lng/$tv$inpath" , "$tree_dest/$lng/$tv" or &terminate("Cannot write to $tree_dest/$lng/$tv - Error $!");
324 } else {
325 &terminate("Cannot write to $tvout");
330 #------------------------------------
331 sub readtv {
332 my $f = shift;
333 if (open TV, $f) {
334 $/ = "\n";
335 my @l = <TV>;
336 close TV;
337 return @l;
338 } else {
339 &terminate("Error opening $f");
343 #------------------------------------
344 # read entries form localize.sdf files
345 #------------------------------------
346 sub read_loc {
347 print "\n\nReading localized titles...";
348 $/ = "\n";
349 my $path = "$source_dir/text";
350 $path =~ s/\//\\/g , if $is_win eq "true";
351 print " in $source_dir/text\n";
352 @files = `find $source_dir/text -name localize.sdf`;
353 for my $fname (@files) {
354 $FS = '\t';
355 print ".";
356 open(LOCALIZE_SDF, $fname) || die 'Cannot open "localize.sdf".'."$fname";
357 while (<LOCALIZE_SDF>) {
358 my $sdf_line = $_;
359 my ($Fld1,$file,$Fld3,$Fld4,$id,$Fld6,$Fld7,$Fld8,$Fld9,$lang,$text) = split($FS, $sdf_line , 12);
360 next if ( $Fld1 =~ /^#/);
361 if ($id eq 'tit') {
362 #strip filename
363 $file =~ s/.*text\\/text\\/g;
364 #convert \ to / in filename
365 $file =~ s/\\/\//g;
366 #fpe: i46823 - need to encode &s, added encoding
367 $text =~ s/&(?!amp;)/&amp;/g;
368 # add entry to the hash
369 $loc_title{$lang}->{$file} = $text;
371 if ($file =~ /tree_strings.xhp/) {
372 #strip filename
373 $file =~ s/.*text/text/g;
374 #convert \ to / in filename
375 $file =~ s/\\/\//g;
376 if ($text =~ /^<help_section/) {
377 #example: <help_section application="scalc" id="08" title="表計算ドキュメント">
378 my ($fld1,$app,$fld3,$id,$fld5,$sec_title) = split('"', $text, 7);
379 #fpe: i46823 - need to encode &s, added encoding
380 if( defined $sec_title )
382 $sec_title =~ s/&(?!amp;)/&amp;/g;
383 #unquot \<item ... /\>
384 terminate( "\n\nERROR: Bad string in file '$fname' will cause invalid xml tree file \n---\n'$sdf_line'\n---\nPlease remove or replace < = '&lt;' and > = '&gt;' within the title attribute '$sec_title'\n") , if( $sec_title =~ /[\<\>]/ );
385 $helpsection{$lang}->{$id} = $sec_title;
387 } elsif ($text =~/<node id=/) {
388 # example: <node id="0205" title="Tabelas em documentos de texto">
389 # BEWARE: title may contain escaped '"' so only match " not preceded by \
390 # using a zero‐width negative look‐behind assertion.
391 my ($fld1,$id,$fld3,$node_title,$Fld5) = split(/(?<!\\)"/, $text, 5);
392 #fpe: i46823 - need to encode &s, added encoding
393 if( defined $node_title )
395 $node_title =~ s/&(?!amp;)/&amp;/g;
396 terminate( "\n\nERROR: Bad string in '$fname' will cause invalid xml tree file \n---\n'$sdf_line'\n---\nPlease remove or replace < = '&lt;' and > = '&gt;' within the title attribute '$node_title'\n") , if( $node_title =~ /[\<\>]/ );
398 $node{$lang}->{$id} = $node_title;
402 close LOCALIZE_SDF;
404 # statistics
405 $total_elements=0;
406 foreach $lang (keys %loc_title) {
407 $no_elements = scalar(keys(%{$loc_title{$lang}}));
408 push(@langstat, "$lang:\t ".$no_elements." matches\n");
409 $total_elements += $no_elements;
411 print "\ndone reading a total of ".$total_elements." localized titles for ".scalar(keys(%loc_title))." languages from ".scalar @files ." files\n";
412 print sort(@langstat);