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