relation.pl 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. # Copyright (C) 2008 Andreas Hofmann
  2. #
  3. # This program is free software; you can redistribute it and/or modify
  4. # it under the terms of the GNU General Public License as published by
  5. # the Free Software Foundation; either version 2 of the License, or
  6. # (at your option) any later version.
  7. #
  8. # This program is distributed in the hope that it will be useful,
  9. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. # GNU General Public License for more details.
  12. #
  13. # You should have received a copy of the GNU General Public License
  14. # along with this program; if not, write to the
  15. # Free Software Foundation, Inc.
  16. # 59 Temple Place, Suite 330
  17. # Boston, MA 02111-1307 USA
  18. use strict;
  19. use v5.10;
  20. AddModuleDescription('relation.pl', 'Relation Extension');
  21. our ($q, %Action, $OpenPageName, @MyRules, $DataDir);
  22. our (@RelationLinking, $RelationPassedFlag);
  23. push(@MyRules, \&RelationRule);
  24. $RelationPassedFlag = 0;
  25. my $referencefile = "References.txt";
  26. my $dummy = RelationRead();
  27. sub RelationRead {
  28. # return scalar(@RelationLinking) if (scalar(@RelationLinking));
  29. open (my $RRR, '<', encode_utf8("$DataDir/$referencefile")) || return(0);
  30. while (<$RRR>) {
  31. chomp;
  32. my ($a,$b,$c) = split(';');
  33. # print "<!--- a,b,c=<$a,$b,$c> ---!>\n";
  34. push @RelationLinking, [$a, $b, $c];
  35. };
  36. close($RRR);
  37. return (scalar(@RelationLinking));
  38. }
  39. sub RelationRule {
  40. if (m/\G((forward@@|backward@@|forward@|backward@):([_A-Za-z0-9 ]+?);)/cg) {
  41. Dirty($1);
  42. my $rememberpos = pos;
  43. my $fwbw =$2;
  44. my $rel=$3;
  45. my $rtext = '';
  46. my $rhead;
  47. $RelationPassedFlag++;
  48. my @result;
  49. if ( substr($fwbw,0,7) eq 'forward' ) {
  50. @result = map { $_->[2] } grep { $_->[0] eq $OpenPageName and $_->[1] eq $rel } @RelationLinking;
  51. $rhead = "<h3>".NormalToFree($OpenPageName)." $rel:</h3>\n";
  52. }
  53. else{
  54. @result = map { $_->[0] } grep { $_->[2] eq $OpenPageName and $_->[1] eq $rel } @RelationLinking;
  55. $rhead = "<h3>$rel ".NormalToFree($OpenPageName).":</h3>\n";
  56. }
  57. if (scalar(@result) == 0 ) {
  58. if (substr($fwbw,-2) eq '@@') {
  59. $rtext = "<!--- RelationRule hits: <$fwbw> <$rel> hiding empty ---!>\n"
  60. }
  61. else {
  62. $rtext = "$rhead<ul><li>-no relation-</li></ul>\n";
  63. }
  64. }
  65. else {
  66. $rtext = $rhead."<ul>\n";
  67. foreach my $LLL (@result) {
  68. $rtext .= "<li>" . GetPageOrEditLink($LLL,$LLL) . "</li>\n";
  69. };
  70. $rtext .= "</ul>\n";
  71. };
  72. pos = $rememberpos;
  73. return $rtext;
  74. }
  75. return;
  76. }
  77. *OldRelationPrintFooter = \&PrintFooter;
  78. *PrintFooter = \&RelationPrintFooter;
  79. sub RelationPrintFooter {
  80. my @params = @_;
  81. if ($RelationPassedFlag > 0) {
  82. print "<div class='footnotes'>\n";
  83. # print "<a href='$OpenPageName?action=checkrelates'>CheckRelations</a><br />\n";
  84. print ScriptLink('action=checkrelates;id='.$OpenPageName, 'CheckRelations', 'index');
  85. print "</div>\n";
  86. };
  87. OldRelationPrintFooter(@params);
  88. };
  89. $Action{'checkrelates'} = sub {
  90. my $id = shift;
  91. my @result = @RelationLinking;
  92. print $q->header;
  93. print "<html><head><title>Edit Relations</title></head><body>\n";
  94. print "<!--- 1 id=$id --->\n";
  95. print "<h3>Relations of $id (to be deleted)</h3>\n";
  96. print "<form action='".ScriptUrl("action=updaterelates")."' method='post'>\n";
  97. my $count = -1;
  98. foreach my $r (@result) {
  99. $count++;
  100. next if ($id ne $r->[0] and $id ne $r->[2]);
  101. print "<input type='checkbox' name='delete$count' value='$count' unchecked >$r->[0] -> $r->[1] -> $r->[2]<br />\n";
  102. };
  103. print "<h3>New Relation of $id (to be created)</h3>\n";
  104. print "$id -> <input name='newrelationto' type='text' size='30' maxlength='30'> -> <input name='newtargetto' type='text' size='30' maxlength='30'><br />\n";
  105. print "<h3>New Relation from $id (to be created)</h3>\n";
  106. print "<input name='newsourcefrom' type='text' size='30' maxlength='30'> -> <input name='newrelationfrom' type='text' size='30' maxlength='30'> -> $id<br />\n";
  107. print "<input type=\"hidden\" name=\"id\" value=\"$id\" /><br />\n";
  108. print "<input type='submit' name='action' value='updaterelates' />&nbsp;\n";
  109. print "</form>\n";
  110. print "</body></html>\n";
  111. };
  112. $Action{'updaterelates'} = sub {
  113. my $id = shift;
  114. print $q->header;
  115. print "<html><head><title>Relations</title></head><body>\n";
  116. my %h = $q->Vars;
  117. print "<h3>Relations of $id</h3>";
  118. my $newrelationto = undef;
  119. my $newtargetto = undef;
  120. my $newrelationfrom = undef;
  121. my $newsourcefrom = undef;
  122. foreach my $r (keys %h) {
  123. if ( $r =~ m/^delete([0-9]+)/ ) {
  124. my $n = $1;
  125. my $s = $h{$r};
  126. print "delete: ". $RelationLinking[$n]->[0]." -> ". $RelationLinking[$n]->[1]." -> " . $RelationLinking[$n]->[2]."<br />\n";
  127. $RelationLinking[$n] = undef;
  128. }
  129. elsif ( $r eq 'newtargetto') {
  130. $newtargetto = $h{$r};
  131. }
  132. elsif ( $r eq 'newrelationto') {
  133. $newrelationto = $h{$r};
  134. }
  135. elsif ( $r eq 'newsourcefrom') {
  136. $newsourcefrom = $h{$r};
  137. }
  138. elsif ( $r eq 'newrelationfrom') {
  139. $newrelationfrom = $h{$r};
  140. }
  141. else {
  142. my $s = $h{$r};
  143. print "other: $r -> $s<br />\n" unless ($r eq 'action' or $r eq 'id');
  144. };
  145. };
  146. if (defined($newrelationto) and defined($newtargetto) and $newrelationto ne '' and $newtargetto ne '') {
  147. print "new: $id -> $newrelationto -> $newtargetto<br />\n";
  148. push @RelationLinking, [$id, $newrelationto, FreeToNormal($newtargetto)];
  149. }
  150. else {
  151. print "no new target<br />\n";
  152. }
  153. if (defined($newrelationfrom) and defined($newsourcefrom) and $newrelationfrom ne '' and $newsourcefrom ne '') {
  154. print "new: $newsourcefrom -> $newrelationfrom -> $id<br />\n";
  155. push @RelationLinking, [FreeToNormal($newsourcefrom), $newrelationfrom, $id];
  156. }
  157. else {
  158. print "no new source<br />\n";
  159. }
  160. open (my $RRR, '>', encode_utf8("$DataDir/$referencefile"));
  161. print "<br />\n";
  162. foreach my $t (@RelationLinking) {
  163. next unless (defined($t));
  164. # print "trace:". $t->[0] .";". $t->[1].";". $t->[2] ."<br />\n";
  165. print $RRR $t->[0] .";". $t->[1].";". $t->[2] ."\n";
  166. };
  167. close($RRR);
  168. print ScriptLink('id='.$id, $id, 'index');
  169. print "</body></html>\n";
  170. };
  171. 1;