like.pl 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. # Copyright (C) 2011-2015 Alex Schroeder <alex@gnu.org>
  2. # Copyright (C) 2011 Ingo Belka <grimmen@mvnet.de>
  3. # Copyright (C) 2011 Mark Zimmermann
  4. #
  5. # This program is free software: you can redistribute it and/or modify it under
  6. # the terms of the GNU General Public License as published by the Free Software
  7. # Foundation, either version 3 of the License, or (at your option) any later
  8. # version.
  9. #
  10. # This program is distributed in the hope that it will be useful, but WITHOUT
  11. # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  13. #
  14. # You should have received a copy of the GNU General Public License along with
  15. # this program. If not, see <http://www.gnu.org/licenses/>.
  16. use strict;
  17. # use warnings;
  18. use v5.10;
  19. AddModuleDescription('like.pl', 'Like Button');
  20. our $LikeRegexp = T('====(\d+) persons? liked this===='); # must match all translations
  21. our $LikeReplacement = T('====%d persons liked this===='); # used for sprintf
  22. our $LikeFirst = T('====1 person liked this====');
  23. our (%Action, %Page, $OpenPageName, @MyFooters);
  24. $Action{like} = \&DoLike;
  25. push(@MyFooters, \&LikeFooter);
  26. sub DoLike {
  27. my $id = shift;
  28. OpenPage(FreeToNormal($id));
  29. return ReBrowsePage($id) unless $Page{text}; # skip empty pages
  30. my $data = $Page{text};
  31. if ($data =~ /$LikeRegexp/) {
  32. my $n = $1;
  33. $n++;
  34. my $to = sprintf($LikeReplacement, $n); # fresh copy
  35. $data =~ s/$LikeRegexp/$to/;
  36. } else {
  37. # data already ends in a newline
  38. $data .= "\n" . $LikeFirst;
  39. }
  40. SetParam('text', $data);
  41. SetParam('title', $OpenPageName);
  42. SetParam('oldtime', $Page{ts});
  43. SetParam('recent_edit', 'on');
  44. SetParam('summary', T('I like this!'));
  45. DoPost($id);
  46. }
  47. sub LikeFooter {
  48. my ($id) = @_;
  49. if ($id
  50. and $Page{revision} # don't like empty pages
  51. and GetParam('action', 'browse') eq 'browse') {
  52. return ScriptLink('action=like;id=' . UrlEncode($id), T('I like this!'), 'like');
  53. }
  54. }