2 eval 'exec perl -wS $0 ${1+"$@"}'
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: update_tree.pl,v $
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 #*************************************************************************
37 use File
::Copy qw
/cp mv/;
43 $is_win = "true" , if ( defined $ENV{USE_SHELL
} && $ENV{USE_SHELL
} eq "4nt" );
46 # update the tree files in <platform>/misc/*
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);
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
};
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";
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
};
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 #-------------------------------
120 #-------------------------------
121 # Update localizations from sdf
122 #-------------------------------
124 @langs = split /\s+/, $with_lang;
126 print "################\nUpdating the treefiles for @langs \n";
133 #-------------------------------
136 $td = timediff
($t1, $t0);
137 print timestr
($td),"\n";
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.
164 #---------------------------------------------------
167 print "Processing en-US\n";
168 undef %helpsection; undef %node;
171 &processtreefiles
('en-US');
174 #---------------------------------------------------
177 print "\n---------------------------------------------------\nProcessing $lng\n";
178 &processtreefiles
($lng);
182 #---------------------------------------------------
183 sub readtreestrings
{
184 print "Reading tree strings for en-US...";
185 if (open TREE
, $treestrings) {
188 s/<\/*help:productname>//gis
;
189 if (/help_section/) {
190 s/^\s*<.*help_section//;
192 ($id = $_) =~ s/^.*id="(\d+)".*$/$1/;
193 ($title = $_) =~ s/^.*title="(.*)".*$/$1/;
194 $helpsection{$id} = $title;
200 ($id = $_) =~ s/^.*id="(\d+)".*$/$1/;
201 ($title = $_) =~ s/^.*title="(.*)".*$/$1/;
207 &terminate
("Error opening $treestrings");
212 #------------------------------------
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
;
221 &terminate
("Cannot open directory $tree_src");
226 #------------------------------------
227 sub processtreefiles
{
229 use File
::Temp qw
/ tempfile /;
232 for $tv(@treeviews) {
233 print "\nProcessing $tv\n";
234 @lines = &readtv
("$tree_src/$tv");
235 print "Read ".scalar @lines." lines\n";
238 ($id = $l) =~ s/^.*id="([^"]*)".*$/$1/gis;
239 ($module = $id) =~ s/^([^\/]*).*$/$1/;
241 $file = "$source_dir/$id";
243 if ($lng eq 'en-US') { # english comes from the file
246 undef $/; $cnt = <F
>; close F
;
247 $cnt =~ s/^.*<title[^>]+id="tit"[^>]*>([^<]*)<\/title>.*$/$1/gis
;
248 $cnt =~ s/'/\'/gis; $cnt =~ s/&/+/gis;
249 $cnt =~ s/"/\'/gis; $cnt =~ s/&/+/gis;
250 $l = "<topic id=\"$module/$id\">$cnt</topic>\n";
253 $l = "<!-- removed $module/$id -->\n";
255 } else { # localized comes from the localize sdf
257 if (defined($loc_title{$lng}->{$id})) {
259 $l = "<topic id=\"$module/$id\">$loc_title{$lng}->{$id}</topic>\n";
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}"/;
272 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
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}"/;
288 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
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") {
306 $line =~ s/\$\[officename\]/%PRODUCTNAME/g;
307 $line =~ s/\$\[officeversion\]/%PRODUCTVERSION/g;
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" );
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 $!");
325 &terminate
("Cannot write to $tvout");
330 #------------------------------------
339 &terminate
("Error opening $f");
343 #------------------------------------
344 # read entries form localize.sdf files
345 #------------------------------------
347 print "\n\nReading localized titles...";
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) {
356 open(LOCALIZE_SDF
, $fname) || die 'Cannot open "localize.sdf".'."$fname";
357 while (<LOCALIZE_SDF
>) {
359 my ($Fld1,$file,$Fld3,$Fld4,$id,$Fld6,$Fld7,$Fld8,$Fld9,$lang,$text) = split($FS, $sdf_line , 12);
360 next if ( $Fld1 =~ /^#/);
363 $file =~ s/.*text\\/text\\/g;
364 #convert \ to / in filename
366 #fpe: i46823 - need to encode &s, added encoding
367 $text =~ s/&(?!amp;)/&/g;
368 # add entry to the hash
369 $loc_title{$lang}->{$file} = $text;
371 if ($file =~ /tree_strings.xhp/) {
373 $file =~ s/.*text/text/g;
374 #convert \ to / in filename
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;)/&/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 < = '<' and > = '>' 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;)/&/g;
396 terminate
( "\n\nERROR: Bad string in '$fname' will cause invalid xml tree file \n---\n'$sdf_line'\n---\nPlease remove or replace < = '<' and > = '>' within the title attribute '$node_title'\n") , if( $node_title =~ /[\<\>]/ );
398 $node{$lang}->{$id} = $node_title;
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);