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