merge the formfield patch from ooo-build
[ooovba.git] / helpcontent2 / helpers / update_tree.pl
blob71579750adcc47a3b63153dae49ea1b17099737c
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 ($source_dir_xhp = $scriptname) =~ s/\/update_tree.pl/\/..\/source/;
76 if ( defined $ENV{TRYSDF} && defined $ENV{LOCALIZESDF} )
78 if( defined $ENV{LOCALIZATION_FOUND} && $ENV{LOCALIZATION_FOUND} eq "YES" )
80 $source_dir = $ENV{TRYSDF};
82 else
84 $source_dir = $ENV{LOCALIZESDF};
86 if( $is_win eq "false" ) { $source_dir =~ s/\/auxiliary\/localize.sdf$// ; }
87 else { $source_dir =~ s/\\auxiliary\\localize.sdf$// ; }
90 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";}
93 $treestrings = "$source_dir/text/shared/tree_strings.xhp";
94 } else {
95 $tree_src = "$prj\/source\/auxiliary";
96 $tree_dest = "$prj\/$destpath\/misc";
97 $source_dir = "$prj\/source";
98 $source_dir_xhp = "$prj\/source";
99 $treestrings = "$source_dir/text/shared/tree_strings.xhp";
101 if ( defined $ENV{TRYSDF} && defined $ENV{LOCALIZESDF} )
103 if( defined $ENV{LOCALIZATION_FOUND} && $ENV{LOCALIZATION_FOUND} eq "YES" )
105 $source_dir = $ENV{TRYSDF};
107 else
109 $source_dir = $ENV{LOCALIZESDF};
111 if( $is_win eq "false" ) { $source_dir =~ s/\/auxiliary\/localize.sdf$// ; }
112 else { $source_dir =~ s/\\auxiliary\\localize.sdf$// ; }
114 #else {die "ERROR: The env variables TRYSDF LOCALIZATION_FOUND LOCALIZESDF not found ... something is wrong!\n";}
118 # Get the English tree files as master
119 #-------------------------------
120 # Update English from xhp
121 #-------------------------------
122 &do_english;
123 #-------------------------------
124 # Update localizations from sdf
125 #-------------------------------
127 @langs = split /\s+/, $with_lang;
128 &read_loc;
129 print "################\nUpdating the treefiles for @langs \n";
130 for $l(@langs) {
131 if ($l ne "en-US") {
132 &do_lang($l);
136 #-------------------------------
138 $t1 = new Benchmark;
139 $td = timediff($t1, $t0);
140 print timestr($td),"\n";
142 ####################
143 # SUBS
144 ####################
145 sub terminate {
146 $err = shift;
147 print "$err\n\n";
148 $msg = <<"MSG";
150 update_tree.pl
151 all languages in WITH_LANG are processed. WITH_LANG=ALL is
152 not supported in manual calls.
154 Updates the *.tree files.
155 At first, the English file is updated based on the English
156 help topic titles as read from the help files. Then, the
157 localized tree files are written based on the English tree
158 file and the localized help topic titles.
160 Requires a valid SO/OOo environment.
162 print "$msg\n";
163 exit( -1 );
164 # die "$msg\n";
167 #---------------------------------------------------
169 sub do_english {
170 print "Processing en-US\n";
171 undef %helpsection; undef %node;
172 &readtreestrings;
173 &gettreefiles;
174 &processtreefiles('en-US');
177 #---------------------------------------------------
178 sub do_lang {
179 $lng = shift;
180 print "\n---------------------------------------------------\nProcessing $lng\n";
181 &processtreefiles($lng);
182 print "\n";
185 #---------------------------------------------------
186 sub readtreestrings {
187 print "Reading tree strings for en-US...";
188 if (open TREE, $treestrings) {
189 while (<TREE>) {
190 chomp;
191 s/<\/*help:productname>//gis;
192 if (/help_section/) {
193 s/^\s*<.*help_section//;
194 s/<\/.*$//;
195 ($id = $_) =~ s/^.*id=&quot;(\d+)&quot;.*$/$1/;
196 ($title = $_) =~ s/^.*title=&quot;(.*)&quot;.*$/$1/;
197 $helpsection{$id} = $title;
200 if (/node id=/) {
201 s/^\s*<.*node //;
202 s/<\/.*$//;
203 ($id = $_) =~ s/^.*id=&quot;(\d+)&quot;.*$/$1/;
204 ($title = $_) =~ s/^.*title=&quot;(.*)&quot;.*$/$1/;
205 $node{$id} = $title;
208 close TREE;
209 } else {
210 &terminate("Error opening $treestrings");
212 print "done\n";
215 #------------------------------------
216 sub gettreefiles {
217 # Read the tree files from the directory
218 # this list is also used for all foreign languages
219 print "Reading tree files...";
220 if (opendir ENUS, "$tree_src") {
221 @treeviews = grep /\.tree/, readdir ENUS;
222 closedir ENUS;
223 } else {
224 &terminate("Cannot open directory $tree_src");
226 print "done\n";
229 #------------------------------------
230 sub processtreefiles {
231 $lng = shift;
232 use File::Temp qw/ tempfile /;
233 use File::Spec;
235 for $tv(@treeviews) {
236 print "\nProcessing $tv\n";
237 @lines = &readtv("$tree_src/$tv");
238 print "Read ".scalar @lines." lines\n";
239 for $l(@lines) {
240 if ($l =~ /topic/) {
241 ($id = $l) =~ s/^.*id="([^"]*)".*$/$1/gis;
242 ($module = $id) =~ s/^([^\/]*).*$/$1/;
243 $id =~ s/^.*?\///;
244 $file = "$source_dir_xhp/$id";
246 if ($lng eq 'en-US') { # english comes from the file
247 if (open F,$file) {
248 print ".";
249 undef $/; $cnt = <F>; close F;
250 $cnt =~ s/^.*<title[^>]+id="tit"[^>]*>([^<]*)<\/title>.*$/$1/gis;
251 $cnt =~ s/&apos;/\'/gis; $cnt =~ s/&amp;/+/gis;
252 $cnt =~ s/&quot;/\'/gis; $cnt =~ s/&/+/gis;
253 $l = "<topic id=\"$module/$id\">$cnt</topic>\n";
254 } else {
255 print "!";
256 $l = "<!-- removed $module/$id -->\n";
258 } else { # localized comes from the localize sdf
259 #print "\nid: $id";
260 if (defined($loc_title{$lng}->{$id})) {
261 print ".";
262 $l = "<topic id=\"$module/$id\">$loc_title{$lng}->{$id}</topic>\n";
263 } else {
264 print "!";
269 if ($l =~/<node/) {
270 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis;
271 if ($lng eq 'en-US') {
272 if (defined($node{$id})) {
273 $l =~ s/title="(.*)"/title="$node{$id}"/;
274 } else {
275 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
277 } else {
278 if (defined($node{$lng}->{$id})) {
279 $l =~ s/title="(.*)"/title="$node{$lng}->{$id}"/;
284 if ($l =~/<help_section/) {
285 ($id = $l) =~ s/^.*id="(\d+)".*$/$1/gis;
286 if ($lng eq 'en-US') {
287 if (defined($helpsection{$id})) {
288 $l =~ s/title="(.*)"/title="$helpsection{$id}"/;
289 } else {
290 print "#";
291 $l =~ s/title="(.*)"/title="NOTFOUND:$id"/;
293 } else {
294 if (defined($helpsection{$lng}->{$id})) {
295 $l =~ s/title="(.*)"/title="$helpsection{$lng}->{$id}"/;
300 if ( ! -d "$tree_dest/$lng" ) {
301 mkdir "$tree_dest/$lng" or die "\nCouldn't create directory \"$tree_dest/$lng\"";
303 my $treeoutdir = "$tree_dest/$lng";
304 my $tmpname_template=$tv."_XXXXX";
305 my ( $treetmpfilehandle, $treetmpfile ) = tempfile($tmpname_template , DIR => File::Spec->tmpdir() );
306 close $treetmpfilehandle ;
307 if (open TV, ">$treetmpfile") {
308 for $line(@lines) {
309 $line =~ s/\$\[officename\]/%PRODUCTNAME/g;
310 $line =~ s/\$\[officeversion\]/%PRODUCTVERSION/g;
311 print TV $line;
313 close TV;
314 chmod 0664, $treetmpfile or &terminate("Cannot change rights on $treetmpfile");
315 if( $^O eq 'MSWin32' )
317 $tree_dest =~ s/\//\\/g ;
318 unlink "$tree_dest\\$lng\\$tv" ;
319 mv $treetmpfile , "$tree_dest\\$lng\\$tv" or &terminate("Cannot mv $treetmpfile to $tree_dest\\$lng\\$tv" );
321 else
323 unlink "$tree_dest/$lng/$tv" ;
324 my $ret=mv $treetmpfile , "$tree_dest/$lng/$tv$inpath" or &terminate("Cannot write to $tree_dest/$lng/$tv$inpath - Error $!");
325 my $ret=mv "$tree_dest/$lng/$tv$inpath" , "$tree_dest/$lng/$tv" or &terminate("Cannot write to $tree_dest/$lng/$tv - Error $!");
327 } else {
328 &terminate("Cannot write to $tvout");
333 #------------------------------------
334 sub readtv {
335 my $f = shift;
336 if (open TV, $f) {
337 $/ = "\n";
338 my @l = <TV>;
339 close TV;
340 return @l;
341 } else {
342 &terminate("Error opening $f");
346 #------------------------------------
347 # read entries form localize.sdf files
348 #------------------------------------
349 sub read_loc {
350 print "\n\nReading localized titles...";
351 $/ = "\n";
352 my $path = "$source_dir/text";
353 $path =~ s/\//\\/g , if $is_win eq "true";
354 print " in $source_dir/text\n";
355 @files = `find $source_dir/text -name localize.sdf`;
356 for my $fname (@files) {
357 $FS = '\t';
358 print ".";
359 open(LOCALIZE_SDF, $fname) || die 'Cannot open "localize.sdf".'."$fname";
360 while (<LOCALIZE_SDF>) {
361 my $sdf_line = $_;
362 my ($Fld1,$file,$Fld3,$Fld4,$id,$Fld6,$Fld7,$Fld8,$Fld9,$lang,$text) = split($FS, $sdf_line , 12);
363 next if ( $Fld1 =~ /^#/);
364 if ($id eq 'tit') {
365 #strip filename
366 $file =~ s/.*text\\/text\\/g;
367 #convert \ to / in filename
368 $file =~ s/\\/\//g;
369 #fpe: i46823 - need to encode &s, added encoding
370 $text =~ s/&(?!amp;)/&amp;/g;
371 # add entry to the hash
372 $loc_title{$lang}->{$file} = $text;
374 if ($file =~ /tree_strings.xhp/) {
375 #strip filename
376 $file =~ s/.*text/text/g;
377 #convert \ to / in filename
378 $file =~ s/\\/\//g;
379 if ($text =~ /^<help_section/) {
380 #example: <help_section application="scalc" id="08" title="表計算ドキュメント">
381 my ($fld1,$app,$fld3,$id,$fld5,$sec_title) = split('"', $text, 7);
382 #fpe: i46823 - need to encode &s, added encoding
383 if( defined $sec_title )
385 $sec_title =~ s/&(?!amp;)/&amp;/g;
386 #unquot \<item ... /\>
387 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 =~ /[\<\>]/ );
388 $helpsection{$lang}->{$id} = $sec_title;
390 } elsif ($text =~/<node id=/) {
391 # example: <node id="0205" title="Tabelas em documentos de texto">
392 # BEWARE: title may contain escaped '"' so only match " not preceded by \
393 # using a zero‐width negative look‐behind assertion.
394 my ($fld1,$id,$fld3,$node_title,$Fld5) = split(/(?<!\\)"/, $text, 5);
395 #fpe: i46823 - need to encode &s, added encoding
396 if( defined $node_title )
398 $node_title =~ s/&(?!amp;)/&amp;/g;
399 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 =~ /[\<\>]/ );
401 $node{$lang}->{$id} = $node_title;
405 close LOCALIZE_SDF;
407 # statistics
408 $total_elements=0;
409 foreach $lang (keys %loc_title) {
410 $no_elements = scalar(keys(%{$loc_title{$lang}}));
411 push(@langstat, "$lang:\t ".$no_elements." matches\n");
412 $total_elements += $no_elements;
414 print "\ndone reading a total of ".$total_elements." localized titles for ".scalar(keys(%loc_title))." languages from ".scalar @files ." files\n";
415 print sort(@langstat);