monster-tag.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #! /usr/bin/perl
  2. # Copyright (C) 2011 Alex Schroeder <alex@gnu.org>
  3. # This program is free software: you can redistribute it and/or modify it under
  4. # the terms of the GNU General Public License as published by the Free Software
  5. # Foundation, either version 3 of the License, or (at your option) any later
  6. # version.
  7. #
  8. # This program is distributed in the hope that it will be useful, but WITHOUT
  9. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  10. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  11. #
  12. # You should have received a copy of the GNU General Public License along with
  13. # this program. If not, see <http://www.gnu.org/licenses/>.
  14. package OddMuse;
  15. # load Oddmuse core
  16. $RunCGI = 0;
  17. do "wiki.pl";
  18. sub unique {
  19. my %h = map {$_ => 1} @_;
  20. return sort keys %h;
  21. }
  22. sub tag {
  23. print '<ul>';
  24. for my $id ($q->param) {
  25. if ($IndexHash{$id} and UserCanEdit($id, 1)) {
  26. my $tags = $q->param($id);
  27. $tags =~ s{"(.*?)"}{$_ = $1; s/ +/_/g; $_ }eg;
  28. my @tags = unique(map { s/_/ /g; $_ } split(' ', $tags));
  29. my $tagline = "Tags: " . join (' ', map { "[[tag:$_]]" } @tags);
  30. OpenPage($id);
  31. my $text = $Page{text};
  32. # delete existing taglines
  33. $text =~ s/\n+Tags: .*//g;
  34. if (@tags) {
  35. $text .= "\n\n$tagline\n";
  36. }
  37. if ($text ne $Page{text}) {
  38. RequestLockOrError(); # fatal
  39. print $q->li(GetPageLink($id) . " tagged " . join(', ', @tags));
  40. Save($id, $text, 'Tagged ' . join(', ', @tags), 1);
  41. ReleaseLock();
  42. }
  43. }
  44. }
  45. print '</ul>';
  46. print $q->p(ScriptLink('action=rc;showedit=1', $RCName));
  47. }
  48. sub tags {
  49. my $id = shift;
  50. OpenPage($id);
  51. my @tags = ();
  52. while ($Page{text} =~ m/\[\[tag:$FreeLinkPattern(?:\|([^]|]+))?\]\]/og) {
  53. my $tag = $1;
  54. $tag = qq{"$tag"} if $tag =~ / /;
  55. push(@tags, $tag);
  56. }
  57. return join(" ", @tags);
  58. }
  59. sub item {
  60. my $id = shift;
  61. print $q->Tr($q->td(GetPageLink($id)),
  62. $q->td($q->textfield(-name=>$id, -default=>tags($id), -size=>80)));
  63. }
  64. sub search {
  65. print $q->start_multipart_form(-method=>'get', -class=>'submit');
  66. print $q->p("Search term: "
  67. . $q->strong($q->param('search')));
  68. print '<table>';
  69. SearchTitleAndBody($q->param('search'), \&item);
  70. print '</table>';
  71. print $q->hidden('tag', 'done');
  72. print $q->submit('go', 'Go!');
  73. print $q->end_form();
  74. }
  75. sub default {
  76. print $q->start_multipart_form(-method=>'get', -class=>'submit');
  77. print $q->p("Search term: "
  78. . $q->textfield(-name=>'search'));
  79. print $q->submit('go', 'Go!');
  80. print $q->end_form();
  81. }
  82. sub main {
  83. $ConfigFile = '/home/alex/campaignwiki/config';
  84. $ModuleDir = '/home/alex/campaignwiki/modules';
  85. $DataDir = '/home/alex/campaignwiki/Monsters';
  86. Init();
  87. $ScriptName = '/wiki/Monsters';
  88. DoSurgeProtection();
  89. if (not $BannedCanRead and UserIsBanned() and not UserIsEditor()) {
  90. ReportError(T('Reading not allowed: user, ip, or network is blocked.'), '403 FORBIDDEN',
  91. 0, $q->p(ScriptLink('action=password', T('Login'), 'password')));
  92. }
  93. if ($q->path_info eq '/source') {
  94. seek DATA, 0, 0;
  95. print "Content-type: text/plain; charset=UTF-8\r\n\r\n", <DATA>;
  96. } else {
  97. $UserGotoBar .= $q->a({-href=>$q->url . '/source'}, 'Source');
  98. print GetHeader('', 'Tag Monsters');
  99. print $q->start_div({-class=>'content'});
  100. if (GetParam('tag')) {
  101. tag();
  102. } elsif (GetParam('search')) {
  103. search();
  104. } else {
  105. default();
  106. }
  107. print $q->p('Questions? Send mail to Alex Schröder <'
  108. . $q->a({-href=>'mailto:kensanata@gmail.com'},
  109. 'kensanata@gmail.com') . '>');
  110. print $q->end_div();
  111. PrintFooter();
  112. }
  113. }
  114. main();
  115. __DATA__