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