summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/po.pm
blob: 35e7c13fffa1622e2060f3a03c8a07b9c9b8ca44 (plain)
  1. #!/usr/bin/perl
  2. # .po as a wiki page type
  3. # Licensed under GPL v2 or greater
  4. # Copyright (C) 2008 intrigeri <intrigeri@boum.org>
  5. # inspired by the GPL'd po4a-translate,
  6. # which is Copyright 2002, 2003, 2004 by Martin Quinson (mquinson#debian.org)
  7. package IkiWiki::Plugin::po;
  8. use warnings;
  9. use strict;
  10. use IkiWiki 2.00;
  11. use Encode;
  12. use Locale::Po4a::Chooser;
  13. use Locale::Po4a::Po;
  14. use File::Basename;
  15. use File::Copy;
  16. use File::Spec;
  17. use File::Temp;
  18. use Memoize;
  19. use UNIVERSAL;
  20. my %translations;
  21. my @origneedsbuild;
  22. my %origsubs;
  23. memoize("istranslatable");
  24. memoize("_istranslation");
  25. memoize("percenttranslated");
  26. sub import {
  27. hook(type => "getsetup", id => "po", call => \&getsetup);
  28. hook(type => "checkconfig", id => "po", call => \&checkconfig);
  29. hook(type => "needsbuild", id => "po", call => \&needsbuild);
  30. hook(type => "scan", id => "po", call => \&scan, last =>1);
  31. hook(type => "filter", id => "po", call => \&filter);
  32. hook(type => "htmlize", id => "po", call => \&htmlize);
  33. hook(type => "pagetemplate", id => "po", call => \&pagetemplate, last => 1);
  34. hook(type => "postscan", id => "po", call => \&postscan);
  35. hook(type => "rename", id => "po", call => \&renamepages, first => 1);
  36. hook(type => "delete", id => "po", call => \&mydelete);
  37. hook(type => "change", id => "po", call => \&change);
  38. hook(type => "cansave", id => "po", call => \&cansave);
  39. hook(type => "canremove", id => "po", call => \&canremove);
  40. hook(type => "canrename", id => "po", call => \&canrename);
  41. hook(type => "editcontent", id => "po", call => \&editcontent);
  42. $origsubs{'bestlink'}=\&IkiWiki::bestlink;
  43. inject(name => "IkiWiki::bestlink", call => \&mybestlink);
  44. $origsubs{'beautify_urlpath'}=\&IkiWiki::beautify_urlpath;
  45. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  46. $origsubs{'targetpage'}=\&IkiWiki::targetpage;
  47. inject(name => "IkiWiki::targetpage", call => \&mytargetpage);
  48. $origsubs{'urlto'}=\&IkiWiki::urlto;
  49. inject(name => "IkiWiki::urlto", call => \&myurlto);
  50. $origsubs{'nicepagetitle'}=\&IkiWiki::nicepagetitle;
  51. inject(name => "IkiWiki::nicepagetitle", call => \&mynicepagetitle);
  52. }
  53. # ,----
  54. # | Table of contents
  55. # `----
  56. # 1. Hooks
  57. # 2. Injected functions
  58. # 3. Blackboxes for private data
  59. # 4. Helper functions
  60. # 5. PageSpec's
  61. # ,----
  62. # | Hooks
  63. # `----
  64. sub getsetup () {
  65. return
  66. plugin => {
  67. safe => 0,
  68. rebuild => 1,
  69. },
  70. po_master_language => {
  71. type => "string",
  72. example => {
  73. 'code' => 'en',
  74. 'name' => 'English'
  75. },
  76. description => "master language (non-PO files)",
  77. safe => 1,
  78. rebuild => 1,
  79. },
  80. po_slave_languages => {
  81. type => "string",
  82. example => {
  83. 'fr' => 'Français',
  84. 'es' => 'Castellano',
  85. 'de' => 'Deutsch'
  86. },
  87. description => "slave languages (PO files)",
  88. safe => 1,
  89. rebuild => 1,
  90. },
  91. po_translatable_pages => {
  92. type => "pagespec",
  93. example => "!*/Discussion",
  94. description => "PageSpec controlling which pages are translatable",
  95. link => "ikiwiki/PageSpec",
  96. safe => 1,
  97. rebuild => 1,
  98. },
  99. po_link_to => {
  100. type => "string",
  101. example => "current",
  102. description => "internal linking behavior (default/current/negotiated)",
  103. safe => 1,
  104. rebuild => 1,
  105. },
  106. po_translation_status_in_links => {
  107. type => "boolean",
  108. example => 1,
  109. description => "display translation status in links to translations",
  110. safe => 1,
  111. rebuild => 1,
  112. },
  113. }
  114. sub checkconfig () {
  115. foreach my $field (qw{po_master_language po_slave_languages}) {
  116. if (! exists $config{$field} || ! defined $config{$field}) {
  117. error(sprintf(gettext("Must specify %s"), $field));
  118. }
  119. }
  120. if (! (keys %{$config{po_slave_languages}})) {
  121. error(gettext("At least one slave language must be defined in po_slave_languages"));
  122. }
  123. map {
  124. islanguagecode($_)
  125. or error(sprintf(gettext("%s is not a valid language code"), $_));
  126. } ($config{po_master_language}{code}, keys %{$config{po_slave_languages}});
  127. if (! exists $config{po_translatable_pages} ||
  128. ! defined $config{po_translatable_pages}) {
  129. $config{po_translatable_pages}="";
  130. }
  131. if (! exists $config{po_link_to} ||
  132. ! defined $config{po_link_to}) {
  133. $config{po_link_to}='default';
  134. }
  135. elsif (! grep {
  136. $config{po_link_to} eq $_
  137. } ('default', 'current', 'negotiated')) {
  138. warn(sprintf(gettext('po_link_to=%s is not a valid setting, falling back to po_link_to=default'),
  139. $config{po_link_to}));
  140. $config{po_link_to}='default';
  141. }
  142. elsif ($config{po_link_to} eq "negotiated" && ! $config{usedirs}) {
  143. warn(gettext('po_link_to=negotiated requires usedirs to be enabled, falling back to po_link_to=default'));
  144. $config{po_link_to}='default';
  145. }
  146. if (! exists $config{po_translation_status_in_links} ||
  147. ! defined $config{po_translation_status_in_links}) {
  148. $config{po_translation_status_in_links}=1;
  149. }
  150. push @{$config{wiki_file_prune_regexps}}, qr/\.pot$/;
  151. }
  152. sub needsbuild () {
  153. my $needsbuild=shift;
  154. # backup @needsbuild content so that change() can know whether
  155. # a given master page was rendered because its source file was changed
  156. @origneedsbuild=(@$needsbuild);
  157. flushmemoizecache();
  158. buildtranslationscache();
  159. # make existing translations depend on the corresponding master page
  160. foreach my $master (keys %translations) {
  161. map add_depends($_, $master), values %{otherlanguages($master)};
  162. }
  163. }
  164. # Massage the recorded state of internal links so that:
  165. # - it matches the actually generated links, rather than the links as written
  166. # in the pages' source
  167. # - backlinks are consistent in all cases
  168. sub scan (@) {
  169. my %params=@_;
  170. my $page=$params{page};
  171. my $content=$params{content};
  172. return unless UNIVERSAL::can("IkiWiki::Plugin::link", "import");
  173. if (istranslation($page)) {
  174. foreach my $destpage (@{$links{$page}}) {
  175. if (istranslatable($destpage)) {
  176. # replace one occurence of $destpage in $links{$page}
  177. # (we only want to replace the one that was added by
  178. # IkiWiki::Plugin::link::scan, other occurences may be
  179. # there for other reasons)
  180. for (my $i=0; $i<@{$links{$page}}; $i++) {
  181. if (@{$links{$page}}[$i] eq $destpage) {
  182. @{$links{$page}}[$i] = $destpage . '.' . lang($page);
  183. last;
  184. }
  185. }
  186. }
  187. }
  188. }
  189. elsif (! istranslatable($page) && ! istranslation($page)) {
  190. foreach my $destpage (@{$links{$page}}) {
  191. if (istranslatable($destpage)) {
  192. # make sure any destpage's translations has
  193. # $page in its backlinks
  194. push @{$links{$page}},
  195. values %{otherlanguages($destpage)};
  196. }
  197. }
  198. }
  199. }
  200. # We use filter to convert PO to the master page's format,
  201. # since the rest of ikiwiki should not work on PO files.
  202. sub filter (@) {
  203. my %params = @_;
  204. my $page = $params{page};
  205. my $destpage = $params{destpage};
  206. my $content = $params{content};
  207. if (istranslation($page) && ! alreadyfiltered($page, $destpage)) {
  208. $content = po_to_markup($page, $content);
  209. setalreadyfiltered($page, $destpage);
  210. }
  211. return $content;
  212. }
  213. sub htmlize (@) {
  214. my %params=@_;
  215. my $page = $params{page};
  216. my $content = $params{content};
  217. # ignore PO files this plugin did not create
  218. return $content unless istranslation($page);
  219. # force content to be htmlize'd as if it was the same type as the master page
  220. return IkiWiki::htmlize($page, $page,
  221. pagetype(srcfile($pagesources{masterpage($page)})),
  222. $content);
  223. }
  224. sub pagetemplate (@) {
  225. my %params=@_;
  226. my $page=$params{page};
  227. my $destpage=$params{destpage};
  228. my $template=$params{template};
  229. my ($masterpage, $lang) = istranslation($page);
  230. if (istranslation($page) && $template->query(name => "percenttranslated")) {
  231. $template->param(percenttranslated => percenttranslated($page));
  232. }
  233. if ($template->query(name => "istranslation")) {
  234. $template->param(istranslation => scalar istranslation($page));
  235. }
  236. if ($template->query(name => "istranslatable")) {
  237. $template->param(istranslatable => istranslatable($page));
  238. }
  239. if ($template->query(name => "HOMEPAGEURL")) {
  240. $template->param(homepageurl => homepageurl($page));
  241. }
  242. if ($template->query(name => "otherlanguages")) {
  243. $template->param(otherlanguages => [otherlanguagesloop($page)]);
  244. map add_depends($page, $_), (values %{otherlanguages($page)});
  245. }
  246. # Rely on IkiWiki::Render's genpage() to decide wether
  247. # a discussion link should appear on $page; this is not
  248. # totally accurate, though: some broken links may be generated
  249. # when cgiurl is disabled.
  250. # This compromise avoids some code duplication, and will probably
  251. # prevent future breakage when ikiwiki internals change.
  252. # Known limitations are preferred to future random bugs.
  253. if ($template->param('discussionlink') && istranslation($page)) {
  254. $template->param('discussionlink' => htmllink(
  255. $page,
  256. $destpage,
  257. $masterpage . '/' . gettext("Discussion"),
  258. noimageinline => 1,
  259. forcesubpage => 0,
  260. linktext => gettext("Discussion"),
  261. ));
  262. }
  263. # Remove broken parentlink to ./index.html on home page's translations.
  264. # It works because this hook has the "last" parameter set, to ensure it
  265. # runs after parentlinks' own pagetemplate hook.
  266. if ($template->param('parentlinks')
  267. && istranslation($page)
  268. && $masterpage eq "index") {
  269. $template->param('parentlinks' => []);
  270. }
  271. } # }}}
  272. sub postscan (@) {
  273. my %params = @_;
  274. my $page = $params{page};
  275. # backlinks involve back-dependencies, so that nicepagetitle effects,
  276. # such as translation status displayed in links, are updated
  277. use IkiWiki::Render;
  278. map add_depends($page, $_), keys %{$IkiWiki::backlinks{$page}};
  279. }
  280. # Add the renamed page translations to the list of to-be-renamed pages.
  281. sub renamepages($$$) {
  282. my ($torename, $cgi, $session) = (shift, shift, shift);
  283. # copy the initial array, so that we can iterate on it AND
  284. # modify it at the same time, without iterating on the items we
  285. # pushed on it ourselves
  286. my @torename=@{$torename};
  287. # Save the page(s) the user asked to rename, so that our
  288. # canrename hook can tell the difference between:
  289. # - a translation being renamed as a consequence of its master page
  290. # being renamed
  291. # - a user trying to directly rename a translation
  292. # This is why this hook has to be run first, before @torename is modified
  293. # by other plugins.
  294. $session->param(po_orig_torename => [ @torename ]);
  295. IkiWiki::cgi_savesession($session);
  296. foreach my $rename (@torename) {
  297. next unless istranslatable($rename->{src});
  298. my %otherpages=%{otherlanguages($rename->{src})};
  299. while (my ($lang, $otherpage) = each %otherpages) {
  300. push @{$torename}, {
  301. src => $otherpage,
  302. srcfile => $pagesources{$otherpage},
  303. dest => otherlanguage($rename->{dest}, $lang),
  304. destfile => $rename->{dest}.".".$lang.".po",
  305. required => 0,
  306. };
  307. }
  308. }
  309. }
  310. sub mydelete(@) {
  311. my @deleted=@_;
  312. map { deletetranslations($_) } grep istranslatablefile($_), @deleted;
  313. }
  314. sub change(@) {
  315. my @rendered=@_;
  316. my $updated_po_files=0;
  317. # Refresh/create POT and PO files as needed.
  318. foreach my $file (grep {istranslatablefile($_)} @rendered) {
  319. my $page=pagename($file);
  320. my $masterfile=srcfile($file);
  321. my $updated_pot_file=0;
  322. # Only refresh Pot file if it does not exist, or if
  323. # $pagesources{$page} was changed: don't if only the HTML was
  324. # refreshed, e.g. because of a dependency.
  325. if ((grep { $_ eq $pagesources{$page} } @origneedsbuild)
  326. || ! -e potfile($masterfile)) {
  327. refreshpot($masterfile);
  328. $updated_pot_file=1;
  329. }
  330. my @pofiles;
  331. map {
  332. push @pofiles, $_ if ($updated_pot_file || ! -e $_);
  333. } (pofiles($masterfile));
  334. if (@pofiles) {
  335. refreshpofiles($masterfile, @pofiles);
  336. map { IkiWiki::rcs_add($_) } @pofiles if $config{rcs};
  337. $updated_po_files=1;
  338. }
  339. }
  340. if ($updated_po_files) {
  341. commit_and_refresh(
  342. gettext("updated PO files"),
  343. "IkiWiki::Plugin::po::change");
  344. }
  345. }
  346. sub cansave ($$$$) {
  347. my ($page, $content, $cgi, $session) = (shift, shift, shift, shift);
  348. if (istranslation($page)) {
  349. if (defined po_to_markup($page, $content, "nonfatal")) {
  350. return undef;
  351. }
  352. else {
  353. return "Could not parse this page's content; is this valid gettext?";
  354. }
  355. }
  356. return undef;
  357. }
  358. sub canremove ($$$) {
  359. my ($page, $cgi, $session) = (shift, shift, shift);
  360. if (istranslation($page)) {
  361. return gettext("Can not remove a translation. Removing the master page, ".
  362. "though, removes its translations as well.");
  363. }
  364. return undef;
  365. }
  366. sub canrename ($$@) {
  367. my ($cgi, $session) = (shift, shift);
  368. my %params = @_;
  369. if (istranslation($params{src})) {
  370. my $masterpage = masterpage($params{src});
  371. # Tell the difference between:
  372. # - a translation being renamed as a consequence of its master page
  373. # being renamed, which is allowed
  374. # - a user trying to directly rename a translation, which is forbidden
  375. # by looking for the master page in the list of to-be-renamed pages we
  376. # saved early in the renaming process.
  377. my $orig_torename = $session->param("po_orig_torename");
  378. unless (scalar grep { $_->{src} eq $masterpage } @{$orig_torename}) {
  379. return gettext("Can not rename a translation. Renaming the master page, ".
  380. "though, renames its translations as well.");
  381. }
  382. }
  383. return undef;
  384. }
  385. # As we're previewing or saving a page, the content may have
  386. # changed, so tell the next filter() invocation it must not be lazy.
  387. sub editcontent () {
  388. my %params=@_;
  389. unsetalreadyfiltered($params{page}, $params{page});
  390. return $params{content};
  391. }
  392. # ,----
  393. # | Injected functions
  394. # `----
  395. # Implement po_link_to 'current' and 'negotiated' settings.
  396. sub mybestlink ($$) {
  397. my $page=shift;
  398. my $link=shift;
  399. my $res=$origsubs{'bestlink'}->(masterpage($page), $link);
  400. if (length $res
  401. && ($config{po_link_to} eq "current" || $config{po_link_to} eq "negotiated")
  402. && istranslatable($res)
  403. && istranslation($page)) {
  404. return $res . "." . lang($page);
  405. }
  406. return $res;
  407. }
  408. sub mybeautify_urlpath ($) {
  409. my $url=shift;
  410. my $res=$origsubs{'beautify_urlpath'}->($url);
  411. if ($config{po_link_to} eq "negotiated") {
  412. $res =~ s!/\Qindex.$config{po_master_language}{code}.$config{htmlext}\E$!/!;
  413. $res =~ s!/\Qindex.$config{htmlext}\E$!/!;
  414. map {
  415. $res =~ s!/\Qindex.$_.$config{htmlext}\E$!/!;
  416. } (keys %{$config{po_slave_languages}});
  417. }
  418. return $res;
  419. }
  420. sub mytargetpage ($$) {
  421. my $page=shift;
  422. my $ext=shift;
  423. if (istranslation($page) || istranslatable($page)) {
  424. my ($masterpage, $lang) = (masterpage($page), lang($page));
  425. if (! $config{usedirs} || $masterpage eq 'index') {
  426. return $masterpage . "." . $lang . "." . $ext;
  427. }
  428. else {
  429. return $masterpage . "/index." . $lang . "." . $ext;
  430. }
  431. }
  432. return $origsubs{'targetpage'}->($page, $ext);
  433. }
  434. sub myurlto ($$;$) {
  435. my $to=shift;
  436. my $from=shift;
  437. my $absolute=shift;
  438. # workaround hard-coded /index.$config{htmlext} in IkiWiki::urlto()
  439. if (! length $to
  440. && $config{po_link_to} eq "current"
  441. && istranslatable('index')) {
  442. return IkiWiki::beautify_urlpath(IkiWiki::baseurl($from) . "index." . lang($from) . ".$config{htmlext}");
  443. }
  444. # avoid using our injected beautify_urlpath if run by cgi_editpage,
  445. # so that one is redirected to the just-edited page rather than to the
  446. # negociated translation; to prevent unnecessary fiddling with caller/inject,
  447. # we only do so when our beautify_urlpath would actually do what we want to
  448. # avoid, i.e. when po_link_to = negotiated
  449. if ($config{po_link_to} eq "negotiated") {
  450. my @caller = caller(1);
  451. my $run_by_editpage = 0;
  452. $run_by_editpage = 1 if (exists $caller[3] && defined $caller[3]
  453. && $caller[3] eq "IkiWiki::cgi_editpage");
  454. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'})
  455. if $run_by_editpage;
  456. my $res = $origsubs{'urlto'}->($to,$from,$absolute);
  457. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath)
  458. if $run_by_editpage;
  459. return $res;
  460. }
  461. else {
  462. return $origsubs{'urlto'}->($to,$from,$absolute)
  463. }
  464. }
  465. sub mynicepagetitle ($;$) {
  466. my ($page, $unescaped) = (shift, shift);
  467. my $res = $origsubs{'nicepagetitle'}->($page, $unescaped);
  468. return $res unless istranslation($page);
  469. return $res unless $config{po_translation_status_in_links};
  470. return $res.' ('.percenttranslated($page).' %)';
  471. }
  472. # ,----
  473. # | Blackboxes for private data
  474. # `----
  475. {
  476. my %filtered;
  477. sub alreadyfiltered($$) {
  478. my $page=shift;
  479. my $destpage=shift;
  480. return ( exists $filtered{$page}{$destpage}
  481. && $filtered{$page}{$destpage} eq 1 );
  482. }
  483. sub setalreadyfiltered($$) {
  484. my $page=shift;
  485. my $destpage=shift;
  486. $filtered{$page}{$destpage}=1;
  487. }
  488. sub unsetalreadyfiltered($$) {
  489. my $page=shift;
  490. my $destpage=shift;
  491. if (exists $filtered{$page}{$destpage}) {
  492. delete $filtered{$page}{$destpage};
  493. }
  494. }
  495. sub resetalreadyfiltered() {
  496. undef %filtered;
  497. }
  498. }
  499. # ,----
  500. # | Helper functions
  501. # `----
  502. sub maybe_add_leading_slash ($;$) {
  503. my $str=shift;
  504. my $add=shift;
  505. $add=1 unless defined $add;
  506. return '/' . $str if $add;
  507. return $str;
  508. }
  509. sub istranslatablefile ($) {
  510. my $file=shift;
  511. return 0 unless defined $file;
  512. return 0 if (defined pagetype($file) && pagetype($file) eq 'po');
  513. return 0 if $file =~ /\.pot$/;
  514. return 1 if pagespec_match(pagename($file), $config{po_translatable_pages});
  515. return;
  516. }
  517. sub istranslatable ($) {
  518. my $page=shift;
  519. $page=~s#^/##;
  520. return 1 if istranslatablefile($pagesources{$page});
  521. return;
  522. }
  523. sub _istranslation ($) {
  524. my $page=shift;
  525. my $hasleadingslash = ($page=~s#^/##);
  526. my $file=$pagesources{$page};
  527. return 0 unless (defined $file
  528. && defined pagetype($file)
  529. && pagetype($file) eq 'po');
  530. return 0 if $file =~ /\.pot$/;
  531. my ($masterpage, $lang) = ($page =~ /(.*)[.]([a-z]{2})$/);
  532. return 0 unless (defined $masterpage && defined $lang
  533. && length $masterpage && length $lang
  534. && defined $pagesources{$masterpage}
  535. && defined $config{po_slave_languages}{$lang});
  536. return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang)
  537. if istranslatable($masterpage);
  538. }
  539. sub istranslation ($) {
  540. my $page=shift;
  541. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  542. my $hasleadingslash = ($masterpage=~s#^/##);
  543. $translations{$masterpage}{$lang}=$page unless exists $translations{$masterpage}{$lang};
  544. return (maybe_add_leading_slash($masterpage, $hasleadingslash), $lang);
  545. }
  546. return;
  547. }
  548. sub masterpage ($) {
  549. my $page=shift;
  550. if ( 1 < (my ($masterpage, $lang) = _istranslation($page))) {
  551. return $masterpage;
  552. }
  553. return $page;
  554. }
  555. sub lang ($) {
  556. my $page=shift;
  557. if (1 < (my ($masterpage, $lang) = _istranslation($page))) {
  558. return $lang;
  559. }
  560. return $config{po_master_language}{code};
  561. }
  562. sub islanguagecode ($) {
  563. my $code=shift;
  564. return ($code =~ /^[a-z]{2}$/);
  565. }
  566. sub otherlanguage ($$) {
  567. my $page=shift;
  568. my $code=shift;
  569. return masterpage($page) if $code eq $config{po_master_language}{code};
  570. return masterpage($page) . '.' . $code;
  571. }
  572. sub otherlanguages ($) {
  573. my $page=shift;
  574. my %ret;
  575. return \%ret unless (istranslation($page) || istranslatable($page));
  576. my $curlang=lang($page);
  577. foreach my $lang
  578. ($config{po_master_language}{code}, keys %{$config{po_slave_languages}}) {
  579. next if $lang eq $curlang;
  580. $ret{$lang}=otherlanguage($page, $lang);
  581. }
  582. return \%ret;
  583. }
  584. sub potfile ($) {
  585. my $masterfile=shift;
  586. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  587. $dir='' if $dir eq './';
  588. return File::Spec->catpath('', $dir, $name . ".pot");
  589. }
  590. sub pofile ($$) {
  591. my $masterfile=shift;
  592. my $lang=shift;
  593. (my $name, my $dir, my $suffix) = fileparse($masterfile, qr/\.[^.]*/);
  594. $dir='' if $dir eq './';
  595. return File::Spec->catpath('', $dir, $name . "." . $lang . ".po");
  596. }
  597. sub pofiles ($) {
  598. my $masterfile=shift;
  599. return map pofile($masterfile, $_), (keys %{$config{po_slave_languages}});
  600. }
  601. sub refreshpot ($) {
  602. my $masterfile=shift;
  603. my $potfile=potfile($masterfile);
  604. my %options = ("markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0);
  605. my $doc=Locale::Po4a::Chooser::new('text',%options);
  606. $doc->{TT}{utf_mode} = 1;
  607. $doc->{TT}{file_in_charset} = 'utf-8';
  608. $doc->{TT}{file_out_charset} = 'utf-8';
  609. $doc->read($masterfile);
  610. # let's cheat a bit to force porefs option to be passed to Locale::Po4a::Po;
  611. # this is undocument use of internal Locale::Po4a::TransTractor's data,
  612. # compulsory since this module prevents us from using the porefs option.
  613. $doc->{TT}{po_out}=Locale::Po4a::Po->new({ 'porefs' => 'none' });
  614. $doc->{TT}{po_out}->set_charset('utf-8');
  615. # do the actual work
  616. $doc->parse;
  617. IkiWiki::prep_writefile(basename($potfile),dirname($potfile));
  618. $doc->writepo($potfile);
  619. }
  620. sub refreshpofiles ($@) {
  621. my $masterfile=shift;
  622. my @pofiles=@_;
  623. my $potfile=potfile($masterfile);
  624. error("[po/refreshpofiles] POT file ($potfile) does not exist") unless (-e $potfile);
  625. foreach my $pofile (@pofiles) {
  626. IkiWiki::prep_writefile(basename($pofile),dirname($pofile));
  627. if (-e $pofile) {
  628. system("msgmerge", "-U", "--backup=none", $pofile, $potfile) == 0
  629. or error("[po/refreshpofiles:$pofile] failed to update");
  630. }
  631. else {
  632. File::Copy::syscopy($potfile,$pofile)
  633. or error("[po/refreshpofiles:$pofile] failed to copy the POT file");
  634. }
  635. }
  636. }
  637. sub buildtranslationscache() {
  638. # use istranslation's side-effect
  639. map istranslation($_), (keys %pagesources);
  640. }
  641. sub resettranslationscache() {
  642. undef %translations;
  643. }
  644. sub flushmemoizecache() {
  645. Memoize::flush_cache("istranslatable");
  646. Memoize::flush_cache("_istranslation");
  647. Memoize::flush_cache("percenttranslated");
  648. }
  649. sub urlto_with_orig_beautiful_urlpath($$) {
  650. my $to=shift;
  651. my $from=shift;
  652. inject(name => "IkiWiki::beautify_urlpath", call => $origsubs{'beautify_urlpath'});
  653. my $res=urlto($to, $from);
  654. inject(name => "IkiWiki::beautify_urlpath", call => \&mybeautify_urlpath);
  655. return $res;
  656. }
  657. sub percenttranslated ($) {
  658. my $page=shift;
  659. $page=~s/^\///;
  660. return gettext("N/A") unless istranslation($page);
  661. my $file=srcfile($pagesources{$page});
  662. my $masterfile = srcfile($pagesources{masterpage($page)});
  663. my %options = (
  664. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  665. );
  666. my $doc=Locale::Po4a::Chooser::new('text',%options);
  667. $doc->process(
  668. 'po_in_name' => [ $file ],
  669. 'file_in_name' => [ $masterfile ],
  670. 'file_in_charset' => 'utf-8',
  671. 'file_out_charset' => 'utf-8',
  672. ) or error("[po/percenttranslated:$page]: failed to translate");
  673. my ($percent,$hit,$queries) = $doc->stats();
  674. return $percent;
  675. }
  676. sub languagename ($) {
  677. my $code=shift;
  678. return $config{po_master_language}{name}
  679. if $code eq $config{po_master_language}{code};
  680. return $config{po_slave_languages}{$code}
  681. if defined $config{po_slave_languages}{$code};
  682. return;
  683. }
  684. sub otherlanguagesloop ($) {
  685. my $page=shift;
  686. my @ret;
  687. my %otherpages=%{otherlanguages($page)};
  688. while (my ($lang, $otherpage) = each %otherpages) {
  689. if (istranslation($page) && masterpage($page) eq $otherpage) {
  690. push @ret, {
  691. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  692. code => $lang,
  693. language => languagename($lang),
  694. master => 1,
  695. };
  696. }
  697. else {
  698. push @ret, {
  699. url => urlto_with_orig_beautiful_urlpath($otherpage, $page),
  700. code => $lang,
  701. language => languagename($lang),
  702. percent => percenttranslated($otherpage),
  703. }
  704. }
  705. }
  706. return sort {
  707. return -1 if $a->{code} eq $config{po_master_language}{code};
  708. return 1 if $b->{code} eq $config{po_master_language}{code};
  709. return $a->{language} cmp $b->{language};
  710. } @ret;
  711. }
  712. sub homepageurl (;$) {
  713. my $page=shift;
  714. return urlto('', $page);
  715. }
  716. sub deletetranslations ($) {
  717. my $deletedmasterfile=shift;
  718. my $deletedmasterpage=pagename($deletedmasterfile);
  719. my @todelete;
  720. map {
  721. my $file = newpagefile($deletedmasterpage.'.'.$_, 'po');
  722. my $absfile = "$config{srcdir}/$file";
  723. if (-e $absfile && ! -l $absfile && ! -d $absfile) {
  724. push @todelete, $file;
  725. }
  726. } keys %{$config{po_slave_languages}};
  727. map {
  728. if ($config{rcs}) {
  729. IkiWiki::rcs_remove($_);
  730. }
  731. else {
  732. IkiWiki::prune("$config{srcdir}/$_");
  733. }
  734. } @todelete;
  735. if (scalar @todelete) {
  736. commit_and_refresh(
  737. gettext("removed obsolete PO files"),
  738. "IkiWiki::Plugin::po::deletetranslations");
  739. }
  740. }
  741. sub commit_and_refresh ($$) {
  742. my ($msg, $author) = (shift, shift);
  743. if ($config{rcs}) {
  744. IkiWiki::disable_commit_hook();
  745. IkiWiki::rcs_commit_staged($msg, $author, "127.0.0.1");
  746. IkiWiki::enable_commit_hook();
  747. IkiWiki::rcs_update();
  748. }
  749. # Reinitialize module's private variables.
  750. resetalreadyfiltered();
  751. resettranslationscache();
  752. flushmemoizecache();
  753. # Trigger a wiki refresh.
  754. require IkiWiki::Render;
  755. # without preliminary saveindex/loadindex, refresh()
  756. # complains about a lot of uninitialized variables
  757. IkiWiki::saveindex();
  758. IkiWiki::loadindex();
  759. IkiWiki::refresh();
  760. IkiWiki::saveindex();
  761. }
  762. # on success, returns the filtered content.
  763. # on error, if $nonfatal, warn and return undef; else, error out.
  764. sub po_to_markup ($$;$) {
  765. my ($page, $content) = (shift, shift);
  766. my $nonfatal = shift;
  767. $content = '' unless defined $content;
  768. $content = decode_utf8(encode_utf8($content));
  769. # CRLF line terminators make poor Locale::Po4a feel bad
  770. $content=~s/\r\n/\n/g;
  771. # There are incompatibilities between some File::Temp versions
  772. # (including 0.18, bundled with Lenny's perl-modules package)
  773. # and others (e.g. 0.20, previously present in the archive as
  774. # a standalone package): under certain circumstances, some
  775. # return a relative filename, whereas others return an absolute one;
  776. # we here use this module in a way that is at least compatible
  777. # with 0.18 and 0.20. Beware, hit'n'run refactorers!
  778. my $infile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-in.XXXXXXXXXX",
  779. DIR => File::Spec->tmpdir,
  780. UNLINK => 1)->filename;
  781. my $outfile = new File::Temp(TEMPLATE => "ikiwiki-po-filter-out.XXXXXXXXXX",
  782. DIR => File::Spec->tmpdir,
  783. UNLINK => 1)->filename;
  784. sub failure ($) {
  785. my $msg = '[po/filter:'.$page.'] ' . shift;
  786. if ($nonfatal) {
  787. warn $msg;
  788. return undef;
  789. }
  790. error($msg, sub { unlink $infile, $outfile});
  791. }
  792. writefile(basename($infile), File::Spec->tmpdir, $content)
  793. or return failure("failed to write $infile");
  794. my $masterfile = srcfile($pagesources{masterpage($page)});
  795. my %options = (
  796. "markdown" => (pagetype($masterfile) eq 'mdwn') ? 1 : 0,
  797. );
  798. my $doc=Locale::Po4a::Chooser::new('text',%options);
  799. $doc->process(
  800. 'po_in_name' => [ $infile ],
  801. 'file_in_name' => [ $masterfile ],
  802. 'file_in_charset' => 'utf-8',
  803. 'file_out_charset' => 'utf-8',
  804. ) or return failure("failed to translate");
  805. $doc->write($outfile) or return failure("could not write $outfile");
  806. $content = readfile($outfile) or return failure("could not read $outfile");
  807. # Unlinking should happen automatically, thanks to File::Temp,
  808. # but it does not work here, probably because of the way writefile()
  809. # and Locale::Po4a::write() work.
  810. unlink $infile, $outfile;
  811. return $content;
  812. }
  813. # ,----
  814. # | PageSpec's
  815. # `----
  816. package IkiWiki::PageSpec;
  817. use warnings;
  818. use strict;
  819. use IkiWiki 2.00;
  820. sub match_istranslation ($;@) {
  821. my $page=shift;
  822. if (IkiWiki::Plugin::po::istranslation($page)) {
  823. return IkiWiki::SuccessReason->new("is a translation page");
  824. }
  825. else {
  826. return IkiWiki::FailReason->new("is not a translation page");
  827. }
  828. }
  829. sub match_istranslatable ($;@) {
  830. my $page=shift;
  831. if (IkiWiki::Plugin::po::istranslatable($page)) {
  832. return IkiWiki::SuccessReason->new("is set as translatable in po_translatable_pages");
  833. }
  834. else {
  835. return IkiWiki::FailReason->new("is not set as translatable in po_translatable_pages");
  836. }
  837. }
  838. sub match_lang ($$;@) {
  839. my $page=shift;
  840. my $wanted=shift;
  841. my $regexp=IkiWiki::glob2re($wanted);
  842. my $lang=IkiWiki::Plugin::po::lang($page);
  843. if ($lang!~/^$regexp$/i) {
  844. return IkiWiki::FailReason->new("file language is $lang, not $wanted");
  845. }
  846. else {
  847. return IkiWiki::SuccessReason->new("file language is $wanted");
  848. }
  849. }
  850. sub match_currentlang ($$;@) {
  851. my $page=shift;
  852. shift;
  853. my %params=@_;
  854. return IkiWiki::FailReason->new("no location provided") unless exists $params{location};
  855. my $currentlang=IkiWiki::Plugin::po::lang($params{location});
  856. my $lang=IkiWiki::Plugin::po::lang($page);
  857. if ($lang eq $currentlang) {
  858. return IkiWiki::SuccessReason->new("file language is the same as current one, i.e. $currentlang");
  859. }
  860. else {
  861. return IkiWiki::FailReason->new("file language is $lang, whereas current language is $currentlang");
  862. }
  863. }
  864. 1