Source files of fsfe.org, pdfreaders.org, freeyourandroid.org, ilovefs.org, drm.info, and test.fsfe.org. Contribute: https://fsfe.org/contribute/web/
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. #! /usr/bin/perl
  2. #
  3. # build.pl - a tool for building FSFE web pages
  4. #
  5. # Copyright (C) 2003 Jonas Öberg
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. # General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  20. # 02110-1301, USA.
  21. #
  22. use File::Find::Rule;
  23. use Getopt::Std;
  24. use File::Path;
  25. use File::Basename;
  26. use XML::LibXSLT;
  27. use XML::LibXML;
  28. use File::Copy;
  29. use POSIX qw(strftime);
  30. use IO::Handle;
  31. use IO::Select;
  32. use Socket;
  33. use Fcntl ':flock';
  34. # This defines the focuses and their respective preferred / original
  35. # language. For example, it says that we should have a focus called
  36. # "se" (Sweden) which has the preferred language "sv" (Swedish).
  37. #
  38. # This also says that documents in the directory /se should be considered
  39. # as having the Swedish version as the original, and so on.
  40. #
  41. our %countries = (global => 'en');
  42. #our %countries = (
  43. # global => 'en',
  44. # de => 'de',
  45. # es => 'es',
  46. # it => 'it',
  47. # fr => 'fr',
  48. # se => 'sv' );
  49. #
  50. # This is a hash of all the languages that we have translations into, and their
  51. # respective names in the local language. Make sure that one entry exists
  52. # here for every language, or it won't be rendered.
  53. #
  54. # NOTE: Make sure that the language also is added to Apache configuration so
  55. # content negotiation works.
  56. #
  57. our %languages = (
  58. ar => 'العربيّة',
  59. bg => 'Български',
  60. ca => 'Català',
  61. cs => 'Česky',
  62. da => 'Dansk',
  63. de => 'Deutsch',
  64. el => 'Ελληνικά',
  65. en => 'English',
  66. es => 'Español',
  67. et => 'Eesti',
  68. fi => 'Suomi',
  69. fr => 'Français',
  70. hr => 'Hrvatski',
  71. hu => 'Magyar',
  72. it => 'Italiano',
  73. ku => 'Kurdî',
  74. mk => 'Mакедонски',
  75. nb => 'Norsk (bokmål)',
  76. nl => 'Nederlands',
  77. nn => 'Norsk (nynorsk)',
  78. pl => 'Polski',
  79. pt => 'Português',
  80. ro => 'Română',
  81. ru => 'Русский',
  82. sk => 'Slovenčina',
  83. sl => 'Slovenščina',
  84. sq => 'Shqip',
  85. sr => 'Srpski',
  86. sv => 'Svenska',
  87. tr => 'Türkçe',
  88. );
  89. our $current_date = strftime "%Y-%m-%d", localtime;
  90. our $current_time = strftime "%Y-%m-%d %H:%M:%S", localtime;
  91. # This static array contains files that can't be out of date
  92. our %cant_be_outdated = (
  93. "news/news" => 1,
  94. "index" => 1
  95. );
  96. #
  97. # Parse the command line options. We need two; where to put the finished
  98. # pages and what to use as base for the input.
  99. #
  100. getopts('o:i:t:duqn', \%opts);
  101. unless ($opts{o}) {
  102. print STDERR "Usage: $0 [-q] [-u] [-d] [-n] [-t #] -o <output directory>\n";
  103. print STDERR " -q Quiet\n";
  104. print STDERR " -u Update only\n";
  105. print STDERR " -d Print some debug information\n";
  106. print STDERR " -n Don't write any files\n";
  107. print STDERR " -t Number of worker childs to create (default: 1)\n";
  108. exit 1;
  109. }
  110. # It might be nice to be able to specify this, but it will break things as
  111. # they are now. This is on the TODO list :-)
  112. $opts{i} = ".";
  113. $| = 1;
  114. $SIG{CHLD} = 'IGNORE';
  115. # Create XML and XSLT parser contexts. Also create the root note for the
  116. # above mentioned XML file (used to feed the XSL transformation).
  117. my $parser = XML::LibXML->new();
  118. my $xslt_parser = XML::LibXSLT->new();
  119. # Parse the global stylesheet
  120. my $global_style_doc = $parser->parse_file($opts{i}."/fsfe.xsl");
  121. my $global_stylesheet = $xslt_parser->parse_stylesheet($global_style_doc);
  122. #
  123. # First topic of today: create all directories we need. Instead of creating
  124. # these as they are used, we create them in a batch at the beginning of each
  125. # run, so we won't have to worry about them later.
  126. # Note though that this also REMOVES the previous paths. You don't want to
  127. # build directly into the production web tree.
  128. #
  129. my @dirs = File::Find::Rule->directory()
  130. ->in($opts{i});
  131. while (my ($path, undef) = each %countries) {
  132. print STDERR "Reseting path for $path\n" unless $opts{q};
  133. rmtree($opts{o}.'/'.$path) unless ($opts{u} || $opts{n});
  134. my @paths = map { $opts{o}."/$path/".$_ } grep(!/^\.\.?$/, @dirs);
  135. foreach (@paths) {
  136. print "Creating $_\n" if $opts{d};
  137. mkpath($_) unless $opts{n};
  138. }
  139. }
  140. #
  141. # Here starts our real work. First we get ourselves a list of all files
  142. # that we need to worry about and then single out the XHTML files. We
  143. # create a hash of hashes, %bases, which contains the basename of each
  144. # file, together with the translations that it exists in.
  145. #
  146. my @files = File::Find::Rule->file()
  147. ->in($opts{i});
  148. my %bases;
  149. foreach (grep(/\.xhtml$/, @files)) {
  150. $_ =~ s/^$opts{i}\/?// unless $opts{i} eq ".";
  151. my ($lang) = ($_ =~ /\.([a-z][a-z])\.xhtml$/);
  152. unless ($lang) { $lang = "en"; }
  153. $_ =~ s/\.[a-z][a-z]\.xhtml$//;
  154. $_ =~ s/\.xhtml$//;
  155. $bases{$_}{$lang} = 1;
  156. }
  157. # Open the file where we will log all outdated and missing translations
  158. open (TRANSLATIONS, '>', "$opts{o}/translations.log");
  159. #
  160. # For each file, translation and focus, we create a new XML file. This will
  161. # contain all information that the XSL needs to produce a finished page.
  162. # The XML file will look like this:
  163. #
  164. # <buildinfo>
  165. # <trlist> <!-- All translations that this page exists in -->
  166. # <tr id="sv">Svenska</tr>
  167. # ...
  168. # </trlist>
  169. # <localmenuset> <!-- Local menu items for some directories -->
  170. # ...
  171. # </localmenuset>
  172. # <menuset> <!-- The menu items for the left hand bar -->
  173. # ...
  174. # </menuset>
  175. # <textset> <!-- The static text set for this language -->
  176. # ...
  177. # </textset>
  178. # <textsetbackup> <!-- The English textset as backup for missing translations -->
  179. # ...
  180. # </textsetbackup>
  181. # <document> <!-- The actual document, as read from the XHTML -->
  182. # <head>
  183. # <title>...</title>
  184. # <body>...</body>
  185. # </head>
  186. # </document>
  187. # </buildinfo>
  188. #
  189. # In addition to this, the buildinfo and document root will be equipped with
  190. # the following attributes:
  191. #
  192. # buildinfo/@original The language code of the original document
  193. # buildinfo/@filename The filename without language or trailing .html
  194. # buildinfo/@dirname The path to the file
  195. # buildinfo/@language The language that we're building into
  196. # buildinfo/@outdated Set to "yes" if the original is newer than this page
  197. # document/@language The language that this documents is in
  198. #
  199. #
  200. # $threads is the number of child processes to fork off to build the tree
  201. #
  202. unless ($threads = $opts{t}) {
  203. $threads = 1;
  204. }
  205. #
  206. # Start the required number of children, for each child we create a socket
  207. # pair to communicate between parent and child. This information is kept in
  208. # the %procs hash, which contains file handles for both child and parent.
  209. #
  210. foreach my $i (1..$threads) {
  211. $procs[$i]{child} = new IO::Handle;
  212. $procs[$i]{parent} = new IO::Handle;
  213. socketpair($procs[$i]{child}, $procs[$i]{parent}, AF_UNIX,
  214. SOCK_STREAM, PF_UNSPEC);
  215. $procs[$i]{child}->autoflush(1);
  216. $procs[$i]{parent}->autoflush(1);
  217. #$procs[$i]{child}->blocking(false);
  218. #$procs[$i]{parent}->blocking(false);
  219. if (fork()) {
  220. #
  221. # The parent doesn't do anything at this stage, except close one of
  222. # the filehandes not used.
  223. #
  224. close($procs[$i]{parent});
  225. } else {
  226. #
  227. # This is the main worker for the children, which wait for a command
  228. # to execute, either DIE or PROCESS. In the case of the first, the child
  229. # exists gracefully, in the case of the second, it calls on process()
  230. # to build the required page and languages.
  231. #
  232. # When waiting for the next page to be sent to it, the child sends NEXT
  233. # to the parent to signify that it's ready for the next command.
  234. #
  235. close($procs[$i]{child});
  236. my $io = $procs[$i]{parent};
  237. print $io "NEXT\n";
  238. while (!$io->error) {
  239. my $cmd = <$io>;
  240. if ($cmd =~ /DIE/) {
  241. exit;
  242. } elsif ($cmd =~ /PROCESS/) {
  243. chomp($cmd);
  244. my (undef, $file, $langs) = split(/\|/, $cmd);
  245. process($file, $langs);
  246. print $io "NEXT\n";
  247. }
  248. }
  249. exit;
  250. }
  251. }
  252. #
  253. # This sets up an IO::Select object with the filehandles of all children.
  254. # The parent uses this when looking for the next available child and blocks
  255. # until any child is ready.
  256. #
  257. my $s = IO::Select->new();
  258. foreach my $i (1..$threads) {
  259. $s->add($procs[$i]{child});
  260. }
  261. while (my ($file, $langs) = each %bases) {
  262. $s->can_read();
  263. my $done = 0;
  264. while (!$done) {
  265. foreach my $fh ($s->can_read()) {
  266. $cmd = <$fh>;
  267. if ($cmd =~ /NEXT/) {
  268. printf $fh "PROCESS|%s|%s\n", $file, join(':', keys(%{$langs}));
  269. $done = 1;
  270. last;
  271. }
  272. }
  273. }
  274. }
  275. #
  276. # When done, we send the DIE command to each child.
  277. #
  278. foreach my $i (1..$threads) {
  279. my $io = $procs[$i]{child};
  280. print $io "DIE\n";
  281. }
  282. #
  283. # This ensures a timely wait for every child to finish processing and shutdown.
  284. #
  285. while (wait() != -1) {
  286. sleep 2;
  287. }
  288. sub process {
  289. my ($file, $langs) = @_;
  290. # if (not $file eq "donate/thankgnus") {
  291. # return;
  292. #}
  293. print "$file\n";
  294. print STDERR "Building $file.. \n" unless $opts{q};
  295. # Create the root note for the above mentioned XML file (used to feed the XSL
  296. # transformation).
  297. my $dom = XML::LibXML::Document->new("1.0", "iso-8859-1");
  298. my $root = $dom->createElement("buildinfo");
  299. $dom->setDocumentElement($root);
  300. #
  301. # Set the current date, to use for comparision in the XSLT.
  302. #
  303. $root->setAttribute("date", $current_date);
  304. #
  305. # Find original language. It's en, unless we're in the country specific
  306. # se/, fr/, de/ and so on, directories.
  307. #
  308. $root->setAttribute("original", "en");
  309. my $srcfocus = "global";
  310. if ($file =~ /^([a-z][a-z])\//) {
  311. $srcfocus = "$1";
  312. $root->setAttribute("original", $countries{$1});
  313. }
  314. $root->setAttribute("filename", "/$file");
  315. #
  316. # Set the directory name attribute
  317. #
  318. my (undef, $current_dir, undef) = fileparse($file);
  319. $root->setAttribute("dirname", "$current_dir");
  320. #
  321. # Find all translations for this document, and create the trlist set
  322. # for them.
  323. #
  324. my $trlist = $dom->createElement("trlist");
  325. foreach my $lang (split(/:/, $langs)) {
  326. my $tr = $dom->createElement("tr");
  327. $tr->setAttribute("id", $lang);
  328. $tr->appendText($languages{$lang});
  329. $trlist->appendChild($tr);
  330. }
  331. $root->appendChild($trlist);
  332. #
  333. # Load the file with local menu's
  334. #
  335. my $localmenu = "$opts{i}/localmenuinfo.xml";
  336. if (-f $localmenu) {
  337. my $menudoc = $dom->createElement("localmenuset");
  338. $root->appendChild($menudoc);
  339. clone_document($menudoc, $localmenu);
  340. }
  341. #
  342. # Load English backup texts
  343. #
  344. my $backup = $dom->createElement("textsetbackup");
  345. $root->appendChild($backup);
  346. clone_document($backup, $opts{i}."/tools/texts-en.xml");
  347. #
  348. # Transform it, once for every focus!
  349. #
  350. while (my ($dir, undef) = each %countries) {
  351. # If we handle a focus specific file, only process it in that focus
  352. # -> we don't handle focus-specific files anymore, commenting next line out, since it's causing errors
  353. #next if (("$srcfocus" ne "global") && ("$dir" ne "$srcfocus"));
  354. print STDERR "$dir " unless $opts{q};
  355. #
  356. # And once for every language!
  357. #
  358. while (my ($lang, undef) = each %languages) {
  359. $root->setAttribute("language", $lang);
  360. #
  361. # This finds the source file to use. If we can't find a translation
  362. # into the language, it uses the english version instead, or that in
  363. # the local language. Or the first version it finds. This should be
  364. # made prettier.
  365. #
  366. my $document = $dom->createElement("document");
  367. $document->setAttribute("language", $lang);
  368. $root->appendChild($document);
  369. my $source = "$opts{i}/$file.$lang.xhtml";
  370. unless (-f $source) {
  371. my $missingsource = $source;
  372. if (-f "$opts{i}/$file.en.xhtml") {
  373. $document->setAttribute("language", "en");
  374. $source = "$opts{i}/$file.en.xhtml";
  375. } elsif (-f "$opts{i}/$file.".$root->getAttribute("original").".xhtml") {
  376. $document->setAttribute("language", $root->getAttribute("original"));
  377. $source = "$opts{i}/$file.".$root->getAttribute("original").".xhtml";
  378. } else {
  379. my $l = (keys %{$bases{$file}})[0];
  380. $document->setAttribute("language", $l);
  381. $source = "$opts{i}/$file.$l.xhtml";
  382. }
  383. if ($dir eq "global") {
  384. lock(*TRANSLATIONS);
  385. print TRANSLATIONS "$lang $missingsource $source\n";
  386. unlock(*TRANSLATIONS);
  387. }
  388. }
  389. if ( (stat("$opts{o}/$dir/$file.$lang.html"))[9] >
  390. (stat($source))[9] && $opts{u} && ! -f "$opts{i}/$file.xsl" ) {
  391. next;
  392. }
  393. #
  394. # Here begins automated magic for those pages which we need to
  395. # assemble other sets of informations for first (automatically
  396. # updated pages).
  397. #
  398. if (-f "$opts{i}/$file.xsl") {
  399. #
  400. # Settle down please, children. First we remove all previous
  401. # document leftovers.
  402. #
  403. foreach ($root->getElementsByTagName("document")) {
  404. $root->removeChild($_);
  405. }
  406. $root->appendChild($document);
  407. # Create the <timestamp> tag automatically for these documents
  408. my $timestamp = $dom->createElement("timestamp");
  409. $timestamp->appendText("\$"."Date: ".$current_time." \$ \$"."Author: automatic \$");
  410. $document->appendChild($timestamp);
  411. #
  412. # Get the list of sources and create the files hash. The files
  413. # hash contains the base name for each file we want to use, and
  414. # then the language for it as a value. It prefers a file in the
  415. # language we're building into, but will accept an English file as
  416. # a substitute.
  417. #
  418. # "Learn all that is learnable and return that information
  419. # to the Creator."
  420. #
  421. open(IN, '<', "$opts{i}/$file.sources");
  422. my @auto_sources = <IN>;
  423. close IN;
  424. my %files;
  425. foreach (@auto_sources) {
  426. if (/(.*):[a-z,]*global/ || /(.*):[a-z,]*$dir/) {
  427. foreach my $f (glob("$1*")) {
  428. if ($f =~ /(.*)\.([a-z][a-z])\.xml$/) {
  429. if (!$files{$1}) {
  430. $files{$1} = $2;
  431. } elsif ($2 eq $lang) {
  432. $files{$1} = $2;
  433. } elsif (($2 eq "en") &&
  434. ($files{$1} ne $lang)) {
  435. $files{$1} = $2;
  436. }
  437. }
  438. }
  439. }
  440. }
  441. #
  442. # With that information, we load the source document and create
  443. # a new element in it, called <set>, which will hold the combined
  444. # knowledge of all the sets in the source files.
  445. #
  446. my $sourcedoc = $parser->parse_file($source);
  447. $sourcedoc->documentElement->setAttribute("date", $current_date);
  448. $sourcedoc->documentElement->setAttribute("lang", $lang);
  449. my $auto_data = $sourcedoc->createElement("set");
  450. while (my ($base, $l) = each %files) {
  451. if (($dir eq "global") && ($l ne $lang)) {
  452. lock(*TRANSLATIONS);
  453. print TRANSLATIONS "$lang $base.$lang.xml $base.$l.xml\n";
  454. unlock(*TRANSLATIONS);
  455. }
  456. print STDERR "Loading $base.$l.xml\n" if $opts{d};
  457. my $source_data = $parser->parse_file("$base.$l.xml");
  458. foreach ($source_data->documentElement->childNodes) {
  459. my $c = $_->cloneNode(1);
  460. # add the filename to nodes (news, events, …) so that we can use it as an identifier (e.g. for RSS)
  461. if (ref($c) eq "XML::LibXML::Element") {
  462. $base =~ /.*[\/_]([^\/_]*$)/;
  463. $c->setAttribute( "filename", $1 );
  464. }
  465. $auto_data->appendChild($c);
  466. }
  467. }
  468. $sourcedoc->documentElement->appendChild($auto_data);
  469. if ( $file eq "donate/thankgnus" and $lang eq "en" ) {
  470. open (TEST, ">/home/nicolas/FSFE/fsfe-web-out/test.xml");
  471. print TEST $sourcedoc->toString();
  472. close (TEST);
  473. }
  474. #
  475. # Get the appropriate textset for this language. If one can't be
  476. # found, use the English. (I hope this never happens)
  477. #
  478. my $textlang = $lang;
  479. unless (-f $opts{i}."/tools/texts-content-$textlang.xml") {
  480. $textlang = "en";
  481. }
  482. my $textdoc = $sourcedoc->createElement("textset-content");
  483. $auto_data->appendChild($textdoc);
  484. clone_document($textdoc, $opts{i}."/tools/texts-content-$textlang.xml");
  485. # Get also backup texts from the English file
  486. my $textdocbak = $sourcedoc->createElement("textset-content-backup");
  487. $auto_data->appendChild($textdocbak);
  488. clone_document($textdocbak, $opts{i}."/tools/texts-content-en.xml");
  489. # TODO: optimise getting texts-content-xx.xml and texts-content-en.xml,
  490. # since it does not depend on the xsl file being treated, we should do it only once!
  491. #
  492. # Transform the document using the XSL file and then push the
  493. # result into the <document> element of the document we're building.
  494. #
  495. my $style_doc = $parser->parse_file("$opts{i}/$file.xsl");
  496. my $stylesheet = $xslt_parser->parse_stylesheet($style_doc);
  497. my $results = $stylesheet->transform($sourcedoc);
  498. foreach ($results->documentElement->childNodes) {
  499. my $c = $_->cloneNode(1);
  500. $document->appendChild($c);
  501. }
  502. #
  503. # Now, while we're just at it, we create the RSS feeds if we want any
  504. #
  505. if (-f "$opts{i}/$file.rss.xsl") {
  506. my $style_doc = $parser->parse_file("$opts{i}/$file.rss.xsl");
  507. my $stylesheet = $xslt_parser->parse_stylesheet($style_doc);
  508. my $results = $stylesheet->transform($sourcedoc);
  509. $stylesheet->output_file($results, "$opts{o}/$dir/$file.$lang.rss")
  510. unless $opts{n};
  511. }
  512. #
  513. # and possibly the corresponding iCal (ics) file
  514. #
  515. if (-f "$opts{i}/$file.ics.xsl") {
  516. my $style_doc = $parser->parse_file("$opts{i}/$file.ics.xsl");
  517. my $stylesheet = $xslt_parser->parse_stylesheet($style_doc);
  518. my $results = $stylesheet->transform($sourcedoc);
  519. $stylesheet->output_file($results, "$opts{o}/$dir/$file.$lang.ics")
  520. unless $opts{n};
  521. }
  522. } else {
  523. #
  524. # If this wasn't an automatically updating document, we simply
  525. # clone the contents of the source file into the document.
  526. #
  527. clone_document($document, $source);
  528. }
  529. #
  530. # Find out if this translation is to be regarded as outdated or not.
  531. # A translation is deemed outdated if it is more than 2 hours older
  532. # than the original. This makes sure a translation committed together
  533. # with the original (but maybe a second earlier) isn't marked outdated.
  534. #
  535. my $originalsource = "$file.".$root->getAttribute("original").".xhtml";
  536. if (( stat("$opts{i}/$originalsource"))[9] > (stat($source))[9] + 7200
  537. and not $cant_be_outdated{$file} ) {
  538. $root->setAttribute("outdated", "yes");
  539. if ($dir eq "global") {
  540. lock(*TRANSLATIONS);
  541. print TRANSLATIONS "$lang $source $originalsource\n";
  542. unlock(*TRANSLATIONS);
  543. }
  544. } else {
  545. $root->setAttribute("outdated", "no");
  546. }
  547. #
  548. # Get the appropriate textset for this language. If one can't be
  549. # found, use the English. (I hope this never happens)
  550. #
  551. my $textlang = $lang;
  552. unless (-f $opts{i}."/tools/texts-$textlang.xml") {
  553. $textlang = "en";
  554. }
  555. my $textdoc = $dom->createElement("textset");
  556. $root->appendChild($textdoc);
  557. clone_document($textdoc, $opts{i}."/tools/texts-$textlang.xml");
  558. #
  559. # Read the fundraising text, if it exists.
  560. #
  561. if (-f $opts{i}."/fundraising.$lang.xml") {
  562. my $fundraisingdoc = $dom->createElement("fundraising");
  563. $root->appendChild($fundraisingdoc);
  564. clone_document($fundraisingdoc, $opts{i}."/fundraising.$lang.xml");
  565. } elsif (-f $opts{i}."/fundraising.en.xml") {
  566. my $fundraisingdoc = $dom->createElement("fundraising");
  567. $root->appendChild($fundraisingdoc);
  568. clone_document($fundraisingdoc, $opts{i}."/fundraising.en.xml");
  569. }
  570. #
  571. # And then we do the same thing for the menues. But first we take the
  572. # global menu here, then we add any information that is specific to
  573. # the focus.
  574. #
  575. foreach ($root->getElementsByTagName("menuset")) {
  576. $root->removeChild($_);
  577. }
  578. my %menu;
  579. foreach ('global', $dir) {
  580. if (-f $opts{i}."/tools/menu-$_.xml") {
  581. my $menudoc = $parser->parse_file($opts{i}."/tools/menu-$_.xml");
  582. foreach my $n ($menudoc->documentElement->getElementsByTagName("menu")) {
  583. $menu{$n->getAttribute("id")} = $n;
  584. }
  585. }
  586. }
  587. my $menuroot = $dom->createElement("menuset");
  588. while (my ($id, $n) = each %menu) {
  589. my $m = $n->cloneNode(1);
  590. $menuroot->appendChild($m);
  591. }
  592. $root->appendChild($menuroot);
  593. #
  594. # Do the actual transformation.
  595. #
  596. my $results = $global_stylesheet->transform($dom);
  597. #
  598. # In post-processing, we replace links pointing back to ourselves
  599. # so that they point to the correct language.
  600. #
  601. foreach ($results->documentElement->getElementsByTagName("a")) {
  602. my $href = $_->getAttribute("href");
  603. if ($href =~ /^http:\/\/www.fsfe.org/) {
  604. if ($_->textContent != "Our global work") {
  605. $href =~ s/http:\/\/www.fsfe.org//;
  606. }
  607. }
  608. if (($href !~ /^http/) && ($href !~ /^#/)) {
  609. # Save possible anchor and remove it from URL
  610. my $anchor = $href;
  611. if (!($anchor =~ s/.*#/#/)) {
  612. $anchor = "";
  613. }
  614. $href =~ s/#.*//;
  615. # proces URL
  616. if (($href =~ /\.html$/) && ($href !~ /\.[a-z][a-z]\.html$/)) {
  617. $href =~ s/\.html$/\.$lang.html/;
  618. } elsif (($href =~ /\.rss$/) && ($href !~ /\.[a-z][a-z]\.rss$/)) {
  619. $href =~ s/\.rss$/\.$lang.rss/;
  620. } elsif (($href =~ /\.ics$/) && ($href !~ /\.[a-z][a-z]\.ics$/)) {
  621. $href =~ s/\.ics$/\.$lang.ics/;
  622. } else {
  623. if (-d $opts{i}."/$href") {
  624. $href =~ s/\/?$/\/index.$lang.html/;
  625. } elsif ($href =~ /\/\w+$/) {
  626. $href .= ".$lang.html";
  627. }
  628. }
  629. # replace anchor
  630. $href .= $anchor;
  631. # For pages running on an external server, use full URL
  632. if ($document->getAttribute("external")) {
  633. $href = "http://www.fsfe.org$href";
  634. }
  635. $_->setAttribute("href", $href);
  636. }
  637. }
  638. print "Writing: $opts{o}/$dir/$file.$lang.html\n" if $opts{d};
  639. $global_stylesheet->output_file($results, "$opts{o}/$dir/$file.$lang.html")
  640. unless $opts{n};
  641. # Add foo.html.xx link which is used by Apache's MultiViews option when
  642. # a user enters foo.html as URL.
  643. link("$opts{o}/$dir/$file.$lang.html", "$opts{o}/$dir/$file.html.$lang")
  644. unless $opts{n};
  645. }
  646. }
  647. print STDERR "\n" unless $opts{q};
  648. }
  649. # Close the logfile for outdated and missing translations
  650. close (TRANSLATIONS);
  651. print STDERR "Fixing index links\n" unless $opts{q};
  652. while (my ($path, undef) = each %countries) {
  653. my @dirs = File::Find::Rule->directory()
  654. ->in($opts{o}."/$path");
  655. foreach (@dirs) {
  656. my $base = basename($_);
  657. while (my ($lang, undef) = each %languages) {
  658. if (-f "$_/$base.$lang.html" &&
  659. ! -f "$_/index.$lang.html") {
  660. link("$_/$base.$lang.html", "$_/index.$lang.html")
  661. unless $opts{n};
  662. link("$_/$base.html.$lang", "$_/index.html.$lang")
  663. unless $opts{n};
  664. }
  665. }
  666. }
  667. }
  668. #
  669. # For all files that are not XHTML source files, we copy them verbatim to
  670. # the final location, for each focus. These should be links instead to
  671. # prevent us from wasting disk space.
  672. #
  673. print STDERR "Copying misc files\n" unless $opts{q};
  674. foreach (grep(!/\.sources$/, grep(!/\.xsl$/, grep(!/\.xml$/, grep(!/\.xhtml$/,
  675. @files))))) {
  676. while (my ($dir, undef) = each %countries) {
  677. if (-f "$opts{i}/$_" && !$opts{n}) {
  678. link("$opts{i}/$_", "$opts{o}/$dir/$_");
  679. }
  680. }
  681. }
  682. #
  683. # Helper function that clones a document. It accepts an XML node and
  684. # a filename as parameters. Using this, it loads the source file into
  685. # the XML node.
  686. #
  687. sub clone_document {
  688. my ($doc, $source) = @_;
  689. my $root = $doc->parentNode;
  690. print "Source: $source\n" if $opts{d};
  691. foreach ($root->getElementsByTagName($doc->nodeName)) {
  692. $root->removeChild($_);
  693. }
  694. $root->appendChild($doc);
  695. my $parser = XML::LibXML->new();
  696. $parser->load_ext_dtd(0);
  697. $parser->recover(1);
  698. my $sourcedoc = $parser->parse_file($source);
  699. foreach ($sourcedoc->documentElement->childNodes) {
  700. $_->unbindNode();
  701. my $n = $_->cloneNode(1);
  702. $doc->appendChild($n);
  703. }
  704. if ($sourcedoc->documentElement->getAttribute("external")) {
  705. $doc->setAttribute("external", "yes");
  706. }
  707. if ($sourcedoc->documentElement->getAttribute("newsdate")) {
  708. # necessary for xhtml news files
  709. $doc->setAttribute("newsdate", $sourcedoc->documentElement->getAttribute("newsdate"));
  710. }
  711. if ($sourcedoc->documentElement->getAttribute("type")) {
  712. # necessary to differentiate news and newsletter pages
  713. # TODO: find a way to copy all such attributes!
  714. $doc->setAttribute("type", $sourcedoc->documentElement->getAttribute("type"));
  715. }
  716. }
  717. #
  718. # Helper functions to lock and unlock the translation log.
  719. #
  720. sub lock {
  721. my ($fh) = @_;
  722. flock($fh, LOCK_EX);
  723. seek($fh, 0, 2);
  724. }
  725. sub unlock {
  726. my ($fh) = @_;
  727. flock($fh, LOCK_UN);
  728. }