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