commentcount.pl 2.1 KB

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