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