summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/svn.pm
blob: ffacb8cf9534a2055d945224a60bd98fc12e14b7 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::svn;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use POSIX qw(setlocale LC_CTYPE);
  7. sub import {
  8. hook(type => "checkconfig", id => "svn", call => \&checkconfig);
  9. hook(type => "getsetup", id => "svn", call => \&getsetup);
  10. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  11. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  12. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  13. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  14. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  15. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  16. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  17. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  18. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  19. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  20. hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
  21. }
  22. sub checkconfig () {
  23. if (! defined $config{svnpath}) {
  24. $config{svnpath}="trunk";
  25. }
  26. if (exists $config{svnpath}) {
  27. # code depends on the path not having extraneous slashes
  28. $config{svnpath}=~tr#/#/#s;
  29. $config{svnpath}=~s/\/$//;
  30. $config{svnpath}=~s/^\///;
  31. }
  32. if (defined $config{svn_wrapper} && length $config{svn_wrapper}) {
  33. push @{$config{wrappers}}, {
  34. wrapper => $config{svn_wrapper},
  35. wrappermode => (defined $config{svn_wrappermode} ? $config{svn_wrappermode} : "04755"),
  36. };
  37. }
  38. }
  39. sub getsetup () {
  40. return
  41. plugin => {
  42. safe => 0, # rcs plugin
  43. rebuild => undef,
  44. section => "rcs",
  45. },
  46. svnrepo => {
  47. type => "string",
  48. example => "/svn/wiki",
  49. description => "subversion repository location",
  50. safe => 0, # path
  51. rebuild => 0,
  52. },
  53. svnpath => {
  54. type => "string",
  55. example => "trunk",
  56. description => "path inside repository where the wiki is located",
  57. safe => 0, # paranoia
  58. rebuild => 0,
  59. },
  60. svn_wrapper => {
  61. type => "string",
  62. example => "/svn/wikirepo/hooks/post-commit",
  63. description => "svn post-commit hook to generate",
  64. safe => 0, # file
  65. rebuild => 0,
  66. },
  67. svn_wrappermode => {
  68. type => "string",
  69. example => '04755',
  70. description => "mode for svn_wrapper (can safely be made suid)",
  71. safe => 0,
  72. rebuild => 0,
  73. },
  74. historyurl => {
  75. type => "string",
  76. example => "http://svn.example.org/trunk/[[file]]",
  77. description => "viewvc url to show file history ([[file]] substituted)",
  78. safe => 1,
  79. rebuild => 1,
  80. },
  81. diffurl => {
  82. type => "string",
  83. example => "http://svn.example.org/trunk/[[file]]?root=wiki&r1=[[r1]]&r2=[[r2]]",
  84. description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
  85. safe => 1,
  86. rebuild => 1,
  87. },
  88. }
  89. # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
  90. sub find_lc_ctype() {
  91. my $current = setlocale(LC_CTYPE());
  92. return $current if $current =~ m/UTF-?8$/i;
  93. # Make some obvious attempts to avoid calling `locale -a`
  94. foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
  95. return $locale if setlocale(LC_CTYPE(), $locale);
  96. }
  97. # Try to get all available locales and pick the first UTF-8 one found.
  98. if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
  99. chomp @locale;
  100. return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
  101. }
  102. # fallback to the current locale
  103. return $current;
  104. }
  105. $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
  106. sub svn_info ($$) {
  107. my $field=shift;
  108. my $file=shift;
  109. my $info=`LANG=C svn info $file`;
  110. my ($ret)=$info=~/^$field: (.*)$/m;
  111. return $ret;
  112. }
  113. sub rcs_update () {
  114. if (-d "$config{srcdir}/.svn") {
  115. if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
  116. warn("svn update failed\n");
  117. }
  118. }
  119. }
  120. sub rcs_prepedit ($) {
  121. # Prepares to edit a file under revision control. Returns a token
  122. # that must be passed into rcs_commit when the file is ready
  123. # for committing.
  124. # The file is relative to the srcdir.
  125. my $file=shift;
  126. if (-d "$config{srcdir}/.svn") {
  127. # For subversion, return the revision of the file when
  128. # editing begins.
  129. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  130. return defined $rev ? $rev : "";
  131. }
  132. }
  133. sub rcs_commit ($$$;$$$) {
  134. # Tries to commit the page; returns undef on _success_ and
  135. # a version of the page with the rcs's conflict markers on failure.
  136. # The file is relative to the srcdir.
  137. my $file=shift;
  138. my $message=shift;
  139. my $rcstoken=shift;
  140. my $user=shift;
  141. my $ipaddr=shift;
  142. my $emailuser=shift;
  143. if (defined $user) {
  144. $message="web commit by $user".(length $message ? ": $message" : "");
  145. }
  146. elsif (defined $ipaddr) {
  147. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  148. }
  149. if (-d "$config{srcdir}/.svn") {
  150. # Check to see if the page has been changed by someone
  151. # else since rcs_prepedit was called.
  152. my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
  153. my $rev=svn_info("Revision", "$config{srcdir}/$file");
  154. if (defined $rev && defined $oldrev && $rev != $oldrev) {
  155. # Merge their changes into the file that we've
  156. # changed.
  157. if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
  158. "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
  159. warn("svn merge -r$oldrev:$rev failed\n");
  160. }
  161. }
  162. if (system("svn", "commit", "--quiet",
  163. "--encoding", "UTF-8", "-m",
  164. IkiWiki::possibly_foolish_untaint($message),
  165. $config{srcdir}) != 0) {
  166. my $conflict=readfile("$config{srcdir}/$file");
  167. if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
  168. warn("svn revert failed\n");
  169. }
  170. return $conflict;
  171. }
  172. }
  173. return undef # success
  174. }
  175. sub rcs_commit_staged ($$$;$) {
  176. # Commits all staged changes. Changes can be staged using rcs_add,
  177. # rcs_remove, and rcs_rename.
  178. my ($message, $user, $ipaddr, $emailuser)=@_;
  179. if (defined $user) {
  180. $message="web commit by $user".(length $message ? ": $message" : "");
  181. }
  182. elsif (defined $ipaddr) {
  183. $message="web commit from $ipaddr".(length $message ? ": $message" : "");
  184. }
  185. if (system("svn", "commit", "--quiet",
  186. "--encoding", "UTF-8", "-m",
  187. IkiWiki::possibly_foolish_untaint($message),
  188. $config{srcdir}) != 0) {
  189. warn("svn commit failed\n");
  190. return 1; # failure
  191. }
  192. return undef # success
  193. }
  194. sub rcs_add ($) {
  195. # filename is relative to the root of the srcdir
  196. my $file=shift;
  197. if (-d "$config{srcdir}/.svn") {
  198. my $parent=IkiWiki::dirname($file);
  199. while (! -d "$config{srcdir}/$parent/.svn") {
  200. $file=$parent;
  201. $parent=IkiWiki::dirname($file);
  202. }
  203. if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
  204. warn("svn add failed\n");
  205. }
  206. }
  207. }
  208. sub rcs_remove ($) {
  209. # filename is relative to the root of the srcdir
  210. my $file=shift;
  211. if (-d "$config{srcdir}/.svn") {
  212. if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
  213. warn("svn rm failed\n");
  214. }
  215. }
  216. }
  217. sub rcs_rename ($$) {
  218. # filenames relative to the root of the srcdir
  219. my ($src, $dest)=@_;
  220. if (-d "$config{srcdir}/.svn") {
  221. # Add parent directory for $dest
  222. my $parent=IkiWiki::dirname($dest);
  223. if (! -d "$config{srcdir}/$parent/.svn") {
  224. while (! -d "$config{srcdir}/$parent/.svn") {
  225. $parent=IkiWiki::dirname($dest);
  226. }
  227. if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
  228. warn("svn add $parent failed\n");
  229. }
  230. }
  231. if (system("svn", "mv", "--force", "--quiet",
  232. "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
  233. warn("svn rename failed\n");
  234. }
  235. }
  236. }
  237. sub rcs_recentchanges ($) {
  238. my $num=shift;
  239. my @ret;
  240. return unless -d "$config{srcdir}/.svn";
  241. eval q{
  242. use Date::Parse;
  243. use XML::SAX;
  244. use XML::Simple;
  245. };
  246. error($@) if $@;
  247. # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
  248. my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
  249. do {
  250. $XML::Simple::PREFERRED_PARSER = pop @parsers;
  251. } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
  252. # --limit is only supported on Subversion 1.2.0+
  253. my $svn_version=`svn --version -q`;
  254. my $svn_limit='';
  255. $svn_limit="--limit $num"
  256. if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
  257. my $svn_url=svn_info("URL", $config{srcdir});
  258. my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
  259. ForceArray => [ 'logentry', 'path' ],
  260. GroupTags => { paths => 'path' },
  261. KeyAttr => { path => 'content' },
  262. );
  263. foreach my $logentry (@{$xml->{logentry}}) {
  264. my (@pages, @message);
  265. my $rev = $logentry->{revision};
  266. my $user = $logentry->{author};
  267. my $when=str2time($logentry->{date}, 'UTC');
  268. foreach my $msgline (split(/\n/, $logentry->{msg})) {
  269. push @message, { line => $msgline };
  270. }
  271. my $committype="web";
  272. if (defined $message[0] &&
  273. $message[0]->{line}=~/$config{web_commit_regexp}/) {
  274. $user=defined $2 ? "$2" : "$3";
  275. $message[0]->{line}=$4;
  276. }
  277. else {
  278. $committype="svn";
  279. }
  280. foreach my $file (keys %{$logentry->{paths}}) {
  281. if (length $config{svnpath}) {
  282. next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
  283. $file=$1;
  284. }
  285. my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
  286. $diffurl=~s/\[\[file\]\]/$file/g;
  287. $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
  288. $diffurl=~s/\[\[r2\]\]/$rev/g;
  289. push @pages, {
  290. page => pagename($file),
  291. diffurl => $diffurl,
  292. } if length $file;
  293. }
  294. push @ret, {
  295. rev => $rev,
  296. user => $user,
  297. committype => $committype,
  298. when => $when,
  299. message => [@message],
  300. pages => [@pages],
  301. } if @pages;
  302. return @ret if @ret >= $num;
  303. }
  304. return @ret;
  305. }
  306. sub rcs_diff ($) {
  307. my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
  308. return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
  309. }
  310. {
  311. my ($lastfile, $lastmtime, $lastctime);
  312. sub findtimes ($) {
  313. my $file=shift;
  314. if (defined $lastfile && $lastfile eq $file) {
  315. return $lastmtime, $lastctime;
  316. }
  317. $lastfile=$file;
  318. my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
  319. my $child = open(SVNLOG, "-|");
  320. if (! $child) {
  321. exec("svn", "log", $file) || error("svn log $file failed to run");
  322. }
  323. my ($cdate, $mdate);
  324. while (<SVNLOG>) {
  325. if (/$svn_log_infoline/) {
  326. $cdate=$1;
  327. $mdate=$1 unless defined $mdate;
  328. }
  329. }
  330. close SVNLOG || error "svn log $file exited $?";
  331. if (! defined $cdate) {
  332. error "failed to parse svn log for $file\n";
  333. }
  334. eval q{use Date::Parse};
  335. error($@) if $@;
  336. $lastctime=str2time($cdate);
  337. $lastmtime=str2time($mdate);
  338. return $lastmtime, $lastctime;
  339. }
  340. }
  341. sub rcs_getctime ($) {
  342. my $file=shift;
  343. return (findtimes($file))[1];
  344. }
  345. sub rcs_getmtime ($) {
  346. my $file=shift;
  347. return (findtimes($file))[0];
  348. }
  349. 1