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.

build.pl 26KB

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