relation.pl 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 3 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, see <http://www.gnu.org/licenses/>.
  15. use strict;
  16. use v5.10;
  17. AddModuleDescription('relation.pl', 'Relation Extension');
  18. our ($q, %Action, $OpenPageName, @MyRules, $DataDir);
  19. our (@RelationLinking, $RelationPassedFlag);
  20. push(@MyRules, \&RelationRule);
  21. $RelationPassedFlag = 0;
  22. my $referencefile = "References.txt";
  23. my $dummy = RelationRead();
  24. sub RelationRead {
  25. # return scalar(@RelationLinking) if (scalar(@RelationLinking));
  26. open (my $RRR, '<', encode_utf8("$DataDir/$referencefile")) || return(0);
  27. while (<$RRR>) {
  28. chomp;
  29. my ($a,$b,$c) = split(';');
  30. # print "<!--- a,b,c=<$a,$b,$c> ---!>\n";
  31. push @RelationLinking, [$a, $b, $c];
  32. };
  33. close($RRR);
  34. return (scalar(@RelationLinking));
  35. }
  36. sub RelationRule {
  37. if (m/\G((forward@@|backward@@|forward@|backward@):([_A-Za-z0-9 ]+?);)/cg) {
  38. Dirty($1);
  39. my $rememberpos = pos;
  40. my $fwbw =$2;
  41. my $rel=$3;
  42. my $rtext = '';
  43. my $rhead;
  44. $RelationPassedFlag++;
  45. my @result;
  46. if ( substr($fwbw,0,7) eq 'forward' ) {
  47. @result = map { $_->[2] } grep { $_->[0] eq $OpenPageName and $_->[1] eq $rel } @RelationLinking;
  48. $rhead = "<h3>".NormalToFree($OpenPageName)." $rel:</h3>\n";
  49. }
  50. else{
  51. @result = map { $_->[0] } grep { $_->[2] eq $OpenPageName and $_->[1] eq $rel } @RelationLinking;
  52. $rhead = "<h3>$rel ".NormalToFree($OpenPageName).":</h3>\n";
  53. }
  54. if (scalar(@result) == 0 ) {
  55. if (substr($fwbw,-2) eq '@@') {
  56. $rtext = "<!--- RelationRule hits: <$fwbw> <$rel> hiding empty ---!>\n"
  57. }
  58. else {
  59. $rtext = "$rhead<ul><li>-no relation-</li></ul>\n";
  60. }
  61. }
  62. else {
  63. $rtext = $rhead."<ul>\n";
  64. foreach my $LLL (@result) {
  65. $rtext .= "<li>" . GetPageOrEditLink($LLL,$LLL) . "</li>\n";
  66. };
  67. $rtext .= "</ul>\n";
  68. };
  69. pos = $rememberpos;
  70. return $rtext;
  71. }
  72. return;
  73. }
  74. *OldRelationPrintFooter = \&PrintFooter;
  75. *PrintFooter = \&RelationPrintFooter;
  76. sub RelationPrintFooter {
  77. my @params = @_;
  78. if ($RelationPassedFlag > 0) {
  79. print "<div class='footnotes'>\n";
  80. # print "<a href='$OpenPageName?action=checkrelates'>CheckRelations</a><br />\n";
  81. print ScriptLink('action=checkrelates;id='.$OpenPageName, 'CheckRelations', 'index');
  82. print "</div>\n";
  83. };
  84. OldRelationPrintFooter(@params);
  85. };
  86. $Action{'checkrelates'} = sub {
  87. my $id = shift;
  88. my @result = @RelationLinking;
  89. print $q->header;
  90. print "<html><head><title>Edit Relations</title></head><body>\n";
  91. print "<!--- 1 id=$id --->\n";
  92. print "<h3>Relations of $id (to be deleted)</h3>\n";
  93. print "<form action='".ScriptUrl("action=updaterelates")."' method='post'>\n";
  94. my $count = -1;
  95. foreach my $r (@result) {
  96. $count++;
  97. next if ($id ne $r->[0] and $id ne $r->[2]);
  98. print "<input type='checkbox' name='delete$count' value='$count' unchecked >$r->[0] -> $r->[1] -> $r->[2]<br />\n";
  99. };
  100. print "<h3>New Relation of $id (to be created)</h3>\n";
  101. print "$id -> <input name='newrelationto' type='text' size='30' maxlength='30'> -> <input name='newtargetto' type='text' size='30' maxlength='30'><br />\n";
  102. print "<h3>New Relation from $id (to be created)</h3>\n";
  103. print "<input name='newsourcefrom' type='text' size='30' maxlength='30'> -> <input name='newrelationfrom' type='text' size='30' maxlength='30'> -> $id<br />\n";
  104. print "<input type=\"hidden\" name=\"id\" value=\"$id\" /><br />\n";
  105. print "<input type='submit' name='action' value='updaterelates' />&nbsp;\n";
  106. print "</form>\n";
  107. print "</body></html>\n";
  108. };
  109. $Action{'updaterelates'} = sub {
  110. my $id = shift;
  111. print $q->header;
  112. print "<html><head><title>Relations</title></head><body>\n";
  113. my %h = $q->Vars;
  114. print "<h3>Relations of $id</h3>";
  115. my $newrelationto = undef;
  116. my $newtargetto = undef;
  117. my $newrelationfrom = undef;
  118. my $newsourcefrom = undef;
  119. foreach my $r (keys %h) {
  120. if ( $r =~ m/^delete([0-9]+)/ ) {
  121. my $n = $1;
  122. my $s = $h{$r};
  123. print "delete: ". $RelationLinking[$n]->[0]." -> ". $RelationLinking[$n]->[1]." -> " . $RelationLinking[$n]->[2]."<br />\n";
  124. $RelationLinking[$n] = undef;
  125. }
  126. elsif ( $r eq 'newtargetto') {
  127. $newtargetto = $h{$r};
  128. }
  129. elsif ( $r eq 'newrelationto') {
  130. $newrelationto = $h{$r};
  131. }
  132. elsif ( $r eq 'newsourcefrom') {
  133. $newsourcefrom = $h{$r};
  134. }
  135. elsif ( $r eq 'newrelationfrom') {
  136. $newrelationfrom = $h{$r};
  137. }
  138. else {
  139. my $s = $h{$r};
  140. print "other: $r -> $s<br />\n" unless ($r eq 'action' or $r eq 'id');
  141. };
  142. };
  143. if (defined($newrelationto) and defined($newtargetto) and $newrelationto ne '' and $newtargetto ne '') {
  144. print "new: $id -> $newrelationto -> $newtargetto<br />\n";
  145. push @RelationLinking, [$id, $newrelationto, FreeToNormal($newtargetto)];
  146. }
  147. else {
  148. print "no new target<br />\n";
  149. }
  150. if (defined($newrelationfrom) and defined($newsourcefrom) and $newrelationfrom ne '' and $newsourcefrom ne '') {
  151. print "new: $newsourcefrom -> $newrelationfrom -> $id<br />\n";
  152. push @RelationLinking, [FreeToNormal($newsourcefrom), $newrelationfrom, $id];
  153. }
  154. else {
  155. print "no new source<br />\n";
  156. }
  157. open (my $RRR, '>', encode_utf8("$DataDir/$referencefile"));
  158. print "<br />\n";
  159. foreach my $t (@RelationLinking) {
  160. next unless (defined($t));
  161. # print "trace:". $t->[0] .";". $t->[1].";". $t->[2] ."<br />\n";
  162. print $RRR $t->[0] .";". $t->[1].";". $t->[2] ."\n";
  163. };
  164. close($RRR);
  165. print ScriptLink('id='.$id, $id, 'index');
  166. print "</body></html>\n";
  167. };
  168. 1;