commentcount.pl 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. # Copyright (C) 2004 Brock Wilcox <awwaiid@thelackthereof.org>
  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('commentcount.pl', 'Comment Count Extension');
  18. our ($CommentsPrefix);
  19. *OldCommentcountAddComment = \&AddComment;
  20. *AddComment = \&NewCommentcountAddComment;
  21. sub NewCommentcountAddComment {
  22. my ($old, $comment) = @_;
  23. my $new = OldCommentcountAddComment($old,$comment);
  24. if($new eq $old) {
  25. # no comment added
  26. } else {
  27. my $num = $new;
  28. if($num =~ /=== (\d+) Comments?\. ===/) {
  29. $num = $1;
  30. $num++;
  31. $new =~ s/=== (\d+) Comments?\. ===/=== $num Comments. ===/;
  32. } else {
  33. $new = "=== 1 Comment. ===\n" . $new;
  34. }
  35. }
  36. return $new;
  37. }
  38. *OldCommentcountScriptLink = \&ScriptLink;
  39. *ScriptLink = \&NewCommentcountScriptLink;
  40. sub NewCommentcountScriptLink {
  41. my ($action, $text, @rest) = @_;
  42. if ($CommentsPrefix && $action =~ /^$CommentsPrefix(.*)/) { # TODO use $CommentsPattern ?
  43. # Add the number of comments here
  44. my $id = $action;
  45. $id =~ s/%([0-9a-f][0-9a-f])/chr(hex($1))/eg; # undo urlencode
  46. my $comments = GetPageContent($id);
  47. my $num = 0;
  48. if($comments =~ /=== (\d+) Comments?\. ===/) {
  49. $num = $1;
  50. }
  51. # Fix plurals
  52. my $plural = T('Comments on');
  53. my $singular = T('Comment on');
  54. $text =~ s/$plural/$singular/ if($num == 1);
  55. $text = $num . ' ' . $text;
  56. }
  57. return OldCommentcountScriptLink($action, $text, @rest);
  58. }